Alex Kart
Alex Kart

Reputation: 21

How to raise flex error into bison

I use flex parser in bison parser to parse lexemes from input file. Some errors (i.e. unclosed brackets or unacceptable symbols) were detected by flex parser. I want notify from flex to bison parser that error was occurred and syntax parsing should failed. Now I use code like this in .l file

RegExpForInvalidChars: {yyerror("Unaccaptable char")};

But bison when called yylex obtained empty lexeme (because I don't return any value) and ignored it. So bad symbols just skipped and parsing is going on.

Upvotes: 1

Views: 1396

Answers (2)

rici
rici

Reputation: 241671

The simplest approach is to simply pass bison a lexeme which it doesn't recognize. This will be more or less automatic if you use the standard fallback rule:

.|\n     { return yytext[0]; }

since that rule will just return the first character of an unrecognized token string.

When the parser receives a token value which it doesn't recognize, it will immediately report a syntax error.

In almost all applications, it is neither necessary nor best practice for the lexer to attempt to recognize errors which will be detected by the parser. For example, most grammars will fail on inputs containing unbalancex parentheses, so augmenting the lexer with logic to maintain a parenthesis count is unnecessary. Moreover, if you eventually decide to attempt to implement error recovery in your parser, you will find that it interacts badly with state tracking implemented in the scanner.

Upvotes: 0

Chris Dodd
Chris Dodd

Reputation: 126175

The usual way is to just have the rule:

.    { return *yytext; }

at the end of your lexer rules. That way any single character that isn't (part of) a valid lexeme will be returned directly to the bison grammar, which will cause a syntax error. If you enable bison's verbose error messages, the syntax error will include the actual invalid character in question.

Upvotes: 1

Related Questions