Presentation is loading. Please wait.

Presentation is loading. Please wait.

S YNTAX A NALYSIS O R P ARSING Lecture 08. B OTTOM -U P P ARSING A bottom-up parser creates the parse tree of the given input starting from leaves towards.

Similar presentations


Presentation on theme: "S YNTAX A NALYSIS O R P ARSING Lecture 08. B OTTOM -U P P ARSING A bottom-up parser creates the parse tree of the given input starting from leaves towards."— Presentation transcript:

1 S YNTAX A NALYSIS O R P ARSING Lecture 08

2 B OTTOM -U P P ARSING A bottom-up parser creates the parse tree of the given input starting from leaves towards the root A bottom-up parser tries to find the right-most derivation of the given input in the reverse order. S ...   (the right-most derivation of  )  (the bottom-up parser finds the right-most derivation in the reverse order) 2

3 R IGHTMOST D ERIVATION 3

4 R IGHTMOST D ERIVATION I N R EVERSE 4

5 LR parsing corresponds to rightmost derivation in reverse 5

6 A reduction step replaces a specific substring (matching the body of a production) Reduction is the opposite of derivation Bottom up parsing is a process of reducing a string  to the start symbol S of the grammar R EDUCTION 6

7 H ANDLE Informally, a handle is a substring (in the parsing string) that matches the right side of a production rule. – But not every substring matches the right side of a production rule is handle A handle of a right sentential form  (   )is a production rule A   and a position of  where the string  may be found and replaced by A to produce the previous right-sentential form in a rightmost derivation  S   A    7

8 H ANDLE P RUNING A right-most derivation in reverse can be obtained by handle-pruning. rmrmrmrmrm S=  0   1   2 ...   n-1   n =  input string Start from  n, find a handle A n  n in  n, and replace  n in by A n to get  n-1. Then find a handle A n-1  n-1 in  n-1, and replace  n-1 in by A n-1 to get  n-2. Repeat this, until we reach S. n-th right-sentential form 8

9 S HIFT -R EDUCE P ARSING Bottom-up parsing is also known as shift-reduce parsing because its two main actions are shift and reduce. data structures: input-string and stack Operations –At each shift action, the current symbol in the input string is pushed to a stack. –At each reduction step, the symbols at the top of the stack (this symbol sequence is the right side of a production) will replaced by the non-terminal at the left side of that production. –Accept: Announce successful completion of parsing –Error: Discover a syntax error and call error recovery 9

10 S HIFT R EDUCE P ARSING S  a T R e T  T b c | b R  d Remaining input: abbcde Rightmost derivation: S  a T R e  a T d e  a T b c d e  a b b c d e 10

11 S HIFT R EDUCE P ARSING S  a T R e T  T b c | b R  d a b Shift a, Shift b  Remaining input: bcde Rightmost derivation: S  a T R e  a T d e  a T b c d e  a b b c d e 11

12 S HIFT R EDUCE P ARSING S  a T R e T  T b c | b R  d a b Shift a, Shift b Reduce T  b   T Remaining input: bcde Rightmost derivation: S  a T R e  a T d e  a T b c d e  a b b c d e 12

13 S HIFT R EDUCE P ARSING S  a T R e T  T b c | b R  d a b Shift a, Shift b Reduce T  b Shift b, Shift c    T b c Remaining input: de Rightmost derivation: S  a T R e  a T d e  a T b c d e  a b b c d e 13

14 S HIFT R EDUCE P ARSING S  a T R e T  T b c | b R  d a b Shift a, Shift b Reduce T  b Shift b, Shift c Reduce T  T b c     T b c T Remaining input: de Rightmost derivation: S  a T R e  a T d e  a T b c d e  a b b c d e 14

15 S HIFT R EDUCE P ARSING S  a T R e T  T b c | b R  d a b Shift a, Shift b Reduce T  b Shift b, Shift c Reduce T  T b c Shift d      T b c T d Remaining input: e Rightmost derivation: S  a T R e  a T d e  a T b c d e  a b b c d e 15

16 S HIFT R EDUCE P ARSING S  a T R e T  T b c | b R  d a b Shift a, Shift b Reduce T  b Shift b, Shift c Reduce T  T b c Shift d Reduce R  d       T b c T d R Remaining input: e Rightmost derivation: S  a T R e  a T d e  a T b c d e  a b b c d e 16

17 S HIFT R EDUCE P ARSING S  a T R e T  T b c | b R  d a b Shift a, Shift b Reduce T  b Shift b, Shift c Reduce T  T b c Shift d Reduce R  d Shift e        T b c T d R e Remaining input: Rightmost derivation: S  a T R e  a T d e  a T b c d e  a b b c d e 17

