Bison custom syntax error

478 Views Asked by At

I have some grammar rules for a C compiler and translator to Matlab language. I want to capture the syntax error due to missing ';' at the end of a statement.

For example I have the return statement:

  stmt_return :     RETURN      {...some actions...}
                    exp ';'     {...others actions...}

              |     RETURN      {...some actions...}
                    ';'         {...others actions...}

How can I handle the lack of ';' and print a custom error message instead of the default message "syntax error".

I tried to add these rules but rightly produce conflicts:

  stmt_return :     RETURN  exp    { yyerror("...")}

              |     RETURN { yyerror("...")}
1

There are 1 best solutions below

0
On

I found this solution:

stmt_return :     RETURN      {...some actions...}
                  exp sc     {...others actions...}

            |     RETURN      {...some actions...}
                  sc         {...others actions...}
            ;

sc          : ';'
            | { yyerror("Missing ';'"); }   error
            ;