#Trying to fix a parser to handle different test cases

2 messages · Page 1 of 1 (latest)

grizzled mural
#

The issue is this block:

statement:
    expression |
    REDUCE operator reductions ENDREDUCE {$$ = $3;} |
    IF expression THEN statement_ ELSE statement_ ENDIF {$$ = ($2 ? $4 : $6);} |
    CASE expression IS cases OTHERS ARROW statement_ ENDCASE {$$ = isnan($4) ? $7 : $4;} ;

it gives me the wrong result for this test case:

-- Conditional expression 

function main returns integer; 
begin 
   if not (5 + 4 >= 9) then 
       6 + 9 * 3; 
   else 
       8 - 9 rem 7; 
   endif; 
end; 

my output:
   1  -- Conditional expression

   2  

   3  function main returns integer;

   4  begin

   5      if not (5 + 4 >= 9) then

   6          6 + 9 * 3;

   7      else

   8          8 - 9 rem 7;

   9      endif;

  10  end;

      

Compiled Successfully

Result = 33

expected output:
   1  -- Conditional expression

   2  

   3  function main returns integer;

   4  begin

   5      if not (5 + 4 >= 9) then

   6          6 + 9 * 3;

   7      else

   8          8 - 9 rem 7;

   9      endif;

  10  end;

      

Compiled Successfully

Result = 6

I know I can fix it by changing the block to this and it'll work:
statement:

expression |
   REDUCE operator reductions ENDREDUCE {$$ = $3;} |
   IF expression THEN statement_ ELSE statement_      ENDIF {$$ = ($2 ? false : true) ? $4 : $6;} |
   CASE expression IS cases OTHERS ARROW statement_ ENDCASE {$$ = isnan($4) ? $7 : $4;} ;

The issue is that I have to get multiple testcases to work and the first way works for other testcases. if you need the full parser, I can post it (just to long to post in the original post)

dreamy lodgeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.