Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPILERS 4 TH SEPTEMBER 2013 WEDNESDAY 11CS10045 SRAJAN GARG.

Similar presentations


Presentation on theme: "COMPILERS 4 TH SEPTEMBER 2013 WEDNESDAY 11CS10045 SRAJAN GARG."— Presentation transcript:

1 COMPILERS 4 TH SEPTEMBER 2013 WEDNESDAY 11CS10045 SRAJAN GARG

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

3 PRECEDENCE FUNCTIONS Create symbols f a and g a 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 f a and g b are in the same group. f a g b

4 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 f a to that of g b. fafa gbgb fafa gbgb a. >b a <. b

5 PRECEDENCE FUNCTIONS For all the terminals a ε T in a given Grammar, make two functions f a & g a. 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.

6 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 f a and value of g(b) is the length of the longest path from the group of g b. Precedence value for a given terminal is given by maximum value of its corresponding function. >>NOTE <<

7 PRECEDENCE TABLE Id*+$ id ·> *< · ·> +< · ·> $< · Consider the Grammar : E + E | E * E | E | id

8 f id =4 g$g$ f*f* f+f+ f$f$ g+g+ g*g* g id

9 f id g$g$ f*f* f+f+ f$f$ g+g+ g*g* g id =5

10 f id g$g$ f * = 4 f+f+ f$f$ g+g+ g*g* g id

11 f id g$g$ f*f* f + = 2 f$f$ g+g+ g*g* g id

12 Similarly by observing longest path from each node... PRECEDENCE FUNCTION TABLE is hence given as.. id * + $ f 4420 g 5310

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

14 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

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

16 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 :X 1 X 2 X 3 X 4 X 5 ;u = X 1 X 2 \ In A  X.YZ, dot signifies u has matched X, A  XY.Z, XY is matched, A  XYZ. -> entire input is matched -> REDUCE!


Download ppt "COMPILERS 4 TH SEPTEMBER 2013 WEDNESDAY 11CS10045 SRAJAN GARG."

Similar presentations


Ads by Google