Download presentation
Presentation is loading. Please wait.
1
Chapter 4. Syntax Analysis (2)
2
Fig. 4.23. Operator-precedence relations.
id + * $ ·> <· Fig Operator-precedence relations.
3
Operator-Precedence Relations from Associativity and Precedence (1/2)
1. If operator θ1 has higher precedence than operator θ2, make θ1 ·> θ2 and θ2 <· θ1 . For example, if * has higher precedence than +, make * ·> + and + <· *. These relations ensure that, in an expression of the form E+E*E+E, the central E*E 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 ·> θ2 and θ2 ·> θ1 if the operators are left-associative, or make θ1 <· θ2 and θ2 <· θ1 if they are right-associative. For example, if + and – are left-associative, then make + ·> +, + ·> -, - ·> - and - ·> +. If is right associative, then make <· . These relations ensure that E-E+E will have handle E-E selected and EEE will have the last EE selected.
4
Operator-Precedence Relations from Associativity and Precedence (2/2)
3. Make θ <· id, id ·> θ, θ <· (, ( <· θ , ) ·> θ, θ ·> ), θ ·> $, and $ <· θ 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. ( = ) $ <· ( $ <· id ( <· ( id ·> $ ) ·> $ ( <· id id ·> ) ) ·> )
5
Fig. 4.25. Operator-precedence relations.
+ - * / ↑ id ( ) $ ·> <· = Fig Operator-precedence relations.
6
Precedence Functions Example 4.29 The Precedence table of Fig has the following pair of precedence functions, For example, * <· id and, f(*) < g(id). Note that f(id) > g(id) suggests that id ·> id; but, in fact, no precedence relation holds between id and id. Other error entries in Fig are similarly replaced by one or another precedence relation. + - * / ↑ ( ) id $ f 2 4 6 g 1 3 5
7
Fig. 4.26. Graph representing precedence functions.
8
Fig. 4.28. Operator-precedence matrix with error entries
id ( ) $ e3 ·> <· = e4 e2 e1 Fig Operator-precedence matrix with error entries
9
Fig. 4.29. Model of an LR parser.
10
Fig. 4.31. Parsing table for expression grammar.
STATE action goto id + * ( ) $ E T F s5 s4 1 2 3 s6 acc r2 s7 r4 4 8 5 r6 6 9 7 10 s11 r1 r3 11 r5 Fig Parsing table for expression grammar.
11
Fig. 4.32. Moves of LR parser on id * id + id.
STACK INPUT ACTION (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) (12) (13) (14) 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 E 1 0 E 1 + 6 0 E id 5 0 E F 3 0 E T 9 id * id + id$ * id + id$ id + id$ + id$ id$ $ shift reduced by F id reduced by T F reduced by T T*F reduced by E T E E + T accept Fig Moves of LR parser on id * id + id.
12
Fig. 4.35. Canonical LR(0) collection for grammar (4.19)
E' · E E · E + T E · T T · T * F T · F F · (E) F · id I5: F id · I6: E E + · T I7: T T * · F I1: E' E · E E · + T I2: E T · T T · * F I8: F ( E · ) I9: E E + T · I3: T F · I4: F (· E ) I10: T T * F · I11: F ( E ) · Fig Canonical LR(0) collection for grammar (4.19)
13
Fig. 4.36. Transition diagram of DFA D form viable prefixes.
14
Fig. 4.37. Canonical LR(0) collection for grammar (4.20).
S' · S S · L = R S · R L · * R L · id R · L I5: L id · I6: S L = · R I1: S' S · I7: L * R · I2: S L · = R R L · I8: I9: S L = R · I3: S R · I4: L * · R Fig Canonical LR(0) collection for grammar (4.20).
15
Fig. 4.39. The goto graph for grammar (4.21).
16
Fig. 4.40. Canonical parsing table for grammar (4.21).
STATE action goto c d $ S C s3 s4 1 2 acc s6 s7 5 3 8 4 r3 r1 6 9 7 r2 Fig Canonical parsing table for grammar (4.21).
17
Constructing LALR Parsing Table
Example 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
Fig. 4.41. LALR parsing table for grammar (4.21).
STATE action goto c d $ S C s36 s47 1 2 acc 5 36 89 47 r3 r1 r2 Fig LALR parsing table for grammar (4.21).
19
Efficient Construction of LALR Parsing Tables
Example 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 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 Kernels of the sets of LR(0) items for grammar (4.20).
20
Efficient Construction of LALR Parsing Tables
Example 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 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 TO I0: S' · S I1: I2: I3: I4: I5: S' S · S L · = R R L · S R · L * · R L id · I6: S L = · R I7: I8: L * R · I9: S L = R · Fig Propagation of lookaheads.
22
Fig. 4.45. Computation of lookaheads.
SET ITEM LOOKAHEADS INIT PASS1 PASS2 PASS3 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 Computation of lookaheads.
23
Fig. 4.46. Set of LR(0) items for augmented grammar (4.22).
E' · E E · E + E E · E * E E · ( E ) E · id I5: E E * · E I1: E' E · E' E · + E E' E · * E I6: E' ( E · ) I2: E ( ·E ) I7: E' E + E · I8: E' E * E · I3: E id · I4: E E + · E I9: E' ( E ) · Fig Set of LR(0) items for augmented grammar (4.22).
24
Fig. 4.47. Parsing table for grammar (4.22).
STATE action goto id + * ( ) $ E s3 s2 1 s4 s5 acc 2 6 3 r4 4 8 5 s9 7 r1 r2 9 r3 Fig Parsing table for grammar (4.22).
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.