Change of associativity for a given recursive grammar.
by ajiten from LinuxQuestions.org on (#6DDG7)
In section 3.7.1, of the book (made available freely online, by the author himself at: https://holub.com/goodies/compiler/c...rDesignInC.pdf, have on page #176), the mention of grammar, for a right-recursive list:
Code:stmt_list -> stmt stmt_list | stmt
stmt -> A | B| Cwhile on the previous page, the left recursive version is given:
Code:stmt_list -> stmt_list stmt| stmt
stmt -> A | B| C(But, am at-first, concerned with the right-recursive version.)
The figure 3.4, on page #176, shows the corresponding parse tree for a right-recursive list(grammar).
On page #177, there is listing 3.1 for: Left Associativity with a Right-Recursive Grammar; given below:
Code:1 stmt_list()
2 {
3 /* Code is generated as you create the tree, before the subtree is
4 * processed.
5 */
6
7 remember= stmt();
8
9 process statement( remember);
10
II if( not_at_end_of_input () )
12 stmt_list();
13 }
14
15 stmt ()
16 {
17 return( read() );
18 }So, it means that given the arithmetic expression: 2*3*5 -112 +2 +3; the parse tree would have the sequence of operators processed as shown by the order imposed by the enclosing parenthesis: ((((2*3)*5) -112 )+2)+3
But, in the next listing (3.2), the author by the below code states to achieve, right-to-left associativity, in the right-recursive grammar.
So, now the given arithmetic expression: 2*3*5 -112 +2; the parse tree would have the sequence of operators processed as shown by the order imposed by the enclosing parenthesis: ((2*(3*5) -112 +(2+3).
Code:1 stmt_list()
2 {
3 /* Code is generated as you create the tree, before the subtree is
4 * processed.
5 */
6
7 remember= stmt();
8
9 if( not_at_end_of_input () )
10 stmt_list();
II
12 process statement( remember); //the statement is changed, as per errata at: https://holub.com/goodies/compiler/compiler.design.in.c.docs.pdf; page #19
13 }
14
15 stmt ()
16 {
17 return( read() );
18 }
Am totally confused, how the change of associativity occurs with change of position, of the line: Code:process statement( remember);, wrt the recursive call.
Code:stmt_list -> stmt stmt_list | stmt
stmt -> A | B| Cwhile on the previous page, the left recursive version is given:
Code:stmt_list -> stmt_list stmt| stmt
stmt -> A | B| C(But, am at-first, concerned with the right-recursive version.)
The figure 3.4, on page #176, shows the corresponding parse tree for a right-recursive list(grammar).
On page #177, there is listing 3.1 for: Left Associativity with a Right-Recursive Grammar; given below:
Code:1 stmt_list()
2 {
3 /* Code is generated as you create the tree, before the subtree is
4 * processed.
5 */
6
7 remember= stmt();
8
9 process statement( remember);
10
II if( not_at_end_of_input () )
12 stmt_list();
13 }
14
15 stmt ()
16 {
17 return( read() );
18 }So, it means that given the arithmetic expression: 2*3*5 -112 +2 +3; the parse tree would have the sequence of operators processed as shown by the order imposed by the enclosing parenthesis: ((((2*3)*5) -112 )+2)+3
But, in the next listing (3.2), the author by the below code states to achieve, right-to-left associativity, in the right-recursive grammar.
So, now the given arithmetic expression: 2*3*5 -112 +2; the parse tree would have the sequence of operators processed as shown by the order imposed by the enclosing parenthesis: ((2*(3*5) -112 +(2+3).
Code:1 stmt_list()
2 {
3 /* Code is generated as you create the tree, before the subtree is
4 * processed.
5 */
6
7 remember= stmt();
8
9 if( not_at_end_of_input () )
10 stmt_list();
II
12 process statement( remember); //the statement is changed, as per errata at: https://holub.com/goodies/compiler/compiler.design.in.c.docs.pdf; page #19
13 }
14
15 stmt ()
16 {
17 return( read() );
18 }
Am totally confused, how the change of associativity occurs with change of position, of the line: Code:process statement( remember);, wrt the recursive call.