11CS10045 SRAJAN GARG
description
Transcript of 11CS10045 SRAJAN GARG
COMPILERS4 T H SEPTEMBER 2013
WEDNESDAY
11CS10045SRAJAN 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. ·>
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
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
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.
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 <<
PRECEDENCE TABLE
Id * + $id ·> ·> ·>
* < · ·> ·> ·>
+ < · < · ·> ·>
$ < · < · < ·
Consider the Grammar : E + E | E * E | E | id
fid=4
g$
f*
f+
f$
g+
g*
gid
fid
g$
f*
f+
f$
g+
g*
gid =5
fid
g$
f* = 4
f+
f$
g+
g*
gid
fid
g$
f*
f+ = 2
f$
g+
g*
gid
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
LR PARSER
•An LR parser makes shift-reduce decisions by maintaining STATES to keep track of where we are in parse.
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
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.
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!