Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vcheckzen opened this issue Jun 23, 2023 · 0 comments
Open

Comments

@vcheckzen
Copy link

What I got

# 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

$ make test
(./parser input01; \
./parser input02; \
./parser input03; \
./parser input04; \
./parser input05)
15 # input01 result
29 # input02 result
syntax error on line 1, token 5 # input03 result
Unrecognised character . on line 3 # input04 result
Unrecognised character a on line 1 # input05 result
$ make test2
(./parser2 input01; \
./parser2 input02; \
./parser2 input03; \
./parser2 input04; \
./parser2 input05)
15 # input01 result
29 # input02 result
syntax error on line 1, token 5 # input03 result
Unrecognised character . on line 3 # input04 result
Unrecognised character a on line 1 # input05 result

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.

acwj/03_Precedence/expr2.c

Lines 96 to 105 in eabf90e

// Loop working on token at our level of precedence
while (1) {
// 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(arithop(tokentype), left, right, 0);

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.

 // 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); 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant