#63·acwj

Test output of 03_Precedence is not the same as what is in Readme

Author: vcheckzenCreated Jun 23, 2023Updated Jun 23, 2023

What I got

bash
# make test2
cc -o parser2 -g expr2.c interp.c main.c scan.c tree.c
(./parser2 input01; \
 ./parser2 input02; \
 ./parser2 input03; \
 ./parser2 input04; \
 ./parser2 input05)
15
29
syntax error on line 1, token 1
Unrecognised character . on line 3
Unrecognised character a on line 1

And what is in Readme

https://github.com/DoctorWkt/acwj/blob/14e9397369dcd428ee7ba6dcf290407403f4c4e7/03_Precedence/Readme.md?plain=1#L392-L414

The parser2's result on input03 is with a wrong token type, it should be 5, not 1. The following is the content of input03

12 34 + -56 * / - - 8 + * 2

Now the parse2 reports error on token + whose enum value equals 1. It should reports on 34 instead, with enum value 5, because the position of which shoude be an operator.

https://github.com/DoctorWkt/acwj/blob/eabf90e74606a2a68fb64326b792d8c4804bc5b2/03_Precedence/expr2.c#L96-L105

On the preceding code, the operator is checked after parsing the right sub tree. I think the check action should be done first with this code to get a right result.

c
 // Loop working on token at our level of precedence 
 while (1) { 
   // Check if the middle position is an operator
   int precedence = arithop(tokentype);

   // Fetch in the next integer literal 
   scan(&Token); 
  
   // Get the right sub-tree at a higher precedence than us 
   right = multiplicative_expr(); 
  
   // Join the two sub-trees with our low-precedence operator 
   left = mkastnode(precedence, left, right, 0);