0% found this document useful (0 votes)
17 views

Syntax Analyzer 2-up to LALR

Uploaded by

mdsaif804403
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Syntax Analyzer 2-up to LALR

Uploaded by

mdsaif804403
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 74

Syntax Analyzer (Parser)-2

Input: list of tokens produced by scanner/LA


Output: tree(syntax) which shows structure of program
Introduction

• Constructs parse tree for an input string beginning at the leaves (the
bottom) and working towards the root (the top)
• Example: id*id

E -> E + T | T id*id F * id T * id T* F T E
T -> T * F | F
F id T*F T
F -> (E) | id id F

id id F id T*F

id F id

id
Bottom Up Parsers
Shift-reduce parser

• It takes the given input string and builds a parse tree-

• Starting from the bottom at the leaves.


• And growing the tree towards the top to the root.

• Two data structures are required to implement a shift-reduce


parser-

• A Stack is required to hold the grammar symbols.


• An Input buffer is required to hold the string to be
parsed.
Shift-reduce parser

• Initially, shift-reduce parser is present in the following


configuration where-

• Stack contains only the $ symbol.


• Input buffer contains the input string with $ at its end.
Shift-reduce parser

• The general idea is to shift some symbols of input to the stack


until a reduction can be applied
• At each reduction step, a specific substring matching the body
of a production is replaced by the nonterminal at the head of
the production
• The key decisions during bottom-up parsing are about when to
reduce and about what production to apply
• A reduction is a reverse of a step in a derivation
• The goal of a bottom-up parser is to construct a derivation in
reverse:

• E=>T=>T*F=>T*id=>F*id=>id*id
Shift-reduce parser

The shift reduce parser performs following basic operations:

• Shift: Moving of the symbols from input buffer onto the stack, this
action is called shift. (push current input symbol to stack)

• Reduce: If handle appears on the top of the stack then reduction of


it by appropriate rule is done. This action is called reduce action.
(symbol replace by non terminal)

• Accept: If stack contains start symbol only and input buffer is


empty at the same time then that action is called accept.

• Error: A situation in which parser cannot either shift or reduce the


symbols, it cannot even perform accept action then it is called error
action.
Shift-reduce parser - Example
Consider the following
grammar-
The priority order is: id > x > –
E→E–E

E→ExE
Stack Input Buffer Parsing Action
E → id $ id – id x id $ Shift
$ id – id x id $ Reduce E → id
Parse the input string $E – id x id $ Shift
$E– id x id $ Shift
id – id x id using a $ E – id x id $ Reduce E → id
shift-reduce parser. $E–E x id $ Shift
$E–Ex id $ Shift
$ E – E x id $ Reduce E → id
$E–ExE $ Reduce E → E x E
$E–E $ Reduce E → E – E
$E $ Accept
• Handle: Handle is a substring that matches the
body of a production. (Handle = RHS of production)
• Handle is a Right Sentential Form + position
where reduction can be performed + production
used for reduction
Handle pruning

• A Handle is a substring that matches the body of a production and whose


reduction represents one step along the reverse of a rightmost derivation

Right sentential form Handle Reducing production


id*id id F->id
F*id F T->F
T*id id F->id
T*F T*F E->T*F
Handle pruning
• Handle: A “handle” of a string is a substring of the string that matches the
right side of a production, and whose reduction to the non terminal of the
production is one step along the reverse of rightmost derivation. (Handle =
RHS of production)

• Handle pruning: The process of discovering a handle and reducing it to


appropriate left hand side non terminal is known as handle pruning.
(Replacing)

E->E+E String: id1+id2*id3


E->E*E Rightmost Derivation
E->id E
E+E
E+E*E
E+E*id3
E+id2*id3
id1+id2*id3
Shift reduce parsing

• A stack is used to hold grammar symbols


• Handle always appear on top of the stack
• Initial configuration:
Stack Input
$ w$
• Acceptance configuration
Stack Input
$S $
Shift reduce parsing (cont.)

