Compilers Recognizing Handles Alex Aiken Recognizing Handles Bad - - PowerPoint PPT Presentation

compilers
SMART_READER_LITE
LIVE PREVIEW

Compilers Recognizing Handles Alex Aiken Recognizing Handles Bad - - PowerPoint PPT Presentation

Compilers Recognizing Handles Alex Aiken Recognizing Handles Bad News There are no known efficient algorithms to recognize handles Good News There are good heuristics for guessing handles On some CFGs, the heuristics always


slide-1
SLIDE 1

Alex Aiken

Compilers

Recognizing Handles

slide-2
SLIDE 2

Alex Aiken

Recognizing Handles

  • Bad News

– There are no known efficient algorithms to recognize handles

  • Good News

– There are good heuristics for guessing handles – On some CFGs, the heuristics always guess correctly

slide-3
SLIDE 3

Alex Aiken

Recognizing Handles

All CFGs Unambiguous CFGs LR(k) CFGs LALR(k) CFGs SLR(k) CFGs

slide-4
SLIDE 4

Alex Aiken

Recognizing Handles

  • It is not obvious how to detect handles
  • At each step the parser sees only the stack, not the

entire input; start with that . . . a is a viable prefix if there is an w such that a|w is a state of a shift-reduce parser

slide-5
SLIDE 5

Alex Aiken

Recognizing Handles

  • What does this mean? A few things:

– A viable prefix does not extend past the right end

  • f the handle

– It’s a viable prefix because it is a prefix of the handle – As long as a parser has viable prefixes on the stack no parsing error has been detected

slide-6
SLIDE 6

Alex Aiken

Recognizing Handles Important Fact #3 about bottom-up parsing: For any grammar, the set of viable prefixes is a regular language

slide-7
SLIDE 7

Alex Aiken

Recognizing Handles

  • Important Fact #3 is non-obvious
  • We show how to compute automata that accept

viable prefixes

slide-8
SLIDE 8

Alex Aiken

Recognizing Handles

  • An item is a production with a “.” somewhere on the

rhs

  • The items for T  (E) are

T  .(E) T  (.E) T  (E.) T  (E).

slide-9
SLIDE 9

Alex Aiken

Recognizing Handles

  • The only item for X  e is X  .
  • Items are often called “LR(0) items”
slide-10
SLIDE 10

Alex Aiken

Recognizing Handles

  • The problem in recognizing viable prefixes is that the

stack has only bits and pieces of the rhs of productions – If it had a complete rhs, we could reduce

  • These bits and pieces are always prefixes of rhs of

productions

slide-11
SLIDE 11

Alex Aiken

Recognizing Handles

Consider the input (int)

E  T + E | T T  int * T | int | (E)

– Then (E|) is a state of a shift-reduce parse – (E is a prefix of the rhs of T  (E)

  • Will be reduced after the next shift

– Item T  (E.) says that so far we have seen (E of this production and hope to see )

slide-12
SLIDE 12

Alex Aiken

Recognizing Handles

  • The stack may have many prefixes of rhs’s

Prefix1 Prefix2 . . . Prefixn-1Prefixn

  • Let Prefixi be a prefix of rhs of Xi  ai

– Prefixi will eventually reduce to Xi – The missing part of ai-1 starts with Xi – i.e. there is a Xi-1 Prefixi-1 Xi b for some b

  • Recursively, Prefixk+1…Prefixn eventually reduces to the

missing part of ak

slide-13
SLIDE 13

Alex Aiken

Recognizing Handles Consider the string (int * int): (int *|int) is a state of a shift-reduce parse “(” is a prefix of the rhs of T  (E) “e” is a prefix of the rhs of E  T “int *” is a prefix of the rhs of T  int * T

slide-14
SLIDE 14

Alex Aiken

Recognizing Handles The “stack of items” T  (.E) E  .T T  int * .T Says We’ve seen “(” of T  (E) We’ve seen e of E  T We’ve seen int * of T  int * T

slide-15
SLIDE 15

Alex Aiken

Recognizing Handles Idea: To recognize viable prefixes, we must – Recognize a sequence of partial rhs’s of productions, where – Each partial rhs can eventually reduce to part of the missing suffix of its predecessor