<p>the examples you gave are correctly parenthesized w.r.t. the associativity of the two grammars and the evaluation order is correct. Consequently, results are correct as well.</p>
<p>You can try this yourself employing <code>GOLD Parser Builder</code> (it is available for free), it is a simple but intuitive BUP compiler-compiler that let's you specify a grammar, parse some input string and see:</p>
<ol>
<li>the reduction order,</li>
<li>the corresponding derivation tree.</li>
</ol>
<p>Inspecting it, you will reach the same conclusion.</p>

CC BY-SA 4.0
2023-08-06T11:47:39.927Z
2023-08-06T11:47:39.927Z
Chaos
<p>Request to vet the evaluation for given two grammars, handling
arithmetic expressions, having two precedence classes of operators:</p>
<pre><code>addop= {+,-}, mulop = {*,/}, 
</code></pre>
<p>such that:</p>
<pre><code>precedence(mulop) &gt; precedence (addop)
</code></pre>
<p>My intent to introduce this example was to show how the C compiler, uses the <em>left-to-right associativity</em> to process a smallish arithmetic expression, as shown below:</p>
<pre><code>200+300+400-50*10/5*2
</code></pre>
<p>Also, wanted to show the same expression processed under right-to-left associativity.
This was to give a suitable small (<em>though irrelevant example, as the C compiler follows only left-to-right associativity for these four operators</em>) example, as the C compiler also has many operators where right-to-left associativity is followed.</p>
<p><em>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.</em></p>
<p>For the case of left-to-right associativity, have a small parsing grammar:</p>
<pre><code>expr -&gt; expr + term | exp - term | term
term -&gt; term * factor | term / factor | factor
factor -&gt; factor digit | digit
digit -&gt; 0|1|2|...|9
</code></pre>
<p>This grammar is also fit for BUP (bottom-up parsing), as left recursion is not a problem for BUP.</p>
<p><em>Though not needed, but it gels in the class to state that, the C-compiler is based on BUP.</em></p>
<p>In the absence of latex skills, to draw binary trees; have shown the similar effect, by the order imposed by the enclosing parenthesis:</p>
<pre><code>((200+300)+400)-(((50*10)/5)*2))
=&gt; 900 - ((100)*2)
=&gt; 700
</code></pre>
<p><strong>Right-to-left associativity</strong>
The corresponding grammar is:</p>
<pre><code>expr -&gt; term + expr | term - expr | term
term -&gt; factor * term | factor / term | factor
factor -&gt; digit factor | digit
digit -&gt; 0|1|2|...|9

((200+(300+(400-((50*(10/(5*2)))))
=&gt; ((200+(300+(400-((50)))
=&gt; 850
</code></pre>

CC BY-SA 4.0
2023-08-01T07:24:52.540Z
Are we to read all this as coming from a CS student or teacher?
Rushi
CC BY-SA 4.0
2023-08-01T07:43:48.907Z
@Rusi I am a faculty, and done masters in CSE, from a prestigious govt. institute in India.
jiten
CC BY-SA 4.0
2023-08-01T07:45:05.083Z
Well that makes the question (more) on topic. But it's very confused — What u want to ask. Bottom up and top down parsing can both handle both precedence. Though with different convenience
Rushi
CC BY-SA 4.0
2023-08-01T07:54:41.650Z
@Rusi Thanks. Want to get the vetting of the processing of the stated arithmetic expression done, by the given left-to-right and right-to-left associative grammars.
jiten
CC BY-SA 4.0
2023-08-01T07:54:48.973Z
@Rusi And also want more examples, or some inputs to create own examples, for operators having right-to-left associativity. And, want both scenarios: a) when only such operators are present in a statement, b) the statement has a mix of operators (i.e., both left-to-right and right-to-left associativity operators are there). That would entail also the inputs for creating suitable rules,  that am confused about, in terms of which phase of smallish compiler should they occur. If such rules are implemented in parser, then need inputs to create a smallish grammar too.
jiten
CC BY-SA 4.0
2023-08-01T08:44:23.347Z
@Rusi Request to please vet my result obtained above for the arithmetic expression: 200+300+400-50*10/5*2, by both grammars listed above, i.e. left-to-right and right-to-left associative ones.
jiten
CC BY-SA 4.0
2023-08-03T07:19:15.583Z
I really can't make our what you're asking. There seems to be basic confusion between (1) (C) language, (2) specific compiler technology (eg yacc) and (3) general compiler knowhow ( eg the 'dragon book' of Aho Sethi Ullman)
Rushi
CC BY-SA 4.0
2023-08-03T07:20:33.980Z
And I suspect that if you clean up the question it will hardly be a CSE question but will fit better in CS or stackoverflow itself
Rushi
CC BY-SA 4.0
2023-08-08T03:00:04.617Z
I'm not sure what's being asked here, but this seems unrelated to CS education.
ggorlen
<compiling>
Processing of arithmetic expressions, under both types of associativity
# Processing of arithmetic expressions, under both types of associativity

(Asked by: username_0 on 2023-08-01)

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

**Tags:** compiling

> **Comments:**
> **username_1**: Are we to read all this as coming from a CS student or teacher?  
> **username_0**: @Rusi I am a faculty, and done masters in CSE, from a prestigious govt. institute in India.  
> **username_1**: Well that makes the question (more) on topic. But it's very confused — What u want to ask. Bottom up and top down parsing can both handle both precedence. Though with different convenience  
> **username_0**: @Rusi Thanks. Want to get the vetting of the processing of the stated arithmetic expression done, by the given left-to-right and right-to-left associative grammars.  
> **username_0**: @Rusi And also want more examples, or some inputs to create own examples, for operators having right-to-left associativity. And, want both scenarios: a) when only such operators are present in a statement, b) the statement has a mix of operators (i.e., both left-to-right and right-to-left associativity operators are there). That would entail also the inputs for creating suitable rules,  that am confused about, in terms of which phase of smallish compiler should they occur. If such rules are implemented in parser, then need inputs to create a smallish grammar too.  
> **username_0**: @Rusi Request to please vet my result obtained above for the arithmetic expression: 200+300+400-50*10/5*2, by both grammars listed above, i.e. left-to-right and right-to-left associative ones.  
> **username_1**: I really can't make our what you're asking. There seems to be basic confusion between (1) (C) language, (2) specific compiler technology (eg yacc) and (3) general compiler knowhow ( eg the 'dragon book' of Aho Sethi Ullman)  
> **username_1**: And I suspect that if you clean up the question it will hardly be a CSE question but will fit better in CS or stackoverflow itself  
> **username_2**: I'm not sure what's being asked here, but this seems unrelated to CS education.  

---

## 1 answers

### Answer by username_3 on 2023-08-06. Score: 1 | Accepted answer

the examples you gave are correctly parenthesized w.r.t. the associativity of the two grammars and the evaluation order is correct. Consequently, results are correct as well.

You can try this yourself employing `GOLD Parser Builder` (it is available for free), it is a simple but intuitive BUP compiler-compiler that let's you specify a grammar, parse some input string and see:

1. the reduction order,
2. the corresponding derivation tree.

Inspecting it, you will reach the same conclusion.


---

