I'm trying to create my own errors, but apparently, envoking yyerror() is not enough to tell parser that there is an error. I made a little example to describe my problem better. So here is a parser that must check if a statement is two numbers with comma between them. And numbers can not start with 0.
input for yacc:
%token DIGIT
%{
#include <stdio.h>
#include <stdlib.h>
void yyerror(char *s);
%}
%%
list: |
list stat ';' {printf("The statement is correct!\n\n");} |
list error ';' {printf("The statement is incorrect!\n\n");}
stat: number ',' number
number: DIGIT {if ($1==0) yyerror("number starts with 0");} |
number DIGIT {$$ = $1*10+$2;}
%%
extern int linenum;
void yyerror(char *s) {
fprintf(stderr, " line %d: %s\n", linenum, s);
}
for lex:
%{
#include <stdio.h>
#include "y.tab.h"
int linenum = 1;
%}
%%
[0-9] {
yylval = yytext[0] - '0';
return DIGIT;
}
[ \t\r]+ ;
\n ++linenum;
. return(yytext[0]);
Input for the parser:
34, 43;
32,fs;
03, 23;
And here is the output:
The statement is correct!
line 2: syntax error
The statement is incorrect!
line 3: number starts with 0
The statement is correct!
Even though the error on the 3rd line is found, parsing still continues. How can I fix it?
Upd: the problem was solved by using YYERROR;