0% found this document useful (0 votes)
2 views3 pages

PRG2205 PracticalTutorial4 WithSolutions

The document contains practical tutorial questions for a programming languages course, focusing on rewriting BNF with operator precedence and associativity, creating EBNF descriptions for Java class definitions, and converting BNF to EBNF. It also includes a grammar for a specific language of strings consisting of equal numbers of '0's followed by '1's. The answers provided demonstrate the application of formal grammar rules and syntax definitions.

Uploaded by

cindycry711
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

PRG2205 PracticalTutorial4 WithSolutions

The document contains practical tutorial questions for a programming languages course, focusing on rewriting BNF with operator precedence and associativity, creating EBNF descriptions for Java class definitions, and converting BNF to EBNF. It also includes a grammar for a specific language of strings consisting of equal numbers of '0's followed by '1's. The answers provided demonstrate the application of formal grammar rules and syntax definitions.

Uploaded by

cindycry711
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

________________________________________________________

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

You might also like