I'm working on the simple aql parser with flex+bison as a toolkit. Right now, I'm trying to provide helpful feedback when a syntax error occurs. I want to show user somethink like this:
Syntax error:
<line_index>: INSERT { name: "John Doe", 1123: 2, is_working: true } IN users
^^^^
I already got information where syntax error occurred (line and column), but I really cant come up with getting error line. I tried to store global line buffer current_line and write content of yytext in YY_USER_ACTION macro. This way:
#define YY_USER_ACTION \
strcpy(current_line+cl_length, yytext); \
for(int i = 0; yytext[i] != '\0'; i++) { \
if(yytext[i] == '\n') { \
memset(current_line, 0, cl_length);
cl_length = 0;
} else {
cl_length++;
}
}
}
But I got duplicated characters at the start of lines because of usage start conditions for lexing python-like indent scopes.
Is there different way to do it with flex macro or something?
Lex is not line oriented, so does not provide any explicit help with notions like line number. The regex operators ^,$ muddy the distinction, but you can readily handle line numbers in your scanner with a rule like:
which makes it easy for something like
yywrap()to transparently funnel multiple files to the lex scanner:There may be other methods with flex, which substantially augments lex.