+ All Categories
Home > Documents > Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1....

Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1....

Date post: 18-Mar-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
49
Parsing
Transcript
Page 1: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

Parsing

Page 2: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

Today

COOL

code

txt

Executable

code

exeLexicalAnalysis

Syntax Analysis

Parsing

AST SymbolTableetc.

Inter.Rep.

(IR)

Code

Gen.

Page 3: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

Top Down Parsing

Parsing

Bottom Up Parsing

Predictive Parsing Shift-reduce Parsing

LL(k) Parsing LR(k) Parsing

Left Recursion

Left Factoring

Page 4: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

Bottom-Up Parsers

Bottom-up parsers: build the nodes on the bottom of the parse tree first.Suitable for automatic parser generation, handle a larger class of grammars.Examples: shift-reduce parser (or LR(k) parsers)

Page 5: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

Bottom-up Parsing

zNo problem with left-recursionzWidely used in practicezLR(1), SLR(1), LALR(1)

Page 6: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

Non-ambiguous CFG

CLR(1)

LALR(1)

SLR(1)

LL(1)

Grammar Hierarchy

Page 7: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

Bottom-up Parsing

zWorks from tokens to start-symbolzRepeat: yidentify handle - reducible sequence: ⌧ non-terminal is not constructed but⌧ all its children have been constructed

yreduce - construct non-terminal and update stack

zUntil reducing to start-symbol

Page 8: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

Bottom-up Parsing1 + (2) + (3)

E + (E) + (3)

+

E → E + (E)E → i

E

1 2 + 3

E

E + (3)

E

( ) ( )

E + (E)

E

E

E

E + (2) + (3) i = 0,1, 2, …, 9

Page 9: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

Bottom-up Parsing

zIs the following grammar LL(1) ?

1 + (2)1 + (2) + (3)

zBut this is a useful grammar

E → E + (E)E → i

zNO

Page 10: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

Bottom-Up Parser

A bottom-up parser, or a shift-reduce parser, beginsat the leaves and works up to the top of the tree.

The reduction steps trace a rightmost derivationon reverse.

S → aABeA → Abc | bB → d

Consider the Grammar:

We want to parse the input string abbcde.

Page 11: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

Bottom-Up Parser Example

a dbb cINPUT:

Bottom-Up ParsingProgram

e OUTPUT:$

ProductionS → aABeA → AbcA → bB → d

Page 12: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

Bottom-Up Parser Example

a dbb cINPUT:

Bottom-Up ParsingProgram

e OUTPUT:

A

b

$

ProductionS → aABeA → AbcA → bB → d

Page 13: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

Bottom-Up Parser Example

a dbA cINPUT:

Bottom-Up ParsingProgram

e OUTPUT:

A

b

$

ProductionS → aABeA → AbcA → bB → d

Page 14: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

Bottom-Up Parser Example

a dbA cINPUT:

Bottom-Up ParsingProgram

e OUTPUT:

A

b

$

ProductionS → aABeA → AbcA → bB → d

We are not reducing here in this example.

A parser would reduce, get stuck and then backtrack!

Page 15: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

Bottom-Up Parser Example

a dbA cINPUT:

Bottom-Up ParsingProgram

e OUTPUT:

A

b

$

ProductionS → aABeA → AbcA → bB → d

c

A

b

Page 16: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

Bottom-Up Parser Example

a dAINPUT:

Bottom-Up ParsingProgram

e OUTPUT:

A c

A

b

$

ProductionS → aABeA → AbcA → bB → d

b

Page 17: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

Bottom-Up Parser Example

a dAINPUT:

Bottom-Up ParsingProgram

e OUTPUT:

A c

A

b

$

ProductionS → aABeA → AbcA → bB → d

b

B

d

Page 18: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

Bottom-Up Parser Example

a BAINPUT:

Bottom-Up ParsingProgram

e OUTPUT:

A c

A

b

$

ProductionS → aABeA → AbcA → bB → d

b

B

d

Page 19: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

Bottom-Up Parser Example

a BAINPUT:

Bottom-Up ParsingProgram

e OUTPUT:

A c

A

b

$

ProductionS → aABeA → AbcA → bB → d

b

B

d

a

S

e

