You will need to expand that grammar before you are finished, but...
- Yes, you will replace the
printf()
statements with appropriate return
statements
- (Or, more likely/better, keep the print statements and add return statements).
- You will wrap the actions in '
{ ... }
' braces.
- You will need to consider how you are going to communicate the token type and the token value back to your parser.
The standard way is to return the token type from yylex()
- the function that is generated by Flex. There is a global variable, ylval
, which can be used to convey the token value. You can control its type. Note that somewhere along the way, you will need to specify the token numbers (token types). That can be an enumeration or a series of #defines
. Classically, the information is provided to the lexical analyzer by the parser. That is, Yacc provides a list of the token numbers that it expects to use, and the Flex uses those numbers (or, more accurately, you use those numbers in the return statements in the code generated by Flex).
To get the tokens from the lexer to your parser, you have to call yylex()
; you usually compile that separately from your parser, though you probably could include its generated source into your parser file if you really wanted to.