11CS10045 SRAJAN GARG

16
COMPILERS 4 TH SEPTEMBER 2013 WEDNESDAY 11CS10045 SRAJAN GARG

description

11CS10045 SRAJAN GARG. COMPILERS 4 th September 2013 Wednesday. ENCODING THE PRECEDENCE TABLE. Grammars that have the property that no production on the right side has an ε or two adjacent non-terminals are called Operator Grammars. - PowerPoint PPT Presentation

Transcript of 11CS10045 SRAJAN GARG

Page 1: 11CS10045 SRAJAN  GARG

COMPILERS4 T H SEPTEMBER 2013

WEDNESDAY

11CS10045SRAJAN GARG

Page 2: 11CS10045 SRAJAN  GARG

ENCODING THE PRECEDENCE TABLE

• Grammars that have the property that no production on the right side has an ε or two adjacent non-terminals are called Operator Grammars.

• In operator –precedence parsing three disjoint precedence relations between certain pairs of terminals has been defined.

They are : 1. < ·2. ֹ�═3. ·>

Page 3: 11CS10045 SRAJAN  GARG

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

Page 4: 11CS10045 SRAJAN  GARG

PRECEDENCE FUNCTIONS

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

a .>b

a <. b

Page 5: 11CS10045 SRAJAN  GARG

PRECEDENCE FUNCTIONS

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.

Page 6: 11CS10045 SRAJAN  GARG

If the graph constructed has a cycle, then no precedence functions exist.since , a .>b; b .> c; c .> aNO 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.

>>NOTE <<

Page 7: 11CS10045 SRAJAN  GARG

PRECEDENCE TABLE

Id * + $id ·> ·> ·>

* < · ·> ·> ·>

+ < · < · ·> ·>

$ < · < · < ·

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

Page 8: 11CS10045 SRAJAN  GARG

fid=4

g$

f*

f+

f$

g+

g*

gid

Page 9: 11CS10045 SRAJAN  GARG

fid

g$

f*

f+

f$

g+

g*

gid =5

Page 10: 11CS10045 SRAJAN  GARG

fid

g$

f* = 4

f+

f$

g+

g*

gid

Page 11: 11CS10045 SRAJAN  GARG

fid

g$

f*

f+ = 2

f$

g+

g*

gid

Page 12: 11CS10045 SRAJAN  GARG

Similarly by observing longest path from each node...PRECEDENCE FUNCTION TABLE is hence given as..

id * + $

f 4 4 2 0

g 5 3 1 0

Page 13: 11CS10045 SRAJAN  GARG

LR PARSER

•An LR parser makes shift-reduce decisions by maintaining STATES to keep track of where we are in parse.

Page 14: 11CS10045 SRAJAN  GARG

The basic principle is an automata whose transitions are driven by a terminal or a non-terminal.

Each state in the automata is a set of “ITEMS’.

Key components are input string, a stack, a driver program, output and a parsing table that has two parts(ACTION & GOTO).

If there is a transition in the FSM on current i/p symbol ACTION : SHIFT

Else ACTION : REDUCE ; Back-Track to previous state

Page 15: 11CS10045 SRAJAN  GARG

ITEMS

Each state in FSM is a set of ITEMS.For any Grammar G, an ITEM is a production

of G with a dot at some position of body.The production A XYZ yields four Items :

A .XYZ A X.YZ A XY.Z A XYZ.

Page 16: 11CS10045 SRAJAN  GARG

ITEMS

The dot ( . ) in the Items signifies the amount of input string that has matched to string corresponding to the symbols on left of the dot.i/p :X1 X2X3X4 X5 ; u = X1 X2 \

In A X.YZ, dot signifies u has matched X, A XY.Z , XY is matched, A XYZ. -> entire input is matched -> REDUCE!