Homework 1 - Syntax
Due: Sep 13 2 PM (in class or by email to instructor).
Problem 1 (15%)
Comsider the language consisting of all strings that have at least 2
a's, followed by n copies of b (n>0), followed by an even
non-negative number of c's, followed by n copies of d. For example,
aaabbbccddd is a legal string.
Write a BNF grammar for the language, without using the ε symbol
(empty) or choice (|).
Problem 2 (15%)
Consider the EBNF production, A --> ab{a}[ab] | c | AA.
- Write a BNF grammar equivalent to the above, without using the ε
symbol.
- Is the above grammar ambiguous? Show why, or prove that it is not
ambiguous.
Problem 3 (15%)
6.13 (ed2: 4.13)
Do only the BNF part.
Problem 4 (15%)
Revise Louden's grammar for expressions to add the following operators:
- ^: binary, infix, left associative, higher priority than + and *
- !: unary, prefix, highest priority of all operators (excluding
parentheses)
Problem 5 (20%)
Louden defines a toy language with assignment,
if-then-else, and while on pp. 544-545 (2nd ed: pp. 582-583).
This toy language is similar to other imperative languages,
except that
it does not contain
booleans. Thus, conditions are integer expressions (you don't
need to know the semantics for this homework). Consider the following
program:
if
a+b
then
while s+t do
s := s + n
n := n
t := s
od
else
t := s
fi
n := 1+2*3+4
- Insert semicolons into the above program so that it is consistent
with Louden's grammar.
- Draw a complete parse tree for the last line of the above program.
- Draw an AST for the entire program, using only the AST
internal nodes we mentioned in class (note that our
sequential composition is binary).
- Estimate
the number of nodes in a parse tree for the entire program. It
goes without saying that you need to justify your
estimate.
Problem 6 (20%)
A variable x is said to be used in
statement
S
if
the
value of x is needed to execute S. Thus (in the toy language),
x would
appear in either the right hand side of an assignment statement or the
condition of an if or while statement.
Suppose you wish to modify Louden's toy language so that uninitialized
variables aren't used in any program statement. For example x:=y would
be an illegal program since y is unitialized.
- If the number of variables is unbounded, is this possible in a
CFG? If yes, outline how you would do it and give a big-O estimate of
the number of productions. If no, argue why not.
- Repeat the above if there may be at most n variables in the
target program.
Hints, Corrections, and Clarifications
Normally, I post hints/etc. in this section, depending on the questions
I get. You want to keep checking.
- All grammars should be unambiguous (except, of course, for
questions
dealing with ambiguity)
- The "toy language" referred to in problem 5 (and throughout this
course) is the language Louden refers to as "A Sample Small Language"
on the referenced pages. Also, it should be pointed out that there are
a few nonterminals (expr, term, number, digit) referenced on the
previous page.