1

Following Boost.Spirit compiler examples I am migrating my Flex/Bison based calculator-like grammar to Spirit based. I want to add a feature #include<another_input.inp>. I have defined the include_statement grammar successfully. Should I follow the way error handling was doing: on_success(include_statement, annotation_function(...)), i.e. for each successful matching of include_statement, get the new input file name and call phrase_parse() again ? or like the Flex/Bison to push/pop the input stack?

Thanks.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Forest Yang
  • 179
  • 5
  • I have no clue what you actually mean. "I have defined the include_statement grammar successfully" - perhaps you could show this. In general my answer here would be: _separate parsing and interpretation_. – sehe Aug 02 '14 at 20:58
  • I meant to say AST parsing well without going into the included file. You mean the "include" should be in parsing instead of AST evaluation ? – Forest Yang Aug 03 '14 at 13:50
  • No. I agree it should be in evaluation.In which case, I simply don't get your question. What does `on_success` have to do with it (that's in-parsing) and what Flex/Bison does is unknown to me. – sehe Aug 03 '14 at 19:48

1 Answers1

1

Guessing, from the little information that is here, that you meant to ask whether you can reuse the same grammar instance, or it should be better to instantiate a new instance to parse the includes, it depends.

You can do both.

When the grammar is stateless (hint: it usually is if you can use it const) there's no difference. Otherwise, prefer to instantiate a separate instance.

However,

  • the point is somewhat moot since it appears you already decided to parse the includes after parsing the main document (if I get your comment right)
  • there's always the danger of global state; Even if the grammar object is const, you could potentially modify external state (e.g. using phx::ref from semantic action) so, this would be an issue, regardless of whether you used separate grammar instances.
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Let me put an example here: input1.txt: a = 3; b = a + 1; input2.txt: include("input1.txt"); c = a + b; These two files are using same grammar, I just want a feature to split one input into several files and include them in a master file. The approach I am using is: parse first the master file into AST and loop through it to find any "include" statement then parse the sub file again (no infinite loop detection) and insert this newly parsed AST into the master AST. But is it better to immediately parse the subfile right after the "include" statement (like recursion) ? but how easy is that? – Forest Yang Aug 05 '14 at 15:13
  • I don't see how it would be better to parse the subfile during parsing the master file. KISS rules. – sehe Aug 05 '14 at 19:33