How to return literals from flex to yacc?
In my yacc file I have things like the following:
var_declaration : type_specifier ID ';'
| type_specifier ID '[' NUM ']' ';' ;
type_specifier : INT | VOID ;
ID, NUM, INT, and VOID are tokens that get returned from flex, so yacc has no problems recognizing 开发者_如何转开发them. The problem is that in the above there are things like '[' and ';'. When these are recognized by flex, what should be returned to yacc?
You can just return the characters themselves. Tokens are guaranteed not to conflict with ASCII characters:
http://www.gnu.org/software/bison/manual/html_node/Token-Decl.html
Bison will automatically select codes that don't conflict with each other or with ASCII characters.
So in your flex file,
[\[\];] { return yytext[0]; }
is OK.
精彩评论