Docsity

LR Parsing: A Comprehensive Guide - Prof. Westley Weimer, Study notes of Programming Languages

Partial preview of the text

Download LR Parsing: A Comprehensive Guide - Prof. Westley Weimer and more Study notes Programming Languages in PDF only on Docsity!

How To Survive CS 415

How To Survive CS 415

PA3 – Parser

PA3 – Parser

Testing, CRM

Testing, CRM

  • PA4 – Type CheckerPA4 – Type Checker
    • CheckpointCheckpoint

PA5 – Interpreter

PA5 – Interpreter

  • Written AssignmentsWritten Assignments
  • MidtermsMidterms

Cheat Sheet

Cheat Sheet

  • Final ExamFinal Exam

LR Parsing

LR Parsing

Bottom-Up Parsing

Bottom-Up Parsing

In One Slide

If the LL(1) table for a grammar G has

multiple entries, then G is not LL(1)!

  • (^) An LR(1) parser reads tokens from left to

right and constructs a bottom-up rightmost

derivation. LR(1) parsers shift terminals and

reduce the input by application productions in

reverse. LR(1) parsing is fast and easy, and

uses a finite automaton with a stack. LR(1)

works fine if the grammar is left-recursive, or

not left-factored.

Constructing LL(1) Parsing Tables

  • Here is how to construct a parsing table T for

context-free grammar G

  • For each production A → α in G do:
    • For each terminal b ∈ First( α ) do
      • T[A, b] = α
    • If α!

ε, for each b ∈ Follow(A) do

  • T[A, b] = α

LL(1) Table Construction Example

  • Recall the grammar

E → T X X → + E | ε

T → ( E ) | int Y Y → *** T |** ε

  • Where in the row of Y we put Y! ε?
    • In the columns of Follow(Y) = { $, +, ) }

T int Y ( E )

ε ε ε

Y * T

ε ε

X + E

E T X T X

int * + ( ) $

Avoid Multiple Definitions!

Top-Down Parsing Strategies

  • Recursive Descent Parsing
    • But backtracking is too annoying, etc.
  • Predictive Parsing, aka. LL(k)
    • Predict production from k tokens of lookahead
    • Build LL(1) table
    • Parsing using the table is fast and easy
    • But many grammars are not LL(1) (or even LL(k))
  • Next: a more powerful parsing strategy for

grammars that are not LL(1)

Bottom-Up Parsing

Bottom-up parsing is more general than top-

down parsing

  • (^) And just as efficient
  • (^) Builds on ideas in top-down parsing

Preferred method in practice

Also called LR parsing

  • (^) L means that tokens are read left to right

R means that it constructs a rightmost derivation

The Idea

LR parsing reduces a string to the start

symbol by inverting productions:

str à input string of terminals

repeat

Identify β in str such that A! β is a production

(i.e., str = α β γ)

Replace β by A in str (i.e., str becomes α A γ)

until str = S

A Bottom-up Parse in Detail (1)

int + ( int + int )

int + (int) + (int)

A Bottom-up Parse in Detail (3)

E

int + ( + int )

int + (int) + (int)

E + (int) + (int)

E + (E) + (int)

E

int )

A Bottom-up Parse in Detail (4)

E

int + ( + int )

int + (int) + (int)

E + (int) + (int)

E + (E) + (int)

E + (int)

E

E

int )

A Bottom-up Parse in Detail (6)

E

E

int + ( + int )

int + (int) + (int)

E + (int) + (int)

E + (E) + (int)

E + (int)

E + (E)

E

E

E

E

A rightmost derivation

in reverse

int )

Important Fact

Important Fact #1 about bottom-up parsing:

An LR parser traces a rightmost derivation in

reverse.