开发者

How to return literals from flex to yacc?

开发者 https://www.devze.com 2022-12-11 21:25 出处:网络
In my yacc file I have things like the following: var_declaration : type_specifier ID \';\' type_specifier ID \'[\' NUM \']\' \';\' ;

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消