Docsity

LR Parsing: Understanding the Differences and Construction of LR Tables - Prof. Alan Matto, Study notes of Programming Languages

Toggle sidebar
bg1
CS 440 — LR Parsing February 25, 2009
1 Introduction and Objectives
LL parsing is easy to use, but unless the grammar is just right, you may need to perform all kinds
of tedious1transformations. For this reason, it is much more common to use LR parsing.
•Be able to explain how LR parsing is different than LL parsing.
•Know how to generate the Characteristic Finite State Automata for a grammar.
•Know what the item sets mean.
•Know how to create the LR Action and Goto tables
•Know what a Shift-reduce conflict is, and how to fix it
•Know what a reduce-reduce conflict is, and how to fix it
•Know how to use the parse tables to generate an “execution trace” of a parse.
2 LR Grammars
2.1 Definition of LR Parser
An LR(1) Parser
•scans the text from Left to right,
•produces a Rightmost derivation,
•using 1token of lookahead.
Contrast to an LL(1) Parser
•scans the text from Left to right,
•produces a Leftmost derivation,
•using 1token of lookahead.
2.2 Example Parsing
Consider the grammar S→E
E→id
E→ ∗ E E
and the input *id**ididid.
What is the resulting parse tree?
1Well, you won’t do it yourself, you’ll make the computer do it for you.
Mattox Beckman Page 1 Illinois Institute of Technology
pf3
pf4
pf5
pf8

Partial preview of the text

Download LR Parsing: Understanding the Differences and Construction of LR Tables - Prof. Alan Matto and more Study notes Programming Languages in PDF only on Docsity!

1 Introduction and Objectives

LL parsing is easy to use, but unless the grammar is just right, you may need to perform all kinds of tedious^1 transformations. For this reason, it is much more common to use LR parsing.

  • Be able to explain how LR parsing is different than LL parsing.
  • Know how to generate the Characteristic Finite State Automata for a grammar.
  • Know what the item sets mean.
  • Know how to create the LR Action and Goto tables
  • Know what a Shift-reduce conflict is, and how to fix it
  • Know what a reduce-reduce conflict is, and how to fix it
  • Know how to use the parse tables to generate an “execution trace” of a parse.

2 LR Grammars

2.1 Definition of LR Parser

An LR(1) Parser

  • scans the text from Left to right,
  • produces a Rightmost derivation,
  • using 1 token of lookahead.

Contrast to an LL(1) Parser

  • scans the text from Left to right,
  • produces a Leftmost derivation,
  • using 1 token of lookahead.

2.2 Example Parsing

Consider the grammar S → E E → id E → ∗ E E and the input * id * * id id id. What is the resulting parse tree? (^1) Well, you won’t do it yourself, you’ll make the computer do it for you.

3 Characteristic Finite State Automaton

3.1 The Goal

  • We want to use a state machine to handle the LR parse for us. Consider the simple grammar A → x y. The resulting state machine is:

These states correspond to different stages of the production.

  1. A → • x y
  2. A → x • y
  3. A → x y • The “•” is called “dot” or “the cursor”.

3.2 Multiple Productions

Consider the grammar A → x y A → z

3.3 Simultaneous Movement

Consider the grammar

A → x y A → q A → x z

(d) If s is already part of our set of states, discard it. Otherwise add it and continue with the next item.

5 Activity

5.1 Activity

Generate the Characteristic Finite State Machine for the following grammar. S → b A e A → x E A → x I E → a N I → c N N → y

5.2 The LR Tables

  • In order to use this in a real parse, you need an extra piece of information: the LR tables.
    1. E → E + T
    2. E → T
    3. T → T ∗
    4. T → int Action Go To State + * int $ E T 0 S 3 1 2 1 S 4 2 R 2 S 5 R 2 3 R 4 R 4 R 4 4 S 3 6 5 R 3 R 3 R 3 6 R 1 S 5 R 1

5.3 Decoding the Table

  • “S n” means to shift the token and go to state n.
  • “R n” means to reduce by rule n.
  • “n” by itself means to go to state n.
  • A blank entry indicates an error.
  • Given the next input, check the table to see what action you need to take.
  • When shifting, push the current state and the new token onto the stack.
  • When reducing, pop the appropriate number of elements and states off the stack and construct a new element via the rule. Put the new element back on the stack.

Action Go To State + * int $ E T 0 S 3 1 2 1 S 4 2 R 2 S 5 R 2 3 R 4 R 4 R 4 4 S 3 6 5 R 3 R 3 R 3 6 R 1 S 5 R 1

6 Follow Sets

6.1 Constructing the table

  • First, generate the CFSM.
  • Add a shift or goto operation for each transition in the machine.
  • When the • is at the end of a production, you will add a reduce rule... but where?

Action Go To State + * int $ E T 0 S 3 1 2 1 S 4 2 R 2 S 5 R 2 3 R 4 R 4 R 4

...

I 2 E → T •

T → T • ∗

In other words, how does state 2 of this machine know what to do?

7 LR Table Construction

7.1 Making the Table

It’s easiest to make the tables while you are making the CFSM.

  1. Make the First and Follow sets for the nonterminals.
  2. As you make the CFSM:

(a) Looking at a terminal? Add a shift operation for that terminal in the Action table. Put the destination state in the Goto table. (b) At the end of a rule X → Y? Add a reduce operation for each terminal in the Follow set of X. (c) Looking at a non-terminal? Add the destination state to the Goto table.

  1. E → if E then • E | if E then • E else E E → • if E then E | • if E then E else E
  2. E → if E then E • | if E then E • else E

Activity! Make the LR Table, and think about what should happen at state 5.

8.2 Shift-Reduce Conflicts

There are two ways that shift-reduce conflicts are usually handled.

  1. Make a precedence rule. E.g. E → E • + E E → E ∗ E •

In this example, since * has higher precedence, the reducing rule will be used, and the shifting rule ignored.

  1. When in doubt, shift. E.g.

E → if E then E • E → if E then E • else E

8.3 Reduce-Reduce Conflicts

S → A | B

A → x y B → x y

What will happen in the CFSM?

I 0 : S → • A

S → • B

A → • x y B → • x y I 1 : S → A • I 2 : S → B • I 3 : A → x • y B → x • y I 4 : A → x y • B → x y • State 4 has a reduce-reduce conflict. This grammar has serious problems.