Previous | Next | |
Parsing_06_evaluation | Parsing_07_syntax_vs_semantic_errors | Rational |
Parsing: (07) Syntax vs. Semantic Errors
The formal definition of the difference
In Java, there is a rule final variables cannot be changed after initial assignment. For example:
Bad Code | Compile error (not a syntax error though) |
---|---|
|
|
Even though this error is reported by the compiler, strictly speaking, it isn’t a syntax error. Leaving off the return type of a method, leaving out a semicolon, or parentheses/braces that aren’t properly balanced are all syntax errors—but things that require subtle analysis of the relationships between remote parts of the code generally are not, stricty speaking, syntax errors.
This distinction between what is and is not, strictly speaking, a syntax error may be hard to fully understand without exploring more about grammars, productions, and ASTs.
So if you aren’t prepared to divide into those topics yet, it may sufficient for the time being to understand that there is a difference between the two.
Formal explanation of the difference
The formal difference between a syntax and semantic error is specified in terms of the context-free grammar and the AST.
It has to do with when and how the error is detected.
-
Any error that can be detected during the process of applying the rules of a context-free grammar and constructing the resulting AST is considered a syntax error
-
Semantic errors are violations of the language specification that can only be discovered after the AST is created, either as the AST is checked, evaluated, or at a later stage when the resulting code is executed. These are violations of rules that cannot be captured in a context-free grammar.
Previous | Next | |
Parsing_06_evaluation | Parsing_07_syntax_vs_semantic_errors | Rational |