Parsing VII The Last Parsing Lecture Copyright 2003

  • Slides: 24
Download presentation
Parsing VII The Last Parsing Lecture Copyright 2003, Keith D. Cooper, Kennedy & Linda

Parsing VII The Last Parsing Lecture Copyright 2003, Keith D. Cooper, Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission to make copies of these materials for their personal use.

LR(1) Table Construction High-level overview 1 Build the canonical collection of sets of LR(1)

LR(1) Table Construction High-level overview 1 Build the canonical collection of sets of LR(1) Items, I a Begin in an appropriate state, s 0 ¨ [S’ • S, EOF], along with any equivalent items ¨ Derive equivalent items as closure( s 0 ) b Repeatedly compute, for each sk, and each X, goto(sk, X) ¨ If the set is not already in the collection, add it ¨ Record all the transitions created by goto( ) This eventually reaches a fixed point 2 Fill in the table from the collection of sets of LR(1) items The canonical collection completely encodes the transition diagram for the handle-finding DFA

Example ( grammar & sets) Simplified, right recursive expression grammar Goal Expr Term –

Example ( grammar & sets) Simplified, right recursive expression grammar Goal Expr Term – Expr Term Factor * Term Factor ident

Example (building the collection) Initialization Step s 0 closure( { [Goal • Expr ,

Example (building the collection) Initialization Step s 0 closure( { [Goal • Expr , EOF] } ) { [Goal • Expr , EOF], [Expr • Term – Expr , EOF], [Expr • Term , EOF], [Term • Factor * Term , –], [Term • Factor , EOF], [Term • Factor , –], [Factor • ident , EOF], [Factor • ident , –], [Factor • ident , *] } S {s 0 }

Example Iteration 1 s 1 goto(s 0 , Expr) s 2 goto(s 0 ,

Example Iteration 1 s 1 goto(s 0 , Expr) s 2 goto(s 0 , Term) s 3 goto(s 0 , Factor) s 4 goto(s 0 , ident ) Iteration 2 s 5 goto(s 2 , – ) s 6 goto(s 3 , * ) Iteration 3 s 7 goto(s 5 , Expr ) s 8 goto(s 6 , Term ) (building the collection)

Example (Summary) S 0 : { [Goal • Expr , EOF], [Expr • Term

Example (Summary) S 0 : { [Goal • Expr , EOF], [Expr • Term – Expr , EOF], [Expr • Term , EOF], [Term • Factor * Term , –], [Term • Factor , EOF], [Term • Factor , –], [Factor • ident , EOF], [Factor • ident , –], [Factor • ident, *] } S 1 : { [Goal Expr • , EOF] } S 2 : { [Expr Term • – Expr , EOF], [Expr Term • , EOF] } S 3 : { [Term Factor • * Term , EOF], [Term Factor • * Term , –], [Term Factor • , EOF], [Term Factor • , –] } S 4 : { [Factor ident • , EOF], [Factor ident • , –], [Factor ident • , *] } S 5 : { [Expr Term – • Expr , EOF], [Expr • Term – Expr , EOF], [Expr • Term , EOF], [Term • Factor * Term , –], [Term • Factor * Term , EOF], [Term • Factor , EOF], [Factor • ident , *], [Factor • ident , –], [Factor • ident , EOF] }

Example ( Summary) S 6 : { [Term Factor * • Term , EOF],

Example ( Summary) S 6 : { [Term Factor * • Term , EOF], [Term Factor * • Term , –], [Term • Factor * Term , EOF], [Term • Factor * Term , –], [Term • Factor , EOF], [Term • Factor , –], [Factor • ident , EOF], [Factor • ident , –], [Factor • ident , *] } S 7: { [Expr Term – Expr • , EOF] } S 8 : { [Term Factor * Term • , EOF], [Term Factor * Term • , –] }

Example (Summary) The Goto Relationship (from the construction)

Example (Summary) The Goto Relationship (from the construction)

Filling in the ACTION and GOTO Tables The algorithm x is the number of

Filling in the ACTION and GOTO Tables The algorithm x is the number of the state for sx set sx S item i sx if i is [A • ad, b] and goto(sx, a) = sk, a T then ACTION[x, a] “shift k” else if i is [S’ S • , EOF] then ACTION[x , EOF] “accept” else if i is [A • , a] then ACTION[x, a] “reduce A ” n NT if goto(sx , n) = sk then GOTO[x, n] k Many items generate no table entry e. g. , [A B , a] does not, but closure ensures that all the rhs’ for B are in sx

Example ( Filling in the tables) The algorithm produces the following table Plugs into

Example ( Filling in the tables) The algorithm produces the following table Plugs into the skeleton LR(1) parser

What can go wrong? What if set s contains [A • a , b]

What can go wrong? What if set s contains [A • a , b] and [B • , 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 Ea. C includes a What is set s contains [A • , a] and [B • , a] ? worked example • Each generates “reduce”, but with a different production • Both define ACTION[s, a] — cannot do both reductions • This fundamental ambiguity is called a reduce/reduce error • Modify the grammar to eliminate it (PL/I’s overloading of (. . . )) In either case, the grammar is not LR(1)

Shrinking the Tables Three options: • Combine terminals such as number & identifier, +

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 (table compression) Implement identical rows once & remap states Requires extra indirection on each lookup Use separate mapping for ACTION & for GOTO • Use another construction algorithm Both LALR(1) and SLR(1) produce smaller tables Implementations are readily available

LR(k) versus LL(k) (Top-down Recursive Descent ) Finding Reductions LR(k) Each reduction in the

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

Summary Top-down recursive descent LR(1) Advantages Fast Good locality Simplicity Good error detection Disadvantages

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

Left Recursion versus Right Recursion • • • Right recursion Required for termination in

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 z y w*(x*(y*z)) * * * w z y x ( (w * x ) * y ) * z

Associativity • What difference does it make? • Can change answers in floating-point arithmetic

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

Hierarchy of Context-Free Languages Context-free languages LR(k) LR(1) Deterministic languages (LR(k)) LL(k) languages Simple

Hierarchy of Context-Free Languages Context-free languages LR(k) LR(1) Deterministic languages (LR(k)) LL(k) languages Simple precedence languages LL(1) languages Operator precedence languages The inclusion hierarchy for context-free languages

Hierarchy of Context-Free Grammars Context-free grammars Floyd-Evans Parsable Unambiguous CFGs Operator Precedence LR(k) LR(1)

Hierarchy of Context-Free Grammars Context-free grammars Floyd-Evans Parsable Unambiguous CFGs Operator Precedence LR(k) LR(1) LL(k) • Operator precedence includes some ambiguous grammars LALR(1) • SLR(1) LR(0) LL(1) is a subset of SLR(1) LL(1) The inclusion hierarchy for context-free grammars

Extra Slides Start Here

Extra Slides Start Here

Beyond Syntax There is a level of correctness that is deeper than grammar fie(a,

Beyond Syntax There is a level of correctness that is deeper than grammar fie(a, b, c, d) int a, b, c, d; {…} fee() { int f[3], g[0], h, i, j, k; char *p; fie(h, i, “ab”, j, k); k = f * i + j; h = g[17]; printf(“<%s, %s>. n”, p, q); p = 10; } What is wrong with this program? (let me count the ways …)

To generate code, we need to understand its meaning ! Beyond Syntax There is

To generate code, we need to understand its meaning ! Beyond Syntax There is a level of correctness that is deeper than grammar fie(a, b, c, d) int a, b, c, d; {…} fee() { int f[3], g[0], h, i, j, k; char *p; fie(h, i, “ab”, j, k); k = f * i + j; h = g[17]; printf(“<%s, %s>. n”, p, q); p = 10; } What is wrong with this program? (let me count the ways …) • declared g[0], used g[17] • wrong number of args to fie() • “ab” is not an int • wrong dimension on use of f • undeclared variable q • 10 is not a character string All of these are “deeper than syntax”

Beyond Syntax To generate code, the compiler needs to answer many questions • •

Beyond Syntax To generate code, the compiler needs to answer many questions • • • Is “x” a scalar, an array, or a function? Is “x” declared? Are there names that are not declared? Declared but not used? Which declaration of “x” does each use reference? Is the expression “x * y + z” type-consistent? In “a[i, j, k]”, does a have three dimensions? Where can “z” be stored? (register, local, global, heap, static) In “f 15”, how should 15 be represented? How many arguments does “fie()” take? What about “printf ()” ? Does “*p” reference the result of a “malloc()” ? Do “p” & “q” refer to the same memory location? Is “x” defined before it is used? These cannot be expressed in a CFG

Beyond Syntax These questions are part of context-sensitive analysis • Answers depend on values,

Beyond Syntax These questions are part of context-sensitive analysis • Answers depend on values, not parts of speech • Questions & answers involve non-local information • Answers may involve computation How can we answer these questions? • Use formal methods Context-sensitive grammars? Attribute grammars? (attributed grammars? ) • Use ad-hoc techniques Symbol tables Ad-hoc code (action routines) In scanning & parsing, formalism won; different story here.

Beyond Syntax Telling the story • The attribute grammar formalism is important Succinctly makes

Beyond Syntax Telling the story • The attribute grammar formalism is important Succinctly makes many points clear Sets the stage for actual, ad-hoc practice • The problems with attribute grammars motivate practice Non-local computation Need for centralized information • Some folks in the community still argue for attribute grammars Knowledge is power Information is immunization We will cover attribute grammars, then move on to ad-hoc ideas