• Basic operations:
• Shift
• Reduce Stack Input Action
• Accept
• Error $ id*id$ shift
• Example: id*id
$id *id$ reduce by F->id
$F *id$ reduce by T->F
$T *id$ shift
$T* id$ shift
$T*id $ reduce by F->id
$T*F $ reduce by T->T*F
$T $ reduce by E->T
$E $ accept
Handle will appear on top of the stack

S S
A
B
B A
α β γ z α γ z
y x y
Stack Input Stack Input
$αβγ yz$ $αγ xyz$
$αβB yz$ $αBxy z$
$αβBy z$
Conflicts during shift reduce parsing

• Two kind of conflicts


• Shift/reduce conflict
• Reduce/reduce conflict
• Example:

Stack Input
… if expr then stmt else …$
Conflicts during shift reduce parsing
• There are two kinds of conflicts that can occur in an SLR(1) parsing table.
A shift-reduce conflict occurs in a state that requests both a shift action and
a reduce action. A reduce-reduce conflict occurs in a state that requests two
or more different reduce actions.

How to determine?

• A full parsing table is not needed, only the canonical collection. In the
canonical collection, find all final items (and only final items), and see if:

• There are both shift and reduce in the same item ("shift-reduce", s/r)
• There are two reduce actions in the same item ("reduce-reduce", r/r)

If none of these is true, there are no conflicts, even in LR(0). If there are
some of the above, SLR(1) still may solve it.
Shift reduce conflict and Reduce/reduce conflict

• Shift reduce conflict: It is caused when grammar allows a


production rule to be reduced in a state and in the same state
another production rule is shifted for same token.

• A reduce/reduce conflict occurs if there are two or more rules


that apply to the same sequence of input. This usually indicates a
serious error in the grammar. For example, here is an erroneous
attempt to define a sequence of zero or more word groupings.
Reduce/reduce conflict

stmt -> id(parameter_list)


stmt -> expr:=expr
parameter_list->parameter_list, parameter
parameter_list->parameter
parameter->id
expr->id(expr_list)
expr->id
expr_list->expr_list, expr
Stack Input
expr_list->expr
… id(id ,id) …$
LR Parsing

• The most prevalent type of bottom-up parsers


• LR(k), mostly interested on parsers with k<=1
• Why LR parsers?
• Table driven
• Can be constructed to recognize all programming language constructs
• Most general non-backtracking shift-reduce parsing method
• Can detect a syntactic error as soon as it is possible to do so
• Class of grammars for which we can construct LR parsers are superset of
those which we can construct LL parsers
LR Parsers
LR (k )

Left-to-Right Rightmost Derivation In Number Of Input


scan Reverse Symbols Of Look
Ahead
• Types of LR Parsers

1. LR(0) Parser
2. Simple LR-Parser (SLR)
3. Canonical LR Parser (CLR)
4. Look ahead LALR Parser.
Comparison of LL & LR Methods

LL(1)

LR(1) LALR SL R LR(0) LL(0)


Advantages of LR Parsers

• LR parsers are constructed to recognize all Programming


Languages.
• The LR-parsing is Non-Backtracking Shift- Reduce Parser
• An LR parser can detect a syntactic errors
• It scans input string from left-to-right and use left most derivation
in reverse
The LR Parsing Algorithm
Input

a1 a2 … ai … an $ Scanner

sm L R Parsing Engine
Xm
s m-1
Xm-1
Compiler Construction

s0 Parser
Action Goto Grammar
Generator
Stack
L R Parsing Tables
Bottom-Up Parsing Algorithms

LR(k) parsing
L: scan input Left to right
R: produce Rightmost derivation
k tokens of lookahead
LR(0)
zero tokens of look-ahead
SLR
Simple LR: like LR(0), but uses FOLLOW sets to build
more “precise” parsing tables
LR(0) is a toy, so we focus on SLR

LR Family:
LR(0)<= SLR(1)<=LALR(1)<=CLR(1)

