
Syntax Analysis is the second phase of a compiler in which the sequence of tokens (from lexical analysis) is checked according to the grammar rules of the...
Syntax Analysis is the second phase of a compiler in which the sequence of tokens (from lexical analysis) is checked according to the grammar rules of the language.
π It determines whether the program is syntactically correct or not.
Source Code β Lexical Analyzer β Syntax Analyzer β Semantic Analyzer
int x = 10 ;
Follows grammar rules
int = x 10 ;
π Parser detects syntax error
π Checks grammar rules (CFG) π Generates parse tree π Detects syntax errors
A Parser is a program that performs syntax analysis by taking tokens as input and producing a parse tree.
Tokens β [Parser] β Parse Tree β Semantic Analysis
a + b * c
Parse tree ensures:
a + (b * c)
Errors that occur when input does not follow grammar rules.
| Error Type | Example |
|---|---|
| Missing symbol | int x = 10 (missing ;) |
| Wrong order | int = x 10; |
| Unbalanced brackets | (a + b |
π Parser must detect and recover from errors
π Example:
π Example:
| Feature | Top-Down | Bottom-Up |
|---|---|---|
| Direction | Root β Leaves | Leaves β Root |
| Approach | Predictive | Reduction |
| Complexity | Simple | Complex |
| Example | LL(1) | LR |
E β E + T | T
T β id
id + id
π Ensures compilation continues even after errors
π Frequently asked:
| Aspect | Syntax Analysis | Parser |
|---|---|---|
| Definition | Checks syntax of tokens | Program that performs parsing |
| Input | Tokens | Tokens |
| Output | Parse tree | Parse tree |
| Function | Validate structure | Build tree & detect errors |
| Errors | Syntax errors | Reports errors |
| Importance | Very High | Very High |
Open this section to view and download past papers
Click on Show Past Papers to load available exam papers.