Faculty of Computer Science
Spring Semester 2025
CS334
Assignment No. 1
Name:
ID:
Page | 1
Question 1:
Consider the grammar:
<list> → { <item>, <list> } | <item>
<item> → <value> | ( <list> )
<value> → A | B | C
a) List the terminal symbols.
b) List the non-terminal symbols.
c) Draw a parse tree for the sentence: (A)
d) Draw a parse tree for the sentence: {B, {C, (A)}}
Question 2:
Prove that the following grammars are ambiguous:
a)
<stmt> → <expr> ;
<expr> → <expr> / <expr> | <var>
<var> → x | y | z
TRY: x / y / z
b)
<term> → <term> @ <term> | <num>
<num> → 1 | 2 | 3
TRY: 1 @ 2 @ 3
Page | 2
Question 3:
Draw the parse tree for the sentence aaaabbbb using the grammar:
S→aSb|aabb
Question 4:
Given the grammar:
E→TE|ε
T→aB|b
B→bT|a
Provide the leftmost and rightmost derivations for the string: aab.
Page | 3
Question 5:
Use the grammar below to show the leftmost derivation and
parse tree for the string: bbabaab.
S → aA | bB
A → bS | aAA
B → aB | bB | b
Question 6:
Using the grammar:
<code> → <line> | <line> # <code>
<line> → <val> := <exp>
<exp> → <exp> & <exp> | <val>
<val> → w | x | y | z
a) Identify terminals and non-terminals.
b) Give the leftmost derivation for x := w & y
Page | 4
Question 7:
Check if the string 0*1+1*0 is valid using top-down parsing for the
grammar:
<expr> → <expr> * <bit> | <expr> + <bit> | <bit>
<bit> → 0 | 1
Question 8:
Draw the parse tree for the string 1+0*(1+0) using the grammar:
<expr> → <expr> + <term> | <term>
<term> → <term> * <factor> | <factor>
<factor> → ( <expr> ) | <digit>
<digit> → 0 | 1
Page | 5
Question 9:
Convert the following BNF to EBNF:
<seq> → <elem> | <elem> ; <seq>
<elem> → key | { <seq> }
Question 10:
Formally describe:
a) All strings over Σ={0,1} containing exactly
two 1’s.
b) All strings where the first symbol is a
followed by an even number of b’s.
Page | 6