Professional Documents
Culture Documents
(Parsing)
By,
1
Reference:
Compilers : Principles, Techniques and Tools
Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman, second
edition
The Role of the Parser
• The parser obtains a string of tokens from the lexical
analyzer verifies that the string of token names can be
generated by the grammar for the source language.
• Parser should report any syntax errors in an intelligible
fashion and recover from commonly occurring errors to
continue processing the remainder of the program.
3
The Role of the Parser
that program.
5
The Role of the Parser
• The Parser
6
Types of Parsers
• There are three general types of parsers for grammars:
universal, top-down, and bottom-up.
• Universal parsing methods such as the Cocke-Younger-
Kasami algorithm and Earley's algorithm can parse any
grammar. These general methods are, however, too
inefficient to use in production compilers.
• Commonly used methods for parsers
1. Top-down parsers
build parse tree from top (root) to bottom(leaves)
2. Bottom-up Parsers
build parse tree start from the leaves and work their
way up to the root (bottom to top)
In either case, the input to the parser is scanned from left
to right, one symbol at a time.
7
Error Recovering Strategies by a Parser
1. Panic Mode:
– Discard symbols until a valid token can be found
2. Phrase Level
– Deleting , inserting , Replacing symbols
3. Error Productions
– Uses error productions that generate erroneous
constructs
4. Global Corrections
– uses algorithms that make only a few changes in the
input string
8
Panic-Mode Recovery
16
Notational Conventions
17
Notational Conventions
18
Notational Conventions
19
Notational Conventions
For Example
20
Derivations
Consider,
+
: derives in one or more steps
22
CFG - Terminology
*
• S
G is S where is a sentence of G.
*
24
Left-Most and Right-Most Derivations
E -E E
-(E) E
-(E+E) E
- E - E - E
( E ) ( E )
E E E + E
- E - E
-(id+E) -(id+id)
( E ) ( E )
E + E E + E
id id id
26
Parse Tree
• Get a parse tree for the derivations:
E E
E + E E * E
id E * E E + E id
id id id id
Ambiguity
• A grammar produces more than one parse tree for a
sentence is called as an ambiguous grammar.
Note :
• We should eliminate the ambiguity in the grammar during the
design phase of the compiler.
29
Left-Most and Right-Most Derivations
Exercise:
30
Answer:
a) 1. S =lm=> SS* => SS+S* => aS+S* => aa+S* => aa+a*
b) S =rm=> SS* => Sa* => SS+a* => Sa+a* => aa+a*
c) Parse tree:
d) Unambiguous
e) L = {Postfix expression consisting of digits, plus and multiple
signs}.
31
Eliminating Ambiguity
Example1:
Ambiguous grammar
"dangling else“
grammar
stmt stmt
E2 S1 E2 S1 S2
1 2
33
Eliminating Ambiguity
• We prefer the second parse tree (else matches with closest if).
• We can rewrite the dangling-else grammar as the following unambiguous
grammar
A
+
A for some string
35
Left Recursion
Note
• Top-down parsing techniques cannot handle left-
recursive grammars.
36
Elimination of Immediate Left-Recursion
37
Immediate Left-Recursion
In general,
A → A 1 | ... | A m | 1 | ... | n
where 1 ... n do not start with A
A → 1 A’ | ... | n A’
A’ → 1 A’ | ... | m A’ | an equivalent grammar
38
Elimination of Immediate Left-Recursion
Exercise
39
Left-Recursion -- Problem
• A grammar need not be immediately left-recursive, but it
still can be left-recursive. By just eliminating the
immediate left-recursion, we may not get a grammar which is
not left-recursive.
40
Eliminate Left-Recursion -- Algorithm
Arrange non-terminals in some order: A1 ... An
for i from 1 to n do {
for j from 1 to i-1 do {
replace each production
Ai → Aj
by
Ai → 1 | ... | k
where Aj → 1 | ... | k
}
eliminate immediate left-recursions among Ai productions
}
41
Eliminate Left-Recursion -- Example
Exercise
S → Aa | b
A → Ac | Sd | f
42
Eliminate Left-Recursion – soln1
S → Aa | b
A → Ac | Sd | f
- Order of non-terminals: S, A
for S:
- we do not enter the inner loop.
- there is no immediate left recursion in S.
for A:
- Replace A → Sd with A → Aad | bd
So, we will have A → Ac | Aad | bd | f
- Eliminate the immediate left-recursion in A
A → bdA’ | fA’
A’ → cA’ | adA’ |
43
Eliminate Left-Recursion – soln2
S → Aa | b
A → Ac | Sd | f
- Order of non-terminals: A, S
for A:
- we do not enter the inner loop.
- Eliminate the immediate left-recursion in A
A → SdA’ | fA’
A’ → cA’ |
for S:
- Replace S → Aa with S → SdA’a | fA’a
So, we will have S → SdA’a | fA’a | b
- Eliminate the immediate left-recursion in S
S → fA’aS’ | bS’
S’ → dA’aS’ |
consider
45
Left-Factoring (cont.)
• In general,
A → 1 | 2 where is non-empty and the first symbols
of 1 and 2 (if they have one)are different.
• when processing we cannot know whether expand
A to 1 or
A to 2
46
Left-Factoring -- Algorithm
Algorithm : Left factoring a grammar.
INPUT: Grammar G.
Method: For each nonterminal A, find the longest prefix a! common to two
or more of its alternatives. If ≠ - i.e., there is a nontrivial common
prefix - replace all of the A-productions by A → 1 | ... | n | ,
where represents all alternatives that do not begin with a, by
A → A’ |
A’ → 1 | ... | n
Here A' is a new nonterminal. Repeatedly apply this transformation until no two alternatives for
a nonterminal have a common prefix.
47
Left-Factoring
Exercise:
48
Left-Factoring – soln1
A → aA’ | cdg | cdeB | cdfB
A’ → bB | B
A → aA’ | cdA’’
A’ → bB | B
A’’ → g | eB | fB
49
Left-Factoring – soln2
A → ad | a | ab | abc | b
A → aA’ | b
A’ → d | | b | bc
A → aA’ | b
A’ → d | | bA’’
A’’ → | c
50
THANK YOU