Chapter - 3
Chapter - 3
Ambiguity: example
Elimination of ambiguity
Precedence/Association
These two derivations point out a problem with the grammar:
The grammar do not have notion of precedence, or implied order of evaluation
To add precedence
o Create a non-terminal for each level of precedence
o Isolate the corresponding part of the grammar
o Force the parser to recognize high precedence sub expressions first
For algebraic expressions
o Multiplication and division, first (level one)
o Subtraction and addition, next (level two)
To add association
o Left-associative : The next-level (higher) non-terminal places at the last of
a production
o Elimination of ambiguity
o To disambiguate the grammar :
Elimination of ambiguity
Precedence/Association
o we can use precedence of operators as follows:
* Higher precedence (left associative)
+ Lower precedence (left associative)
o We get the following unambiguous grammar:
Left Recursion
Generally, we can eliminate immediate left recursion from them by the following
technique. First we group the A-productions as:
A Aα1 |Aα2 |…. |Aαm |β1 | β2|….| βn
Left factoring
When a non-terminal has two or more productions whose right-hand sides start
with the same grammar symbols, the grammar is not LL(1) and cannot be used
for predictive parsing
A predictive parser (a top-down parser without backtracking) insists that the
grammar must be left-factored.
• In general: A αβ1 | αβ2 , where α-is a non-empty and the first symbol of β1
and β2.
When processing α we do not know whether to expand A to αβ1 or to αβ2, but
if we re-write the grammar as follows:
A αA`
A` β1 | β2 so, we can immediately expand A to αA`.
Example: given the following grammar:
S iEtS | iEtSeS | a
E b
Left factored, this grammar becomes: S iEtSS` | a
S` eS | ε
E b
Syntax analysis
Every language has rules that prescribe the syntactic structure of well-formed
programs.
The syntax can be described using Context Free Grammars (CFG) notation.
The use of CFGs has several advantages:
o helps in identifying ambiguities
o it is possible to have a tool which produces automatically a parser using the
grammar
o a properly designed grammar helps in modifying the parser easily when the
language changes
Top-down parsing
Recursive Descent Parsing (RDP)
This method of top-down parsing can be considered as an attempt to find the
left most derivation for an input string. It may involve backtracking.
To construct the parse tree using RDP:
o We create one node tree consisting of S.
o Two pointers, one for the tree and one for the input, will be used to indicate
where the parsing process is.
o Initially, they will be on S and the first input symbol, respectively.
o Then we use the first S-production to expand the tree. The tree pointer will
be positioned on the left most symbol of the newly created sub-tree.
As the symbol pointed by the tree pointer matches that of the symbol pointed
by the input pointer, both pointers are moved to the right.
Whenever the tree pointer points on a non-terminal, we expand it using the first
production of the non-terminal.
Top-down parsing
Recursive Descent Parsing (RDP)
Whenever the pointers point on different terminals, the production that was
used is not correct, thus another production should be used. We have to go
back to the step just before we replaced the non-terminal and use another
production.
If we reach the end of the input and the tree pointer passes the last symbol of
the tree, we have finished parsing.
Example:
G: S cAd
A ab|a
Draw the parse tree for the input string cad using the above method.
Non-recursive predictive parsing
It is possible to build a non-recursive parser by explicitly maintaining a stack.
This method uses a parsing table that determines the next production to be
applied. The input buffer contains the string to be parsed followed by $ (the
right end marker)
LR parser
Conflict during shift/reduce parsing
The LR(k) stack stores strings of the form: S0X0S1X1…XmSm where
o Si is a new symbol called state that summarizes the information contained
in the stack
o Sm is the state on top of the stack
o Xi is a grammar symbol
The parser program decides the next step by using:
the top of the stack (Sm),
the input symbol (ai), and
the parsing table which has two parts: ACTION and GOTO.
then consulting the entry ACTION[Sm , ai] in the parsing action table
Structure of the LR Parsing Table
The parsing table consists of two parts:
o a parsing-action function ACTION and
o a goto function GOTO.
The ACTION function takes as arguments a state i and a terminal a (or $, the
input endmarker).
The value of ACTION[i, a] can have one of four forms:
o Shift j, where j is a state. The action taken by the parser shifts input a on
the top of the stack, but uses state j to represent a.
o Reduce A β, The action of the parser reduces β on the top of the stack
to head A.
o Accept, The parser accepts the input and finishes parsing.
o Error, The parser discovers an error
GOTO function, defined on sets of items, to states.
o GOTO[Ii, A] = Ij, then GOTO maps a state i and a non-terminal A to state j.
LR parser configuration
Behavior of an LR parser describes the complete state of the parser.
A configuration of an LR parser is a pair:
Example: Construction of the set of Items for the augmented grammar above
G1‟.
The set of Items construction
Example: Construction of the set of Items for the augmented grammar above
G1‟.
LR (0) automation
SLR table construction algorithm
1. Construct C = {I0, I1, ......, IN} the collection of the set of LR (0) items for G`.
2. State i is constructed from Ii and a) If [A α.aβ] is in Ii and Goto (Ii, a) = Ij (a is
a terminal) then action [i, a]=shift j b) If [A α.] is in Ii then action [i, a] = reduce
A α for a in Follow (A) for A ≠ S` c) If [S` S.] is in Ii then action [i, $] = accept.
o If no conflicting action is created by 1 and 2 the grammar is SLR (1); otherwise
it is not.
3. For all non-terminals A, if Goto (Ii, A) = Ij then Goto [i, A] = j
4. All entries of the parsing table not defined by 2 and 3 are made error
5. The initial state is the one constructed from the set of items containing
[S` .S]
SLR table construction algorithm
Example: Construct the SLR parsing table for the grammar G1`
Follow (E) = {+, ), $} Follow (T) = {+, ), $, *
Follow (F) = {+, ), $,*}
E` E
1 E E + T
2 E T
3 TT*F
4TF
5 F (E)
6 F id}
SLR table construction algorithm
By following the method we find the Parsing table used earlier.