Suppose you have a language where a valid program is a sequence of statements an
ID: 3721801 • Letter: S
Question
Suppose you have a language where a valid program is a sequence of statements and
nothing else. Every statement ends with semicolon. A statement is either input
READ(variable) or assignment variable=expression, where expression is C-like
expression involving () and +,-,*,/ all arithmetical binary operators except - which is
both unary and binary, and no other operators. Associativity is set so that all
operators are left to right except * which is right to left, Precedence is set so that ()
overrides anything, the rest, from the strongest to the weakest are:
unary minus
+ and -, the same
* and /, the same.
There are no numbers nor anything else. Variables are not defined.
Example program:
READ(x);
READ(y);
x=y+x/(x*y);
Design unambiguous CFG. Make sure to state what are the tokens.
Explanation / Answer
<STMT>-><EXPR> ;
<EXPR>-> READ(ID)/<ASSIGN>=<EXPR>
<EXPR>-><EXPR>+<TERM>/<TERM>/(EXPR>)
<EXPR>-><EXPR>-<TERM>/-(EXPR)
<TERM>-><FACTOR>*<TERM>/<FACTOR>
<FACTOR>-><FACTOR>/<DENOM>
<EXPR>->ID
THE TOKENS ARE ID ,+ ,*,/ - and READ
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.