CSC3403 Assignment 1 2007 Sample solution
CSC3403 Assignment 1 2007 Sample solution
CSC3403 Assignment 1 2007 Sample solution
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>CSC3403</strong> <strong>Assignment</strong> 1 <strong>2007</strong> <strong>Sample</strong> <strong>solution</strong><br />
All questions were allocated 4 marks each.<br />
These answers are sample only. In some cases there are many possible correct <strong>solution</strong>s. Any<br />
correct <strong>solution</strong> will receive full marks.<br />
1. Consider the following grammar<br />
assign → id := expr<br />
id → A | B | C<br />
expr → expr + term | term<br />
term → term * factor | factor<br />
factor → id | - id<br />
Now consider the sentence of the grammar:<br />
A := - A + B * C<br />
Produce a rightmost derivation of this sentence.<br />
Solution<br />
assign ⇒ id := expr<br />
⇒ id := expr + term<br />
⇒ id := expr + term * factor<br />
⇒ id := expr + term * id<br />
⇒ id := expr + term * C<br />
⇒ id := expr + factor * C<br />
⇒ id := expr + id * C<br />
⇒ id := expr + B * C<br />
⇒ id := term + B * C<br />
⇒ id := factor + B * C<br />
⇒ id := - id + B * C<br />
⇒ id := - A + B * C<br />
⇒ A := - A + B * C<br />
2. For the grammar and sentence in Question 1, show the parse tree for the sentence.<br />
Solution<br />
assign<br />
/|\<br />
/ | \<br />
/ | \<br />
id := expr<br />
| /|\<br />
| / | \<br />
A<br />
expr + term<br />
| /|\<br />
| / | \<br />
term term * factor<br />
| | |<br />
| | |<br />
factor factor id<br />
/ | | |<br />
/ | | |<br />
- id id C<br />
| |<br />
| |<br />
A B
3. For the grammar and sentence in Question 1, show the abstract syntax tree for the<br />
sentence.<br />
Solution<br />
:=<br />
/ \<br />
A +<br />
/ \<br />
- *<br />
| / \<br />
A B C<br />
4. Consider the following grammar<br />
S → a S | X<br />
X → b X | Y<br />
Y → a Y | ǫ<br />
Write a single EBNF rule that describes this grammar.<br />
(That is, write a rule of the form: “S → EBNF string”.)<br />
Solution Either of the following is acceptable<br />
S → {a}{b}{a}<br />
S → a ∗ b ∗ a ∗<br />
5. Prove that the grammar in question 4 is ambiguous<br />
Solution We choose a sentence of the language that can be produced by two different derivations<br />
(that is, also, two different parse trees).<br />
S ⇒ aS ⇒ aX ⇒ aY ⇒ a<br />
S ⇒ X ⇒ Y ⇒ aY ⇒ a<br />
6. Write an unambiguous grammar in BNF that describes the same language as grammar<br />
of question 4.<br />
Solution The key here is to determine first why the grammar is ambiguous. The problem<br />
occurs with sentences like “aaaa”. It is possible to generate the ’a’ symbol from either the<br />
existing S rule or the Y rule. A key insight is that sentences that contain at least one b can<br />
only be produced in one way. So one <strong>solution</strong> is to write a grammar that produces either<br />
sentences of the form a ∗ , or it produces sentences of the form a ∗ b + a ∗ . A possible <strong>solution</strong> is<br />
as follows.<br />
S → a S | b X | ǫ<br />
X → b X | Y<br />
Y → a Y | ǫ<br />
Only the S rule has changed. Note S → b X ensures that if the Y rule is used to generate a<br />
symbols, then there must be at least one b in the sentence.<br />
7. Describe in English the strings that will be produced by the following grammar. Strings<br />
consist of the terminal symbols a, b, c, and k. You must identify the number (e.g. one,<br />
zero or more, zero or one, one or more) of occurrences of each symbol.<br />
S → a S b b | X<br />
X → c X | c Y<br />
Y → d Y | k<br />
Solution a n c + d ∗ kb m where n ≥ 0 and m = 2n and x n means that symbol x is repeated n<br />
times.
8. Write the BNF grammar rules for the language whose sentences have the following<br />
structure.<br />
(a) Zero or more x followed by one or more y.<br />
(b) One or more x followed by one or more y. There are the same number of x as there<br />
are y.<br />
(c) A single x, followed by optionally one y, followed by a single z.<br />
Solution<br />
(a) S → x S | y Y<br />
Y → y Y | ǫ<br />
(b) S → x S y | x y<br />
(c) S → x y z | x z<br />
9. Write the BNF grammar rules for the language whose sentences have the following<br />
structure<br />
[x](y + |z) +<br />
Solution Examination of this EBNF description reveals that it is a little over-complex. Indeed<br />
an equivalent description would be [x](y|z) + . (If you don’t believe this, try to find a sentence<br />
that can be produced by the original specification, but which cannot be produced by the new<br />
one.)<br />
S → x Y | Y<br />
Y → y Y | z Y | y | z<br />
10. Draw a single syntax diagram to describe the following grammar fragment. Assume ident<br />
and expression are defined elsewhere (you may simply refer to them in your diagram).<br />
Terminal symbols are enclosed in boxes .<br />
designator → ident<br />
| ident slist<br />
slist → suffix slist<br />
| suffix<br />
suffix → . ident<br />
| [ explist ]<br />
| ^<br />
explist → expression , explist<br />
| expression<br />
Solution<br />
designator<br />
ident<br />
.<br />
ident<br />
[<br />
^<br />
expression<br />
,<br />
]