Вы находитесь на странице: 1из 10

1. Consider the following grammar: 1 E E +T 2 E T 3 T TF 4 TF 5 F F* 6 F a 7 F b Construct an SLR parsing table for this grammar. 2.

2. Construct an LR(1) parsing table for the grammar in question 1. 3. Show that the following grammar S AaAb | BbBa , A e , B e is LL(1) but not SLR(1). 4. Construct an SLR parsing table for the grammar R R bar R | RR | R star | ( R ) | a | b Terminal symbols are {a, b, (, ), star, bar} while nonterminal symbols is {R}. This denotes the language of regular expressions over the alphabet {a,b}. Use the usual precedence of the regular expression operators to guide the resolution of any conflicts. 5. Consider the grammar B B or T | T , T T and F | F , F not F | ( B ) | true | false Terminal symbols are {(,), true, false, and, or} and nonterminal symbols are {B, T, F}. Construct a parse tree for the sentence not (true or false). Show that this grammar generates all boolean expressions.

1/10

6. Grammar A S SaS | e Grammar D S Xc | Y X a Y a Grammar B S S | Sa | e Grammar E PS S a | bB B e | Se | S ; B Grammar C S Sa | a

For each grammar above, answer the following questions: 1). Is the grammar ambiguous? * If no, give a short explanation. * If yes, show an input that has 2 different parse trees and give an unambiguous grammar that accepts the same language. 2). Is the grammar LL(1) ? Provide a short explanation. For example, if the grammar is not LL(1), show 2 parse trees in which left-most derivations have the same prefix. 3). Is the grammar LR(0) ? * If no, show a conflicting state (a set of dotted items), and an input that is accepted by the grammar and reaches the conflicting state. Investigate the two possibilities to patch the parsing table, and their consequences. In both cases, show the parsing of your example input. * If yes, construct a parsing table. 4). Is the grammar SLR(1) ? * If no, show a conflicting state and an input that accepted by the grammar and reaches the conflicting state. Explain why FOLLOW sets do not solve the conflict. * If yes, construct the FOLLOW sets and the parsing table.

2/10

1. Consider the following grammar:

0 S E$ 1 E E +T 2 E T 3 T TF 4 TF 5 F F* 6 F a 7 F b Construct an SLR parsing table for this grammar. Answer: nullable First S no a, b E no a, b T no a, b F no a, b Start state is 0

Follow $ +, $ +, $, a, b *, +, $, a, b

LR(0) finite state machine for the above grammar SLR(1) parsing table 0 1 2 3 4 5 6 7 8 9 a s4 s4 r4 r6 r7 s4 r3 r5 s4 b s5 s5 r4 r6 r7 s5 r3 r5 s5 + s6 r2 r4 r6 r7 r3 r5 r1
3/10

$ acc r2 r4 r6 r7

E g1

T g2

F g3 g7

s8 r6 r7 s8 r5

g9 r3 r5 r1

g3

g7

2. Construct an LR(1) parsing table for the grammar in question 1. nullable no no no no First a, b a, b a, b a, b Follow $ +, $ +, $, a, b *, +, $, a, b

S E T F Start state is 0

LR(1) finite state machine for the above grammar a s4 s4 r4 r6 r7 s4 r3 r5 s4 b s5 s5 r4 r6 r7 s5 r3 r5 s5 + s6 r2 r4 r6 r7 r3 r5 r1 * $ acc r2 r4 r6 r7 g9 s8 r5 r3 r5 r1 E g1 T g2 F g3 g7

0 1 2 3 4 5 6 7 8 9

s8 r6 r7

g3

g7

4/10

3. Show that the following grammar S AaAb | BbBa , A e , B e is LL(1) but not SLR(1). The LOOK set of the production S AaAb is { a }, while the LOOK set of the production S BbBa is { b }, so at most one production of the nonterminal S is applicable for any lookahead. The nonterminals A and B have only one production each, so again there is a clear choice. Hence the grammar is LL(1). The set of items for the initial state of the SLR state machine is S ' .S S . AaAb S .BbBa A. B . The item A . suggests a reduction using A e when the lookahead token is in FOLLOW(A), while the item B . suggests a reduction using B e when the lookahead token is in FOLLOW(B). However, in this case FOLLOW(A) = { a, b } FOLLOW(B) = { a, b } Therefore this state has a reduce/reduce conflict between those two productions whenever the lookahead token is a or b.

