Você está na página 1de 7

COMPILER DESIGN UNIT 4

LR(0) Example (belongs to Un t !) Consider the augmented grammar A -> A A -> ( A ) | a The DFA for it is given below. A A ->. A A ->. (A) A ->. a I0 A -> a . I2 ( A A -> (A . ) A -> (. A) A -> . (A) A ->. a I3 I4 ) A -> (A) . I5 A -> A . I1 a $ accept

Parsing Table for the grammar is given below ST"TE "CTION ! $ # " % & shift reduce reduce shift shift reduce RULE ( " A -> A A -> a " A -> ( A ) # & % INPUT a # ) GOTO A $

L m ts o# SLR($) Pa%s ng Po&e% '()($) parsing is a simple e*tension of ()(!) parsing that is powerful enough to handle all practical language structures. +nfortunatel,- there are a few situations in which '()($) parsing is not .uite powerful enough- and this will lead us to stud, the more powerful general ()($) and (A()($) parsing. The ne*t e*ample is a t,pical situation in which '()($) parsing fails. L m ts o# SLR($) Example Consider the following grammar rules for statements- e*tracted and simplified from Pascal (a similar situation occurs in C) stmt call_stmt assign_stmt exp exp /0 -> -> -> -> call_stmt | assign_stmt identifier var 1 exp var 2 exp 3 | identifier var | number

4ote that both assignments and procedure calls begin with an identifier. 5t is not until either the end of the statement or the to6en 1 is seen that a parser can decide whether the statement is an assignment or a call. 7e simplif, this situation to the following grammar- where we have removed the alternative choices for a variable- and simplified the statement options- without changing the basic situation S /0 id | V 1 E V /0 id E /0 V | n

To show how this grammar results in a parsing conflict in '()($) parsingconsider the start state of the DFA of sets of items S -> . S S -> . id S -> . V 1 E V -> . id This state has a shift transition on ' to the state S -> id . V -> id . 4ow- Follow (S) 1 8 9 : and Follow (V) 1 8 1 - 9 : ( 1 because of the rule S -> V 1 E and 9 because an E can be a V). Thus- the '()($) parsing algorithm calls for a reduction in this state b, both the rule S /0 id and the rule V /0 id under input s,mbol 9. (This is reduce/reduce conflict). This parsing is a ;phon,< problem caused b, the wea6ness of the '()($) method. 5ndeed- the reduction b, V -> id should never be made in this state when the input is 9- since a variable can never occur at the end of a statement until after the to6en 1 is seen and shifted. GENER"L LR($) an' L"LR($) P"RSING 5n this section we stud, the most general form of ()($) parsing- sometimes called (anon (al ()($) parsing. This method overcomes the problem with '()($) parsing described at the end of the previous section- but at a cost of substantiall, increased comple*it,. 5ndeed- general ()($) parsing is usuall, considered too comple* to user in the construction of parsers in most situations. Fortunatel,- a modification of general ()($) parsing- called (A()($) for ;loo6ahead< () parsing)- retains most of the benefits of general ()($) parsing- while preserving the efficienc, of the '()($) method. The (A()($) method has become the method of choice for parser generators such as =acc. ) n te "*tomata o# LR($) tems The difficult, with the '()($) method is that it applies loo6aheads after the construction of the DFA of ()(!) items- a construction that ignores loo6aheads. The power of the general ()($) method is that it uses a new DFA that has the loo6aheads built into its construction from the start. This DFA uses items that are an e*tension of ()(!) items. The, are called LR($) tems because the, include a single loo6ahead to6en in each item. >ore precisel,- an ()($) item is a pair

consisting of an ()(!) item and a loo6ahead to6en. 7e write ()($) items using s.uare brac6ets as 2 A -> . , a 3 where A -> . is an ()(!) item and a is a to6en (the loo6ahead). To complete the definition of the automaton used for general ()($) parsing- we need to define the transitions between ()($) items. These are similar to the ()(!) transitions e*cept that the, also 6eep trac6 of loo6aheads. As with ()(!) items the, include - transitions- and it is necessar, to build a DFA where states are sets of items that are - closures. The ma?or difference between the ()(!) and ()($) automata comes in the definition of the - transitions. 7e give the definition of the easier case (the non - transitions) first- which are essentiall, identical to those of the ()(!) case. Definition of LR(1) transitions (part 1). @iven an ()($) item 2 A -> . X , a 3 where X is an, s,mbol (terminal or nonterminal)- there is a transition on X to the item 2 A -> X . , a 3. 4ote that in this case the same loo6ahead a appears in both items. Thus- these transitions do not cause new loo6aheads to appear. Anl, - transitions ;create< new loo6aheads- as follows. Definition of LR(1) transitions (part 2). @iven an ()($) item 2 A -> . B , a 3 where B is a nonterminal- there are - transitions to items 2 B -> + , 3 for ever, production B -> and f!r ever" t!#en in First ( a ). 4ote how these - transitions 6eep trac6 of the conte*t in which the structure B needs to be recogniBed. 5ndeed the item 2 A -> . B , a 3 sa,s that at this point in the parse we ma, want to recogniBe a B, but onl, if this B is followed b, a string derivable from the string a, and such strings must begin with a to6en in F5)'T( a ). 'ince the string follows B in the production A -> B - if a is constructed to be in FA((A7( A )- then F5)'T( a ) is a subset of FA((A7( B )and the Cs in the items 2 B -> + , 3 will alwa,s be in FA((A7( B ). The power of the general ()($) method lies in the fact that the set F5)'T( a ) ma, be a pr!per subset of FA((A7( B ). (An '()($) parser essentiall, ta6es the loo6aheads from the entire FA((A7 set.) 4ote also that the original loo6ahead a appears as one of the s onl, if can derive the empt, string. >uch of the time (especiall, in practical situations)- this will onl, occur if is itself , in which case we get the special case of an - transition from 2 A -> . B , a 3 to 2 B -> + , a 3 .

