0

So basically, in my bison file if yyparse fails (i.e there is syntax error) I want to print 'ERROR' statement and not print anything else that I do in the above part of bison file in fac the stmt part. if yyparse returns 1 is there any way to skip the content of middle part of bison file? such as idk maybe writing if statement above the stmt part etc? I would appreciate any help! Thanks in advance.

Such as :

%{
#include "header.h"
#include <stdio.h>


void yyerror (const char *s) 
{}


extern int line;

%}

%token   ...///

%// some tokens types etc...
%union
{
  St class;
  int value;
  char* str;
  int line_num;
float float_value;
}

%start prog


%%
prog:       '[' stmtlst ']'
;

stmtlst:    stmtlst stmt |
;

stmt:       setStmt | if | print | unaryOperation | expr
        {
        if ($1.type==flt && $1.line_num!=0) {
               printf("Result of expression on %d is (",$1.line_num);
        printf( "%0.1f)\n", $1.float_value);
        $$.type=flt;
        }
               else if ($1.type==integer && $1.line_num!=0){
        $$.type=integer;
              printf("Result of expression on %d is (%d)\n",$1.line_num,$1.value);
                }
                else if ($1.type==string && $1.line_num!=0) {
        $$.type=string;
              printf("Result of expression on %d is (%s)\n",$1.line_num,$1.str);
                } 
        else if ($1.type==mismatch && $1.line_num!=0)
                {
        $$.type=mismatch;
                  printf("Type mismatch on %d \n",$1.line_num);

            }
    else{ }
 }
%%

int main ()
{
if (yyparse()) {
// if parse error happens only print this printf and not above stmt part
printf("ERROR\n");
return 1;
}
else {
// successful parsing
return 0;
}
}
rici
  • 234,347
  • 28
  • 237
  • 341
Beg
  • 21
  • 6
  • Have you read the documentation on error recovery (https://www.gnu.org/software/bison/manual/html_node/Error-Recovery.html#Error-Recovery) – SoronelHaetir Apr 12 '21 at 20:21
  • @SoronelHaetir well I am not trying to recover error here, I want to skip other printf statements except the one in yyparse section – Beg Apr 12 '21 at 20:25

1 Answers1

1

Unfortunately, time travel is not an option. By the time the error is detected, the printf calls have already happened. You cannot make them unhappen.

What you can so (and must do if you are writing a compiler rather than a calculator) is create some data structure which represents the parsed program, instead of trying to execute it immediately. That data structure -- which could be an abstract syntax tree (AST) or a list of three-address instructions, or whatever, will be used to produce the compiled program. Only when the compiled program is run will the statements be evaluated.

rici
  • 234,347
  • 28
  • 237
  • 341
  • Thank you. This was the answer that I was afraid of :D I though of creating an array and appending each value that I want to print to it and finally in main I will print elements of the array with a printf statement however I store both string values and integer values, so what kind of an array it should be I am lost here maybe you can help out ?Thanks btw. @rici – Beg Apr 12 '21 at 20:39
  • @beg: assuming you will eventually want to include loops in your language, as well as normal stuff like reading user input, you cannot evaluate the parsed text as you go. You must store the structure of yhe parsed text, not the result of evaluating it. Search for "abstract syntax tree" and/or "three address code" and/or "intermediate representation". You'll find lots of ideas. – rici Apr 12 '21 at 20:54
  • Also, you.might try workng through [*Structure and Interpretation of Computer Programs*](https://mitpress.mit.edu/sites/default/files/sicp/index.html). Think of it as a kind of mental yoga: stretching exercises. – rici Apr 12 '21 at 20:58
  • Actually what I am doing is not really a complicated language I only deal with operators like + - * etc and I want to print only the results of them like float integer or a concatenated string. Im guessing your suggestions are way harder things than mine. But thanks a lot! @rici – Beg Apr 12 '21 at 21:16
  • language is a made up one. and my goal is ["+",3,2] result is 5 or ["+", 'Hello','World'] result is HelloWorld etc – Beg Apr 12 '21 at 21:18
  • @Beg: you don't know how hard it is until you try it. Maybe it's way easier than you think. Maybe it's a bit hard but doing it teaches you something really fascinating and useful. SICP definitely changed yhe way I think about programming. But anyway. If you're just going to print the numbers, you could convert them to strings before you put them in the list. See `snprintf` or `asprintf` if you have it in your C library. Or you could use a *discriminated union* (another phrase to search for). – rici Apr 12 '21 at 21:37
  • Thanks so much. I will search and try to implement the methods you mentioned. Have a nice one – Beg Apr 12 '21 at 22:15
  • Error recovery is hard. Simplest thing you could do is: resynchronise on the ';' (or on the ;\n' ,if you are a hipster) The cheapest way is to unwind&dump the stack. – wildplasser Apr 12 '21 at 23:48