CLR(1) Is more powerful.


LR(K) parser

The LR(K) parser performs following basic operations:

• Shift: Moving of the symbols from input buffer onto the stack, this action is
called shift.

• Reduce: If handle appears on the top of the stack then reduction of it by


appropriate rule is done. This action is called reduce action.

• Accept: If stack contains start symbol only and input buffer is empty at the
same time then that action is called accept.

• Error: A situation in which parser cannot either shift or reduce the symbols, it
cannot even perform accept action then it is called error action.
LR(0) steps:
• Right sentential form
CFG G, S-> alpha, alpha – T or NT, A right sentential form is a sentential
form that can be derived by right most derivation.

• Augmented Grammar- To construct DFA recognizing the viable prefix


we make use of augmented grammar.
Purpose of augmenting grammar is to make it explicitly clear when to accept
the string and stop parsing. We will stop parsing when parser is on the verge
of caring out reduction using S’ -> S.

• LR(0) items
. Dot anywhere in the right side, including the beginning or end. In the case of
an epsilon production then B -> epsilon, B -> . Is an item

• First and follow set

• Construction LR parsing table i..e ACTION And GOTO table


Construction LR parsing table i..e ACTION
And GOTO table
• Steps to construct action table:
▪ For every state Ii in the items check if any terminals symbol say ‘a’ is present
after (.) then perform ACTION [Ii, a ] = Sj,
where, S is used for shift action and j is the next state for terminal ‘a’ in the
item.
▪ For every state Ii in the items check the production like A-> alpha .
then for every b in the follow (A) do ACTION[Ii, b] = Rj
where, S is used for reduce action and j is the production number for
A-> alpha in the grammar.

• Step to construct goto table:


▪ A GOTO table is simply a mapping of transition in the DFA on non terminal.
For every state Ii in the item check if any non terminal symbol say ‘A’ is
present after dot then perform GOTO[Ii, A] = j
where, j is the next state for non terminal ‘A’ in the item.
States of an LR parser

• States represent set of items


• An LR(0) item of G is a production of G with the dot at some position
of the body:
• For A->XYZ we have following items
• A->.XYZ
• A->X.YZ
• A->XY.Z
• A->XYZ.
• In a state having A->.XYZ we hope to see a string derivable from XYZ next
on the input.
• What about A->X.YZ?
States of an LR parser

• An LR(0) Item of a Grammar G is a Production of G with a


Dot (. ) at some position of the right side.
• .Production A -> XYZ yields the Four items:

• A->•XYZ We hope to see a string derivable from XYZ next on


the input.

• A->X•YZ We have just seen on the input a string derivable from


X and that we hope next to see a string derivable from YZ next on
the input.
States of an LR parser

• A-> XY•Z
• A-> XYZ•
• The production A->  generates only one
item, A-> •.

• Each of this item is a Viable prefixes

• Closure Item: An Item created by the closure operation on a state.

• Complete Item : An Item where the Item Dot is at the end of the
RHS.
• The LR parser consists of
1) Input 2)Output 3)Stack 4) Driver Program 5) Parsing Table

• The Driver Program is same for all LR Parsers.

• Only the Parsing Table changes from one parser to the other.

• In CLR method the stack holds the states from the LR(0)
automation and canonical LR and LALR methods are same
• The Driver Program uses the Stack to store a string

s 0 X 1 s 1 X 2 …X m s m

• Where s m is the Top of the Stack.


• The S k ‘s are State Symbols
• The X i ‘s are Grammar Symbols.
• Together State and Grammar Symbols determine a
Shift-reduce Parsing Decision.
• The Parsing Program reads characters from an
Input Buffer one at a time

• The Current Input Symbols are used to index the


parsing table and determine the shift-reduce
parsing decision

• In an implementation, the grammar symbols need not


appear on the stack
Parse Table
• The LR Shift-Reduce Parsers can be efficiently
implemented by computing a table to guide the
processing

