________________________________________________________
PRG2205 – Programming Languages – Practical_Tutorial 4
Answer the solve the following questions:
1- Recall the BNF of Example 3.4. Rewrite the BNF of Example 3.4 to give + precedence
over * and force + to be right associative.
Answer:
<assign> → <id> = <expr>
<id> → A | B | C
<expr> → <expr> * <term> * have low preference because + is in term level
| <term>
<term> → <factor> + <term> + <term> is right associative because LHS <term> appears at RHS <term>
| <factor>
<factor> → ( <expr> )
| <id>
2- Write EBNF descriptions for the following:
• A Java class definition header statement
Answer:
<class_head> → {<modifier>} class <id> [extends class_name]
[implements <interface_name> {, <interface_name>}]
<modifier> → public | abstract | final
3- Convert the BNF of Example 3.3 to EBNF.
Answer:
<assign> → <id> = <expr>
<id> → A | B | C
<expr> → <expr> (+ | *) <expr>
| (<expr>)
| <id>
2
4- Write a grammar for the language consisting of strings that have x copies of the digit
0 followed by the same number x of copies of the digit 1, where x > 0. For example,
the strings 01, 00001111, and 0000011111 are in the language but 0, 1, 10, and 00011
are not.
Answer:
S → 0 S 1 | 01