Search code examples
compiler-constructionyacclex

best way to scan number of files with a specific header using Lex/yacc?


I have defined my own grammar using yacc.My language gives user a flexibility to supply a extra header explicitly while invoking compiler which will be applicable to each of file to be compiled using my compiler.

I can think of two solutions to work with this scenario.

1.Append header to each of the file and compile each of the file separately.This is not a good idea as it involves editing the given source files which is necessary to append at beginning of the file.

2.Use yywrap to iterate over the list of files to be compiled and process header every time we find a new file. This is not good as it involves repeated parsing of the same file.

Without processing the header,files doesn't satisfy the grammar.

Please share the thoughts ,how it can be done in a best way ?


Solution

  • If your compiler is intended to compile multiple files independently, as opposed to simply concatenating them and compiling them as a single unit, and the header file is required for each input file, then you have little choice but to include it during the processing of each input file. You can do that with the yywrap mechanism, or by explicitly switching buffers using yy_create_buffer and yy_switch_to_buffer. See the flex manual for more details and sample code.

    As you point out, that solution involves reparsing the header file for every input file, which might be time-consuming.

    If your parse has no effect other than to create an AST, which is later processed in order to produce the compiled output or other analysis, then you could make this process more efficient by creating the AST for the header file once, and then building the ASTs for each input file by starting with a copy of the header file's AST.

    You could even serialize the AST for the header file, and then read the serialized version instead of reparsing it, although then you will need to have some logic to verify that the serialized AST corresponds to the most recent version of the header file. This mechanism () is implemented in different forms by a number of C/C++ compilers (gcc, clang, Visual Studio, for example).