Exam 1 Practice Problems - Programming Languages | CSCI 4430, Exams of Programming Languages

Material Type: Exam; Class: PROGRAMMING LANGUAGES; Subject: Computer Science; University: Rensselaer Polytechnic Institute; Term: Fall 2008;

Typology: Exams

2011/2012

Uploaded on 02/13/2012

koofers-user-icm-1
koofers-user-icm-1 ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Exam 1 Practice Problems
Posted Thursday, October 9, 2008 (Exam on Thursday, October 23)
I Regular Expressions.
P1. Consider the following regular expression which has been divided into three parts:
โˆ’ โˆ’ 1โˆ’ โˆ’ โˆ’ โˆ’ 2โˆ’ โˆ’ โˆ’ โˆ’ 3โˆ’ โˆ’
(a|b)โˆ—(a|c) (dโˆ—|cโˆ—)+
For each string below either write that it is not generated by this regular expression, or circle
and label the sections of each string generated by regular expression parts 1, 2, 3. For example:
a a a c d
โˆ’ โˆ’ 1โˆ’ โˆ’ โˆ’ โˆ’ 2โˆ’ โˆ’ โˆ’ โˆ’ 3โˆ’ โˆ’
โ€ข(i) b a d c
โ€ข(ii) bbbbbbc
โ€ข(iii) acdcda
โ€ข(iv) aaaaaaad
โ€ข(v) acaccc
โ€ข(vi) accccc
P2. 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) aaabbabb
โ€ข(ii) bbbaaababa
โ€ข(iii) a a a b
โ€ข(iv) bbaabaabb
โ€ข(v) bbaababababa
1
pf3
pf4

Partial preview of the text

Download Exam 1 Practice Problems - Programming Languages | CSCI 4430 and more Exams Programming Languages in PDF only on Docsity!

Exam 1 Practice Problems

Posted Thursday, October 9, 2008 (Exam on Thursday, October 23)

I Regular Expressions.

P1. Consider the following regular expression which has been divided into three parts:

โˆ’ โˆ’ 1 โˆ’ โˆ’ โˆ’ โˆ’ 2 โˆ’ โˆ’ โˆ’ โˆ’ 3 โˆ’ โˆ’ (a|b)โˆ—^ (a|c) (dโˆ—|cโˆ—)+ For each string below either write that it is not generated by this regular expression, or circle and label the sections of each string generated by regular expression parts 1, 2, 3. For example:

a a a c d โˆ’ โˆ’ 1 โˆ’ โˆ’ โˆ’ โˆ’ 2 โˆ’ โˆ’ โˆ’ โˆ’ 3 โˆ’ โˆ’

  • (i) b a d c
  • (ii) b b b b b b c
  • (iii) a c d c d a
  • (iv) a a a a a a a d
  • (v) a c a c c c
  • (vi) a c c c c c

P2. 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

1

II Context-free grammars, ambiguity and precedence.

P1. 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.
  • (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.

P1. 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.

VI Prolog.