views:

55

answers:

1

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?

+4  A: 

You can just return the characters themselves. Tokens are guaranteed not to conflict with ASCII characters:

http://www.gnu.org/software/bison/manual/html%5Fnode/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.

Kinopiko
What does [\[\];] find?
Phenom
It is a rule which matches any one of the three characters '[', ']', or ';'.
Kinopiko
Oops, accidentally clicked the down arrow. If you edit your answer it will allow me to change it and I'll click the up arrow.I also need to match other things like () and {}. Is there something similar for those?
Phenom
Sure, use [{}] to match { or }, etc. The [ ] is a character class and this is a regular expression which matches anything inside it.
Kinopiko
@Phenom - I edited it but the downvote is still there.
Kinopiko
I had to manually changed it. It's not there anymore.
Phenom