0% found this document useful (0 votes)
30 views15 pages

CD9

The document discusses operator precedence parsing and how to eliminate left recursion from context-free grammars. It explains how to encode operator precedence into tables using precedence functions by assigning integers based on the precedence relations. An example of applying these concepts to a simple grammar for arithmetic expressions is also provided.

Uploaded by

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

CD9

The document discusses operator precedence parsing and how to eliminate left recursion from context-free grammars. It explains how to encode operator precedence into tables using precedence functions by assigning integers based on the precedence relations. An example of applying these concepts to a simple grammar for arithmetic expressions is also provided.

Uploaded by

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

Compilers

OPERATOR PRECEDENCE PARSING


A shift reduce parser can easily be build for grammars with the property that
production has λ or two adjacent non terminals. This grammar is known as operator
grammar. That is,
Example. The grammar
E  E A E |(E) | -E | id
A  + |-| *|/ |
is not an operator grammar as RHS has two non terminals but the following
grammar is operator grammar.
E  E + E | E* E | id
Operator precedence parsing uses recursive descent for statements and higher level
constructs.
Compiler

ENCODING THE PRECEDENCE TABLE


In operator precedence parsing three disjoint precedence relations between
certain pairs of terminals has been defined. They are
1. < ·
2. ֹ═
3. ·>
They guide the selection of handles and have the following meaning.
(1) a < · b ↔ a yields precedence to b
(2) a · = b ↔ a has the same precedence as b
(3) a · > b ↔ a takes precedence over b
The associativity and precedence of operators are used to determine precedence
relations between terminals. They are used to resolve ambiguities. The dangling
else grammar is also used.
Compiler

PRECEDENCE FUNCTIONS
• Create symbols fa and ga for each a ε T that is a terminal or $.
• Partition the created symbols into as many groups as possible, in such a
way that if a ֹ═ b, then fa and gb are in the same group.

fa
gb
Compiler

• Create a directed graph whose nodes are the groups found in previous
step. For any a and b, if a <. b, place an edge from the group of gb to
the group of fa. If a .> b, place an edge from the group of fa to that
of gb.

fa gb fa gb

a .>b a <. b
Compiler
• For all the terminals a ε T in a given Grammar,
make two functions fa & ga.
• Let a , b ε T , then if..
a ֹ═ b → f(a) = g(b)
a ·> b → f(a) > g(b)
a <· b → f(a) < g(b)

These two functions encode the table to map terminal symbols to


integers.
Compiler
Note.
• If the graph constructed has a cycle, then no precedence
functions exist.
since , a .>b; b .> c; c .> a
NO PRECEDENCE!
• If there are no cycles, value of f(a) is the length of the
longest path beginning at the group of fa and value of g(b) is
the length of the longest path from the group of gb.
• Precedence value for a given terminal is given by maximum
value of its corresponding function.
Compiler

PRECEDENCE TABLE
Consider the Grammar : E + E | E * E | E | id

Id * + $
id ·> ·> ·>

* <· ·> ·> ·>

+ <· <· ·> ·>

$ <· <· <·


fid=4 gid

f* g*

f+ g+

f$ g$
fid gid =5

f* g*

f+ g+

f$ g$
fid gid

f* = 4 g*

f+ g+

f$ g$
fid gid

f* g*

f+ = 2 g+

f$ g$
Compiler

id * + $
Similarly by observing longest
path from each node...
PRECEDENCE FUNCTION
TABLE is hence given as.. f 4 4 2 0

g 5 3 1 0
Compiler
Eliminating Left Recursion
Applying the transformation to the Grammar, we get:
Expr  Term Expr'
Expr'  +Term Expr' | – Term Expr' | 
Term  Factor Term'
Term'  *Factor Term' | / Factor Term' | 
(Goal  Expr and Factor  number | id

General algorithm: works for non-cyclic, no -productions grammars


1. Arrange the non-terminal symbols in order: A1, A2, A3, …, An
2. For i=1 to n do
for j=1 to i-1 do
I) replace each production of the form AiAj with
the productions Ai 1  | 2  | … | k 
where Aj 1 | 2 | … | k are all the current Aj productions
II) eliminate the immediate left recursion among the A i
20 Dec 2023 COMP36512 Lecture 8 13
Compiler

Example
Goal  Expr Term  Factor * Term
Expr  Term + Expr | Factor / Term
| Term – Expr | Factor
| Term Factor  number
| id
We have a problem with the different rules for Expr as well as those for Term. In both
cases, the first symbol of the right-hand side is the same (Term and Factor, respectively).
E.g.:
FIRST(Term)=FIRST(Term)FIRST(Term)={number, id}.
FIRST(Factor)=FIRST(Factor)FIRST(Factor)={number, id}.
Applying left factoring:
Expr  Term Expr´ FIRST(+)={+}; FIRST(–)={–}; FIRST()={};
Expr´ + Expr | – Expr |  FIRST(–) FIRST(+)  FIRST()= =
Term  Factor Term´ FIRST(*)={*}; FIRST(/)={/}; FIRST()={};
Term´ * Term | / Term |  FIRST(*) FIRST(/)  FIRST()= =
14
Compiler

1. Goal  Expr Rule Sentential Form Input


- Goal | x – 2*y
2. Expr  Term Expr´ 1 Expr | x – 2*y
3. Expr´ + Expr 2 Term Expr´ | x – 2*y
4. | - Expr 6 Factor Term´ Expr´ | x – 2*y
11 id Term´ Expr´ | x – 2*y
5. |
Match id Term´ Expr´ x | – 2*y
6. Term  Factor Term´ 9 id  Expr´ x | – 2*y
7. Term´ * Term 4 id – Expr x | – 2*y
8. | / Term Match id – Expr x – | 2*y
2 id – Term Expr´ x – | 2*y
9. | 6 id – Factor Term´ Expr´ x – | 2*y
10. Factor  number 10 id – num Term´ Expr´ x – | 2*y
11. | id Match id – num Term´ Expr´ x – 2 | *y
7 id – num * Term Expr´ x – 2 | *y
Match id – num * Term Expr´ x – 2* | y
6 id – num * Factor Term´ Expr´ x – 2* | y
11 id – num * id Term Expr´ x – 2* | y
The next symbol Match id – num * id Term´ Expr´ x – 2*y |
9 id – num * id Expr´ x – 2*y |
determines each choice 5 id – num * id x – 2*y |
correctly. No backtracking
needed.
20 Dec 2023 COMP36512 Lecture 8 15

You might also like