I'm wanting to work on a toy language with a Flex/Bison-generated parser with bindings to Rust. For simplicity, I'd like to have Bison simply call a Rust-defined createCstNode()
function for each matched rule to create a concrete syntax tree (which will be translated into an AST in Rust for further processing). The function call will need to include the matched rule type as an argument so that my Rust code will know what type of node it is (expression, if-statement, while-statement, function call, literal string, number, etc.).
From looking through the generated Bison parser, it looks like there is a variable yyn
which seems to be an integer representing the matched rule, though I haven't seen it documented anywhere. I'm aware that the %defines
option will give me an enum of tokens in parser.tab.h
, but I need both terminal and non-terminal symbols enumerated. I also have seen the %token-table
option, which gives non-terminal symbols as well, but isn't quite what I need either, and also goes straight into the parser.tab.c
file rather than the parser.tab.h
file, which makes using something like rust-bindgen more difficult.
So is there any way to have Bison generate an enum similar to the yytokentype
enum, which includes non-terminal symbols, and is placed into a header file? Or am I stuck with defining my own enum for the CST node types which match up with the symbols I have? Is yyn
documented anywhere? Is it safe to use as a way of identifying the rule which was matched in an action? Is there some better way I can be going about this?