In: Computer Science
Q1) Convert the following set of BNF rules to a single EBNF rule.
<E> --> <E> + <T> | <E> - <T> | <T>
Q2) Briefly explain how the expected type and actual type of <expr>
in the following two BNF rules are determined:
<assign> --> <var> = <expr> (Rule 1) <expr> --> <var> + <var> (Rule 2)
Answer 1.
BNF:
<E> --> <E> + <T>
| <E> - <T>
| <T>
Conversion to EBNF:
<E> --> <E> <O> <T> +
<O> --> ‘+’ | ‘-‘
E equals to ‘E’ ‘O’ ‘T’
Where
E is expression
O is operator which can be ‘+’ or ‘-‘
T is term which is one or more.
Answer 2.
actual-type: a synthesized attribute which stores the actual type of a <VAR> or an <EXPRESSION>. In the case of an <EXPRESSION> , the type is computed given the types of the component <VAR>s.
expected-type: an inherited attribute which stores the expected type of <EXPRESSION> . It is determined by the type of the <VAR> on the left-hand-side of the expression statement.
Answer 1.
BNF:
<E> --> <E> + <T>
| <E> - <T>
| <T>
Conversion to EBNF:
<E> --> <E> <O> <T> +
<O> --> ‘+’ | ‘-‘
E equals to ‘E’ ‘O’ ‘T’
Where
E is expression
O is operator which can be ‘+’ or ‘-‘
T is term which is one or more.
Answer 2.
actual-type: a synthesized attribute which stores the actual type of a <VAR> or an <EXPRESSION>. In the case of an <EXPRESSION> , the type is computed given the types of the component <VAR>s.
expected-type: an inherited attribute which stores the expected type of <EXPRESSION> . It is determined by the type of the <VAR> on the left-hand-side of the expression statement.