0% found this document useful (0 votes)
5 views8 pages

Bottom Up Parser - Shift-Reduce Parsers

The document discusses bottom-up parsing, specifically shift-reduce parsers, which include operator-precedence parsers and LR-parsers. It details the components and construction of operator precedence matrices, as well as the algorithm for operator-precedence parsing. Additionally, it provides examples and methods for creating operator-precedence relations using leading and trailing functions.

Uploaded by

joshirajashri20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

Bottom Up Parser - Shift-Reduce Parsers

The document discusses bottom-up parsing, specifically shift-reduce parsers, which include operator-precedence parsers and LR-parsers. It details the components and construction of operator precedence matrices, as well as the algorithm for operator-precedence parsing. Additionally, it provides examples and methods for creating operator-precedence relations using leading and trailing functions.

Uploaded by

joshirajashri20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

OPP

Bottom up parser - Shift-Reduce Parsers


• Bottom up parsing is also known as shift-reduce parsing. There are two
main categories of 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:
EAB EEOE EE + E |
Aa Eid E*E|
Bb O+ | * | / | ε E / E | id
not operator grammar not operator grammar operator grammar

• Operator precedence parser-


• A parser that reads and understand an operator precedence grammar is
called as Operator precedence parser.

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

Operator Precedence output


Parsing Program

.
.
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.

a <. b b has higher precedence than a


a =· b b has same precedence as a
.
a >b b has lower precedence than a
The determination of correct precedence relations between terminals
are based on the traditional notions of associativity and precedence of
operators. (Unary minus causes a problem).
17

Construction of operator precedence matrix


1. Brackets have highest precedence as compared to other operators & its
associativity is from Right to Left.
2. * & / has more precedence as compared to + operators & its
associativity is from Left to Right.
3. + & - has lowest precedence as compared to all other operators & its
associativity is from Left to Right.
4. Operand always have highest precedence, because operators are
operated on operands & there is no any associativity among the two
operands.
5. $ is neither operand nor operator. So it having lowest precedence as
compared to operator as well as operands & there is no any
associativity presents in between two $’s.
6. For equal precedence operator, associativity is considered.
7. Highest precedence operator have .> relation with lowest precedence
operator. 18

OPP CC MODERN
How to Create Operator-Precedence Relations
We use associativity and precedence relations among operators.

1. If operator O1 has higher precedence than operator O2,


 O1 .> O2 and O2 <. O1
2. If operator O1 and operator O2 have equal precedence,
they are left-associative  O1 .> O2 and O2 .> O1
they are right-associative  O1 <. O2 and O2 <. O1
3. For all operators O,
O <. id, id .> O, O <. (, (<. O, O .> ), ) .> O, O .> $, and
$ <. O

4. Also, let
(=·) $ <. ( id .> ) ) .> $
( <. ( $ <. id id .> $ ) .> )
( <. id
19

Using Operator -Precedence Relations


Consider the grammar:
E  E+E | E-E | E*E | E/E | E^E | (E) | id

+ - * / ^ id ( ) $
+ .> .> <. <. <. <. <. .> .>

- .> .> <. <. <. <. <. .> .>

* .> .> .> .> <. <. <. .> .>

/ .> .> .> .> <. <. <. .> .>

^ .> .> .> .> <. <. <. .> .>

id .> .> .> .> .> ERR ERR .> .>

( <. <. <. <. <. <. <. =· ERR


) .> .> .> .> .> ERR ERR .> .>

$ <. <. <. <. <. <. <. ERR ERR


20

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
}

• Use stack for operator parsing as follows:


1. Let $ be on the TOS and input string w$ has pointer .
2. If TOS < pointer symbol or TOS= pointer symbol then push input
symbol to the stack and advance the input pointer to next symbol.
3. If TOS > pointer symbol then pop the TOS until TOS < terminal
symbol which is most recently popped.
Hence the string between < and > as handle , which is solved first.
4. else Error.

22

OPP CC MODERN
Operator-Precedence Parsing Algorithm -- Example
stack input action E  E+E | E*E | id

$ id + id*id$ $ <. id shift (Push id)


$id +id*id$ id .> + reduce E  id (pop id)
$ +id*id$ $ <. + shift (push +) id + * $
.> .> .>
$+ id*id$ + <. id shift (push id) id
$+id *id$ id .> * reduce E  id (pop id) + <. .> <. .>

$+ *id$ + <. * shift (push * ) * <. .> .> .>


$+* id$ * <. id shift (push id)
$ <. <. <.
$+*id $ id .> $ reduce E  id (pop id)
$+* $ * .> $ reduce E  E*E (pop *)
$+ $ + .> $ reduce E  E+E (pop +)
$ $ accept

24

Consider the following grammar and construct the operator precedence


parser-
S→(L)|a
L→L,S|S
Then parse the following string: ( a , ( a , a ) )

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 > > > >

( < < > > >

) < > > >

, < < > > >

$ < < < <


Parse the given string-
Step-01:
Given string to be parsed is:
(a,(a,a))
Now, Inserting $ at both the ends of the string, we get-
$(a,(a,a))$
Now, Inserting precedence operators in between the string, we get-
$<(<a>,<(<a>,<a>)>)>$

29

Operator Precedence Relations Using Leading() &


Trailing() Functions
To produce the Table we have to follow the procedure as:

• 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

Operator Precedence Relations

EE+T | T
TT*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

You might also like