📘 1. Syntax Analysis (Parsing)
✅ Definition
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.
🔄 Position in Compiler
Source Code → Lexical Analyzer → Syntax Analyzer → Semantic Analyzer
📊 Basic Working
- Input → Tokens from lexical analyzer
- Output → Parse Tree / Syntax Tree
📌 Example
Input Tokens:
int x = 10 ;
Valid Statement ✅
Follows grammar rules
Invalid Statement ❌
int = x 10 ;
👉 Parser detects syntax error
⭐ Key Points (Exam)
👉 Checks grammar rules (CFG)
👉 Generates parse tree
👉 Detects syntax errors
🧠 2. Role of the Parser
✅ Definition
A Parser is a program that performs syntax analysis by taking tokens as input and producing a parse tree.
🔧 Main Functions of Parser
1. Check Syntax
- Verifies whether input follows grammar rules
2. Build Parse Tree
- Represents structure of program
3. Report Errors
- Detects and reports syntax errors
4. Interface with Other Phases
- Takes tokens from lexical analyzer
- Sends parse tree to semantic analyzer
📊 Role Diagram
Tokens → [Parser] → Parse Tree → Semantic Analysis
🌳 3. Parse Tree (Reminder)
- Root → Start symbol
- Leaves → Tokens
- Shows hierarchical structure
📌 Example
a + b * c
Parse tree ensures:
a + (b * c)
⚠️ 4. Syntax Errors
✅ Definition
Errors that occur when input does not follow grammar rules.
📌 Examples
| Error Type |
Example |
| Missing symbol |
int x = 10 (missing ;) |
| Wrong order |
int = x 10; |
| Unbalanced brackets |
(a + b |
⭐ Key Point (Exam)
👉 Parser must detect and recover from errors
🔄 5. Types of Parsing
🔹 1. Top-Down Parsing
- Starts from start symbol
- Builds parse tree from root to leaves
👉 Example:
- Recursive Descent Parser
- LL(1) Parser
🔹 2. Bottom-Up Parsing
- Starts from input string
- Builds parse tree from leaves to root
👉 Example:
- Shift-Reduce Parser
- LR Parser
📊 Comparison
| Feature |
Top-Down |
Bottom-Up |
| Direction |
Root → Leaves |
Leaves → Root |
| Approach |
Predictive |
Reduction |
| Complexity |
Simple |
Complex |
| Example |
LL(1) |
LR |
🧪 6. Example (Parsing Process)
Grammar:
E → E + T | T
T → id
Input:
id + id
Steps:
- Parser reads tokens
- Matches with grammar rules
- Builds parse tree
- Confirms validity
🎯 7. Error Handling in Parsing
🔧 Techniques (Basic Idea)
- Panic mode recovery
- Phrase-level recovery
👉 Ensures compilation continues even after errors
🎯 8. Important Exam Concepts
👉 Frequently asked:
- Define syntax analysis
- Role of parser
- Types of parsing
- Syntax errors with examples
- Difference between top-down and bottom-up
- Draw parsing process diagram
📝 9. Short Notes (Quick Revision)
Syntax Analysis
- Second compiler phase
- Checks grammar rules
Parser
- Builds parse tree
- Detects errors
Parsing Types
📊 10. Final Summary Table
| 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 |
✅ Final Conclusion
- Syntax Analysis ensures the program follows correct grammar
- The Parser is the core component that performs this task
- It builds the parse tree and detects errors
- Essential for further phases like semantic analysis