Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide example for the on_success() handler #738

Open
doj opened this issue Oct 4, 2022 · 4 comments
Open

provide example for the on_success() handler #738

doj opened this issue Oct 4, 2022 · 4 comments

Comments

@doj
Copy link

doj commented Oct 4, 2022

https://www.boost.org/doc/libs/1_80_0/libs/spirit/doc/html/spirit/qi/tutorials/mini_xml___error_handling.html mentions the on_success() handler, but the handler is not used in the example shown on this page. It is also not used in the full example source code https://www.boost.org/doc/libs/1_80_0/libs/spirit/example/qi/mini_xml3.cpp

It will be useful to show the on_success() handler in this example, ideally how the iterator parameters can be used to calculate line and column numbers of the input stream.

@Kojoley
Copy link
Collaborator

Kojoley commented Oct 5, 2022

Documentation improvements are highly appreciated!

Note that Qi is in maintenance mode, it is preferred to write new parsers with X3.

@djowel
Copy link
Collaborator

djowel commented Oct 5, 2022

Documentation improvements are highly appreciated!

I second that.

@drexlerd
Copy link

drexlerd commented Nov 8, 2023

Edit: it is also possible return fail in the on_success callback via _pass(context) = false.

I also would like to see more examples, especially when parsing context-sensitive languages. I am parsing a context-sensitive language and therefore, I would need to reject successful parses depending on what is stored in the Context. My approach would be to provide template specializations of the on_success callback for specific ast nodes and then throw an expectation_failure if I want to reject. Is this a reasonable approach when parsing a context-sensitive language with x3?

@cppljevans
Copy link
Contributor

cppljevans commented Nov 17, 2023

Edit: it is also possible return fail in the on_success callback via _pass(context) = false.

I also would like to see more examples, especially when parsing context-sensitive languages. I am parsing a context-sensitive language and therefore, I would need to reject successful parses depending on what is stored in the Context. My approach would be to provide template specializations of the on_success callback for specific ast nodes and then throw an expectation_failure if I want to reject. Is this a reasonable approach when parsing a context-sensitive language with x3?

This example shows 2 methods. Which method is decided by defined(USE_SEMANTIC_ACTIONS) preprocessor macro. When defined, it uses semantic actions which obscure the grammar with the semantic action expressions. When !defined, it uses a single line expression, as_attr, in the Parser grammar
which delegates the handling of the context sensitivity to the transform_attribute_tagged which is much cleaner. Maybe you could throw the expectation_failure in the [post function]

OTOH this says symbol tables are very useful for context sensitive parsers and semantic actions are one way to update the symbol table, hence, maybe the above suggested use of as_attr instead of semantic actions wouldn't work too well.

On the Other Other Hand, symbol tables are usually passed as inherited attributes, according to this, and x3 does not have inherited attributes. I'd assume inherited attribute could be simulated by storing the symbol table in the context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants