Chapter 4. Syntax Analysis (2) - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 4. Syntax Analysis (2)

Description:

... of equal precedence (they may in fact be the same operator), then make ? ... that id id; but, in fact, no precedence relation holds between id and id. ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 25
Provided by: borameCs
Category:

less

Transcript and Presenter's Notes

Title: Chapter 4. Syntax Analysis (2)


1
Chapter 4. Syntax Analysis (2)
2
Fig. 4.23. Operator-precedence relations.
id
id gt gt gt
lt gt lt gt
lt gt gt gt
lt lt lt
3
Operator-Precedence Relations from Associativity
and Precedence (1/2)
  • 1. If operator ?1 has higher precedence than
    operator ?2, make ?1 gt ?2 and ?2 lt ?1 . For
    example, if has higher precedence than , make
    gt and lt . These relations ensure that,
    in an expression of the form EEEE, the central
    EE is the handle that will be reduced first.
  • 2. If ?1 and ?2 are operators of equal
    precedence (they may in fact be the same
    operator), then make ?1 gt ?2 and ?2 gt ?1 if the
    operators are left-associative, or make ?1 lt ?2
    and ?2 lt ?1 if they are right-associative. For
    example, if and are left-associative, then
    make gt , gt -, - gt - and - gt . If ? is
    right associative, then make ? lt ?. These
    relations ensure that E-EE will have handle E-E
    selected and E?E?E will have the last E?E
    selected.

4
Operator-Precedence Relations from Associativity
and Precedence (2/2)
  • 3. Make ? lt id, id gt ?, ? lt (, ( lt ? , ) gt
    ?, ? gt ), ? gt , and lt ? for all operators
    ?. Also, let
  • These rules ensure that both id and (E) will be
    reduced to E. Also, serves as both the left and
    right endmarker, causing handles to be found
    between s wherever possible.


( ) lt ( lt id
( lt ( id gt ) gt
( lt id id gt ) ) gt )
5
- / ? id ( )
gt gt lt lt lt lt lt gt gt
- gt gt lt lt lt lt lt gt gt
gt gt gt gt lt lt lt gt gt
/ gt gt gt gt lt lt lt gt gt
? gt gt gt gt lt lt lt gt gt
id gt gt gt gt gt gt gt
( lt lt lt lt lt lt lt
) gt gt gt gt gt gt gt
lt lt lt lt lt lt lt

Fig. 4.25. Operator-precedence relations.
6
Precedence Functions
  • Example 4.29 The Precedence table of Fig. 4.25
    has the following pair of precedence functions,
  • For example, lt id and, f() lt g(id). Note
    that f(id) gt g(id) suggests that id gt id but,
    in fact, no precedence relation holds between id
    and id. Other error entries in Fig. 4.25 are
    similarly replaced by one or another precedence
    relation.

- / ? ( ) id
f 2 2 4 4 4 0 6 6 0
g 1 1 3 3 5 5 0 5 0
7
Fig. 4.26. Graph representing precedence
functions.
8
Fig. 4.28. Operator-precedence matrix with error
entries
id ( )
id e3 e3 gt gt
( lt lt e4
) e3 e3 gt gt
lt lt e2 e1

9
Fig. 4.29. Model of an LR parser.
10
STATE action action action action action action goto goto 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
Fig. 4.31. Parsing table for expression grammar.
11
Fig. 4.32. Moves of LR parser on id id id.
STACK STACK INPUT ACTION
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) (12) (13) (14) 0 0 id 5 0 F 5 0 T 2 0 T 2 7 0 T 2 7 id 5 0 T 2 7 F 10 0 T 2 0 E 1 0 E 1 6 0 E 1 6 id 5 0 E 1 6 F 3 0 E 1 6 T 9 0 E 1 id id id id id id id id id id id id id id id id shift reduced by F ? id reduced by T ? F shift shift reduced by F ? id reduced by T ? TF reduced by E ? T shift shift reduced by F ? id reduced by T ? F E ? E T accept
12
Fig. 4.35. Canonical LR(0) collection for
grammar (4.19)
I0 E' ? E E ? E T E ? T T ? T F T ? F F ? (E) F ? id I5 F ? id
I0 E' ? E E ? E T E ? T T ? T F T ? F F ? (E) F ? id I6 E ? E T T ? T F T ? F F ? (E) F ? id
I0 E' ? E E ? E T E ? T T ? T F T ? F F ? (E) F ? id I7 T ? T F F ? (E) F ? id
I1 E' ? E E ? E T I7 T ? T F F ? (E) F ? id
I2 E ? T T ? T F I8 F ? ( E ) E ? E T
I2 E ? T T ? T F I9 E ? E T T ? T F
I3 T ? F I9 E ? E T T ? T F
I4 F ? ( E ) E ? E T E ? T T ? T F T ? F F ? (E) F ? id I9 E ? E T T ? T F
I4 F ? ( E ) E ? E T E ? T T ? T F T ? F F ? (E) F ? id I10 T ? T F
I4 F ? ( E ) E ? E T E ? T T ? T F T ? F F ? (E) F ? id I11 F ? ( E )
13
Fig. 4.36. Transition diagram of DFA D form
viable prefixes.
14
Fig. 4.37. Canonical LR(0) collection for grammar
(4.20).
I0 S' ? S S ? L R S ? R L ? R L ? id R ? L I5 L ? id
I0 S' ? S S ? L R S ? R L ? R L ? id R ? L I6 S ? L R R ? L L ? R L ? id
I1 S' ? S I7 L ? R
I2 S ? L R R ? L I8 R ? L
I2 S ? L R R ? L I9 S ? L R
I3 S ? R I9 S ? L R
I4 L ? R R ? L L ? R L ? id I9 S ? L R
I4 L ? R R ? L L ? R L ? id
I4 L ? R R ? L L ? R L ? id
15
Fig. 4.39. The goto graph for grammar (4.21).
16
STATE action action action goto goto
STATE c d S C
0 s3 s4 1 2
1 acc
2 s6 s7 5
3 s3 s4 8
4 r3 r3
5 r1
6 s6 s7 9
7 r3
8 r2 r2
9 r2
Fig. 4.40. Canonical parsing table for grammar
(4.21).
17
Constructing LALR Parsing Table
  • Example 4.44. Consider the grammar
  • S' ? S
  • S ? aAd bBd aBe bAe
  • A ? c
  • B ? c
  • which generates the four strings acd, ace,
    bcd, and bce. The reader can check that the
    grammar is LR(1) by constructing the sets of
    items. Upon doing so, we find the set of items
    A ? c , e , B ? c , e valid for
    viable prefix ac and A ? c , e , B ? c
    , d valid for bc. Neither of these sets
    generates a conflict, and their cores are the
    same. However, their union, which is
  • A ? c , d / e
  • B ? c , d / e
  • generates a reduce/reduce conflict, since
    reductions by both A ? c and B ? c are called for
    on inputs d and e.

18
STATE action action action goto goto
STATE c d S C
0 s36 s47 1 2
1 acc
2 s36 s47 5
36 s36 s47 89
47 r3 r3 r3
5 r1
89 r2 r2 r2
Fig. 4.41. LALR parsing table for grammar (4.21).
19
Efficient Construction of LALR Parsing Tables
  • Example 4.46. Let us again consider the augmented
    grammar
  • S' ? S
  • S ? L R R
  • A ? R id
  • B ? L
  • The kernels of the sets of LR(0) items for this
    grammar are shown in Fig. 4.42.

I0 S' ? S
I1 S' ? S
I2 S ? L R R ? L
I3 S ? R
I4 L ? R
I5 L ? id
I6 S ? L R
I7 L ? R
I8 R ? L
I9 S ? L R
Fig. 4.42. Kernels of the sets of LR(0) items for
grammar (4.20).
20
Efficient Construction of LALR Parsing Tables
  • Example 4.47. Let us construct the kernels of the
    LALR(1) items for the grammar in the previous
    example. The kernels of the LR(0) items were
    shown in Fig. 4.42. When we apply Algorithm 4.12
    to the kernel of set of items I0, we compute
    closure (S' ? S, ), which is
  • S' ? S,
  • S ? L R,
  • S ? R,
  • L ? R, /
  • L ? id, /
  • R ? L,

21
Fig.4.44. Propagation of lookaheads.
FROM FROM TO TO
I0 S' ? S I1 I2 I2 I3 I4 I5 S' ? S S ? L R R ? L S ? R L ? R L ? id
I2 S ? L R I6 S ? L R
I4 L ? R I4 I5 I7 I8 L ? R L ? id L ? R R ? L
I6 S ? L R I4 I5 I8 I9 L ? R L ? R R ? L S ? L R
22
Fig. 4.45. Computation of lookaheads.
SET ITEM LOOKAHEADS LOOKAHEADS LOOKAHEADS LOOKAHEADS
SET ITEM INIT PASS1 PASS2 PASS3
I0 S' ? S
I1 S' ? S
I2 S ? L R
I2 R? L
I3 S ? R
I4 L ? R / / /
I5 L ? id / / /
I6 S ? L R
I7 L ? R / /
I8 R ? L / /
I9 S ? L R
23
Fig. 4.46. Set of LR(0) items for augmented
grammar (4.22).
I0 E' ? E E ? E E E ? E E E ? ( E ) E ? id I5 E ? E E E ? E E E ? E E E ? ( E ) E ? id
I1 E' ? E E' ? E E E' ? E E I6 E' ? ( E ) E' ? E E E' ? E E
I2 E ? ( E ) E ? E E E ? E E E ? ( E ) E ? id I7 E' ? E E E' ? E E E' ? E E
I2 E ? ( E ) E ? E E E ? E E E ? ( E ) E ? id I8 E' ? E E E' ? E E E' ? E E
I3 E ? id I8 E' ? E E E' ? E E E' ? E E
I4 E ? E E E ? E E E ? E E E ? ( E ) E ? id I9 E' ? ( E )
24
STATE action action action action action action goto
STATE id ( ) E
0 s3 s2 1
1 s4 s5 acc
2 s3 s2 6
3 r4 r4 r4 r4
4 s3 s2 8
5 s3 s2 8
6 s4 s5 s9
7 r1 s5 r1 r1
8 r2 r2 r2 r2
9 r3 r3 r3 r3
Fig. 4.47. Parsing table for grammar (4.22).
Write a Comment
User Comments (0)
About PowerShow.com