More Parsing Algorithms - tudelft-cs4200.github.io

72
More Parsing Algorithms CS4200 | Compiler Construction | October 15, 2020 Eelco Visser

Transcript of More Parsing Algorithms - tudelft-cs4200.github.io

Page 1: More Parsing Algorithms - tudelft-cs4200.github.io

More Parsing Algorithms

CS4200 | Compiler Construction | October 15, 2020

Eelco Visser

Page 2: More Parsing Algorithms - tudelft-cs4200.github.io

Predictive parsing - predictive/recursive descent parsing- LL parsing- LL(k) grammars

Generalized LR Parsing - LR parsing with shift/reduce conflicts

This Lecture

Page 3: More Parsing Algorithms - tudelft-cs4200.github.io

Predictive (Recursive Descent) Parsing

Page 4: More Parsing Algorithms - tudelft-cs4200.github.io

Vocabulary Σ - finite, nonempty set of elements (words, letters)- alphabet

String over Σ- finite sequence of elements chosen from Σ- word, sentence, utterance

Formal language λ - set of strings over a vocabulary Σ-λ ⊆ Σ*

A Theory of Language: Formal Languages

Page 5: More Parsing Algorithms - tudelft-cs4200.github.io

Formal grammar G = (N, Σ, P, S) - nonterminal symbols N - terminal symbols Σ- production rules P ⊆ (N∪Σ)* N (N∪Σ)* × (N∪Σ)*- start symbol S∈N

Grammar classes - type-0, unrestricted- type-1, context-sensitive: (a A c, a b c)- type-2, context-free: P ⊆ N × (N ∪ Σ)*- type-3, regular: (A, x) or (A, xB)

A Theory of Languages: Formal Grammars

Page 6: More Parsing Algorithms - tudelft-cs4200.github.io

Formal grammar - G = (N, Σ, P, S)Derivation relation -⇒G ⊆ (N∪Σ)* × (N∪Σ)*

-α β γ %=>G α β’ γ %%<=> ∃(β, β’)∈P

Formal language - L(G) ⊆ Σ*- L(G) = {w ∈ Σ* | S ⇒G* w}

Classes of formal languages

A Theory of Languages: Formal Languages

Page 7: More Parsing Algorithms - tudelft-cs4200.github.io

Predictive Parsing: Recursive Descent

public void parseExp() { consume(WHILE); parseExp(); consume(DO); parseExp();}

Exp = “while” Exp “do” Exp

Page 8: More Parsing Algorithms - tudelft-cs4200.github.io

Predictive Parsing: Lookahead

public void parseExp() {

switch current() { case WHILE: consume(WHILE); parseExp(); %%...; break; case IF : consume(IF); parseExp(); %%...; break; default : error(); }}

Exp = “while” Exp “do” ExpExp = “if” Exp “then” Exp “else” Exp

Page 9: More Parsing Algorithms - tudelft-cs4200.github.io

Rows - nonterminal symbols N - symbol to parse

Columns - terminal symbols Σk

- look ahead k

Entries - production rules P- possible conflicts

With N on the stack and T in the input, predict P

Predictive Parsing: Parse Table

T1 T2 T3 ...

N1 N1 →... N1 →...

N2 N2 →...

N3 N3 →... N3 →...

N4 N4 →...

N5 N5 →...

N6 N6 →... N6 →...

N7 N7 →...

N8 N8 →... N8 →... N8 →...

...

Page 10: More Parsing Algorithms - tudelft-cs4200.github.io

Predictive Parsing: Automaton

… tn $t1

$S

input

parse table

stack

Page 11: More Parsing Algorithms - tudelft-cs4200.github.io

Predictive Parsing: Automaton

x … $

$

x…

Recognize predicted symbol

Page 12: More Parsing Algorithms - tudelft-cs4200.github.io

x … $

$

Xi

x

XiXi → Y1... Yk…

Yk

…Y1

Predictive Parsing: Automaton

Predict production

