Presentation is loading. Please wait.

Presentation is loading. Please wait.

Parsing Wrap-up. from Cooper and Torczon2 Filling in the A CTION and G OTO Tables The algorithm Many items generate no table entry  Closure( ) instantiates.

Similar presentations


Presentation on theme: "Parsing Wrap-up. from Cooper and Torczon2 Filling in the A CTION and G OTO Tables The algorithm Many items generate no table entry  Closure( ) instantiates."— Presentation transcript:

1 Parsing Wrap-up

2 from Cooper and Torczon2 Filling in the A CTION and G OTO Tables The algorithm Many items generate no table entry  Closure( ) instantiates F IRST (  ) directly for [  ,a ]  set CC x  CC  item i  CC x if i is [    a ,b] and goto( CC x,a) = CC k, a  T then A CTION [x,a]  “shift k” else if i is [S’  S, EOF ] then A CTION [x,a]  “accept” else if i is [   ,a] then A CTION [x,a]  “reduce    ”  n  NT if goto( CC x,n) = CC k then G OTO [x,n]  k x is the state number

3 from Cooper and Torczon3 What can go wrong? What if set s contains [ a ,b] and [ ,a] ? First item generates “shift”, second generates “reduce” Both define ACTION[s,a] — cannot do both actions This is a fundamental ambiguity, called a shift/reduce error Modify the grammar to eliminate it (if-then-else) Shifting will often resolve it correctly What if set s contains [ ,a] and [ ,a] ? Each generates “reduce”, but with a different production Both define ACTION[s,a] — cannot do both reductions This is a fundamental ambiguity, called a reduce/reduce conflict Modify the grammar to eliminate it (P L/I’ s and FORTRAN’s overloading of (...)) In either case, the grammar is not LR(1)

4 from Cooper and Torczon4 Shrinking the Tables Three options: Combine terminals such as number & identifier, + & -, * & /  Directly removes a column, may remove a row  For expression grammar, 198 (vs. 384) table entries Combine rows or columns  Implement identical rows once & remap states  Requires extra indirection on each lookup  Use separate mapping for A CTION & for G OTO Use another construction algorithm  Both L ALR(1) and S LR(1) produce smaller tables  Implementations are readily available

5 from Cooper and Torczon5 Direct Encoding Rather than using a table-driven interpreter … Generate spaghetti code that implements the logic Each state becomes a small case statement or if-then-else Analogous to direct coding a scanner Advantages No table lookups and address calculations No representation for don’t care states No outer loop —it is implicit in the code for the states This produces a faster parser with more code but no table

6 from Cooper and Torczon6 LR(k) versus LL(k) (Top-down Recursive Descent ) Finding Reductions LR(k)  Each reduction in the parse is detectable with  the complete left context,  the reducible phrase, itself, and  the k terminal symbols to its right LL(k)  Parser must select the reduction based on  The complete left context  The next k terminals Thus, LR(k) examines more context “… in practice, programming languages do not actually seem to fall in the gap between LL(1) languages and deterministic languages” J.J. Horning, “LR Grammars and Analysers”, in Compiler Construction, An Advanced Course, Springer-Verlag, 1976

7 from Cooper and Torczon7 Left Recursion versus Right Recursion Right recursion Required for termination in top-down parsers Uses (on average) more stack space Produces right-associative operators Left recursion Works fine in bottom-up parsers Limits required stack space Produces left-associative operators Rule of thumb Left recursion for bottom-up parsers Right recursion for top-down parsers * * * w x y z w * ( x * ( y * z ) ) * * * z w x y ( (w * x ) * y ) * z

8 from Cooper and Torczon8 Associativity What difference does it make? Can change answers in floating-point arithmetic Exposes a different set of common subexpressions Consider x+y+z What if y+z occurs elsewhere? Or x+y? or x+z? What if x = 2 & z = 17 ? Neither left nor right exposes 19. Best choice is function of surrounding context + + x y zxy z + + + xyz Ideal operator Left association Right association

9 from Cooper and Torczon9 Hierarchy of Context-Free Languages Context-free languages Deterministic languages (LR(k)) LL(k) languagesSimple precedence languages LL(1) languagesOperator precedence languages LR(k)  LR(1) The inclusion hierarchy for context-free languages

10 from Cooper and Torczon10 Hierarchy of Context-Free Grammars Inclusion hierarchy for context-free grammars Operator precedence includes some ambiguous grammars LL(1) is a subset of SLR(1) Context-free grammars Unambiguous CFGs Operator Precedence Floyd-Evans Parsable LR(k) LR(1) LALR(1) SLR(1) LR(0) LL(k) LL(1)

11 from Cooper and Torczon11 Summary Advantages Fast Good locality Simplicity Good error detection Fast Deterministic langs. Automatable Left associativity Disadvantages Hand-coded High maintenance Right associativity Large working sets Poor error messages Large table sizes Top-down recursive descent LR(1)


Download ppt "Parsing Wrap-up. from Cooper and Torczon2 Filling in the A CTION and G OTO Tables The algorithm Many items generate no table entry  Closure( ) instantiates."

Similar presentations


Ads by Google