LR($) Example $ Consider the grammar A /0 ( A ) | a DFA sets of () ($) items for the grammar are as below 4ote that- from state !- there is a transition on the to6en ( to state # which will have the closure of the set consisting of the item 2A /0 (+ A) - 93. 'ince there are - transitions from this item to the items- 2A -> + (A) , )3 and 2A ->+ a , )3. This is because- in the item 2A /0 (+ A) - 93- A is being recogniBed in the )D' in t$e c!ntext !f parent$eses% That is- the FA((A7 of the right hand A is F5)'T( ), ) 1 8 ) :. A [A ->. A, $] [A ->. (A) , $] [A ->. a , $] I0 [A -> A . , $] I1 a [A -> a . , $] I3 ( A [A -> (. A) , $] [A -> . (A) , )] [A ->. a , )] I2 [A -> (A . ) , $] I4 a ) [A -> (A) . , $] I7

[A -> a . , )] I6

A ( [A -> (. A) , )] [A -> . (A) , )] [A ->. a , )]

) [A -> ( A . ) , )] I8 [A -> ( A ) . , )] I9

T-e Gene%al LR($) Pa%s ng "lgo% t-m This is similar to that of '()($) parsing algorithm- e*cept that it uses the loo6ahead to6ens in the ()($) items instead of the FA((A7 sets. (et s be the current state (at the top of the parsing stac6). Then actions are defined as follows. $. 5f state s contains an, ()($) item of the form 2 A -> . X , a 3 where X is a terminal- and X is the ne*t to6en in the input string- then the action is to shift the current input to6en onto the stac6- and the new state to be pushed on the stac6 is the state containing the ()($) item 2 A -> X . , a 3. #. 5f state s contains the complete ()($) item 2 A -> . , a 3 - and the ne*t to6en in the input string is a- then the action is to reduce b, the rule A -> % A reduction b, the rule S -> S, where S is the start state- is e.uivalent to acceptance. (This will happen onl, if the ne*t input to6en is 9.) 5n other cases- the new state is computed as follows. )emove the string and all of its corresponding states from the parsing stac6. Correspondingl,- bac6 up in the DFA to the state from which the construction of began. E, construction- this state must contain an ()($) item of the form 2 B -> + A , 3. Push A onto the stac6- and push the state containing the item 2 B -> A + , 3 . ". 5f the ne*t input to6en is such that neither of the above two cases applies- an error is declared. As with the previous methods- we sa, that a grammar is an LR($) g%amma% if the application of the above general ()($) parsing rules results in no ambiguit,. 5n particular- a grammar is ()($) iff- for an, state S- the following two conditions are satisfied $. For an, item 2 A -> . X , a 3 in s with X a terminal- there is no item in s of the form 2 B -> + F 3 (otherwise there is a shift/reduce conflict). #. There are no two items in s of the form 2 A -> . , a 3 and 2 B -> . , a 3 (otherwise- there is a reduce/reduce conflict). 7e remar6 that a parsing table can be constructed from the DFA sets of ()($) items that e*presses the general ()($) parsing algorithm. This table has e*actl, the same form for '()($) parsers- as the ne*t e*ample shows.

7e give the general ()($) parsing table for the grammar of LR($) Example $ in the Table below. 5t can be easil, constructed from the DFA drawn alread,. 5n the table we have used the following numbering for the grammar rule choices in the reduction actions ($) (#) ST"TE ! $ # " % & G H I J ( s# s& s& a s" sG r# sH sG r# r$ sJ r$ I A /0 ( A ) A /0 a "CTION ) 9 accept % GOTO A $

LR($) Example . 'how that general ()($) parsing solves the loo6ahead problem for the grammar of L m ts o# SLR($) Example reproduced below S /0 id | V 1 E V /0 id E /0 V | n

Você também pode gostar