Docsity

LR parsing and LR parse table construction, Study Guides, Projects, Research of Compiler Design

Partial preview of the text

Download LR parsing and LR parse table construction and more Study Guides, Projects, Research Compiler Design in PDF only on Docsity!

LR(0) parsing^ 1.^

Construct transition relation between states „^ Use algorithms

Initial item set

and

Next item set

„^

States are set of LR(0) items „^ Shift items of the form

P→

α•Sβ

„^

Reduce items of the form

P→

2.^

Construct parsing table „^ If every state contains no conflicts use LR(0) parsingalgorithm „^ If states contain conflict^ „

Rewrite grammar or „ Resolve conflict or „ Use stronger parsing technique

FUNCTION Initial item set RETURNING an item set:

SET New item set TO Empty;// Initial contents – obtain from the start symbol:FOR EACH production rule S

→α^ for the start symbol S:

SET New item set TO New item set + item S

RETURN

ε^ closure (New item set);

FUNCTION Next item set (Item set, Symbol) RETURNING an item set:

SET New item set TO Empty;// Initial contents – obtain from token moves:FOR EACH item N

Sβ^ IN item set;

IF S = Symbol;SET New item set TO New item set + N

→αS

  • β

RETURN

ε^ closure (New item set);

Data definitions:^ A set S of LR(0) items. Initializations:^ S is prefilled externally with one or more LR(0) items. Inference rules:^ If S holds an item of the form P

Nβ, then for each production rule N

→γ^ in G,

S must also contain the item N

ε^ closure algorithm for LR(0) item sets for a grammar G

p. 155 p. 158 p. 158

E^ →^ T

Z^ → •

E$

E^ → •

T

E^ → •

E+T

T^ → •

i T^ → •

(E)

T^ →^ (

• E)

E^ → •

T

E^ → •

E+T

T^ → •

i T^ → •

(E)

T^ →^ i

Z^ →^ E

E^ →^ E

• +T

T^ →^ (E

E^ →^ E

• +T

E^ →^ E+

• T

T^ → •

i T^ → •

(E)

Z^ →^ E$

•^

T^ →^ (E)

E^ →^ E+T

S^0

S^6

S^7

S^5

S^1

S^2

S^3 S^4

S^8 S 9

T^

T

i^

i i T

+^

E

E $

T→(E)

shift

shift 6 8

E→T

T→I

E→E+T

shift 4

Z→E$

shift

shift 6 1

T E $ ) ( + i

state

GOTO tablesymbol

ACTION table

S^0

i + i $

shift

Stack

Input

Action

S^ i S^0

5

+ i $^

reduce by T

→i

S^ T S^0

6

+ i $^

reduce by E

→T

S^ E S^0

1

+ i $^

shift

S^ E S^0

+ S^1

i $^

shift

S^ E S^0

+ S^1

i S^5

$^

reduce by T

→i

S^ E S^0

+ S^1

T S^4

$^

reduce by E

→E+T

S^ E S^0

1

$^

shift

S^ E S^0

$ S^1

reduce by Z

→E$

S^ Z^0

stop

LR(0) parsing of i+i