Page 20: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

Bottom-Up Parser Example

SINPUT:

Bottom-Up ParsingProgram

OUTPUT:

A c

A

b

$

ProductionS → aABeA → AbcA → bB → d

b

B

d

a

S

e

This parser is known as an LR Parser because it scans the input from Left to right, and it constructs

a Rightmost derivation in reverse order.

Page 21: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

Bottom-Up Parser Example

The scanning of productions for matching withhandles in the input string, and backtracking makesthe method used in the previous example veryinefficient.

Can we do better?

Page 22: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

LR Parser Example

Input

Stack

LR ParsingProgram

action goto

Output

Page 23: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

Shift reduce parser

2. Apply the shift-reduce parsing algorithm to construct the parse tree

1. Construct the action-goto table from the given grammar

Page 24: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

Shift reduce parser

1. Construct the action-goto table from the given grammar

This is what make difference between different typsof shift reduce parsing such as SLR, CLR, LALR

In this course due to short of time we will not study how to construct the action-goto table

Page 25: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

Shift reduce parser2. Apply the shift-reduce parsing algorithm to construct the parse tree

The following algorithm shows how we can construct the move parsing table for an input string w$ with respect to a given grammar G.

set ip to point to the first symbol of the input string w$repeat forever begin

if action[top(stack), current-input(ip)] = shift(s) then beginpush current-input(ip) then s on top of the stackadvance ip to the next input symbol

endelse if action[top(stack), current-input(ip)] = reduce A à ß thenbegin

pop 2*|ß| symbols off the stack;

output the production A à ßend

else error()end

push A then goto[top(stack), A] on top of the stack;

else if action[top(stack), current-input(ip)] = accept thenreturn

Page 26: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

LR Parser Example

action goto State id + * ( ) $ E T F

0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1

10 r3 r3 r3 r3 11 r5 r5 r5 r5

The following grammar:

(1) E → E + T(2) E → T(3) T → T ∗ F(4) T → F(5) F → ( E ) (6) F → id

Can be parsed with this actionand goto table

s represents shiftr represents reduceacc represents acceptempty represents error

Page 27: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

LR Parser Exampleid idid+ ∗INPUT: $

STACK: E0

(1) E → E + T(2) E → T(3) T → T ∗ F(4) T → F(5) F → ( E ) (6) F → id

LR ParsingProgram

action goto State id + * ( ) $ E T F

0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1

10 r3 r3 r3 r3 11 r5 r5 r5 r5

GRAMMAR:

OUTPUT:

Page 28: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

OUTPUT:LR Parser Example

id idid∗ +INPUT: $

STACK:

(1) E → E + T(2) E → T(3) T → T ∗ F(4) T → F(5) F → ( E ) (6) F → id

LR ParsingProgramE5

id0

action goto State id + * ( ) $ E T F

0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1

10 r3 r3 r3 r3 11 r5 r5 r5 r5

F

id

GRAMMAR:

Page 29: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

OUTPUT:

0

LR Parser Exampleid idid∗ +INPUT: $

STACK:

(1) E → E + T(2) E → T(3) T → T ∗ F(4) T → F(5) F → ( E ) (6) F → id

LR ParsingProgram

action goto State id + * ( ) $ E T F

0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1

10 r3 r3 r3 r3 11 r5 r5 r5 r5

F

id

GRAMMAR:

Page 30: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

OUTPUT:

E3F0

LR Parser Exampleid idid∗ +INPUT: $

STACK:

(1) E → E + T(2) E → T(3) T → T ∗ F(4) T → F(5) F → ( E ) (6) F → id

LR ParsingProgram

action goto State id + * ( ) $ E T F

0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1

10 r3 r3 r3 r3 11 r5 r5 r5 r5

T

F

id

GRAMMAR:

Page 31: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

OUTPUT:

0

LR Parser Exampleid idid∗ +INPUT: $

STACK:

(1) E → E + T(2) E → T(3) T → T ∗ F(4) T → F(5) F → ( E ) (6) F → id

LR ParsingProgram

action goto State id + * ( ) $ E T F

0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1

10 r3 r3 r3 r3 11 r5 r5 r5 r5

T

F

id

GRAMMAR:

