A syntax error is an error caused by source code that does not conform to the grammatical rules of the programming language or other formal language being processed. Programming languages have syntax rules specifying how tokens such as identifiers, keywords, literals, operators, delimiters and punctuation may be arranged into valid constructs. If source code violates those rules, a compiler, interpreter, parser or other language processor may be unable to construct the required syntactic structure.
Typical examples include an unmatched parenthesis, a missing delimiter where one is required, an incorrectly formed keyword construct, malformed literal syntax or an unexpected token. The exact definition and timing of syntax-error detection vary between languages and execution models, but the central idea is that the written program is not grammatically valid according to the relevant language rules.
Syntax belongs to the formal structure of a language and is distinct from semantics. Syntax describes which arrangements of symbols constitute valid expressions, statements and declarations, whereas semantics concerns what valid constructs mean or do. For example, a language might require a particular arrangement of parentheses around an expression. Missing a required parenthesis can make the source syntactically invalid.
Conversely, a syntactically valid expression may still have an incorrect meaning, such as using the wrong variable or applying an inappropriate calculation. That latter problem is not necessarily a syntax error. This distinction is fundamental because a parser primarily determines whether source text can be structured according to the grammar, while later compiler or runtime stages may need to establish types, resolve names and determine behavioural correctness.
Compilers and interpreters commonly detect syntax errors through lexical and parsing stages. Lexical analysis converts source characters into tokens such as identifiers, keywords and literals, while parsing determines whether the resulting token sequence conforms to the language grammar. A malformed token can therefore be detected during lexical analysis, while an invalid arrangement of otherwise valid tokens may be detected during parsing.
Parser implementations may use techniques such as recursive descent, LL parsing, LR parsing, parser combinators or other methods depending on the language and toolchain. When an error is detected, the language processor generally reports a diagnostic containing information such as the source location, unexpected token and expected alternatives. Error recovery mechanisms may allow a parser to continue far enough to report multiple errors in one pass rather than stopping after the first problem.
Syntax errors commonly occur because programmers omit, add or misplace structural elements. Unclosed parentheses, brackets or quotation marks are frequent examples. A programmer may also accidentally use punctuation belonging to another language, write a keyword incorrectly, place a declaration where a statement is expected, or produce a malformed function or class definition. Indentation can be syntactically significant in languages such as Python, meaning that inconsistent indentation can produce an indentation-related syntax error or alter the program structure.
In languages with semicolon-based statement termination, missing or misplaced semicolons can sometimes cause syntax diagnostics, although automatic semicolon insertion or similar language rules can change the result. Syntax errors are therefore language-specific and cannot be identified accurately without considering the grammar and version of the language involved.
Syntax errors are often detected before ordinary program execution in compiled languages, because the compiler must successfully parse the source before producing executable output. Interpreted or dynamically executed languages may parse source at program startup, module import, function compilation or another stage, and some environments compile portions of code only when they are reached. Consequently, the phrase "syntax error happens before runtime" is useful in many common contexts but is not a universal description of every language implementation.
Some systems can also generate or evaluate source dynamically, meaning syntactic validity may be checked during execution of the surrounding program. The essential distinction is not simply when the error occurs in wall-clock time but whether the language processor rejects the source because it cannot interpret its structure according to the grammar.
Syntax errors differ from semantic, type, runtime and logic errors. A syntactically invalid expression may prevent compilation or parsing altogether. A syntactically valid expression can nevertheless refer to an undefined name, violate a type constraint or perform an invalid operation, depending on the language and processing stage. A runtime error occurs when executable code encounters an invalid condition during execution, such as an unavailable file or division by zero where the language treats it as exceptional.
A logic error is different again: the program executes but produces behaviour that does not satisfy the intended requirement. The boundaries between compiler diagnostics and runtime diagnostics vary by language, so terms should be interpreted in relation to the particular language's compilation and execution model.
Modern development environments reduce the practical difficulty of many syntax errors through syntax highlighting, autocomplete, incremental parsing and immediate diagnostics. An integrated development environment may underline an invalid construct while the programmer is typing it, sometimes before the program is explicitly built or run. Formatters and linters can also identify structural inconsistencies, although lint warnings are not necessarily syntax errors. Syntax-aware editors can often insert matching delimiters automatically and show the nesting structure of brackets and blocks. These tools do not replace understanding the grammar: a construct can be perfectly valid syntactically while still being semantically wrong or logically incorrect. They instead make violations of the formal language structure easier to locate and correct.
Syntax errors are important because they represent failures at the interface between human-written source and the formal language understood by a compiler or interpreter. The historical development of programming languages emphasised increasingly precise grammars so that machines could process programs deterministically and provide meaningful diagnostics. Formal grammars, tokenisation and parsing theory underpin modern programming-language implementations, while error-recovery techniques aim to make diagnostics useful to programmers.
A syntax error is therefore not simply "a typo" even though typographical mistakes frequently cause one. It is specifically a failure to conform to the language's structural rules. Correcting it means changing the source so that the relevant lexical and grammatical constraints are satisfied; only after that can later stages determine whether the resulting program is meaningful and behaves as intended.