<expr> ::= <expr> + <expr> | <expr> - <expr> | <expr> * <expr> | <expr> / <expr> | <expr> ^ <expr> | <expr> ! | <expr> % | ( <expr> ) | ~ <expr> | <NUMBER> | E | PI |
Revise BNF grammar to qualify these requirements :
(i)
precedence
!,% >= ~ >= ^ >= *,/ >= +,−
(ii)
Left associativity : +, -, *, /
Right associativity : ^
Solution:
1. In order to preserve the precedence of operators the productions of BNF grammar must follow the order.
the high priority operator's expression must be specified in the rear production. Least priority operator expression is specified in the begining production,
for example. if E->E+E | E*E | id is the grammar and precedence order is * >= + then productions will be
E->E+T | T
T->T*F |F
F->id
2.if the operator is left associative then production of concerned expression must have left recursion , otherwise if the operator is right associative the production of concerned expression must have right recursion... in the above example +,* are left associative , because the productions have left recursion.
based on the least precedence and associativity the productions can be defined as follows.
3) operators +, - are of least precedence and are left associative ,from steps 1 and 2,
<expr1>::=<expr1>+<expr2> | <expr1> - <expr2> | <expr2>
4)next least precedence is given to *, / and are left associative.
<expr2>::=<expr2>*<expr3> | <expr2> / <expr3> | <expr3>
5)next least precedence is given to ^ and is right associative hence the production is
<expr3> ::=<expr4> ^ <expr3> | <expr4> /*production contains right recursion */
6)in ascending order the next operator is ~ and its production can be
<expr4> ::=~<expr5> | <expr5>
7)the high priority operator is either ! or % the productions are
<expr5> ::= <expr5> ! | <expr5>% | <expr6>
8) the remain righthand sides can be written as
<expr6> ::=(<expr1>) | <NUMBER> | E | PI
based on above steps the given BNF grammar cab be converted in the following manner.
<expr1>::=<expr1>+<expr2> | <expr1> - <expr2> | <expr2>
<expr2>::=<expr2>*<expr3> | <expr2> / <expr3> | <expr3>
<expr3> ::=<expr4> ^ <expr3> | <expr4>
<expr4> ::=~<expr5> | <expr5>
<expr5> ::= <expr5> ! | <expr5>% | <expr6>
<expr6> ::=(<expr1>) | <NUMBER> | E | PI
<expr> ::= <expr> + <expr> | <expr> - <expr> | <expr> * <expr> | <expr> /...
Question Set 2 1. Given the following grammar dactor>-> ( <expr> ) a) What is the associativity of each of the operators? What is precedence of the operators? Show a leftmost derivation and parse tree for the following sentence: b) c) A-A(B(C A)) d) Rewrite the BNF grammar above to give precedence over and force to be right associative.
Considering the following BNF grammar, answer the questions. <prog> - <assign> | <expr> <assign> = <id> = <expr> <expr> := <expr> + <term> | <expr> - <term> | <term> <term> := <factor> | <factor> * <term> <factor> ::= ( <expr> ) | <id> | <num> <id>::= ABC <num> := 0|1|2|3 2a - What is the associativity of the * operator? (5 points) 2b - For the * and + operators, do they have the same precedence, does the * operator...
Question Set 2 1. Given the following grammar dactor>-> ( <expr> ) a) What is the associativity of each of the operators? What is precedence of the operators? Show a leftmost derivation and parse tree for the following sentence: b) c) A-A(B(C A)) d) Rewrite the BNF grammar above to give precedence over and force to be right associative.
Q3. Convert the following recursive BNF grammar to EBNF: (20%) <assign>-> <id> = <expr> <expr> -> <d>+ <expr> | <id> * <expr> 1 (<expr>) | <i>
This BNF grammar defines expressions with three operations, *, -, and +, and the variables “a”, “b”, “c”, and “d”. <expr> ::= <thing> | <thing> * <expr> <object> ::= <element> | <element> – <object> <thing> ::= <object> | <thing> + <object> <element> ::= a|b|c|d|(<object>) a) Give the order of precedence among the three operations. b) Give the order (left-to-right or right-to-left) of execution for each operation. c) Explain how the parentheses defined for the nonterminal <element> may be used in...
The questions in this section are based on the grammar given as the following: prog -> assign | expr assign -> id = expr expr -> expr + term | expr - term | term term -> factor | factor * term factor -> ( expr ) | id | num id -> A | B | C num -> 0 | 1 | 2 | 3 (2a) What is the associativity of the * operator? (5 points) (2b) What...
1. Write a BNF description of the logical expressions and the relational expressions in C++. Make sure that the BNF reflects the order of precedence of the operators, as well as, the associativity rules. 2. Using the BNF rules in 1., give a rightmost derivation and show a parse tree for the expression below. 3. Prove that the following grammar is ambiguous and rewrite the grammar to remove ambiguity «newexp> → «newexp> @ <newexp> ulvl w I <other> <other> →
The questions in this section are based on the grammar given as the following: prog -> assign | expr assign -> id = expr expr -> expr + term | expr - term | term term -> factor | factor * term factor -> ( expr ) | id | num id -> A | B | C num -> 0 | 1 | 2 | 3 (2a) What is the associativity of the * operator? (5 points) (2b) What...
Given the grammar G6 from the book: G6 Extend this grammar to include three additional operators (your answer must be one grammar that adds all of): A. Subtraction, -, with left associativity and precedence equal to that of addition (+) [2pt] B. Division, /, with left associativity and precedence equal to that of multiplication () [2pt] C. Unary negation, , with right associativity and precedence higher than multiplication but lower than parentheses 0 [3 pt] Unary negation is (typically) an...
4. Assume the following rules of associativity and precedence for expressions: Precedence Highest *, /, not +,-,&, mod - (unary) =,/=,<,<=,>=,> and or, xor Lowest Associativity Left to right Show the order of evaluation of the following expressions by parenthesizing all sub expressions and placing a superscript on the right parenthesis to indicate order. For example, for the expression a+b*c+d the order of evaluation would be represented as ((a+(b*c)1)2 +d)3 a * b - 1 + c a * (b...