In some languages a new scope can be opened and closed again. In C++ this would be the curly brace. Assuming one creates a new scope on encountering an opening curly brace and closes it on encountering the matching closing curly brace. What would happen in case of a parse error before the closing curly brace is encountered? How could one make certain, that the scope opened by the opening curly brace is properly trashed?
-
How does this have anything to do with Spirit? It seems more suited for programming.se, and it is really only tangentially related to parsing at all - regardless of what tools you use to help generating parser code. – sehe Jun 30 '23 at 15:13
-
@sehe This is a question for boost-spirit. Can one simply rely on that the object created by the semantic action for the opening brace is property destroyed when it is not being needed anymore, e.g. when the rule is finished successfully or by an error? – Frank Puck Jun 30 '23 at 15:31
-
Okay, I've answered that reduced question. I still maintain that the sematics of scopes doesn't come in during parsing - at all. The only reason to conflate parsing with interpretation (or even execution) would be if you need to be executing the script before you have received it. While possible, it does raise a lot of red flags related to security and atomicity or updates. I'd like my script to fail to run if it contains a syntax error near the end, not to have executed partly leaving my data in a broken state. – sehe Jun 30 '23 at 15:54
2 Answers
@sehe This is a question for boost-spirit. Can one simply rely on that the object created by the semantic action for the opening brace is property destroyed when it is not being needed anymore, e.g. when the rule is finished successfully or by an error?
How else would it work? Deterministic destruction at object lifetime is a property of the language. So, unless you're willing to assume Qi leaks memory by design, you're good to go.
Of course, you can forcibly create a memory leak if you insist, e.g. eps[px::new_<int>(42)]
, but you're able to do the same without Spirit (just use the memory leak operator, operator* new
).
Now this comes into play especially/only when you want to use dynamic polymorphism in your AST types:
- How can I use polymorphic attributes with boost::spirit::qi parsers?
- Parsing inherited struct with boost spirit
As I've said before:
You're using semantic actions. As a rule this already misses the sweet spot for embedded grammars, which is why I linked
126135 answers to Boost Spirit: "Semantic actions are evil"?.
Make that 136 :)
There was this time where I fixed the leaks in a grammar that uses pointer attributes: Building a Custom Expression Tree in Spirit:Qi (Without Utree or Boost::Variant)

- 374,641
- 47
- 450
- 633
If parse error occurs before the matching closing brace, it will be taken as invalid scope. The compiler in this case will report it as parse error:
You should check for the semicolons in your code either there is a missing one.
You can restructure your code to avoid parse error.
There are some online tools like onlinegdb that can resolve these issues to some extent by beautifying your code.

- 174
- 5