• The Parsing Table consists of two parts:

• A Parsing Action Function and

• A GOTO function.
Closure algorithm

SetOfItems CLOSURE(I) {
J=I;
repeat
α.Bβ in J)
for (each item A->
for (each prodcution B->γ of G)
if (B->.γ is not in J)
add B->.γ to J;
until no more items are added to J on one round;
return J;
GOTO algorithm

SetOfItems GOTO(I,X) {
J=empty;
if (A->α.X β is in I)
add CLOSURE(A-> αX. β ) to J;
return J;
}
The Action Table

• The Action Table specifies the actions of the parser (e.g.,


shift or reduce), for the given parse state and the next
token

• Rows are State Names;


• Columns are Terminals
L R Driver Program

• The LR driver Program determines Sm, the state on top of the


stack and ai, the Current Input symbol.
• It then consults Action[ Sm, ai ] which can take one of four
values:
• Shift
• Reduce
• Accept
• Error
• If Action[ Sm, ai ] = Shift S

• Where S is a State, then the Parser pushes


• ai and S on to the Stack.

• If Action[ Sm, ai ] = Reduce A -> β,

• Then ai and Sm are replaced by A


• if S was the state appearing below ai in the Stack,
then GOTO[S, A] is consulted and the state pushed
onto the stack.

• If Action[ Sm, ai ] = Accept,

• Parsing is completed

• If Action[ Sm, ai ] = Error,

• The Parser discovered an Error.


GOTO Table

• The GOTO table specifies which state to put on top of the stack
after a reduce.

• Rows are State Names;


• Columns are Non-Terminals

• The GOTO Table is important to find out the next state after every
reduction.

• The GOTO Table is indexed by a state of the parser and a Non Terminal
(Grammar Symbol).
ex : GOTO[S, A]

• The GOTO Table simply indicates what the next state of the parser if it
has recognized a certain.
LR(0) Parser
• The LR Parser is a Shift-reduce Parser that makes use of a
Deterministic Finite Automata, recognizing the Set Of All Viable
Prefixes by reading the stack from Bottom To Top.

• if a Finite-State Machine that recognizes viable prefixes of the right


sentential forms is constructed, it can be used to guide the handle selection
in the Shift-reduce Parser.
Augmented Grammar

• If G is a Grammar with Start Symbol S, the Augmented


Grammar G’ is G with a New Start Symbol S`, and New
Production S` -> S$.
.

• The Purpose of the Augmented Grammar is to indicate to the


parser when it should stop parsing and announce acceptance of
the input
Draw backs of LR(0) Parser

• LR(0) is the Simplest Technique in the LR family.

• LR(0) Parsers are too weak to be of practical use

• LR(0) accepts only small class of LR(0) grammar because if


conflicts occurs.

• The Fundamental Limitation of LR(0) is that no look ahead


tokens are used.

• LR(0) Parsing is the weakest and it is not used much in practice


because of its limitations.
Constructing canonical LR(0) item sets

• Augmented grammar:
• G with addition of a production: S’->S
• Closure of item sets:
• If I is a set of items, closure(I) is a set of items constructed from I by the
following rules:
• Add every item in I to closure(I)
• If A->α.Bβ is in closure(I) and B->γ is a production then add the item B-
>.γ to clsoure(I).
• Example:

I0=closure({[E’->.E]}
E’->E E’->.E
E -> E + T | T E->.E+T
T -> T * F | F E->.T
T->.T*F
F -> (E) | id T->.F
F->.(E)
F->.id
Constructing canonical LR(0) item sets
(cont.)
• Goto (I,X) where I is an item set and X is a grammar symbol is closure of
set of all items [A-> αX. β] where [A-> α.X β] is in I
• Example
I1
E’->E.
E E->E.+T
I0=closure({[E’->.E]}
E’->.E I2
E->.E+T T
E’->T.
E->.T T->T.*F
T->.T*F I3
T->.F ( F->(.E)
F->.(E) E->.E+T
E->.T
F->.id T->.T*F
T->.F
F->.(E)
F->.id
Canonical LR(0) items

Void items(G’) {
C= CLOSURE({[S’->.S]});
repeat
for (each set of items I in C)
for (each grammar symbol X)
if (GOTO(I,X) is not empty and not in C)
add GOTO(I,X) to C;
until no new set of items are added to C on a round;
}
E’->E
E -> E + T | T
Example T -> T * F | F
acc F -> (E) | id
$ I6 I9
E->E+.T
I1 T->.T*F T
E’->E. + T->.F
E->E+T.
T->T.*F
E E->E.+T
F->.(E)
F->.id
I0=closure({[E’->.E]} I2
E’->.E T I7
F I10
E->.E+T E->T. * T->T*.F
F->.(E) T->T*F.
E->.T T->T.*F id F->.id
T->.T*F id
T->.F I5
F->.(E)
F->.id ( F->id. +
I4
F->(.E)
I8 I11
E->.E+T
E->.T
E E->E.+T )
T->.T*F F->(E.) F->(E).
T->.F
F->.(E)
F->.id

I3
T>F.
Use of LR(0) automaton

• Example: id*id

Line Stack Symbols Input Action


(1) 0 $ id*id$ Shift to 5
(2) 05 $id *id$ Reduce by F->id
(3) 03 $F *id$ Reduce by T->F
(4) 02 $T *id$ Shift to 7
(5) 027 $T* id$ Shift to 5
(6) 0275 $T*id $ Reduce by F->id
(7) 02710 $T*F $ Reduce by T->T*F
(8) 02 $T $ Reduce by E->T
(9) 01 $E $ accept
LR-Parsing model

INPUT a1 … ai … an $

Sm LR Parsing Program Output


Sm-1

$
ACTION GOTO
LR parsing algorithm

let a be the first symbol of w$;


while(1) { /*repeat forever */
let s be the state on top of the stack;
if (ACTION[s,a] = shift t) {
push t onto the stack;
let a be the next input symbol;
} else if (ACTION[s,a] = reduce A->β) {
pop |β| symbols of the stack;
let state t now be on top of the stack;
push GOTO[t,A] onto the stack;
output the production A->β;
} else if (ACTION[s,a]=accept) break; /* parsing is done */
else call error-recovery routine;
}
Constructing SLR parsing table

• Method
• Construct C={I0,I1, … , In}, the collection of LR(0) items for G’
• State i is constructed from state Ii:
• If [A->α.aβ] is in Ii and Goto(Ii,a)=Ij, then set ACTION[i,a] to “shift j”
• If [A->α.] is in Ii, then set ACTION[i,a] to “reduceA->α” for all a in follow(A)
• If {S’->.S] is in Ii, then set ACTION[I,$] to “Accept”
• If any conflicts appears then we say that the grammar is not SLR(1).
• If GOTO(Ii,A) = Ij then GOTO[i,A]=j
• All entries not defined by above rules are made “error”
• The initial state of the parser is the one constructed from the set of items
containing [S’->.S]
Example grammar which is not SLR(1)
S -> L=R | R
L -> *R | id
R -> L
I0 I1 I3 I5 I7
S’->.S S’->S. S ->R. L -> id. L -> *R.
S -> .L=R
S->.R I2 I4 I6
I8
L -> .*R | S ->L.=R L->*.R S->L=.R
R -> L.
L->.id R ->L. R->.L R->.L
R ->. L L->.*R L->.*R I9
L->.id L->.id S -> L=R.

Action
=
Shift 6
2 Reduce R->L
More powerful LR parsers

• Canonical-LR or just LR method


• Use lookahead symbols for items: LR(1) items
• Results in a large collection of items
• LALR: lookaheads are introduced in LR(0) items
Constructing LR(1) sets of items
SetOfItems Closure(I) {
repeat
for (each item [A->α.Bβ,a] in I)
for (each production B->γ in G’)
for (each terminal b in First(βa))
add [B->.γ, b] to set I;
until no more items are added to I;
return I;
}

SetOfItems Goto(I,X) {
initialize J to be the empty set;
for (each item [A->α.Xβ,a] in I)
add item [A->αX.β,a] to set J;
return closure(J);
}

void items(G’){
initialize C to Closure({[S’->.S,$]});
repeat
for (each set of items I in C)
for (each grammar symbol X)
if (Goto(I,X) is not empty and not in C)
add Goto(I,X) to C;
until no new sets of items are added to C;
}
Example

S’->S
S->CC
C->cC
C->d
Canonical LR(1) parsing table

• Method
• Construct C={I0,I1, … , In}, the collection of LR(1) items for G’
• State i is constructed from state Ii:
• If [A->α.aβ, b] is in Ii and Goto(Ii,a)=Ij, then set ACTION[i,a] to “shift j”
• If [A->α., a] is in Ii, then set ACTION[i,a] to “reduceA->α”
• If {S’->.S,$] is in Ii, then set ACTION[I,$] to “Accept”
• If any conflicts appears then we say that the grammar is not LR(1).
• If GOTO(Ii,A) = Ij then GOTO[i,A]=j
• All entries not defined by above rules are made “error”
• The initial state of the parser is the one constructed from the set of items
containing [S’->.S,$]
Example

S’->S
S->CC
C->cC
C->d
LALR Parsing Table

• For the previous example we had:

I4
C->d. , c/d
I47
C->d. , c/d/$
I7
C->d. , $

⚫ State merges cant produce Shift-Reduce conflicts. Why?


⚫ But it may produce reduce-reduce conflict
Example of RR conflict in state merging

S’->S
S -> aAd | bBd | aBe | bAe
A -> c
B -> c
An easy but space-consuming LALR table
construction
• Method:
1. Construct C={I0,I1,…,In} the collection of LR(1) items.
2. For each core among the set of LR(1) items, find all sets having that core,
and replace these sets by their union.
3. Let C’={J0,J1,…,Jm} be the resulting sets. The parsing actions for state i,
is constructed from Ji as before. If there is a conflict grammar is not
LALR(1).
4. If J is the union of one or more sets of LR(1) items, that is J = I1 UI2…IIk
then the cores of Goto(I1,X), …, Goto(Ik,X) are the same and is a state
like K, then we set Goto(J,X) =k.
• This method is not efficient, a more efficient one is discussed in
the book
Compaction of LR parsing table

• Many rows of action tables are identical


• Store those rows separately and have pointers to them from different states
• Make lists of (terminal-symbol, action) for each state
• Implement Goto table by having a link list for each nonterinal in the form
(current state, next state)
Operator precedence parsing
• Operator precedence grammar is kinds of shift reduce parsing method. It is
applied to a small class of operator grammars.

• A grammar is said to be operator precedence grammar if it has two properties:


• No R.H.S. of any production has a ∈ (A -> ∈ Wrong ).
• No two non-terminals are adjacent. ( A -> AB Wrong).

• Operator precedence can only established between the terminals of the


grammar. It ignores the non-terminal.

• There are the three operator precedence relations:


• a ⋗ b means that terminal "a" has the higher precedence than terminal "b".
• a ⋖ b means that terminal "a" has the lower precedence than terminal "b".
• a ≐ b means that the terminal "a" and "b" both have same precedence.
Operator precedence parsing
Advantages:

The advantages of operator precedence parsing are-

• The implementation is very easy and simple.

• The parser is quite powerful for expressions in programming languages.

Disadvantage:

The disadvantages of operator precedence parsing are-

• The handling of tokens known to have two different precedence becomes


difficult.

• Only small class of grammars can be parsed using this parser.


Operator precedence parsing
• Important Note-

• Operator precedence table is not stored by the operator precedence


parsers.
• This is because it occupies the large space.
• Instead, operator precedence parsers are implemented in a very unique
style.
• They are implemented using operator precedence functions.

• Operator Precedence Functions-

• Precedence functions perform the mapping of terminal symbols to the


integers.
• To decide the precedence relation between symbols, a numerical
comparison is performed.
• It reduces the space complexity to a large extent.
Operator precedence parsing
• S-> aAd
A-> bc

S
String : abcd

a A d

$<a<b=c>d>$
b c
Operator precedence parsing
1. Suppose A-> X1, X2,………Xn
• If Xi and Xi+1 are terminals
then Xi=Xi+1
• If Xi and Xi+2 are terminals and Xi+1 is non terminal, then
then Xi=Xi+2
• If Xi is terminal and Xi+1 is non terminal then
Xi < Lead (Xi+1)
• If Xi is non terminal and Xi+1 is terminal then
then Trial(Xi) > Xi+1
• $ < Lead (S) and Trial (S) > $
Operator precedence parsing
• Example:

1) T -> T +T | T * T | id

2) S -> SAS | a …….. Convert


A -> bSb | b

S -> SbSbS | a | SbS


A -> bSb | b
Operator precedence parsing
• Example:

1) T -> T +T | T * T | id

Operator Precedence Relation: id ⋗ * ⋗ + ⋗ $

id + * $
Id - ⋗ ⋗ ⋗

+ ⋖ ⋗ ⋖ ⋗

* ⋖ ⋗ ⋗ ⋗

$ ⋖ ⋖ ⋖ -
Operator precedence parsing

If (Symbol(Top of the stack operator) < L(Less Precedence) ……. Push


• Example: If (Symbol (Top of the stack operator) > L (Less Precedence) ) ……. Pop

1) T -> T +T | T * T | id
Input String: id+id*id $
Operator Precedence Relation: Top of the stack: $ id + id * id
id ⋗ * ⋗ + ⋗ $
1. Id ⋗ + ………. Pop $ id
2. $ ⋖ id ……… push $ id
id + * $ 3. $ ⋖ + ………. Push $+
4. + ⋖ id ………. Push $ + id
Id - ⋗ ⋗ ⋗ 5. Id ⋗ * ………. Pop $+
6. + ⋖ * ………. Push $+*
+ ⋖ ⋗ ⋖ ⋗
7. * ⋖ id ……… push $ + * id
* ⋖ ⋗ ⋗ ⋗ 8. Id ⋗ $ ………. Pop $+*
9. * ⋗ $ ……… pop $+
$ ⋖ ⋖ ⋖ - 10. + ⋗ $ ……… pop $
11. $ $ ………… Stop Accept
Operator precedence parsing
• Example:

1) T -> T +T | T * T | id

Operator Precedence Relation: id ⋗ * ⋗ + ⋗ $

T T T

Input String: id + id * id $
Operator precedence parsing
• Example:

1) T -> T +T | T * T | id
The graph representing the precedence
functions is-
Size: N*N
id + * $
Id - ⋗ ⋗ ⋗

+ ⋖ ⋗ ⋖ ⋗

* ⋖ ⋗ ⋗ ⋗

$ ⋖ ⋖ ⋖ -
Operator precedence parsing
• Example:

1) T -> T +T | T * T | id
The graph representing the precedence
functions is-

Here, the longest paths are-

fid → gx → f+ → g+ → f$

gid → fx → gx → f+ → g+ → f$
Operator precedence parsing
• Example: The graph representing the precedence
functions is- (not in closed loop)
1) T -> T +T | T * T | id

Size: N*N id + * $
Id - ⋗ ⋗ ⋗

+ ⋖ ⋗ ⋖ ⋗

* ⋖ ⋗ ⋗ ⋗

$ ⋖ ⋖ ⋖ -

Size: 2*N id + * $
fid < gid …. 4<5
F fid – 4 2 4 0 f+ < g* …….2<3
g gid – 5 1 3 0 fid → gx → f+ → g+ → f$
gid → fx → gx → f+ → g+ → f$

You might also like