Tải bản đầy đủ (.pdf) (65 trang)

slike bài giảng môn chương trình dịch chương 4 syntax analysis

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (168.56 KB, 65 trang )

Syntax Analysis II
Quan Thanh Tho

LR Parsing
• The most general parsing technique
• Usually known as LR(k) parsing technique
–Left-to-right scanning
–Rightmost derivation detected in reverse
–k
lookahead tokens, (pratically k ≤ 1)
Faculty of CSE Syntax Analysis II 2
LR Parsing Techniques
• Simple LR (SLR): easy to implement but
not practically powerful
• Canonical LR: powerful but expensive in
terms of computation
• Lookahead LR (LALR): reasonably
powerful and expensive
Faculty of CSE Syntax Analysis II 3
LR Parsing Algorithm
LR
Parsing
Program
Input
a+b$
Stack
Output
Grammatically
correct?
Which
productions


involved?
s
m
X
m
s
m-1
action goto
Faculty of CSE Syntax Analysis II 4
Stack
• Stack-type data structure
• Store a string of the form s
0
X
1
s
1
X
2
…X
m
s
m
where s
m
is on the top
•X
i
: a grammar symbol
•s

i
: a
state
• When implemented, grammar symbols are
not necessarily stored in the stack
•X
1
X
2
…X
m
î a part of a sentential form
corresponding to the
scanned part of input
Faculty of CSE Syntax Analysis II 5
Action – Goto Table
• Rows: States
• Columns: grammar symbols, consisting of two
parts:
action (for terminals) and goto (for
nonterminal)
• action[s
m
,a
i
]
– shift
s, where s is a state
– reduce by a rule A å β
– accept

– error
• goto[s
m
,A
i
]ås
n
Faculty of CSE Syntax Analysis II 6
Input
• Similar to that of LL(1) table-driven
predictive parser
• When parsed, the contents of input imply
the unexpended part of the initial input
string
Faculty of CSE Syntax Analysis II 7
Example 1
0
1
2
3
4
5
s3 1 2
+ id $ E T
s4
a 5
r2 r2
r3 r3
s3 5
r1 r1

(1) EåE+T
(2) EåT
(3) Tåid
Faculty of CSE Syntax Analysis II 8
Configuration
• Configuration = stack contents +
unexpended input
• A configuration represents a sentential
form
(s
0
X
1
s
1
X
2
…X
m
s
m
,a
i
a
i+1
…a
n
$)
úX
1

X
2
…X
m
a
i
a
i+1
…a
n
Faculty of CSE Syntax Analysis II 9
LR Parsing Operational
Mechanism
• Initial configuration: (0, a
1
…a
n
$))
• Moving from the current configuration to a new
one until reaching
accept or error status
• action[s
m
,a
i
] = shift
s
(s
0
X

1
s
1
X
2
…X
m
s
m
,a
i
a
i+1
…a
n
$) å
(s
0
X
1
s
1
X
2
…X
m
s
m
a
i

s,a
i+1
…a
n
$)
• action[s
m
,a
i
] = reduce
Aåβ
(s
0
X
1
s
1
X
2
…X
m
s
m
,a
i
a
i+1
…a
n
$) å

(s
0
X
1
s
1
X
2

X
m-r
s
m-r
As, a
i
a
i+1
…a
n
$)
where
r = |β|, goto[s
m-r
,A] = s
Faculty of CSE Syntax Analysis II 10
Example 2
(1)Eå
E+T
(2) EåT
(3) Tå

id
Stack
0
0 id 3
0 T 2
0 E 1
0 E 1 + 4
0 E 1 + 4 id 3
0 E 1 + 4 T 5
0 E 1
Input
id+id$
+id$
+id$
+id$
id$
$
$
$
Faculty of CSE Syntax Analysis II 11
0
1
2
3
4
5
s3 1 2
+ id $ E T
s4
a 5

