1 / 16

11CS10045 SRAJAN GARG

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.

nguyet
Download Presentation

11CS10045 SRAJAN GARG

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 11CS10045SRAJAN GARG COMPILERS 4thSeptember 2013 Wednesday

  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 : • < · • ֹ═ • ·>

  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, thenfa and gb are in the same group. fa gb

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

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

  6. >>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 faand value ofg(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.

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

  8. fid=4 gid g* f* f+ g+ g$ f$

  9. fid gid =5 g* f* f+ g+ g$ f$

  10. fid gid g* f* = 4 f+ g+ g$ f$

  11. fid gid g* f* f+ = 2 g+ g$ f$

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

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

More Related