Docsity

Midterm Exam 1 Practice Problems for Compilers | CS 414, Exams of Computer Science

Toggle sidebar
bg1
Department of Computer Science University of San Francisco
Computer Science 414
Spring 2008
Mitderm 1 Practice Problems
We will go over solutions on Monday, March 10th
1. Describe the difference between the languages described by the following 3 regular expressions:
(a) a*b*
(b) ab*
(c) (ab)*
2. Give a regular expression for C comments. In C, anything between /* and */ is a comment. In C, com-
ments do not nest. So, /* this is a comment */ and /* This is also /* a legal C comment */
are both legal C comments, while /* this is /* not */ a legal C comment */ and /* this is
also */ not a legal C comment */ are not legal C comments. (HINT – this is harder than it
looks. Feel free to use the [^c] form, which matches any character except a c. Use escape character
for characters with meaning – \* for *, and so on.)
3. Give a Context-Free Grammar for each of the following langauges:
(a) The set of all strings over {(, ), [, ]}which form balanced parenthesis. That is, (), ()(), ((()())()),
[()()], and ([()[]()]) are all in the language, but )(, (() and (] are not in the language.
(b) The set of all strings over {num, +, -, *, /}which are legal binary post-fix expressions. Thus
num, num num +, num num num * -, and num num - num * are all in the language, while num
*, num * num, and num num num - are not in the language. Is your CFG ambiguous?
4. For the following CFGs, compute First and Follow sets for each non-terminal, and then create an LL(1)
parse table
(a)
Terminals = {for, to, id, :=, num, print}
Non-Terminals = {S, E}
Rules = (1) S→for id := Eto E S
(2) S→print E
(3) E→id
(4) E→num
Start Symbol = S
(b)
Terminals = {a, b, c, $}
Non-Terminals = {S, A, B}
Rules = (1) S→ABc$
(2) A→aA
(3) A→ǫ
(4) B→bB
(5) B→ǫ
Start Symbol = S
1
pf2

Partial preview of the text

Download Midterm Exam 1 Practice Problems for Compilers | CS 414 and more Exams Computer Science in PDF only on Docsity!

Department of Computer Science University of San Francisco

Computer Science 414 Spring 2008

Mitderm 1 Practice Problems

We will go over solutions on Monday, March 10th

  1. Describe the difference between the languages described by the following 3 regular expressions:

(a) ab (b) ab* (c) (ab)*

  1. Give a regular expression for C comments. In C, anything between /* and / is a comment. In C, com- ments do not nest. So, / this is a comment / and / This is also /* a legal C comment / are both legal C comments, while / this is /* not */ a legal C comment / and / this is also */ not a legal C comment */ are not legal C comments. (HINT – this is harder than it looks. Feel free to use the [^c] form, which matches any character except a c. Use escape character for characters with meaning – * for *, and so on.)
  2. Give a Context-Free Grammar for each of the following langauges:

(a) The set of all strings over {(, ), [, ]} which form balanced parenthesis. That is, (), ()(), ((()())()), [()()], and ([()]) are all in the language, but )(, (() and (] are not in the language. (b) The set of all strings over {num, +, -, *, /} which are legal binary post-fix expressions. Thus num, num num +, num num num * -, and num num - num * are all in the language, while num *, num * num, and num num num - are not in the language. Is your CFG ambiguous?

  1. For the following CFGs, compute First and Follow sets for each non-terminal, and then create an LL(1) parse table

(a) Terminals = {for, to, id, :=, num, print} Non-Terminals = {S, E} Rules = (1) S → for id := E to E S (2) S → print E (3) E → id (4) E → num Start Symbol = S (b) Terminals = {a, b, c, $} Non-Terminals = {S, A, B} Rules = (1) S → ABc$ (2) A → aA (3) A → ǫ (4) B → bB (5) B → ǫ Start Symbol = S

  1. Given the following CFG and LR parse table

Terminals = {num, *, +, (, ), $} Non-Terminals = {E′, E, T, F } Rules = (0) E′^ → E$ (1) E → E + T (2) E → T (3) T T * F (4) T → F (5) F → num (4) F → ( E ) Start Symbol = E′ num * + ( ) $ E T F 1 s5 s6 g2 g3 g 2 s8 s7 accept 3 s8 r(2) r(2) r(2) 4 r(4) r(4) r(4) r(4) 5 r(5) r(5) r(5) r(5) 6 s6 g11 g13 g 7 s5 s6 g10 g 8 s5 s6 g 9 r(3) r(3) r(3) r(3) 10 s8 r(1) r(1) r(1) 11 s7 s 12 r(6) r(6) r(6) r(6) Which of the following strings are successfully parsed? If a string is not successfully parsed, give the contents of the stack (both states an symbols) when parsing fails.

(a) num + num * num - num $ (b) num * num + + num $ (c) num + num * * num $ (d) num * ( num + num ) num $ (e) num + ((num * num)) $

  1. Create a set of LR(0) items and transitions for the following grammar. Show that the grammar is not LR(0). Give the SLR(1) parse table for the grammar. Terminals = {num, id, ., $} Non-Terminals = {E′, E, V } Rules = (0) E′^ → E$ (1) E → V (2) E → num (3) V → id (4) V → V. id Start Symbol = E′