Request to vet the evaluation for given two grammars, handling arithmetic expressions, having two precedence classes of operators:
addop= {+,-}, mulop = {*,/},
such that:
precedence(mulop) > precedence (addop)
My intent to introduce this example was to show how the C compiler, uses the left-to-right associativity to process a smallish arithmetic expression, as shown below:
200+300+400-50*10/5*2
Also, wanted to show the same expression processed under right-to-left associativity. This was to give a suitable small (though irrelevant example, as the C compiler follows only left-to-right associativity for these four operators) example, as the C compiler also has many operators where right-to-left associativity is followed.
But, still have to search for good examples for those operators, as well as to search for different examples, for more levels of precedence , than just two; as the C-compiler has 15 levels of precedence. Hence, request for sources for the same too.
For the case of left-to-right associativity, have a small parsing grammar:
expr -> expr + term | exp - term | term
term -> term * factor | term / factor | factor
factor -> factor digit | digit
digit -> 0|1|2|...|9
This grammar is also fit for BUP (bottom-up parsing), as left recursion is not a problem for BUP.
Though not needed, but it gels in the class to state that, the C-compiler is based on BUP.
In the absence of latex skills, to draw binary trees; have shown the similar effect, by the order imposed by the enclosing parenthesis:
((200+300)+400)-(((50*10)/5)*2))
=> 900 - ((100)*2)
=> 700
Right-to-left associativity The corresponding grammar is:
expr -> term + expr | term - expr | term
term -> factor * term | factor / term | factor
factor -> digit factor | digit
digit -> 0|1|2|...|9
((200+(300+(400-((50*(10/(5*2)))))
=> ((200+(300+(400-((50)))
=> 850