Syntax errors occur when the text in a document being analyzed does not conform to the required structure defined by the grammar. Syntax error detection is more complicated than lexical error detection because the analyzer not only needs to make the best guess about what the problem might be, but it also needs to make the best guess about what the solution might be so it can provide meaningful error messages.
Example productions for a non-terminal symbol named VariableDeclarationStatement:
VariableDeclarationStatement → Type IdentifierToken SemicolonToken
VariableDeclarationStatement → Type IdentifierToken EqualsToken Expression SemicolonToken
VariableDeclarationStatement → VarKeyword IdentifierToken EqualsToken Expression SemicolonToken
Document content to be parsed: "int x = ;"
During lexical analysis the following significant tokens will be produced:
The Syntax Parsing Engine will start syntax analyzing the significant tokens and when it gets to the "SemicolonToken" it will assume an error is found because none of the above production bodies match the processed tokens. The Syntax Parsing Engine will try to figure out what is either missing from the text or not supposed to be there. To do this the parser determines all possible productions that can be built in the current state and tries to match all of them with the tokens parsed.
It is clear that the first production is not the one that should be created, because that production has a semicolon immediately after the identifier (and not an equal sign as in the example text). Also it is clear that the third production is not the one to be created, because that production starts with a "var" keyword. In this case the only possible production is the second and based on its definition it is clear that the error in this case is a missing expression.
The following topics provide additional information related to this topic.