Assign Cs323

8
ANSWER 2. Write EBNF descriptions for the 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;” is a statement list. Assume the following non-terminals are given: <type>, <id>, <literal>, <assign>, <expr>, and <stmt_list>. <for> -> for ‘(‘ [[<type>] <id> = <expr> {, [<type>] <id> = <expr>}] ; [<expr>] ; [<expr> {, <expr>}] ‘)’ ‘{‘ <stmt_list> ‘}’ c. A Java switch statement

Transcript of Assign Cs323

Page 1: Assign Cs323

ANSWER

2. Write EBNF descriptions for the 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, Dwhere “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;” is a statement list.

Assume the following non-terminals are given: <type>, <id>, <literal>, <assign>, <expr>, and <stmt_list>.

<for> -> for ‘(‘ [[<type>] <id> = <expr> {, [<type>] <id> = <expr>}] ; [<expr>] ; [<expr> {, <expr>}] ‘)’ ‘{‘ <stmt_list> ‘}’

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;

Page 2: Assign Cs323

}

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 <expr>, <literal>, and <stmt_list> are given.

<switch> -> switch ‘(‘ <expr> ‘)’ ‘{‘ {case <literal> : <stmt_list>} [default : <stmt_list>] ‘}’

3. Rewrite the given grammar to give + precedence over * and force + to be right associative.

<assign> -> <id> = <expr>

<id> -> A | B | C

<expr> -> <expr> * <term> | <term>

<term> -> <factor> + <term> | <factor>

<factor> -> ( <expr> ) | <id>

4. Rewrite the given grammar to add ++ and - - unary operators of Java

<assign> -> <id> = <expr>

<id> -> A | B | C

<expr> -> <expr> + <term> | <term>

<term> -> <term> * <factor> | <factor>

<factor> -> ( <expr> ) | <id> | <id> ++ | <id> - -

5.Write a BNF description of the Boolean expression of Java, including the three operators &&, ||, and !, and the relational expressions with operators = =, !=, <, <=, >=, >.

<Boolean_expr> -> <Boolean_expression> || <Boolean_term> | <Boolean_term>

<Boolean_term> -> <Boolean_term> && <Boolean_factor> | <Boolean_factor>

<Boolean_factor> -> id | ! <Boolean_factor> | ( <Boolean_expr> ) | <relation_expr>

<relation_expr> -> 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:

<assign> => <id> = <expr>

=> A = <expr>

=> A = <id> * <expr>

Page 3: Assign Cs323

=> A = A * <expr>

=> A = A * ( <expr> )

=> A = A * ( <id> + <expr> )

=> A = A * ( B + <expr> )

=> A = A * (B + ( <expr> ))

=> A = A * (B + ( <id> * <expr> ))

=> A = A * (B + ( C * <expr> ))

=> A = A * (B + ( C * <id> ))

=> A = A * (B + ( C * A ))

b)B = C * (A * C + B)

Derivation:

<assign> => <id> = <expr>

=> B = <expr>

=> B = <id> * <expr>

=> B = C * <expr>

=> B = C * ( <expr> )

=> B = C * ( <id> * <expr> )

=> B = C * ( A * <expr> )

=> B = C * ( A * <id> + <expr> )

=> B = C * ( A * C + <expr> )

=> B = C * (A * C + <id> )

=> B = C * (A * C + B )

c) A = A * (B + ( C ))

Derivation:

<assign> => <id> = <expr>

=> A = <expr>

=> A = <id> * <expr>

=> A = A * <expr>

=> A = A * ( <expr> )

=> A = A * ( <id> + <expr> )

=> A = A * ( B + <expr> )

=> A = A * (B + ( <expr> ))

=> A = A * (B + ( <id> ))

Page 4: Assign Cs323

=> A = A * (B + ( C ))

7.a.. A =(A+B)*Cb. A = B+C+Ac. A= A*(B+C)d. A= B*(C*(A+B)

Ans 7.a.

<assign> => <id> = <expr>=>A = <expr>=>A = <expr>*<id>=>A = (<expr>) *<id>=>A = (<id>+<expr>)*<id>=>A = (A + <expr>)*<id>=>A = (A+<id>)*<id>=>A = (A+B)*<id>

=>A = (A+B)*C

b.A= B+C+A<assign> => <id> = <expr>=> A = <expr>=> A= <expr>+<term>

` => A= <expr>+<term>+<term>=> A= <term>+<term>+ <term>=> A= <factor>+<term>+<term>=> A= <id>+<term>+<term>=> A= B+ <term> + <term>=> A= B+<factor>+<term>=> A= B+<id>+<term>=> A= B+C+<term>=> A= B +C+<factor>=> A= B+C+<id>=> A= B+C+A

c.A = A * (B+C)<assign> => <id> = <expr>A=<expr>A=<id>*<expr>A= A *<expr>A =A *(<expr>)A =A *( <id>+<expr>)

Page 5: Assign Cs323

A = A *( C + (<expr>)A = A *( C + (<id> + <expr>)A = A *( C + ( A + <expr>)A = A * ( C + ( A + <id> )

A= A * (C+ (A + B)

d.

A = B*(C*(A+B))A = <expr>A = <id>*<expr>A = B*<expr>A = B*(<expr>)A = B*(<id>*<expr>)A = B*(C*<expr>)A = B*(C*(<expr>))A = B*(C*(<id>+<expr>))A = B*(C*(A+<expr>))A = B*(C*(A+<id>))A = B*(C*(A+B))

8.Prove that the following grammar is ambiguous:

<S> -> <A>

<A> -> <A> + <A> | <id>

<id> -> a | b | c

Example ambiguous sentence: a + b + c

      Parse tree 1:

      <S>

                  <A>

                              <A>

                                          <A>

                                                      <id>

                                                                  a

                                          +

                                          <A>

                                                      <id>

                                                                  b

                              +

Page 6: Assign Cs323

                              <A>

                                          <id>

                                                      c

 

      Parse tree 2:

      <S>

                  <A>

                              <A>

                                          <id>

                                                      a

                              +

                              <A>

                                          <A>

                                                      <id>

                                                                  b

                                          +

                                          <A>

                                                      <id>

                                                                  c

Note in the above representation of a tree, all symbol(s) with N column indentations are children nodes of the parent node that is immediately above these symbol(s) and has N-1 column indentations.

9. Modify the grammer in eg. 3.4 to add a unary minus operator that has higher preceedencethan either + or *.

Ans 9.<assign> -> <id> = <expr><id> -> A | B | C

<expr> -> <expr> * <term> | <term>

<term> -> <factor> + <term> | <factor>

<factor> -> ( <expr> ) | <id>

10.Describe, in English, the language defined by the following grammar in BNF <S> -> <A> <B> <C>

<A> -> a <A> | a

<B> -> b <B> | b

<C> -> c <C> | c

Page 7: Assign Cs323

Ans.One or more a's followed by one or more b's followed by one or more c's.

11.

Consider the following grammer:<S> -> <A>a<B>b<A> -><A> b | b<B> -> a<B> | aWhich of the following sentences are in the language generated by thre grammer ?a. baabb. bbbabc.bbaaaaad.bbaab

Ans.Options a b and d are correct