Consider the following grammar with terminals x y z nonte
Consider the following grammar with terminals: {x, y, z, +.-, *, /} non-terminals: {E, T, F, V} start symbol: E production rules: (a) What are the associativities of \"+\" and \"-\" operators? Explain your answer. (b) If a grammatically correct (according to the above grammar) expression has more \"+\" operations than \"-\" operations, then as per the above grammar, can we infer that the semantics of the \"+\" operations will be evaluated before that of \"-\" operations? Explain your answer. (c) Is x + y/z * x + y - x grammatically correct as per the above grammar? Explain your answer.
Solution
a) in case of operator precedence
associativity of + - is from right to left
a-b-c so first a-b will get evaluated then the rest the expression will get evaluated as ((a-b)-c)
power has the max priority
mul, divide has the same priority but less than power
+, - also has the same priority but less than power and / and *
b) no in this priority is from the starting to downwards
eg : 4+(5*3) = 19 cause acc to c language compiler * has higher priority then + so depends on the compiler
+ has more priority than -
associativity of + - * / is from right to left while for power it is left to right
means a+b+c acc to rule (a+b) will get calculated and then total of a+b result with c .
