Docsity

Practice Problem - Regular Expressions - Programming Languages | CSCI 4430, Exams of Programming Languages

Toggle sidebar
bg1
Practice Problems
I. Regular Expressions.
Problem 1. Consider the following regular expression divided into parts:
โˆ’ โˆ’ 1โˆ’ โˆ’ โˆ’ โˆ’ 2.1โˆ’ โˆ’ โˆ’ โˆ’ 2.2โˆ’ โˆ’ โˆ’ โˆ’ 2.3โˆ’ โˆ’ โˆ’ โˆ’ 2.4โˆ’ โˆ’
(aa|bb)โˆ—((ab|ba) (aa|bb)โˆ—(ab|ba) (aa|bb)โˆ—)โˆ—
For each string below either write that it is generated by the regular expression or that it is
not generated by it:
โ€ข(i) a a a b b a b b
โ€ข(ii) b b b a a a b a b a
โ€ข(iii) a a a b
โ€ข(iv) b b a a b a a b b
โ€ข(v) b b a a b a b a b a b a
II. Context-free grammars, ambiguity and precedence.
Problem 1. Consider the following grammar with two operators, # and $. Capital letters
denote nonterminals, and small letters denote terminals.
S::= B
B::= B#E|B$E|E
E::= B|a|b
โ€ข(i) This grammar is ambiguous. Using the shortest sequence of terminals possible, show
that the grammar is ambiguous.
โ€ข(ii) What is the precedence of # over $? Choose one (a) # has higher precedence than
$, (b) # has equal precedence with $, and (c) # has lower precedence than $.
โ€ข(iii) Give evidence to support your answer to part.
1
pf3
pf4
pf5

Partial preview of the text

Download Practice Problem - Regular Expressions - Programming Languages | CSCI 4430 and more Exams Programming Languages in PDF only on Docsity!

Practice Problems

I. Regular Expressions.

Problem 1. Consider the following regular expression divided into parts:

(aa|bb)โˆ—^ ((ab|ba) (aa|bb)โˆ—^ (ab|ba) (aa|bb)โˆ—)โˆ— For each string below either write that it is generated by the regular expression or that it is not generated by it:

  • (i) a a a b b a b b
  • (ii) b b b a a a b a b a
  • (iii) a a a b
  • (iv) b b a a b a a b b
  • (v) b b a a b a b a b a b a

II. Context-free grammars, ambiguity and precedence.

Problem 1. Consider the following grammar with two operators, # and $. Capital letters denote nonterminals, and small letters denote terminals.

S ::= B B ::= B#E | B$E | E E ::= B | a | b

  • (i) This grammar is ambiguous. Using the shortest sequence of terminals possible, show that the grammar is ambiguous.
  • (ii) What is the precedence of # over $? Choose one (a) # has higher precedence than $, (b) # has equal precedence with $, and (c) # has lower precedence than $.
  • (iii) Give evidence to support your answer to part.

1

  • (iv) Suppose that we wanted to add a unary operator % with lower precedence than #. Add parentheses to the following expression to show how it would be evaluated given these precedences % a # b
  • (v) Modify the original grammar so that it is not ambiguous making the fewest changes to rules possible.

III. LL and LR Grammars.

Problem 1. Consider the if-then-else grammar below. S and E denote nonterminals and i, t, e, b and a denote terminals:

S ::= iEtSeS | iEtS | a E ::= b

  • (i) Rewrite this grammar to eliminate the common prefix in the first two productions for S.
  • (ii) Construct the LL(1) parsing table for the grammar from (i). Show your FIRST and FOLLOW sets for relevant sequences of symbols.
  • (iii) This grammar is not LL(1). Is it possible to resolve the conflict(s) in such a way that the LL(1) parser will parse if-then-else statements normally (i.e., an else will be associated with the nearest then)? Justify your answer.
  • (ii) Construct the CFSM for the original grammar. Make sure that you show each state, fill in all items in the state, label all transitions, and write the necessary โ€Reduce by...โ€ labels on states.
  • (ii) For each state with a conflict list the state, the lookahead token and the type of conflict (i.e., shift-reduce, or reduce-reduce).
  • (iii) Draw the parse tree(s) for this expression: id + โˆ’id + id
  • (iv) Is this grammar ambiguous? Why or why not?

Suppose that we want the string โˆ’id + id + id to have only one possible rightmost derivation (call this property P ): S โ‡’ E โ‡’ E+E โ‡’ E+E+E โ‡’ E+E+id โ‡’ E+id+id โ‡’ โˆ’E+id+id โ‡’ โˆ’id+id+id

  • (v) Draw the parse tree for this derivation.
  • (vi) Describe in English the precedence and associativity needed to ensure property P.
  • (vii) For each conflict in the CFSM, explain how the conflict should be resolved to ensure property P (as a shift or as a reduce).