ANSWER 2. Write EBNF descriptions for the following following a) A Java class definition header statement b)A Java method call statement c)A C switch statement. d)A C union statement e)C float literals Ans 2.a. Public class A extends B implements C, D where “public” is a modifier and “A” ,”B”, “C”, and “D” are identifiers. Ans 2. b. If the loop is such as: for (int k = 0, m = 100; k < n; k++, m++) { x = x + 1; y = y – 1; } where “int k = 0, m = 100” is an variable declaration, in which “int” is a type name, “k” and “m” are identifiers, and “0” and “100” are literals. If there is no appearance of “int”, “k = 0, m = 100” are a sequence of assignments. Also, “k < n” is an expression, “k++; m++” are also expressions, and “x=x+1;y=y+1;” “x=x+1;y=y+1;” is a statement list.
Assume the following non-terminals are given: , , , , , and .
-> for ‘(‘ [[] = {, [] = }] ; [] ; [ {, }] ‘)’ ‘{‘ ‘}’ c. A Java switch statement The following is an example switch statement:
switch (a+b) { case 1 : x = 7; break; case 2 : x = 8; break; default : x = 9;
}
where “a+b” is an expression, “1” and “2” are literals, and “x=7;break;”, “x=8;break;” and “x=9;” are statement lists. Assume non-terminals , , and are given.
-> switch ‘(‘ ‘)’ ‘{‘ {case : } [default : ] ‘}’
3. Rewrite the given grammar to give + precedence over * and force + to be right associative. -> = -> A | B | C -> * | -> + | -> ( ) |
4. Rewrite the given grammar to add ++ and - - unary operators of Java
-> = -> A | B | C -> + | -> * | -> ( ) | | ++ | - 5.Write a BNF description of the Boolean expression of Java, including the three operators &&, ||, and !, and the relational expressions with operators = =, !=, <, <=, >=, >. -> || | -> && | -> id | ! | ( ) | -> id = = id | id != id | id < id | id <= id | id >= id | id > id 6.Using the above grammar show a parse tree and a leftmost derivation for each of the following statements: a) A = A * (B + (C * A)) Derivation: => = => A = => A = *
=> A = A * => A = A * ( ) => A = A * ( + ) => A = A * ( B + ) => A = A * (B + ( )) => A = A * (B + ( * )) => A = A * (B + ( C * )) => A = A * (B + ( C * )) => A = A * (B + ( C * A )) b)B = C * (A * C + B)
Derivation: => = => B = => B = * => B = C * => B = C * ( ) => B = C * ( * ) => B = C * ( A * ) => B = C * ( A * + ) => B = C * ( A * C + ) => B = C * (A * C + ) => B = C * (A * C + B )
c) A = A * (B + ( C )) Derivation: => = => A = => A = * => A = A * => A = A * ( ) => A = A * ( + ) => A = A * ( B + ) => A = A * (B + ( )) => A = A * (B + ( ))