LR-review - Bottom-up parsing Bottom-up parsing algorithms...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Bottom-up parsing Bottom-up parsing algorithms are in general more powerful than top-down methods, but not surprisingly, the constructions required in these algorithms are also more complex. It is difficult to write a bottom-up parser by hand for anything but the most trivial of grammars, but fortunately,there are excellent parser generator tools like yacc that build a parser from an input specification. Shift-reduce parsing is the most commonly used and most powerful of the bottom-up techniques. LR parsing LR parsers (“L” for left to right scan of input; “R” for rightmost derivation) are efficient, tabledriven shift-reduce parsers. 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 LL parsers. In fact,virtually all programming language constructs for which CFGs can be written can be parsed with
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
. An LR parser uses two tables: 1. The action table Action[s,a] tells the parser what to do when the state on top of the stack is s and terminal a is the next input token. The possible actions are to shift a state onto the stack, to reduce the handle on top of the stack, to accept the input, or to report an error. 2. The goto table Goto[s,X] indicates the new state to place on top of the stack after a reduce of the non-terminal X while state s is on top of the stack. The two tables are usually combined, with the action table specifying entries for terminals, and the goto table specifying entries for non-terminals.
Background image of page 2
Types of LR parsers There are three types of LR parsers: LR(k), simple LR(k), and lookahead LR(k) (abbreviated to LR(k), SLR(k), LALR(k)). The k identifies the number of tokens of lookahead. We will usually only concern ourselves with 0 or 1 tokens of lookahead, but it does generalize to k > 1. The different classes of parsers all operate the same way (as shown above, being driven by their action and goto tables), but they differ in how their action and goto tables are constructed, and the size of those tables.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Constructing LR(0) parsing tables LR(0) configuration or item . A configuration is a production of the grammar with a dot at some position on its right side. 1. Construct F = { I0 , I1 , . .. In }, the collection of configurating sets for G'. 2. State i is determined from Ii. The parsing actions for the state are determined as follows: a) If A –> u• is in Ii then set a ction[i,a] to reduce A –> u for all input. ( A not equal to S' ). b) If S' –> S• is in Ii then set a ction[i,#] to accept. c) If
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 17

LR-review - Bottom-up parsing Bottom-up parsing algorithms...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online