Bottom Up Parser - Shift-Reduce Parsers
Bottom Up Parser - Shift-Reduce Parsers
1. Operator-Precedence Parser
– simple, but only a small class of grammars.
CFG
LR
LALR
2. LR-Parsers SLR
– covers wide range of grammars.
• SLR – simple LR parser
• LR – most general LR parser
• LALR – intermediate LR parser ( lookahead LR parser)
– SLR, LR and LALR work same, only their parsing tables are different.
13
Operator-Precedence Parser
• Operator grammar -
– When grammar has expression with many operators like +,-,*,/ etc. in
which each operator having some precedence and associativity such
grammar is called as operator grammar.
– small, but an important class of grammars
– we may have an efficient operator precedence parser (a shift-reduce
parser) for an operator grammar.
• Operator precedence grammar -
- Any grammar G is called an Operator precedence grammar if it
meets the following conditions-
1. The grammar in which only one precedence relation holds
between two terminals
2. There exist no production rule which contains ε (epsilon) on its
right hand side.
3. There exist no production rule which contains two non-terminals
adjacent to each other on its right hand side.
14
OPP CC MODERN
• Ex:
EAB EEOE EE + E |
Aa Eid E*E|
Bb O+ | * | / | ε E / E | id
not operator grammar not operator grammar operator grammar
Note : Operator precedence can only established between the terminals of the
grammar. It ignores the non-terminal.
15
Components of OPP
Input a + b * c $
Buffer
stack
.
.
S
Operator Precedence Relation
X
Table (Matrix)
$
16
OPP CC MODERN
Operator precedence parser consist of :
• Input buffer: contains the string to be parsed followed by $, a symbol
used for indicating the end of the input.
• Stack : containing the sequence of grammar symbols with a $ at the
bottom of the stack.
• Operator Precedence Matrix: containing the precedence relationship
between a pair of terminals.
In operator-precedence parsing, we define three disjoint precedence
relations between certain pairs of terminals.
OPP CC MODERN
How to Create Operator-Precedence Relations
We use associativity and precedence relations among operators.
4. Also, let
(=·) $ <. ( id .> ) ) .> $
( <. ( $ <. id id .> $ ) .> )
( <. id
19
+ - * / ^ id ( ) $
+ .> .> <. <. <. <. <. .> .>
OPP CC MODERN
Operator-Precedence Parsing Algorithm
• The input string is w$, the initial stack is $ and a table holds precedence
relations between certain terminals
Algorithm:
set p to point to the first symbol of w$ ;
repeat forever
if ( $ is on top of the stack and p points to $ ) then return
else {
let a be the topmost terminal symbol on the stack and let b be the symbol pointed to by p;
if ( a <. b or a =· b ) then { /* SHIFT */
push b onto the stack;
advance p to the next input symbol;
}
else if ( a .> b ) then /* REDUCE */
repeat pop stack
until ( the top of stack terminal is related by <. to the terminal most recently
popped );
else error();
21
}
22
OPP CC MODERN
Operator-Precedence Parsing Algorithm -- Example
stack input action E E+E | E*E | id
24
Solution-
The terminal symbols in the given grammar are-
{(,),a,,}
Now, we build the operator precedence table for these operators-
27
OPP CC MODERN
Operator Precedence Table-
a ( ) , $
a > > > >
29
• LEADING () : for each NT, those terminals that can be the first terminal
in a string derived from that NT
1. LEADING(A) = { a| A ⇒+ γaδ, where γ is ∊ or a single non-terminal.}
2. If A Bα then LEADING (B) is added to the LEADING(A)
• TRAILING() : for each NT, those terminals that can be the last terminal
in a string derived from that NT
1. TRAILING(A) = { a| A ⇒+ γaδ, where δ is ∊ or a single non-terminal.}
2. If A αB then TRAILING (B) is added to the TRAILING(A)
33
OPP CC MODERN
Leading and Trailing for every NT involved in given grammar:
1. For any production like X YaZ
Leading(X) = {a} if Y is ∊ or single NT
2. For any production like X Bα
Leading(X) = Leading (B)
3. For any production like X YaZ
Trailing(X) = {a} if Z is ∊ or single NT
4. For any production like X αB
Trailing(X) = Trailing(B)
5. For any production like A B
Leading(A) = Leading (B) and Trailing(A) = Trailing(B)
6. For any production like A a
Leading(A) = {a} and Trailing(A) = {a}
34
EE+T | T
TT*F | F
F(E) | id
To find the Table we have to find the last & first terminal for
each non-terminal as follows:
Non terminal Leading Trailing
E *, +, (, id *, +, ), id
T *, (, id *, ), id
F (, id ), id
35
OPP CC MODERN