18 S HIFT R EDUCE P ARSING S  a T R e T  T b c | b R  d a b Shift a, Shift b Reduce T  b Shift b, Shift c Reduce T  T b c Shift d Reduce R  d Shift e Reduce S  a T R e         T b c T d R e S Remaining input: Rightmost derivation: S  a T R e  a T d e  a T b c d e  a b b c d e 18

19 E XAMPLE S HIFT -R EDUCE P ARSING Consider the grammar: StackInputAction $id 1 + id 2 $shift $id 1 + id 2 $reduce 6 $F+ id 2 $reduce 4 $T+ id 2 $reduce 2 $E+ id 2 $shift $E +id 2 $shift $E + id 2 reduce 6 $E + Freduce 4 $E + Treduce 1 $Eaccept 19

20 S HIFT -R EDUCE P ARSING Handle will always appear on Top of stack, never inside Possible forms of two successive steps in any rightmost derivation CASE 1: S A B    yzyz S  *  A z   Byz   yz rmrmrm STACK $  INPUT yz$ After Reducing the handle $B$B yz$ Shifting from Input $  By z$ Reduce the handle $A$A z$ 20

21 S HIFT -R EDUCE P ARSING Case 2: S AyzAyz B   x S  *  BxA z   Bx yz   x yz rmrmrm STACK $  $B$B $  Bxy $  BxA INPUT xyz$ After Reducing the handle xyz$ Shifting from Input z$ Reducing the handle z$ 21

22 C ONFLICTS D URING S HIFT -R EDUCE P ARSING There are context-free grammars for which shift-reduce parsers cannot be used. Stack contents and the next input symbol may not decide action: –shift/reduce conflict: Whether make a shift operation or a reduction. –reduce/reduce conflict: The parser cannot decide which of several reductions to make. If a shift-reduce parser cannot be used for a grammar, that grammar is called as non-LR(k) grammar. k lookhead left to right scanning right-most derivation An ambiguous grammar can never be a LR grammar. 22

23 S HIFT -R EDUCE C ONFLICT IN A MBIGUOUS G RAMMAR stmt  if expr then stmt | if expr then stmt else stmt | other STACKINPUT …. if expr then stmt else ….$ We can’t decide whether to shift or reduce?

24 R EDUCE -R EDUCE C ONFLICT IN A MBIGUOUS G RAMMAR 1. stmt  id ( parameter_list ) 2. stmt  expr:=expr 3. parameter_list  parameter_list, parameter 4. parameter_list  parameter 5. parameter_list  id 6. expr  id ( expr_list) 7. expr  id 8. expr_list  expr_list, expr 9. expr_list  expr STACKINPUT …. id ( id, id ) …$ We can’t decide which production will be used to reduce id ?

25 S HIFT -R EDUCE P ARSERS There are two main categories of shift-reduce parsers 1.Operator-Precedence Parser –simple, but only a small class of grammars. CFG LR LALR SLR 2.LR-Parsers –covers wide range of grammars. SLR – simple LR parser LR – most general LR parser LALR – intermediate LR parser (lookhead LR parser) –SLR, LR and LALR work same, only their parsing tables are different.

26 LR P ARSERS LR parsing is attractive because: –LR parsing is most general non-backtracking shift-reduce parsing, yet it is still efficient. –The class of grammars that can be parsed using LR methods is a proper superset of the class of grammars that can be parsed with predictive parsers. LL(1)-Grammars  LR(1)-Grammars –An LR-parser can detect a syntactic error as soon as it is possible to do so a left-to-right scan of the input. –LR parsers can be constructed to recognize virtually all programming language constructs for which CFG grammars canbe written Drawback of LR method: –Too much work to construct LR parser by hand Fortunately tools (LR parsers generators) are available

27 LL VS. LR LR (shift reduce) is more powerful than LL (predictive parsing) Can detect a syntactic error as soon as possible. LR is difficult to do by hand (unlike LL) 27

28 LR P ARSING A LGORITHM SmSm XmXm S m-1 X m-1.. S1S1 X1X1 S0S0 a1a1...aiai anan $ Action Table terminals and $ Goto Table non-terminal statesstates four different actions s teach item is aa state number t e s LR Parsing Algorithm input stack output