Page 32: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

OUTPUT:LR Parser Example

id idid∗ +INPUT: $

STACK:

(1) E → E + T(2) E → T(3) T → T ∗ F(4) T → F(5) F → ( E ) (6) F → id

LR ParsingProgramE2

T0

action goto State id + * ( ) $ E T F

0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1

10 r3 r3 r3 r3 11 r5 r5 r5 r5

T

F

id

GRAMMAR:

Page 33: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

OUTPUT:LR Parser Example

id idid∗ +INPUT: $

STACK:

(1) E → E + T(2) E’ → T(3) T → T ∗ F(4) T → F(5) F → ( E ) (6) F → id

LR ParsingProgram

action goto State id + * ( ) $ E T F

0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1

10 r3 r3 r3 r3 11 r5 r5 r5 r5

E7∗2T0

T

F

id

GRAMMAR:

Page 34: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

OUTPUT:LR Parser Example

id idid∗ +INPUT: $

STACK:

(1) E → E + T(2) E’ → T(3) T → T ∗ F(4) T → F(5) F → ( E ) (6) F → id

LR ParsingProgramE5

id7∗2T0

action goto State id + * ( ) $ E T F

0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1

10 r3 r3 r3 r3 11 r5 r5 r5 r5

T

F

id

F

id

GRAMMAR:

Page 35: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

OUTPUT:LR Parser Example

id idid∗ +INPUT: $

STACK:

(1) E → E + T(2) E’ → T(3) T → T ∗ F(4) T → F(5) F → ( E ) (6) F → id

LR ParsingProgramE7

∗2T0

action goto State id + * ( ) $ E T F

0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1

10 r3 r3 r3 r3 11 r5 r5 r5 r5

T

F

id

F

id

GRAMMAR:

Page 36: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

OUTPUT:LR Parser Example

id idid∗ +INPUT: $

STACK:

(1) E → E + T(2) E’ → T(3) T → T ∗ F(4) T → F(5) F → ( E ) (6) F → id

LR ParsingProgramE10

F7∗2T0

action goto State id + * ( ) $ E T F

0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1

10 r3 r3 r3 r3 11 r5 r5 r5 r5

T

∗T F

F

id

id

GRAMMAR:

Page 37: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

OUTPUT:

0

LR Parser Exampleid idid∗ +INPUT: $

STACK:

(1) E → E + T(2) E → T(3) T → T ∗ F(4) T → F(5) F → ( E ) (6) F → id

LR ParsingProgram

action goto State id + * ( ) $ E T F

0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1

10 r3 r3 r3 r3 11 r5 r5 r5 r5

T

∗T F

F

id

id

GRAMMAR:

Page 38: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

OUTPUT:LR Parser Example

id idid∗ +INPUT: $

STACK:

(1) E → E + T(2) E → T(3) T → T ∗ F(4) T → F(5) F → ( E ) (6) F → id

LR ParsingProgram2

T0

T

∗T F

F

id

idaction goto State

id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1

10 r3 r3 r3 r3 11 r5 r5 r5 r5

E

GRAMMAR:

Page 39: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

OUTPUT:

0

LR Parser Exampleid idid∗ +INPUT: $

STACK:

(1) E → E + T(2) E → T(3) T → T ∗ F(4) T → F(5) F → ( E ) (6) F → id

LR ParsingProgram

action goto State id + * ( ) $ E T F

0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1

10 r3 r3 r3 r3 11 r5 r5 r5 r5

T

∗T F

F

id

id

E

GRAMMAR:

Page 40: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

OUTPUT:LR Parser Example

id idid∗ +INPUT: $

STACK:

(1) E → E + T(2) E’ → T(3) T → T ∗ F(4) T → F(5) F → ( E ) (6) F → id

LR ParsingProgram1

E0

action goto State id + * ( ) $ E T F

0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1

10 r3 r3 r3 r3 11 r5 r5 r5 r5

T

∗T F

F

id

id

E

GRAMMAR:

Page 41: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

OUTPUT:LR Parser Example

id idid∗ +INPUT: $

STACK:

(1) E → E + T(2) E’ → T(3) T → T ∗ F(4) T → F(5) F → ( E ) (6) F → id

