Professional Documents
Culture Documents
Lecture#8 - Chap#2 (Syntax Directed Translator (Part-IV) )
Lecture#8 - Chap#2 (Syntax Directed Translator (Part-IV) )
Compiler Construction
A Simple Syntax-Directed Translator,
(Part-IV)
by Safdar Hussain
Topics
• FIRST & FIRST Sets, Left-Recursion, Left Factoring
• Translator for Simple Expressions, Adding a Lexical Analyzer
• Token Attributes, Generic Instructions for Stack Manipulation
Overview
• This chapter contains introductory material
• Building a simple compiler
– Syntax Definition
– Syntax-Directed Translation
– Parsing
– A Translator for Simple Expressions
– The Lexical Analyzer
Grammar
AR A A’
|R OR | A’
RR A’ A’
| |
<id, “y”> <assign, > <num, 31> <+, > <num, 28> <*, > <id, “x”>
token
tokenval Parser
(token attribute) parse()
factor()
{ if (lookahead == „(„)
{ match(„(„); expr(); match(„)‟);
}
else if (lookahead == NUM)
{ printf(“ %d “, tokenval); match(NUM);
}
else error();
}
-3
25