Syntax Analyzer Exercises Example 4 5 Write The

  • Slides: 20
Download presentation
Syntax Analyzer Exercises

Syntax Analyzer Exercises

Example 4. 5 • Write The grammar that defines simple arithmetic expressions. • In

Example 4. 5 • Write The grammar that defines simple arithmetic expressions. • In this grammar, the terminal symbols are : Id + - * / ( ) • The nonterminal symbols are expression, term and factor, and expression is the start symbol • Answer: • or

Exercise 4. 2. 1 Consider the context-free grammar: S -> S S + |

Exercise 4. 2. 1 Consider the context-free grammar: S -> S S + | S S * | a and the string aa + a*. a) Give a leftmost derivation for the string. b) Give a rightmost derivation for the string. c) Give a parse tree for the string. ! d) Is the grammar ambiguous or unambiguous? Justify your answer. ! e) Describe the language generated by this grammar.

Exercise 4. 2. 1 answer a. S SS* SS+S* aa+a* b. S SS* Sa*

Exercise 4. 2. 1 answer a. S SS* SS+S* aa+a* b. S SS* Sa* SS+a* Sa+a* aa+a* c. d. Unambiguous e. The set of all postfix expressions consist of addition and multiplication

Exercise 4. 2. 2. a Repeat Exercise 4. 2. 1 for the following grammar

Exercise 4. 2. 2. a Repeat Exercise 4. 2. 1 for the following grammar and string: S -> 0 S 1 | 0 1 with string 000111. • S 0 S 1 00 S 11 000111 • Parse tree? • Unambiguous • The set of all strings of 0 s and followed by an equal number of 1 s

Exercise 4. 2. 2. b Repeat Exercise 4. 2. 1 for the following grammar

Exercise 4. 2. 2. b Repeat Exercise 4. 2. 1 for the following grammar and string: S -> + S S | * S S | a with string + * aaa. • S +SS +*SSS +*aa. S +*aaa • S +Sa +*SSa +*Saa +*aaa • Parse tree? • Unambiguous • The set of all prefix expressions consist of addition and multiplication.

Exercise 4. 2. 2. c Repeat Exercise 4. 2. 1 for the following grammar

Exercise 4. 2. 2. c Repeat Exercise 4. 2. 1 for the following grammar and string: S -> S + S | S S | (S) | S * | a with string (a+a)*a • S S*S (S)*S (S+S)*S (a+a)*S (a+a)*a • S Sa S*a (S)*a (S+a)*a (a+a)*a • Parse tree? • Ambiguous

Exercise 4. 2. 2. d Repeat Exercise 4. 2. 1 for the following grammar

Exercise 4. 2. 2. d Repeat Exercise 4. 2. 1 for the following grammar and string: S -> a S b S | b S a S | ε with string aabbab • S a. Sb. S aabba. Sb. S aabbab • S a. Sba. Sb a. Sbab aa. Sbbab aabbab • Parse tree? • Ambiguous • The set of all strings of 'a's and 'b's of the equal number of 'a's and 'b's

Exercise 4. 2. 2. e Repeat Exercise 4. 2. 1 for the following grammar

Exercise 4. 2. 2. e Repeat Exercise 4. 2. 1 for the following grammar and string: S -> (L) | a and L -> L, S | S with string ((a, a), a, (a)) • S (L) (L, S, S) ((S), S, S) ((L, S), S, S) ((S, S), S, S) ((a, a), a, (L)) ((a, a), a, (S)) ((a, a), a, (a)) • S (L) (L, S) (L, (L)) (L, (a)) (L, S, (a)) (L, a, (a)) (S, a, (a)) ((L), a, (a)) ((L, S), a, (a)) ((S, a), a, (a)) ((a, a), a, (a)) • Parse tree ? ? • Unambiguous

Exercise 4. 2. 2. f Repeat Exercise 4. 2. 1 for the following grammar