Page 13: More Parsing Algorithms - tudelft-cs4200.github.io

LL Parse Tables

Page 14: More Parsing Algorithms - tudelft-cs4200.github.io

Predictive Parsing: Filling the Table

entry X = α ∈ P at row X and column T

T∈ FIRST(α)

nullable(α) ∧ T∈ FOLLOW(X)

letters that α can start with

letters that can follow X

w ⇒G* ε

Page 15: More Parsing Algorithms - tudelft-cs4200.github.io

nullable(X)-(X, ε) ∈ P %=> nullable(X)

-(X0, X1…Xk)∈P ∧ nullable(X1) ∧ … ∧ nullable(Xk) ⇒ nullable(X0)

nullable(α) -nullable(ε) -nullable(X1 … Xk) = nullable(X1) ∧ … ∧ nullable(Xk)

Predictive Parsing: Nullable

Page 16: More Parsing Algorithms - tudelft-cs4200.github.io

FIRST(X)-X∈Σ : FIRST(X) = {X}-(X0, X1 … Xi … Xk)∈P ∧ nullable(X1 … Xi) ⇒ FIRST(X0) ⊇ FIRST(Xi+1)

FIRST(w) -FIRST(ε) = {} -¬nullable(X) ⇒ FIRST(Xw) = FIRST(X) -nullable(X) ⇒ FIRST(Xw) = FIRST(X) ∪ FIRST(w)

Predictive Parsing: First Set

Page 17: More Parsing Algorithms - tudelft-cs4200.github.io

FOLLOW(X)-(X0, X1 … Xi … Xk)∈P ∧ nullable(Xi+1 … Xk) ⇒ FOLLOW(Xi) ⊇ FOLLOW(X0)

-(X0, X1 … Xi … Xk)∈P ⇒ FOLLOW(Xi) ⊇ FIRST(Xi+1 … Xk)

Predictive Parsing: Follow Set

Page 18: More Parsing Algorithms - tudelft-cs4200.github.io

Example

p0: Start = Exp EOFp1: Exp = Term Exp’p2: Exp’ = “+” Term Exp’p3: Exp’ =p4: Term = Fact Term’p5: Term’ = “*” Fact Term’p6: Term’ =p7: Fact = Num p8: Fact = “(” Exp “)”

nullable FIRST FOLLOW

Start

Exp

Exp’

Term

Term’

Fact

Page 19: More Parsing Algorithms - tudelft-cs4200.github.io

Example: Nullable

(X, ε) ∈ P ⇒ nullable(X)

(X0, X1 … Xk)∈P ∧

nullable(X1) ∧ … ∧ nullable(Xk) ⇒ nullable(X0)

nullable FIRST FOLLOW

Start

Exp

Exp’

Term

Term’

Fact

p0: Start = Exp EOFp1: Exp = Term Exp’p2: Exp’ = “+” Term Exp’p3: Exp’ =p4: Term = Fact Term’p5: Term’ = “*” Fact Term’p6: Term’ =p7: Fact = Num p8: Fact = “(” Exp “)”

Page 20: More Parsing Algorithms - tudelft-cs4200.github.io

Example: Nullable

nullable FIRST FOLLOW

Start no

Exp no

Exp’ yes

Term no

Term’ yes

Fact no(X, ε) ∈ P ⇒ nullable(X)

(X0, X1 … Xk)∈P ∧

nullable(X1) ∧ … ∧ nullable(Xk) ⇒ nullable(X0)

p0: Start = Exp EOFp1: Exp = Term Exp’p2: Exp’ = “+” Term Exp’p3: Exp’ =p4: Term = Fact Term’p5: Term’ = “*” Fact Term’p6: Term’ =p7: Fact = Num p8: Fact = “(” Exp “)”

Page 21: More Parsing Algorithms - tudelft-cs4200.github.io

Example: FIRST

(X0, X1 … Xi … Xk)∈P ∧

nullable(X1 … Xi) ⇒ FIRST(X0) ⊇ FIRST(Xi+1)

