Home > Enterprise >  How to get any additional info about the occurred error in Bison?
How to get any additional info about the occurred error in Bison?

Time:11-13

I'm just starting with Flex/Bison and I'm trying to translate pascal-look-alike variables declarations like this:

VAR
    V1: INT;
    V2: INT;
END_VAR

into C variables declarations like this:

int main ()
{
    int V1;
    int V2;
}

I wrote lex- and yacc-files describing needed grammar, then compiled it with flex, bison and gcc utilities in cmd. It's all done without error. But when I try to push a code file with several variables declarations (same as the first code example) in executable file, I only get "syntax error" written in cmd. I don't know how to get any additional info about this error, though the code has to be very simple.

--- UPDATE ---

I used Öö Tiib answer and it helped to get info about the error.

I was looking for a mistake in my lex-file: the program was expecting ':' character, but it faced some undefined character. That was because the space character in regular expression [; j] caused lexer to identify it as the token, though white spaces should be ignored as written in regular expression [ \t\n]. That's how lex-file shall look then:

%{
    #include "parser.tab.h"
    #include <string.h>
%}

%%
[a-z]([a-z]|[0-9])* {
    strcpy (yylval.var, yytext);
    return ID;
}

"VAR"               { return VAR; }
"END_VAR"           { return END_VAR; }
"INT"               { return INT; }
[:;]                { return *yytext; }
[ \t\n];
%%

int yywrap ()
{
    return 1;
}

CodePudding user response:

For more informative error strings you need %error-verbose in yacc file. Perhaps like that in your file:

%{
    #include <stdio.h>

    extern FILE * yyout;
    extern char * yylex ();
    void yyerror (char *s);
%}
%error-verbose

%union
{
    int number;
    char var [10];
}

...

More information on error analysis are in manual, your question does not indicate how far you are there with your studies.

  • Related