I'm learning and practicing bison. Currently I put all of the grammar in one .y file.
However, when the grammar grows, it become hard to read.
So I wonder if I could separate them into different files....
I've searched the bision manual but haven't find anything..
Bison does not have this feature, although some other yacc derivatives do.
Unfortunately, context-free grammars cannot be composed in any meaningful way; the entire grammar needs to be processed to build the parsing tables. So it is necessary to construct a complete grammar file, if only as a temporary; a mechanism similar to the C preprocessor's #include
directive would be necessary. (Adding support for conditionals would also sometimes be handy.)
A simple-minded preprocessor which handles a small subset of the C preprocessor would be easy to write (some people even use sed
for this purpose, although that wouldn't be my first choice), or you could use a standard macro processor like m4
(which bison depends on, so you must have it installed). It's straightforward to add a preprocessing step to a Makefile
, so the build process can still be automated. But there is one big shortcoming: line numbers in the preprocessed file will bear no relationship to line numbers in the original source, so the preprocessed file needs to be kept around for debugging (or even sometimes to understand compiler warnings and errors).
Independent of the above, one way to keep grammar files short is to avoid using long-winded actions. To the extent possible, reduction actions should be kept to single function calls. (This is a natural way to write grammars which produce an AST, for example.) Moreover, the action functions themselves should be placed in a different C source file (or even multiple C source files) so that they don't clutter up the grammar file.
Some code editors which implement folding do know how to fold grammar files, although the feature is rarely if ever enable by default. That can also make grammars easier to read.