116k views
0 votes
Rewrite the bnf of example 3.4 to give precedence over * and force to be right associative

User Eleonora
by
8.6k points

2 Answers

5 votes

Final answer:

To give precedence to the * operator and force it to be right associative in the example 3.4 BNF, modify the production rules appropriately.

Step-by-step explanation:

To rewrite the BNF of example 3.4 to give precedence over * and force it to be right associative, we can modify the production rules accordingly. Let's assume the original BNF is as follows:



<expr> ::= <expr> * <term> | <term>
<term> ::= <term> + <factor> | <factor>
<factor> ::= ( <expr> ) | id
<id> ::= a | b | c



To give precedence over *, we need to introduce a new nonterminal symbol and modify the production rules. Here's the modified BNF:



<expr> ::= <expr> + <term> | <term>
<term> ::= <term> * <factor> | <factor>
<factor> ::= ( <expr> ) | id
<id> ::= a | b | c



This modification ensures that the * operator has higher precedence than the + operator. Additionally, to force * to be right associative, we need to modify the production rule for <term>. Here's the updated BNF:



<expr> ::= <expr> + <term> | <term>
<term> ::= <term> * <factor> | <factor> * <term> | <factor>
<factor> ::= ( <expr> ) | id
<id> ::= a | b | c

User EarlyPoster
by
8.8k points
0 votes

Final answer:

To revise the BNF to give precedence to '*' and enforce right associativity, terms involving multiplication are evaluated first, and the grammar rule for the term is defined in a right-recursive form. This approach naturally enforces the desired order of operations and associativity for multiplication in the context of programming languages or mathematical expression grammars.

Step-by-step explanation:

The question pertains to rewriting the Backus-Naur Form (BNF) of an example to give precedence to the multiplication operator (*) and to force it to be right associative. In the context of grammars for programming languages or mathematical expressions, to give precedence to the multiplication operator over addition operators, we typically structure the grammar rules so that terms with the multiplication operator are evaluated before expressions with addition or subtraction.

Original BNF

Let's say our original BNF looks like this:

<expression> ::= <term> | <expression> '+' <term> | <expression> '-' <term>

<term> ::= <factor> | <term> '*' <factor> | <term> '/' <factor>

<factor> ::= <number> | '(' <expression> ')'

Revised BNF for Precedence and Right Associativity

To enforce precedence and right associativity for '*', a revised BNF could be:

<expression> ::= <term> ('+' <expression>)? | <term> ('-' <expression>)?

<term> ::= <factor> ('*' <factor>)*

<factor> ::= <number> | '(' <expression> ')'

In the revised BNF, the <term> production has been changed to a right-recursive form, which naturally enforces right associativity. Here, the <factor> would be evaluated first (giving precedence to '*'), and a <term> can be followed by zero or more '*' operations on other <factor>s, which means that multiplication will 'pile up' to the right.

The complete question is: rewrite the bnf of example 3.4 to give precedence over * and force to be right associative is:

User Nitrovatter
by
8.2k points