nullable FIRST FOLLOW

Start no

Exp no

Exp’ yes

Term no

Term’ yes

Fact no

p0: Start = Exp EOFp1: Exp = Term Exp’p2: Exp’ = “+” Term Exp’p3: Exp’ =p4: Term = Fact Term’p5: Term’ = “*” Fact Term’p6: Term’ =p7: Fact = Num p8: Fact = “(” Exp “)”

Page 22: More Parsing Algorithms - tudelft-cs4200.github.io

Example: FIRST

(X0, X1 … Xi … Xk)∈P ∧

nullable(X1 … Xi) ⇒ FIRST(X0) ⊇ FIRST(Xi+1)

nullable FIRST FOLLOW

Start no Num (

Exp no Num (

Exp’ yes +

Term no Num (

Term’ yes *

Fact no Num (

p0: Start = Exp EOFp1: Exp = Term Exp’p2: Exp’ = “+” Term Exp’p3: Exp’ =p4: Term = Fact Term’p5: Term’ = “*” Fact Term’p6: Term’ =p7: Fact = Num p8: Fact = “(” Exp “)”

Page 23: More Parsing Algorithms - tudelft-cs4200.github.io

Example: FOLLOW

(X0, X1 … Xi … Xk)∈P ∧

nullable(Xi+1 … Xk) ⇒ FOLLOW(Xi) ⊇ FOLLOW(X0)

(X0, X1 … Xi … Xk)∈P ⇒ FOLLOW(Xi) ⊇ FIRST(Xi+1 … Xk)

nullable FIRST FOLLOW

Start no Num (

Exp no Num (

Exp’ yes +

Term no Num (

Term’ yes *

Fact no Num (

p0: Start = Exp EOFp1: Exp = Term Exp’p2: Exp’ = “+” Term Exp’p3: Exp’ =p4: Term = Fact Term’p5: Term’ = “*” Fact Term’p6: Term’ =p7: Fact = Num p8: Fact = “(” Exp “)”

Page 24: More Parsing Algorithms - tudelft-cs4200.github.io

Example: FOLLOW

(X0, X1 … Xi … Xk)∈P ∧

nullable(Xi+1 … Xk) ⇒ FOLLOW(Xi) ⊇ FOLLOW(X0)

(X0, X1 … Xi … Xk)∈P ⇒ FOLLOW(Xi) ⊇ FIRST(Xi+1 … Xk)

nullable FIRST FOLLOW

Start no Num (

Exp no Num ( ) EOF

Exp’ yes + ) EOF

Term no Num ( + ) EOF

Term’ yes * + ) EOF

Fact no Num ( * + ) EOF

p0: Start = Exp EOFp1: Exp = Term Exp’p2: Exp’ = “+” Term Exp’p3: Exp’ =p4: Term = Fact Term’p5: Term’ = “*” Fact Term’p6: Term’ =p7: Fact = Num p8: Fact = “(” Exp “)”

Page 25: More Parsing Algorithms - tudelft-cs4200.github.io

Example: LL Parse Table

entry (X, w)∈P at row X and column T

T∈ FIRST(w)

nullable(w) ∧ T∈ FOLLOW(X)

+ * Num ( ) EOF

Start

Exp

Exp’

Term

Term’

Fact

p0: Start = Exp EOFp1: Exp = Term Exp’p2: Exp’ = “+” Term Exp’p3: Exp’ =p4: Term = Fact Term’p5: Term’ = “*” Fact Term’p6: Term’ =p7: Fact = Num p8: Fact = “(” Exp “)”

Page 26: More Parsing Algorithms - tudelft-cs4200.github.io

Example: LL Parse Table

entry (X, w)∈P at row X and column T

T∈ FIRST(w)

nullable(w) ∧ T∈ FOLLOW(X)

+ * Num ( ) EOF

Start p0 p0

Exp p1 p1

Exp’ p2 p3 p3

Term p4 p4

Term’ p6 p5 p6 p6

Fact p7 p8

p0: Start = Exp EOFp1: Exp = Term Exp’p2: Exp’ = “+” Term Exp’p3: Exp’ =p4: Term = Fact Term’p5: Term’ = “*” Fact Term’p6: Term’ =p7: Fact = Num p8: Fact = “(” Exp “)”

Page 27: More Parsing Algorithms - tudelft-cs4200.github.io

Example: Parsing

+ * Num ( ) EOF

Start p0 p0

Exp p1 p1

Exp’ p2 p3 p3

Term p4 p4

Term’ p6 p5 p6 p6

Fact p7 p8

p0: Start = Exp EOFp1: Exp = Term Exp’p2: Exp’ = “+” Term Exp’p3: Exp’ =p4: Term = Fact Term’p5: Term’ = “*” Fact Term’p6: Term’ =p7: Fact = Num p8: Fact = “(” Exp “)”

Page 28: More Parsing Algorithms - tudelft-cs4200.github.io

Grammar Classes

context-free grammars

LL(k)

LL(1)

LL(0)

Given the next n tokens, predict next production

Page 29: More Parsing Algorithms - tudelft-cs4200.github.io

Predictive Parsing: Encoding Precedence

Fact = Num Fact = “(” Exp “)”Term = Term “*” FactTerm = Fact Exp = Exp “+” TermExp = Term

Exp = Num Exp = “(” Exp “)”Exp = Exp “*” Exp Exp = Exp “+” Exp

Page 30: More Parsing Algorithms - tudelft-cs4200.github.io

Predictive Parsing: Eliminating Left Recursion

Term’ = “*” Fact Term’Term’ =Term = Fact Term’Exp’ = “+” Term Exp’Exp’ =

Term = Term “*” FactTerm = Fact Exp = Exp “+” TermExp = Term

Page 31: More Parsing Algorithms - tudelft-cs4200.github.io

Predictive Parsing: Left Factoring

Exp = “if” Exp “then” Exp ElseElse = “else” ExpElse =

Exp = “if” Exp “then” Exp “else” ExpExp = “if” Exp “then” Exp

Page 32: More Parsing Algorithms - tudelft-cs4200.github.io

Summary

Page 33: More Parsing Algorithms - tudelft-cs4200.github.io

How can we parse context-free languages effectively? - predictive parsing algorithms

Which grammar classes are supported by these algorithms? - LL(k) grammars, LL(k) languages

How can we generate compiler tools from that? - implement automaton- generate parse tables

What are other techniques for implementing top-down parsers? - Parser Combinators - PEGs- ALL(*)

Summary

Page 34: More Parsing Algorithms - tudelft-cs4200.github.io

Formal languages - Noam Chomsky: Three models for the description of language. 1956- J. E. Hopcroft, R. Motwani, J. D. Ullman: Introduction to Automata

Theory, Languages, and Computation. 2006

Syntactic analysis - Andrew W. Appel, Jens Palsberg: Modern Compiler Implementation

in Java, 2nd edition. 2002- Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman, Monica S. Lam:

Compilers: Principles, Techniques, and Tools, 2nd edition. 2006

Literature

Page 35: More Parsing Algorithms - tudelft-cs4200.github.io

ALL(*) - Terence John Parr, Sam Harwell, Kathleen Fisher. Adaptive LL(*) parsing:

the power of dynamic analysis. In OOPSLA 2014.

Parsing Expression Grammars - Bryan Ford. Parsing Expression Grammars: a recognition-based syntactic

foundation. In POPL 2004.

Parser Combinators - Graham Hutton. Higher-Order Functions for Parsing. Journal of Functional

Programming, 1992.- A. Moors, F. Piessens, Martin Odersky. Parser combinators in Scala.

Technical Report Department of Computer Science, K.U. Leuven, February 2008.

Literature

Page 36: More Parsing Algorithms - tudelft-cs4200.github.io

Generalized LR Parsing

36

Page 37: More Parsing Algorithms - tudelft-cs4200.github.io

Generalized Parsing - Parse all interpretations of the input => handle ambiguous grammars- Parsers split whenever finding an ambiguous interpretation and act

in (pseudo) parallel- Multiple parsers can join whenever they finish parsing an ambiguous

fragment of the input- Some parsers may "die", if the ambiguity was caused by a lack of

lookahead

Generalized Parsing

Page 38: More Parsing Algorithms - tudelft-cs4200.github.io

Generalized LR - Multiple parsers are synchronized on shift actions- Each parser has its own stack, and as they share states, the overall

structure becomes a graph (GSS)- If two parsers have the same state on top of their stack, they are

joined into a single parser- Reduce actions affect all possible paths from the top of the stacks

Generalized LR

Page 39: More Parsing Algorithms - tudelft-cs4200.github.io

39

S = E $E = E + E E = E * EE = a

Page 40: More Parsing Algorithms - tudelft-cs4200.github.io

40

S → . E $ E → . E + E E → . E * E E → . a

0

S = E $E = E + E E = E * EE = a

Page 41: More Parsing Algorithms - tudelft-cs4200.github.io

41

S → . E $ E → . E + E E → . E * E E → . a

S → E . $ E → E . + E E → E . * E

E → a .

E

a

10

2

S = E $E = E + E E = E * EE = a

Page 42: More Parsing Algorithms - tudelft-cs4200.github.io

42

S → . E $ E → . E + E E → . E * E E → . a

S → E . $ E → E . + E E → E . * E

E → a .

E → E + . E E → . E + E E → . E * E E → . a

E → E * . E E → . E + E E → . E * E E → . a

S → E $ .E$

a

*

+

10 3

2

4

5

S = E $E = E + E E = E * EE = a

Page 43: More Parsing Algorithms - tudelft-cs4200.github.io

E → E + . E E → . E + E E → . E * E E → . a

4

43

S → . E $ E → . E + E E → . E * E E → . a

S → E . $ E → E . + E E → E . * E

E → a .

E → E * . E E → . E + E E → . E * E E → . a

E → E * E . E → E . + E E → E . * E

S → E $ .E$

a

*

+ E

a

10 3

2

5

6

S = E $E = E + E E = E * EE = a

Page 44: More Parsing Algorithms - tudelft-cs4200.github.io

E → E + . E E → . E + E E → . E * E E → . a

4

44

S → . E $ E → . E + E E → . E * E E → . a

S → E . $ E → E . + E E → E . * E

E → a .

E → E * . E E → . E + E E → . E * E E → . a

E → E + E . E → E . + E E → E . * E

E → E * E . E → E . + E E → E . * E

S → E $ .E$

a

*

+ E

E

a

a

10 3

2

5

7

6

S = E $E = E + E E = E * EE = a

Page 45: More Parsing Algorithms - tudelft-cs4200.github.io

E → E + . E E → . E + E E → . E * E E → . a

4

45

S → . E $ E → . E + E E → . E * E E → . a

S → E . $ E → E . + E E → E . * E

E → a .

E → E * . E E → . E + E E → . E * E E → . a

E → E + E . E → E . + E E → E . * E

E → E * E . E → E . + E E → E . * E

S → E $ .E$

a

*

+ E

*

E

+a

10 3

2

5

7

6

a

S = E $E = E + E E = E * EE = a

Page 46: More Parsing Algorithms - tudelft-cs4200.github.io

46

S → . E $ E → . E + E E → . E * E E → . a

S → E . $ E → E . + E E → E . * E

E → a .

E → E + . E E → . E + E E → . E * E E → . a

E → E * . E E → . E + E E → . E * E E → . a

E → E + E . E → E . + E E → E . * E

E → E * E . E → E . + E E → E . * E

S → E $ .E$

a

*

+ E

*

*

+

E

+a

a

10 3

2

4

5

7

6

S = E $E = E + E E = E * EE = a

Page 47: More Parsing Algorithms - tudelft-cs4200.github.io

47

Nonterminal

Nullable First Follow

S

E

StateAction Goto

a + * $ S E

0

1

2

3

4

5

6

7

SLR Table

(0) S = E $(1) E = E + E (2) E = E * E(3) E = a

Page 48: More Parsing Algorithms - tudelft-cs4200.github.io

Nonterminal

Nullable First Follow

S no a -

E no a +, *, $

48

StateAction Goto

a + * $ S E

0

1

2

3

4

5

6

7

SLR Table

(0) S = E $(1) E = E + E (2) E = E * E(3) E = a

Page 49: More Parsing Algorithms - tudelft-cs4200.github.io

StateAction Goto

a + * $ S E

0 s2 1

1 s4 s5 acc

2 r3 r3 r3

3 acc acc acc acc

4 s2 7

5 s2 6

6 s4/r2 s5/r2 r2

7 s4/r1 s5/r1 r1

Nonterminal

Nullable First Follow

S no a -

E no a +, *, $

49

SLR Table

(0) S = E $(1) E = E + E (2) E = E * E(3) E = a

Page 50: More Parsing Algorithms - tudelft-cs4200.github.io

50

input:State Action Goto

a + * $ S E0 s2 11 s4 s5 acc2 r3 r3 r33 acc acc acc acc4 s2 75 s2 66 s4/r2 s5/r2 r27 s4/r1 s5/r1 r1

a + a * a $

0

Parsing(0) S = E $(1) E = E + E (2) E = E * E(3) E = a

Page 51: More Parsing Algorithms - tudelft-cs4200.github.io

51

input:State Action Goto

a + * $ S E0 s2 11 s4 s5 acc2 r3 r3 r33 acc acc acc acc4 s2 75 s2 66 s4/r2 s5/r2 r27 s4/r1 s5/r1 r1

+ a * a $

Parsing

0

2a synchronize on shifts

(0) S = E $(1) E = E + E (2) E = E * E(3) E = a

Page 52: More Parsing Algorithms - tudelft-cs4200.github.io

State Action Gotoa + * $ S E

0 s2 11 s4 s5 acc2 r3 r3 r33 acc acc acc acc4 s2 75 s2 66 s4/r2 s5/r2 r27 s4/r1 s5/r1 r1

52

input: + a * a $

Parsing

0

2a

(0) S = E $(1) E = E + E (2) E = E * E(3) E = a

Page 53: More Parsing Algorithms - tudelft-cs4200.github.io

State Action Gotoa + * $ S E

0 s2 11 s4 s5 acc2 r3 r3 r33 acc acc acc acc4 s2 75 s2 66 s4/r2 s5/r2 r27 s4/r1 s5/r1 r1

53

input: + a * a $

Parsing

0

2a

1a : E

(0) S = E $(1) E = E + E (2) E = E * E(3) E = a

Page 54: More Parsing Algorithms - tudelft-cs4200.github.io

State Action Gotoa + * $ S E

0 s2 11 s4 s5 acc2 r3 r3 r33 acc acc acc acc4 s2 75 s2 66 s4/r2 s5/r2 r27 s4/r1 s5/r1 r1

54

input: a * a $

Parsing

0

2a

1a : E 4+

synchronize

(0) S = E $(1) E = E + E (2) E = E * E(3) E = a

Page 55: More Parsing Algorithms - tudelft-cs4200.github.io

State Action Gotoa + * $ S E

0 s2 11 s4 s5 acc2 r3 r3 r33 acc acc acc acc4 s2 75 s2 66 s4/r2 s5/r2 r27 s4/r1 s5/r1 r1

55

input: a * a $

Parsing

01a : E 4

+

(0) S = E $(1) E = E + E (2) E = E * E(3) E = a

Page 56: More Parsing Algorithms - tudelft-cs4200.github.io

State Action Gotoa + * $ S E

0 s2 11 s4 s5 acc2 r3 r3 r33 acc acc acc acc4 s2 75 s2 66 s4/r2 s5/r2 r27 s4/r1 s5/r1 r1

56

input: * a $

Parsing

01a : E 4

+2a

synchronize

(0) S = E $(1) E = E + E (2) E = E * E(3) E = a

Page 57: More Parsing Algorithms - tudelft-cs4200.github.io

State Action Gotoa + * $ S E

0 s2 11 s4 s5 acc2 r3 r3 r33 acc acc acc acc4 s2 75 s2 66 s4/r2 s5/r2 r27 s4/r1 s5/r1 r1

57

input: * a $

Parsing

01a : E 4

+2a

(0) S = E $(1) E = E + E (2) E = E * E(3) E = a

Page 58: More Parsing Algorithms - tudelft-cs4200.github.io

State Action Gotoa + * $ S E

0 s2 11 s4 s5 acc2 r3 r3 r33 acc acc acc acc4 s2 75 s2 66 s4/r2 s5/r2 r27 s4/r1 s5/r1 r1

58

input: * a $

Parsing

01a : E 4

+2a

a : E 7

(0) S = E $(1) E = E + E (2) E = E * E(3) E = a

Page 59: More Parsing Algorithms - tudelft-cs4200.github.io

State Action Gotoa + * $ S E

0 s2 11 s4 s5 acc2 r3 r3 r33 acc acc acc acc4 s2 75 s2 66 s4/r2 s5/r2 r27 s4/r1 s5/r1 r1

59

input: * a $

Parsing

01a : E 4

+2a

a : E 71a + a : E

(0) S = E $(1) E = E + E (2) E = E * E(3) E = a

Page 60: More Parsing Algorithms - tudelft-cs4200.github.io

State Action Gotoa + * $ S E

0 s2 11 s4 s5 acc2 r3 r3 r33 acc acc acc acc4 s2 75 s2 66 s4/r2 s5/r2 r27 s4/r1 s5/r1 r1

60

input: a $

Parsing

01a : E 4

+2a

a : E 71a + a : E

5

**

synchronize

(0) S = E $(1) E = E + E (2) E = E * E(3) E = a

Page 61: More Parsing Algorithms - tudelft-cs4200.github.io

State Action Gotoa + * $ S E

0 s2 11 s4 s5 acc2 r3 r3 r33 acc acc acc acc4 s2 75 s2 66 s4/r2 s5/r2 r27 s4/r1 s5/r1 r1

61

input: a $

Parsing

01a : E 4

+ a : E 7

1a + a : E

5*

*

(0) S = E $(1) E = E + E (2) E = E * E(3) E = a

Page 62: More Parsing Algorithms - tudelft-cs4200.github.io

State Action Gotoa + * $ S E

0 s2 11 s4 s5 acc2 r3 r3 r33 acc acc acc acc4 s2 75 s2 66 s4/r2 s5/r2 r27 s4/r1 s5/r1 r1

62

input: $

Parsing

01a : E 4

+ a : E 7

1a + a : E

5*

*

2a

synchronize

(0) S = E $(1) E = E + E (2) E = E * E(3) E = a

Page 63: More Parsing Algorithms - tudelft-cs4200.github.io

State Action Gotoa + * $ S E

0 s2 11 s4 s5 acc2 r3 r3 r33 acc acc acc acc4 s2 75 s2 66 s4/r2 s5/r2 r27 s4/r1 s5/r1 r1

63

input: $

Parsing

01a : E 4

+ a : E 7

1a + a : E

5*

*

2a

(0) S = E $(1) E = E + E (2) E = E * E(3) E = a

Page 64: More Parsing Algorithms - tudelft-cs4200.github.io

State Action Gotoa + * $ S E

0 s2 11 s4 s5 acc2 r3 r3 r33 acc acc acc acc4 s2 75 s2 66 s4/r2 s5/r2 r27 s4/r1 s5/r1 r1

64

input: $

Parsing

01a : E 4

+ a : E 7

1a + a : E

5*

*

6a : E

(0) S = E $(1) E = E + E (2) E = E * E(3) E = a

Page 65: More Parsing Algorithms - tudelft-cs4200.github.io

State Action Gotoa + * $ S E

0 s2 11 s4 s5 acc2 r3 r3 r33 acc acc acc acc4 s2 75 s2 66 s4/r2 s5/r2 r27 s4/r1 s5/r1 r1

65

input:$

Parsing

01a : E 4

+ a : E 7

1a + a : E

5*

*

6a : E

(0) S = E $(1) E = E + E (2) E = E * E(3) E = a

Page 66: More Parsing Algorithms - tudelft-cs4200.github.io

State Action Gotoa + * $ S E

0 s2 11 s4 s5 acc2 r3 r3 r33 acc acc acc acc4 s2 75 s2 66 s4/r2 s5/r2 r27 s4/r1 s5/r1 r1

66

input: $

Parsing

01a : E 4

+ a : E 7

1a + a : E

5*

*

6a : E

7a * a : E

(0) S = E $(1) E = E + E (2) E = E * E(3) E = a

Page 67: More Parsing Algorithms - tudelft-cs4200.github.io

State Action Gotoa + * $ S E

0 s2 11 s4 s5 acc2 r3 r3 r33 acc acc acc acc4 s2 75 s2 66 s4/r2 s5/r2 r27 s4/r1 s5/r1 r1

67

input: $

Parsing

01a : E 4

+ a : E 7

1a + a : E

5*

*

6a : E

7a * a : E

(0) S = E $(1) E = E + E (2) E = E * E(3) E = a

Page 68: More Parsing Algorithms - tudelft-cs4200.github.io

State Action Gotoa + * $ S E

0 s2 11 s4 s5 acc2 r3 r3 r33 acc acc acc acc4 s2 75 s2 66 s4/r2 s5/r2 r27 s4/r1 s5/r1 r1

68

input: $

Parsing

01a : E 4

+ a : E 7

1a + a : E

5*

*

6a : E

7a * a : E

1[a + a] * a : E

(0) S = E $(1) E = E + E (2) E = E * E(3) E = a

Page 69: More Parsing Algorithms - tudelft-cs4200.github.io

State Action Gotoa + * $ S E

0 s2 11 s4 s5 acc2 r3 r3 r33 acc acc acc acc4 s2 75 s2 66 s4/r2 s5/r2 r27 s4/r1 s5/r1 r1

69

input: $

Parsing

01a : E 4

+ a : E 7

1a + a : E

5*

*

6a : E

7a * a : E

1[a + a] * a : E

(0) S = E $(1) E = E + E (2) E = E * E(3) E = a

Page 70: More Parsing Algorithms - tudelft-cs4200.github.io

State Action Gotoa + * $ S E

0 s2 11 s4 s5 acc2 r3 r3 r33 acc acc acc acc4 s2 75 s2 66 s4/r2 s5/r2 r27 s4/r1 s5/r1 r1

70

input: $

Parsing

[a + a] * a : Eor

a + [a * a] : E

01a : E 4

+ a : E7

1a + a : E

5*

*

6a : E

7

a * a : E

1

(0) S = E $(1) E = E + E (2) E = E * E(3) E = a

Page 71: More Parsing Algorithms - tudelft-cs4200.github.io

State Action Gotoa + * $ S E

0 s2 11 s4 s5 acc2 r3 r3 r33 acc acc acc acc4 s2 75 s2 66 s4/r2 s5/r2 r27 s4/r1 s5/r1 r1

71

input: $

Parsing

synchronize

accept with trees on the link to initial state

[a + a] * a : Eor

a + [a * a] : E

01a : E 4

+ a : E7

1a + a : E

5*

*

6a : E

7

a * a : E

1 3$

(0) S = E $(1) E = E + E (2) E = E * E(3) E = a

Page 72: More Parsing Algorithms - tudelft-cs4200.github.io

Except where otherwise noted, this work is licensed under