Syntax Analysis
Chapter 4
Faryal Shamsi
Lecturer Computer Science
Sukkur IBA University
Syntax Analysis
Tree Traversals
• Breadth first search
• Also called level order search
• Not suitable for parsing
• May be used in finding shortest path
• Depth first search
Depth First Search
• 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
• In top-down parsers (popular: easy to program), construction starts
at the root and proceeds towards the leaves
• while in bottom-up parsers, construction starts at the leaves and
proceeds towards the root .
Top-Down Parsing
Predictive Parsing or
Recursive Descendent Parsing (RDP)
• Recursive-descent parsing is a top-down method of syntax analysis in
which a set of recursive procedures is used to process the input.
• One procedure is associated with each nonterminal of a grammar.
Predictive Parsing or
Recursive Descendent Parsing (RDP)
• Some grammars have no back tracking are called predictive.
• Backtracking example, if we have the two productions –
stmt if expr then stmt else stmt
stmt if expr then stmt
• This is a top-down process in which the parser attempts to verify that
the syntax of the input stream is correct as it is read from left to right.
Predictive Parsing or
Recursive Descendent Parsing (RDP)
• A basic operation necessary for this involves reading characters from
the input stream and matching then with terminals from the grammar
that describes the syntax of the input.
• Our recursive descent parsers will lookahead one character and
advance the input stream reading pointer when proper matches
occur.
Recursive Descendent Parsing
The predictive parser consists of procedures for the
nonterminals stmt and optexpr of the grammar and
an additional procedure match, used to simplify the
code for stmt and optexpr.
Left - Recursion
• It is possible for a recursive-descent parser to loop forever. A problem
arises with "left-recursive" productions like:
expr expr + term | expr - term | term
• Solution:
• expr term rest
• rest + term rest | - term rest
• rest
Designing a Recursive Descendent
Parser
Designing a Recursive Descendent
Parser
Designing a Recursive Descendent
Parser
• For Your Reference
• The complete Program is given –
• at Page 75
• Course Text Book
• Complete Runnable Code is available –
• @ e-Learning
Exercises for RDP 2.4
Construct recursive-descent parsers, starting with the following
grammars:
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