5/10

4. Construct an SLR parsing table for the grammar R R bar R | RR | R star | ( R ) | a | b Terminal symbols are {a, b, (, ), star, bar} while nonterminal symbols is {R}. This denotes the language of regular expressions over the alphabet {a,b}. Use the usual precedence of the regular expression operators to guide the resolution of any conflicts. SR$, R R bar R | RR | R star | ( R ) | a | b nullable no no First (, a, b (, a, b Follow $ (, ), a, b, bar, star, $

S R Start state is 0

SLR(1) finite state machine for the above grammar


6/10

Parsing table ( s2 s2 s2 r5 r6 s2/r2 conflict s2 r3 s2 s2/r1 conflict r4 ) a s3 s3 s3 r5 r6 s3/r2 conflict s3 r3 s3 s3/r1 conflict r4 b s4 s4 s4 r5 r6 s4/r2 conflict s4 r3 s4 s4/r1 conflict r4 bar s6 r5 r6 r2/s6 conflict r3 s6 r1/s6 conflict r4 star s7 r5 r6 s7/r2 conflict r3 s7 s7/r1 conflict r4 $ accept r5 r6 r2 R g1 g5 g8

0 1 2 3 4 5 6 7 8 9 10

r5 r6 r2

g5 g9

r3 s10 r1 r4

r3 r1 r4 g5 g5

7/10

5. Consider the grammar B B or T | T , T T and F | F , F not F | ( B ) | true | false Terminal symbols are {(,), true, false, and, or} and nonterminal symbols are {B, T, F}. Construct a parse tree for the sentence not (true or false). Show that this grammar generates all boolean expressions. B T F not F not ( B) not ( B or T ) not ( B or F ) not (T or F ) not (T or F ) not (T or false) not ( F or false) not (true or false) To show the grammar generates all boolean expressions B T F true B T F false B T F not F not true B T F not F not false B T T and F T and false F and false true and false B B or T T or T F or T true or T true or F true or false B T F not F not B not T not T and F not F and F not true and F not true and false B T F not F not B not B or T not T or T not F or T not true or T not true or F not true or false

8/10

6. Grammar A S SaS | e Grammar B S S | Sa | e Grammar C S Sa | a

Grammar D Grammar E S Xc | Y PS S a | bB X a Y a B e | Se | S ; B For each grammar above, answer the following questions: 1). Is the grammar ambiguous? * If no, give a short explanation. * If yes, show an input that has 2 different parse trees and give an unambiguous grammar that accepts the same language. 2). Is the grammar LL(1) ? Provide a short explanation. For example, if the grammar is not LL(1), show 2 parse trees in which left-most derivations have the same prefix. 3). Is the grammar LR(0) ? * If no, show a conflicting state (a set of dotted items), and an input that is accepted by the grammar and reaches the conflicting state. Investigate the two possibilities to patch the parsing table, and their consequences. In both cases, show the parsing of your example input. * If yes, construct a parsing table. 4). Is the grammar SLR(1) ? * If no, show a conflicting state and an input that accepted by the grammar and reaches the conflicting state. Explain why FOLLOW sets do not solve the conflict. * If yes, construct the FOLLOW sets and the parsing table. Grammar A Grammar B S SaS | e S S | Sa | e 1). ambiguous, input: aa 1). ambiguous, because of the production S -> S 2). not LL(1), inputs: aa, aaa 2). not LL(1), inputs: aa, aaa 3). not LR(0), shift/reduce conflict : 3). not LR(0), shift/reduce conflict: S SaS g and S S gaS S S g and S S ga 4). not SLR(1), FOLLOW(S) = { a } 4). not SLR(1) FOLLOW(S) = {a} Grammar C S Sa | a Grammar D S Xc | Y X a Y a 1). non-ambiguous 2). not LL(1), inputs: ac, a 3). not LR(0), reduce/reduce conflict X a g and Y a g 4). SLR(1), FOLLOW(X) = {c} FOLLOW(Y) = {}

1). non-ambiguous 2). not LL(1), inputs: aa, aaa 3). LR(0) 4). SLR(1) because it is LR(0)

9/10

Grammar E PS S a | bB B e | Se | S ; B 1). non-ambiguous 2). not LL(1) 3). LR(0) dfa 4). SLR(1) because LR(0)

10/10

Вам также может понравиться