Docsity

Midterm Exam Questions - Introduction to Compilers | CMSC 430, Exams of Computer Science

Toggle sidebar
bg1
CMSC 430 Midterm, Section 0101, October 22, 1996
Consider the following grammar G:
S
!
AS
j
A
!
aA
j
b
1 [40]. If G is a grammar of the following type, give the appropriate parsing tables; if not, explain whynot.
(Note: Remember to give parsing tables if the grammar is of the indicated type. A yes/no answer is not
sucient.)
(a) LL(1)
(b) LR(0)
(c) SLR(1)
(d) LR(1)
(e) LALR(1)
2 [10]. Show that the language generated by G is a regular language.
3 [10]. What are the characteristics of an SLR(0) grammar? In particular, compare SLR(0) grammars to
one or more of the classes of grammars that wehave already studied (e.g., the 5 classes asked for in question
1above). Explain your answer.
4 [8]. Give the prex and postx for each of the following:
(a) (a+b)/(c+d)+(e+f )
(b) a/b/c+d+e
5 [10]. Consider the regular expression: (0
?
1)
?
(0
?
1)
Give the minimal state DFA that accepts this set.
6 [12]. Attribute grammars:
(a)InYACC, rules like:
Expr : Expr '+' Term
f
$$ = $1 + $3
g
represent (inherited, synthesized) [pick one] attributes.
(b) WhydoesYACC use an LR or LALR parsing algorithm rather than an LL parsing algorithm?
Explain this relative to the question of using attribute grammars.
(c) Could a tool somewhat likeYACC be developed that used a precedence parsing algorithm? Explain.
(d) Assume you have an attribute
code
that output the postx for a nonterminal, and an attribute
type
that gavethetype of data represented by a nonterminal (either
integer
or
real
). Give a possible denition
for
E:code
in the production of part (a) that allows for coersion of integer to real data.
7 [10]. Answer or explain each of the following statements with a short explanation (2-3 sentences each).
(a) The pumping lemma can be used to show that a given language can be generated by a context free
grammar.
(b) If a grammar is ambiguous, the language it generates cannot be LR(1).
(c) Since the regular expression
a
?
b
?
can be used to generate
a
n
b
n
, that means the language
a
n
b
n
is a
regular language since it is generated by a regular expression.
1

Partial preview of the text

Download Midterm Exam Questions - Introduction to Compilers | CMSC 430 and more Exams Computer Science in PDF only on Docsity!

CMSC 430 Midterm, Section 0101, Octob er 22, 1996

Consider the following grammar G:

S! A S j  A! a A j b

1 [40]. If G is a grammar of the following typ e, give the appropriate parsing tables; if not, explain why not. (Note: Rememb er to give parsing tables if the grammar is of the indicated typ e. A yes/no answer is not sucient.) (a) LL(1) (b) LR(0) (c) SLR(1) (d) LR(1) (e) LALR(1)

2 [10]. Show that the language generated by G is a regular language.

3 [10]. What are the characteristics of an SLR(0) grammar? In particular, compare SLR(0) grammars to one or more of the classes of grammars that we have already studied (e.g., the 5 classes asked for in question 1 ab ove). Explain your answer.

4 [8]. Give the pre x and p ost x for each of the following: (a) (a+b)/(c+d)+(e+f ) (b) a/b/c+d+e

5 [10]. Consider the regular expression: (0?^ 1)?^ (0?^ 1)

Give the minimal state DFA that accepts this set.

6 [12]. Attribute grammars: (a) In YACC, rules like: Expr : Expr '+' Term f$$ = $1 + $3g represent (inherited, synthesized) [pick one] attributes.

(b) Why do es YACC use an LR or LALR parsing algorithm rather than an LL parsing algorithm? Explain this relative to the question of using attribute grammars.

(c) Could a to ol somewhat like YACC b e develop ed that used a precedence parsing algorithm? Explain.

(d) Assume you have an attribute code that output the p ost x for a nonterminal, and an attribute ty pe that gave the typ e of data represented by a nonterminal (either integ er or r eal ). Give a p ossible de nition for E :code in the pro duction of part (a) that allows for co ersion of integer to real data.

7 [10]. Answer or explain each of the following statements with a short explanation (2-3 sentences each).

(a) The pumping lemma can b e used to show that a given language can b e generated by a context free grammar.

(b) If a grammar is ambiguous, the language it generates cannot b e LR(1).

(c) Since the regular expression a?^ b?^ can b e used to generate an^ bn^ , that means the language an^ bn^ is a regular language since it is generated by a regular expression.