LR ParsingProgram

action goto State id + * ( ) $ E T F

0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1

10 r3 r3 r3 r3 11 r5 r5 r5 r5

T

∗T F

F

id

id

E

6+1E0

GRAMMAR:

Page 42: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

LR Parser Exampleid idid∗ +INPUT: $

STACK:

(1) E → E + T(2) E’ → T(3) T → T ∗ F(4) T → F(5) F → ( E ) (6) F → id

LR ParsingProgram

action goto State id + * ( ) $ E T F

0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1

10 r3 r3 r3 r3 11 r5 r5 r5 r5

OUTPUT:

T

∗T F

F

id

id

E

5id6+1E0

F

id

GRAMMAR:

Page 43: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

LR Parser Exampleid idid∗ +INPUT: $

STACK:

(1) E → E + T(2) E’ → T(3) T → T ∗ F(4) T → F(5) F → ( E ) (6) F → id

LR ParsingProgram

OUTPUT:

T

∗T F

F

id

id

E

6+1E0

F

id

GRAMMAR:

action goto State id + * ( ) $ E T F

0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3

Page 44: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

LR Parser Exampleid idid∗ +INPUT: $

STACK:

(1) E → E + T(2) E’ → T(3) T → T ∗ F(4) T → F(5) F → ( E ) (6) F → id

LR ParsingProgram

action goto State id + * ( ) $ E T F

0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1

10 r3 r3 r3 r3 11 r5 r5 r5 r5

OUTPUT:

T

∗T F

F

id

id

E

3F6+1E0

F

id

GRAMMAR:

T

Page 45: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

LR Parser Exampleid idid∗ +INPUT: $

STACK:

(1) E → E + T(2) E’ → T(3) T → T ∗ F(4) T → F(5) F → ( E ) (6) F → id

LR ParsingProgram

OUTPUT:

T

∗T F

F

id

id

E

6+1E0

F

id

GRAMMAR:

action goto State id + * ( ) $ E T F

0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1

10 r3 r3 r3 r3 11 r5 r5 r5 r5

Page 46: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

LR Parser Exampleid idid∗ +INPUT: $

STACK:

(1) E → E + T(2) E’ → T(3) T → T ∗ F(4) T → F(5) F → ( E ) (6) F → id

LR ParsingProgram

action goto State id + * ( ) $ E T F

0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1

10 r3 r3 r3 r3 11 r5 r5 r5 r5

OUTPUT:

T

∗T F

F

id

id

E

9T6+1E0

F

id

GRAMMAR:

T

E

+

Page 47: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

LR Parser Exampleid idid∗ +INPUT: $

STACK:

(1) E → E + T(2) E → T(3) T → T ∗ F(4) T → F(5) F → ( E ) (6) F → id

LR ParsingProgram0

GRAMMAR:

action goto State id + * ( ) $ E T F

0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1

10 r3 r3 r3 r3 11 r5 r5 r5 r5

OUTPUT:

T

∗T F

F

id

id

E

F

id

T

E

+

Page 48: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

LR Parser Exampleid idid∗ +INPUT: $

STACK:

(1) E → E + T(2) E’ → T(3) T → T ∗ F(4) T → F(5) F → ( E ) (6) F → id

LR ParsingProgram

action goto State id + * ( ) $ E T F

0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1

10 r3 r3 r3 r3 11 r5 r5 r5 r5

OUTPUT:

T

∗T F

F

id

id

E

1E0

F

id

GRAMMAR:

T

E

+

Page 49: Parsing - 会津大学公式ウェブサイトhamada/LP/L4-1-LP.pdf · Shift reduce parser 1. Construct the action-goto table from the given grammar This is what make difference between

Constructing Parsing Tables

All LR parsers use the same parsing program thatwe demonstrated in the previous slides. What differentiates the LR parsers are the action and the goto tables:Simple LR (SLR): succeeds for the fewest grammars, but is the easiest to implement.

Canonical LR: succeeds for the most grammars, but is the hardest to implement. It splits states when necessary to prevent reductions that would get the parser stuck.

Lookahead LR (LALR): succeeds for most common syntacticconstructions used in programming languages, but producesLR tables much smaller than canonical LR.


Recommended