r2 r2
r3 r3
s3 5
r1 r1
Rule
T→
id
E →
T
T →
id
E →
E+T
accept
id + id ←
T + id ← E+ id ← E + T ← E
Example 3
(1)Eå
E+T
(2) EåT
(3) Tå
id
Faculty of CSE Syntax Analysis II 12
0
1
2
3
4
5
s3 1 2

+ id $ E T
s4
a 5
r2 r2
r3 r3
s3 5
r1 r1
Stack
0
0 id 3
0 T 2
0 E 1
0 E 1 + 4
Input
id+$
+$
+$
+$
$
Rule
T→id
E →
T
error
Hands-on Exercise
• Give the moves made by the LR parser on
input id+id+id
Faculty of CSE Syntax Analysis II 13
In-depth Operational Mechanism
Analysis

• Shift: get one token from scanner
• Reduce: Generate a new
sentential form in
order to construct a rightmost derivation
in
reverse
– When should we reduce by Aåα?
• When we have α on the stack and the input token
in
Follow(A)
– When should we shift?
• When we have the current configuration of
(s
0
X
1
s
1
X
2
…X
m
s
m
,a
i
a
i+1
…a
n

$) and βa
i
apprears on RHS of a
production (why?) where β = X
k
X
k+1
…X
m
Faculty of CSE Syntax Analysis II 14
In-depth Operational Mechanism
Analysis (cont’d)
• How can we keep track of α and β on the
stack
– Re-check the grammar whenever the stack
contents are changed å high computational
cost
• How can we manage many instances of α
and β that may occur concurrently
Faculty of CSE Syntax Analysis II 15
Example 4
• Given configuration (E+T,+id$)
– α
1
= T, α
2
= E+T
Faculty of CSE Syntax Analysis II 16
Item Set
•LR(

0) item (item for short) = production +
dot at the RHS
• Item set = set of items
–A å XYZ
–A å
•XYZ
–A å X
• YZ
–A å XY
• Z
–A å XYZ

Faculty of CSE Syntax Analysis II 17
Closure Operation
• An item corresponds to a
track of a
production currently applied
• Problem: there may be multiple tracks co-
existing
E å E+ • T
T å • id
• Solution: Closure operation introduced
Faculty of CSE Syntax Analysis II 18
Closure Operation (cont’d)
• Closure(I): find all of tracks (items)
possibly inferred from a set of items I
– Add all items in I to closure(I)
–If Aå α
•Bβ in I and B å γ is a production, add
Bå •γ to closure(I)

Faculty of CSE Syntax Analysis II 19
Example 5
•I:
EåE+•T
EåT•
• closure(I)
EåE+•T
EåT•
Tå•id
Faculty of CSE Syntax Analysis II 20
Goto Operation
• How to follow tracks in an item set, given a new
input symbol
a?
–Aåα
•aβ : possible to move on
–Aåα
•bβ : impossible to move on
• When a track is going further, that may cause
other tracks to do so
EåE+•T
EåT•
Tå•id
current input symbol: id

•id is moveable, meaning so is EåE+•T
Faculty of CSE Syntax Analysis II 21
Goto Operation (cont’d)
• Goto operation:
goto(I,X)= closure([Aåα X •β]) where Aåα•Xβ

is an item in I
Faculty of CSE Syntax Analysis II 22
Example 6
•I:
EåE+•T
EåT•
Tå•id
• goto(I,T)
EåE+•T
• goto(I,id)
Tåid•
EåE+T•
Faculty of CSE Syntax Analysis II 23
Augmented Grammar
• When would we know that we have
reached the
root node of the parse tree?
– When E å E+T• is caught?
• Augmented Grammar:
– Add
S’åS to the original grammar
– When
S’åS• reached, we are done
Faculty of CSE Syntax Analysis II 24
Kernel Items
• Kernel Items: S’å•S and all items whose
dots are
not at the left end
• Nonkernel items:
other else (generate by

applying closure on kernel items)
Faculty of CSE Syntax Analysis II 25

×