04 Syntax Analysis - RDP
04 Syntax Analysis - RDP
04 Syntax Analysis - RDP
Chapter 4
Faryal Shamsi
Lecturer Computer Science
• Pre-order
• In-order
• Post order
Pre-Order
A→B→D→E→C→F→
G
In-Order
D→B→E→A→F→C→
G
Post-Order
D→E→B→F→G→C→
A
Post-Order
Syntax analysis / Parsing
• This section introduces a parsing method called recursive descent, which can
be used both to parse and to implement syntax-directed translators.
• Parsing Methods
• Top-down parser
• Bottom-up parser
• Parsing Methods: refer to the order in which nodes in the parse tree are
constructed.
Parsing
• Solution:
1. S -> + S S | - S S | a
2. S -> S ( S ) S | ε
3. S -> 0 S 1 | 0 1
1 ) S -> + S S | - S S | a
void S(){ default:
switch(lookahead){ throw new SyntaxException();
case "+": }
match("+"); S(); S(); • }
break; • void match(Terminal t){
case "-": • if(lookahead = t){
match("-"); S(); S(); • lookahead = nextTerminal();
break; • }else{
case "a": • throw new SyntaxException()
match("a"); • }
break; • }
2 ) S -> S ( S ) S | ε
• void S(){
• if(lookahead == "("){
• match("("); S(); match(")"); S();
• }
•}
3 ) S -> 0 S 1 | 0 1
void S(){
switch(lookahead){
case "0":
match("0"); S(); match("1");
break;
case "1":
// match(epsilon);
break;
default:
throw new SyntaxException();
}
}
Recursive Descent Parser
Recursive Descendent Parsing
(lookahead)
When to Use Ɛ Productions
• As a default when no other production can be used
Recursive Descendent Parsing
(lookahead)
Introducing ‘First’ Sets