29 A C ONFIGURATION OF LR P ARSING A LGORITHM A configuration of a LR parsing is: ( S o X 1 S 1... X m S m,a i a i+1... a n $ ) StackRest of Input S m and a i decides the parser action by consulting the parsing action table.(Initial Stackcontains just S o ) A configuration of a LR parsing represents the right sentential form: X 1... X m a i a i+1... a n $

30 A CTIONS OF A LR-P ARSER 1.shift s-- shifts the next input symbol and the state s onto the stack ( S o X 1 S 1... X m S m, a i a i+1... a n $ ) € ( S o X 1 S 1... X m S m a i s, a i+1... a n $ ) 2.reduce A  (or rN where N is a production number) –pop 2|  |(=r) items from the stack; –then push A and swheres=goto[s m-r,A] ( S o X 1 S 1... X m S m, a i a i+1... a n $ ) € ( S o X 1 S 1... X m-r S m-r A s, a i... a n $ ) –Output is the reducing production reduce A  2.Accept – Parsing successfully completed 3.Error-- Parser detected an error (an empty entry in the action table)

31 Reduce Action pop 2|  |(=r) items from the stack;let us assume that  = Y 1 Y 2...Y r then push A and swheres=goto[s m-r,A] ( S o X 1 S 1... X m-r S m-r Y 1 S m-r...Y r S m, a i a i+1... a n $ ) € ( S o X 1 S 1... X m-r S m-r A s, a i... a n $ ) In fact, Y 1 Y 2...Y r is a handle. X 1... X m-r A a i... a n $ c X 1... X m Y 1...Y r a i a i+1... a n $

32 LR P ARSER S TACK ( S )

33

34 C ONSTRUCTING SLR P ARSING T ABLES – LR(0) I TEM An item indicates how much of a production we have seen at a given point in the parsing process For Example the item A  X  YZ –We have already seen on the input a string derivable from X –We hope to see a string derivable from YZ For Example the item A   XYZ –We hope to see a string derivable from XYZ For Example the item A  XYZ  –We have already seen on the input a string derivable from XYZ –It is possibly time to reduce XYZ to A Special Case: Rule: A   yields only one item A  

35 C ONSTRUCTING SLR P ARSING T ABLES A collection of sets of LR(0) items (the canonical LR(0) collection) is the basisfor constructing SLR parsers. Canonical LR(0) collection provides the basis of constructing a DFA called LR(0) automaton –This DFA is used to make parsing decisions Each state of LR(0) automaton represents a set of items in the canonical LR(0) collection To construct the canonical LR(0) collection for a grammar –Augmented Grammar –CLOSURE function –GOTO function

36 C ONSTRUCTING SLR P ARSING T ABLES – LR(0) I TEM An LR(0) item of a grammar G is a production of G a dot at the some position of the right side. Ex: A  aBb Possible LR(0) Items: A   aBb (four different possibility) A  a  Bb A  aB  b A  aBb  Sets of LR(0) items will be the states of action and goto table of the SLR parser. –States represent sets of “items” LR parser makes shift-reduce decision by maintaining states to keep track of where we are in a parsing process

37 G RAMMAR A UGMENTATION

38 T HE C LOSURE O PERATION IfIis a set of LR(0) items for a grammar G, then closure(I)is the set of LR(0) items constructed from I by the two rules: 1.Initially, every LR(0) item in I is added to closure(I). 2.If A  .B  is in closure(I)and B  is a production rule of G; then B .  will be in the closure(I). We will apply this rule until no more new LR(0) items can be added to closure(I).

