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.
  1. Write a BNF grammar equivalent to the above, without using the ε symbol.
  2. 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:

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

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.

Hints, Corrections, and Clarifications

Normally, I post hints/etc. in this section, depending on the questions I get. You want to keep checking.
  1. All grammars should be unambiguous (except, of course, for questions dealing with ambiguity)
  2. 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.