Exercise 4. 2. 2. f Repeat Exercise 4. 2. 1 for the following grammar and string: With string id+id*id a. E E+E id+E*E id+id*id or E E*E E+E*E id+id*E id+id*id E E id b. E E+E*E E+E*id E+id*id id+id*id or E E*id E+E*id E+id*id id+id*id c. Ambiguous d. Parse tree e. The set of all arthimatic expression consist of addition , subtraction, multiplication and division. + E E * id or E E id + E id E * E E id id 10

Example Consider the context-free grammar: stmt if expr then stmt | if expr then

Example Consider the context-free grammar: stmt if expr then stmt | if expr then stmt else stmt | otherstmts and the string if E 1 then if E 2 then S 1 else S 2 Give a parse tree for the string. Is the grammar ambiguous or unambiguous? ambiguous

Example • Rewrite the following grammar to be disambiguated according to the precedence and

Example • Rewrite the following grammar to be disambiguated according to the precedence and associativity rules. • E E+E | E*E | E^E | id | (E) • Answer: E E+T | T T T*F | F F G^F | G G id | (E)

Example • Eliminate left recursion of the following expression: E E+T | T ;

Example • Eliminate left recursion of the following expression: E E+T | T ; A = E , α = +T , β = T, T T*F | F ; A = T , α = *F , β = F F id | (E) E T E’ E’ +T E’ | T F T’ T’ *F T’ | F id | (E)

Exercise 4. 3. 1 • The following is a grammar for regular expressions over

Exercise 4. 3. 1 • The following is a grammar for regular expressions over symbols a and b only, eliminate left recursion from the original law: • E-> E + T | T ; A = E , α = +T , β = T, • T -> T F | F ; A = T , α = F , β = F • F -> F * | P ; A = F , α = *, β = P • P -> a | b • • E -> T E’ E' -> + T E’ | ε T -> F T’ T' -> F T’ | ε F -> P F’ F -> * F’ | ε P -> a | b

Exercise 4. 3. 2. a Repeat Exercise 4. 3. 1 for the following grammar

Exercise 4. 3. 2. a Repeat Exercise 4. 3. 1 for the following grammar : • S -> S S + | S S * | a ; A = S , α 1 = S+, α 2 = S*, β = a • S -> a S’ • S’ -> S+S’ | S*S’ | ε

Exercise 4. 3. 2. b Repeat Exercise 4. 2. 1 for the following grammar:

Exercise 4. 3. 2. b Repeat Exercise 4. 2. 1 for the following grammar: • S -> 0 S 1 | 0 1 ; A = S , α = 0, β 1 = S 1, β 2 = 1 • S -> 0 S’ • S’ -> S 1 | 1 • S -> 0 S’ • S’ -> 0 S’ 1 | 1

Exercise 4. 3. 2. c Repeat Exercise 4. 2. 1 for the following grammar:

Exercise 4. 3. 2. c Repeat Exercise 4. 2. 1 for the following grammar: S Aa | b A Ac | Sd | Replace A Sd with A Aad | bd A Ac | Aad | bd | ; A = A , α 1 = c, α 2 = ad, β 1 = bd, β 2 = S Aa | b A bd. A’ | A’ A’ c. A’ | ad. A’ |

Example 4. 22 Eliminate left factor of the following grammar which abstracts the "dangling-else"

Example 4. 22 Eliminate left factor of the following grammar which abstracts the "dangling-else" problem: S -> i E t S| i E t S e S | a E -> b • Here, i, t, and e stand for if, then, and else; E and S stand for "conditional expression" and "statement. " Left-factored, this grammar becomes: • α = i E t S , β 1 = ε, β 2 = e S • S -> i E t S S’ • S’ = ε | e S • E -> b

Example 1 Eliminate left factor of the following grammar : A ab. B |

Example 1 Eliminate left factor of the following grammar : A ab. B | a. B | cdg | cde. B | cdf. B A a. A’ | cdg | cde. B | cdf. B A’ b. B | B A a. A’ | cd. A’’ A’ b. B | B A’’ g | e. B | f. B 19

Example 2 Eliminate left factor of the following grammar : A ad | abc

Example 2 Eliminate left factor of the following grammar : A ad | abc | b A a. A’ | b A’ d | | bc A a. A’ | b A’ d | | b. A’’ | c 20