Add a unary minus operator that has higher precedence than either + or *, to the following the grammar (below):
<assign> -> <id> = <expr>
<id> -> A | B | C
<expr> -> <expr> + < term> | <term>
<term> -> <term> * <factor> | <factor>
<factor> -> ( <expr> ) | <id>
please dont copy and paste
To add an operator of higher precedence always add it in lower level of grammer so that it is evaluated first(as grammer is left recursive). In the answer 1 that's the ereason -<id> is below <term> which consists of *. so always -will be evaluated first. Same is the case with answer 2.
Answer1:
< assign > ==> < id > = < expr >
<id> ==> A | B | C
< expr > ==> < expr > + < term > | < term >
< term > ==> < term > * < factor > | < factor >
< factor > ==> ( <expr> ) | +<id> | – <id>
--------------------------------------------------------------------------------------------------------------------
Answer 2:
< assign > ==> < id> = <expr>
< id > ==> A | B | C
< expr > ==> < expr > + <term> | <term> | - <factor>
<term> ==> <term> * <factor> | <factor>
<factor> ==> (<expr>) | <id>
Add a unary minus operator that has higher precedence than either + or *, to the...
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...
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...
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 3: Given the following grammar: assign → id := expr expr → expr + term \ term term -term *factor lfactor factor-(expr) id Using the above grammar, show a leftmost derivation (first five steps) for the following assignment statement: A ((A B)+ C) a. [3 marks] b. Using the above grammar, show a rightmost derivation (first five steps) for the following assignment statement: A:-A+B+C)+A [3 marks] Draw the abstract syntax tree for each of the above statements [4 marks]...
6. (8 pts) Using grammar below show a Parse tree and leftmost derivation for a). A = A * (B+C) <assign> à<id> = <expr> <id> à A | B|C <expr>à <expr> + <term> | <term> <term> à <term> * <factor> |<factor> <factor> à ( <expr> ) |<id>
3. Using the grammar below, show a parse tree and a leftmost derivation for the statement. A = ( A + (B)) * C assign <idxpr expr>? <expr> <term> term <term factor factor (<expr>) l <term I <factor l <id> 4. Prove that the following grammar is ambiguous (Give sentence that has two parse trees, and show the parse trees):
(20 pts) To understand the value of recursion in a programming language: implement the binary search algorithm first as a recursive function and again using a conditional loop. Your program should create an array of characters holding the letters ‘A’ – ‘Z’ and find the index in the array where the letter ‘K’ is stored. You may use any programming language that supports recursion. (5pts) Define syntax and semantics and give an example. (5pts) Why is it important for a...
1. (p. 2-3.) Which of the following is NOT a reason for studying concepts of programming languages according to Sebesta? a. Increased capacity to express ideas. b. Improved background for choosing appropriate languages. c. Increased ability to design new languages. d. Increased ability to learn new languages. 2. (p. 5-6.) What programming language has dominated scientific computing over the past 50 years? a. FORTRAN b. ALGOL c. SNOBOL d. PL/I 3. (p. 6.) What programming language has dominated artificial intelligence...
for compiler design Compster Science epene CS 347 Compiler Design Assiznment 2 Due Date: October 28, 2018 Exercise 1 Consider the following grammar: cassign expr l <id> a) Show that this grammar is ambiguous. b) Do the necessary changes to make it unambiguous (you should consider that has more priority than -). Exercise 2 Consider the following BNF Grammar: A [B, A] | B B: CI(A; C) D::= a | b | c For each of the strings listed below,...