Properly Including parser.h in parser.c When Located in Different Directories with Bison

54 Views Asked by At

I'm working on a project where I'm using Bison to generate a parser, and Bison generates parser.c in one directory (let's call it "src/parser") and parser.h in another directory (let's call it "include/parser"). My project structure looks like this:

- project_directory/
  - grammar/
    - grammar.y
    - scanner.l
  - src/parser
    - parser.c
  - include/parser
    - parser.h

Now, my question is, how do I include parser.h correctly in parser.c considering they are in different directories?

With flex I am using the following %options and I have the desired result with no problem compiling.

%option noyywrap yylineno
%option outfile = "src/lexer/scanner.c" header-file="include/lexer/scanner.h"

So I tried doing the same thing with bison and in my grammar.y file I wrote

%header "include/parser/parser.h"
%output "src/parser/parser.c"

But bison generates the parser.c file with #include "parser.h" which results in the following error:

src/parser/parser.c:105:10: fatal error: parser.h: No such file or directory
  105 | #include "parser.h"
      |          ^~~~~~~~~~
compilation terminated

When I change by hand the include statement to #include "parser/parser.h" it compiles just fine, as well when I remove the %header declaration from my bison file it compiles with no errors as expected.

0

There are 0 best solutions below