Syntactic Analysis Parsing part 2 of 2 - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

Syntactic Analysis Parsing part 2 of 2

Description:

Use algorithms Initial item set and Next item set. States are set of LR(0) items ... SET Input token index TO Input token index 1; // shifted ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 9
Provided by: RomanMa8
Category:

less

Transcript and Presenter's Notes

Title: Syntactic Analysis Parsing part 2 of 2


1
Winter 2007-2008LR(0) Parsing Summary
Mooly Sagiv and Roman Manevich School of Computer
Science Tel-Aviv University
2
LR(0) parsing
  • 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?a?Sß
  • Reduce items of the form P?a?
  • Construct parsing table
  • If every state contains no conflicts use LR(0)
    parsing algorithm
  • If states contain conflict
  • Rewrite grammar or
  • Resolve conflict or
  • Use stronger parsing technique

3
e closure algorithm for LR(0) item sets for a
grammar G
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?a?Nß, then for each production rule
N?? in G, S must also contain the item N???.
p. 155
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?a
for the start symbol S SET
New item set TO New item set item S??a
RETURN e closure (New item set)
p. 158
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?a?Sß IN item set IF S Symbol
SET New item set TO New item set
N?aS?ß RETURN e closure (New item set)
p. 158
4
Example LR(0) for grammar G
Grammar G Z ? E E ? T E ? E T T ? i T ? ( E )
Conventionnon-terminals denoted by upper-case
lettersterminals denoted by lower-case letters
5
S6
E ? T?
T
S7
T
S0
T ? (?E)E ? ?TE ? ?ETT ? ?iT ? ?(E)
Z ? ?EE ? ?TE ? ?ETT ? ?iT ? ?(E)
(
(
S5
i
i
T ? i?
E
E
S8
(
S1
T ? (E?)E ? E?T
Z ? E?E ? E?T
i


)
E ? E?TT ? ?iT ? ?(E)

S3
S9
S2
T ? (E)?
Z ? E?
T
S4
E ? ET?
6
GOTO tablesymbol
ACTION table
7
LR(0) Parsing with a push-down automaton
IMPORT input token 1.. // from the lexical
analyzer SET Input token index TO 1SET
Reduction stack TO Empty stackPUSH Start State
ON Reduction stack While Reduction stack ?
Start state, Start symbol, End state
SET State TO Top of Reduction stack SET
Action TO Action table State IF Action
shift // Do a shift move
SET shifted token TO Input token
Input token index SET Input
token index TO Input token index 1 // shifted
PUSH Shifted token ON Reduction
stack Set New State TO Goto
table State, Shifted token .class
PUSH New state ON Reduction stack // can be
empty ELSE IF Action (reduce, N?a)
// Do a reduction move
Pop the symbols of a from the Reduction
stack SET State TO Top of
Reduction stack // update state
PUSH N ON Reduction stack SET
New state TO Goto table State, N
PUSH New State ON Reduction stack // cannot be
empty ELSE Action Empty
ERROR Error at token , Input token Input
token index
p. 162
8
LR(0) parsing of ii
Stack Input Action
S0 i i shift
S0 i S5 i reduce by T?i
S0 T S6 i reduce by E?T
S0 E S1 i shift
S0 E S1 S i shift
S0 E S1 S3 i S5 reduce by T?i
S0 E S1 S3 T S4 reduce by E?ET
S0 E S1 shift
S0 E S1 S2 reduce by Z?E
S0 Z stop
Write a Comment
User Comments (0)
About PowerShow.com