39 T HE C LOSURE O PERATION -- E XAMPLE E'  E closure({E' . E}) = E  E+T { E'   Ekernel items E  TE   E+T T  T*FE   T T  FT   T*F F  (E)T   F F  idF   (E) F   id}

40 GOTO O PERATION If I is a set of LR(0) items and X is a grammar symbol (terminal or non-terminal), then GOTO(I,X) is defined as follows: – IfA    X  in I then every item in closure({A   X   }) will be in GOTO(I,X). Example: E   T,I ={E’   E,E   E+T, T   T*F,T   F, F   (E),F   id}  T, T   T*F, T   F, GOTO(I,E) = { E’  E , E  E  +T } GOTO(I,T) = { E  T , T  T  *F } GOTO(I,F) = {T  F  } GOTO(I,() = { F  (  E), E   E+T, E  F   (E), F   id} GOTO(I,id) = { F  id  }

41 C ONSTRUCTION OF T HE C ANONICAL LR(0) C OLLECTION (CC) To create the SLR parsing tables for a grammar G, we will create the canonical LR(0) collection of the grammar G’. Algorithm: C is { closure({S'   S}) } repeat the followings until no more set of LR(0) items can be added to C. for each I in C and each grammar symbol X if GOTO(I,X) is not empty and not in C add GOTO(I,X) to C GOTO function is a DFA on the sets in C.

42 T HE C ANONICAL LR(0) C OLLECTION -- E XAMPLE I 0 : E’ .E E .E+T I 1 : E’  E. E  E.+T I 6 : E  E+.T T .T*F I 9 : E  E+T. T  T.*F E .TT .F T .T*F T .F I 2 : E  T. T  T.*F F .(E) F .id I 10 : T  T*F. F .(E) F .idI 3 : T  F. I 7 : T  T*.F F .(E) I 11 : F  (E). I 4 : F  (.E) E .E+T F .id E .T T .T*F I 8 : F  (E.) E  E.+T T .F F .(E) F .id I 5 : F  id.

43 T RANSITION D IAGRAM (DFA) OF G OTO F UNCTION I0I0 I1I1 I2I2 I3I3 I6I6 I7I7 I 8 to I 2 to I 3 to I 4 I 9 to I 3 I 10 to I 4 to I 5 I 11 to I 6 to I 7 id to I 4 to I 5 ( F * E ET T )+)+ F TF(TF( I 4 id I 5 id ( * F ( id +

44 C ONSTRUCTING SLR P ARSING T ABLE ( OF AN AUGUMENTED GRAMMAR G’) 1. Construct the canonical collection of sets of LR(0) items for G’. C  {I 0,...,I n } 2. Create the parsing action table as follows:  Ifa is a terminal, A .a  in I i and goto(I i,a)=I j then action[i,a] is shift j.  IfA .is in I i, then action[i,a] is reduce A  for all a in FOLLOW(A) where A  S’.  IfS’  S.is in I i, then action[i,$] is accept.  If any conflicting actions generated by these rules, the grammar is not SLR(1). 3. Create the parsing goto table  for all non-terminals A,  if goto(I i,A)=I j then goto[i,A]=j 4. All entries not defined by (2) and (3) are errors. 5. Initial state of the parser containsS’ .S

45 P ARSING T ABLES OF E XPRESSION G RAMMAR stateid+*()$ETF 0s5s4123 1s6acc 2r2s7r2 3r4 4s5s4823 5r6 6s5s493 7s5s410 8s6s11 9r1s7r1 10r3 11r5 Action TableGoto Table

46 (SLR) P ARSING T ABLES FOR E XPRESSION G RAMMAR stateid+*()$ETF 0s5s4123 1s6acc 2r2s7r2 3r4 4s5s4823 5r6 6s5s493 7s5s410 8s6s11 9r1s7r1 10r3 11r5 Action TableGoto Table Key to Notation S4=“Shift input symbol and push state 4” R5= “Reduce by rule 5” Acc=Accept (blank)=Syntax Error

47 E XAMPLE LR P ARSE : ( ID + ID )* ID

48

49

50 A CTIONS OF A (S)LR-P ARSER -- E XAMPLE stack 0 input id*id+id$ action shift 5 output 0id5*id+id$ reduce by F  idF  id 0F3 0T2 0T2*7 *id+id$ *id+id $ id+id $ reduce by T  F shift 7 shift 5 TFTF 0T2*7id5+id$ reduce by F  idF  id 0T2*7F10+id$ reduce by T  T*FT  T*F 0T2 0E1 0E1+6 0E1+6id5 +id$ +i d$ id$ $ reduce by E  T shift 6 shift 5 reduce by F  id E  T F  id 0E1+6F3$ reduce by T  FTFTF 0E1+6T9 0E1 $$$$ reduce by E  E+T accept E  E+ T

51 LR P ARSING A LGORITHM

52 SLR G RAMMAR : R EVIEW An LR parser using SLR parsing tables for a grammar G is called as the SLR parser for G. If a grammar G has an SLR parsing table, it is called SLR grammar (or SLR grammar in short). Every SLR grammar is unambiguous, but every unambiguous grammar is not a SLR grammar. If the SLR parsing table of a grammar G has a conflict, we say that that grammar is not SLR grammar.

53 C ONFLICT E XAMPLE

54 C ONFLICT E XAMPLE 2

55 C ONFLICT

56 Any Questions ? 56


Download ppt "S YNTAX A NALYSIS O R P ARSING Lecture 08. B OTTOM -U P P ARSING A bottom-up parser creates the parse tree of the given input starting from leaves towards."

Similar presentations


Ads by Google