What Is a Syntax Error?
A syntax error is a mistake in the structure of code, such as a missing colon, bracket or quotation mark, that breaks the grammar rules of a programming language. The compiler or interpreter rejects the program before any of it runs.
Updated September 24, 2026
Before Python runs a single line of your program, it reads the whole file and checks that it follows the grammar of the language. If one line breaks that grammar, nothing runs at all. Run this program and watch what does not happen: the first print never prints.
File "main.py", line 4
if temperature > 30
^
SyntaxError: expected ':'
Line 1 is perfectly valid, yet "Checking the temperature" is missing from the output. Python rejected the file as a whole because an if line must end with a colon. Add the : after 30, run it again, and both lines print.
How a syntax error is detected
Every programming language has a grammar: which words are allowed, where brackets open and close, what must follow if. The program that checks it is called a parser, and it works in the first stage of turning source code into something a machine can execute.
- The tokenizer splits the text into tokens:
if,temperature,>,30, and so on. - The parser reads the tokens in order and fits them into the grammar. After
if temperature > 30it expects a:, but it finds the end of the line. - It stops and reports the place where it gave up: the file, the line number, and a caret (
^) under the position. - Because parsing failed, no code is generated, so no line of the program runs.
In CPython the parser runs every time you start a script. In languages with a separate build step, the compiler does this check: gcc, clang and javac refuse to produce a program while a single syntax error remains. That is why syntax errors are counted among compile-time errors, in contrast with a runtime error, which happens while the program is already running.
Common syntax errors in Python
These are the mistakes behind most syntax errors beginners see, with the exact message Python 3.12 prints.
| Code | Message | Fix |
|---|---|---|
if x > 3 | SyntaxError: expected ':' | End if, for, while, def and class lines with : |
print("Hi" | SyntaxError: '(' was never closed | Close every bracket you open |
name = "Ada | SyntaxError: unterminated string literal (detected at line 1) | Close the quotation mark |
if x = 5: | SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? | Compare with == |
print "Hi" | SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)? | Python 3 needs print(...) |
if a > 0 && b > 0: | SyntaxError: invalid syntax | Python uses and, or and not |
An if with no indented line under it | IndentationError: expected an indented block after 'if' statement on line 1 | Indent the body by four spaces |
IndentationError is a kind of SyntaxError: in Python, indentation is part of the grammar.
Not every typo is a syntax error. pritn("hello") follows the grammar perfectly, because a name followed by parentheses is a function call. Python only discovers that no function called pritn exists when it reaches that line, and then it raises NameError: name 'pritn' is not defined. Did you mean: 'print'?. That is a runtime error, and any lines above it have already run.
Why the reported line can be wrong
The parser reports the point where it could no longer continue, which is not always the point where the mistake is. An unclosed bracket is the classic case: the parser cannot know the bracket was never closed until it has read past the place where it should have been.
File "main.py", line 1
prices = [4.50, 3.20, 7.80
^
SyntaxError: '[' was never closed
Python 3.10 and later point at the opening [ on line 1. Python 3.9 and older reported only invalid syntax on line 2, the line after the real mistake. So when a reported line looks correct, check the end of the line above it.
Other parsers behave the same way. Given SELECT name FORM users;, with FROM misspelled, SQLite reads FORM as an alias (a second name) for the name column and only fails one word later:
Parse error near line 1: near "users": syntax error
SELECT name FORM users;
^--- error here
Syntax errors in C, Java and JavaScript
Compiled languages report syntax errors when you build, not when you run. Here is a declaration without its semicolon, compiled with Clang (the compiler that the gcc command runs on macOS):
#include <stdio.h>
int main(void) {
int count = 3
printf("%d\n", count);
return 0;
}
main.c:4:18: error: expected ';' at end of declaration
javac reports the same mistake in Java as error: ';' expected, and no .class file is written. In C, one missing brace can produce a dozen errors further down the file. Fix the first one and compile again, because the others are often consequences of it.
JavaScript engines such as V8 parse a whole script before running it, just like Python. Node.js rejects this file before printing "start":
console.log("start");
if (x > 1 {
console.log("big");
}
SyntaxError: Unexpected token '{'
In a browser, a syntax error stops that one script file from running, while other scripts on the page still run.
How to fix a syntax error
- Read the last line of the message first. It names the problem: a missing
:, an unclosed(, an unexpected token. - Go to the reported line and look at the caret. If the line looks fine, check the line above it.
- Check pairs. Every
(,[,{and quotation mark needs a partner. Most editors highlight the matching bracket when the cursor is next to one. - Check the punctuation the language requires: colons and indentation in Python, semicolons and braces in C, Java and JavaScript.
- Fix only the first error, then run again. Later messages are often caused by the first one.
- Watch the syntax highlighting. After an unclosed quotation mark, the rest of the file usually turns the color of a string, which shows you where it started.
The good news is that syntax errors are the easiest kind to fix: the tool tells you that something is wrong and roughly where. The harder kinds come later, when the code runs.
Where to go next
A program that passes the parser can still crash while it runs, which is a runtime error. A program that runs to the end but prints the wrong answer has a logic error, and that page compares all three types in one table. For the grammar rules themselves, read Python syntax or C syntax, and practice them in the Python course.
Frequently Asked Questions
What is an example of a syntax error?
print("Hello" is a syntax error, and so is an if line without its colon in Python: if x > 3. In C and Java, a missing semicolon at the end of a statement is the most common one. In every case the code does not follow the grammar, so it cannot run.What is the difference between a syntax error and a semantic error?
int in Java; others use it as another name for a logic error.What is a syntax error in C?
gcc and clang print the file, line and column, for example main.c:4:18: error: expected ';' at end of declaration, and produce no executable until every error is fixed.Why doesn't Python fix syntax errors automatically?
Did you mean print(...)?, but leaves the decision to you.What does syntax error mean on a calculator?
ERR:SYNTAX.