"Hello, World!" example does not seem to work #168
Replies: 11 comments
-
The example parses the argument with a grammar that expects an input in a particular form, otherwise it will - correctly - produce a parse error. Try |
Beta Was this translation helpful? Give feedback.
-
(Since the PEGTL is a parser library, the "Hello, World!" example is a kind of reverse-hello-world, it starts by parsing the arguments, rather than just printing something.) |
Beta Was this translation helpful? Give feedback.
-
My apology, the question was invalid. Thank you for your help! |
Beta Was this translation helpful? Give feedback.
-
No harm done, we all have to start somewhere. Feel free to ask any further questions about understanding the example (or using the PEGTL in general) here... |
Beta Was this translation helpful? Give feedback.
-
Dear @ColinH , I am now very happily using PEGTL but there is one fragment in my grammar that somehow does not work. The fragment is the following grammar: struct C; where A is a simple grammar which works well: Should be the following more readable: The thing that I do not understand is that trying to parse " ( ab | cd ) " goes terribly wrong unless I completely remove the W structure: When W is removed, the input string is parsed flawlessly and the program finds the correct execution: " C" -> " B" -> " ( C| C)" -> " ( A | A ) " -> " ( ab | cd ) " The question is: Is there any issue with the grammar? What does a context free grammar without left recursion must fulfill to be good with PEGTL? Thank you very much for your help and for making the PEGTL tool. |
Beta Was this translation helpful? Give feedback.
-
The PEGTL can not be used with context-free grammars, instead you need to use parsing expression grammars (PEGs). The two are similar and might look identical, but there are differences with what language they expression. I currently don't see the problem immediately, although I suspect that the greedy nature of PEGs plays a role here. You can, however, use the tracer to see how the grammar is working. You can find the tracer in the contrib-section, an example is available here: https://github.com/taocpp/PEGTL/blob/master/src/example/pegtl/uri_trace.cpp |
Beta Was this translation helpful? Give feedback.
-
Thank you for your tips - the tracer is very helpful. I am quite sure the following grammar is PEG but still there is some problem when parsing "(1|2)" by grammar C: struct C; The output of parse<C, action> of "(1|2)" would be: Matched digit with '1' The grammar generates language containing "(1|2)" and it has no issues when tested by PEGTL analyze tool: C -> B -> (C|C) -> (digit|digit) -> (1|2). Could anyone explain this behavior? |
Beta Was this translation helpful? Give feedback.
-
Analysis only catches infinite loops (aka (indirect) left recursion). Can you give us the REAL code? The above excerpt doesn't references 'H' and 'star' is incomplete, so I can't really know what's going on. Please create a small but complete program that shows the problem. I used this program to confirm that a boiled-down version of your grammar works as expected: #include <tao/pegtl.hpp>
#include <tao/pegtl/contrib/tracer.hpp>
using namespace tao::pegtl;
struct C;
struct B : seq< one< '(' >, C, one< '|' >, C, one<')'> > {};
struct C : sor< plus< digit >, B > {};
int main()
{
memory_input in( "(1|2)", "" );
return parse< C, nothing, tracer >( in );
} |
Beta Was this translation helpful? Give feedback.
-
Thank you for the boiled-down version. Unfortunately, I need the grammar to parse both "(1|2)" and "1|2" (or even "(1|((1|1)|2)|1|(2|2|2))"). I would love to parse logical expressions. This is the smallest grammar I was able to think of that shows my problem:
Thank you very much for looking on it. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the complete program, now I see the problem. As I suspected earlier, it has to do with the greedy nature of PEGs. When you pass |
Beta Was this translation helpful? Give feedback.
-
Thank you so much, it is clear to me now. |
Beta Was this translation helpful? Give feedback.
-
Executing
./pegtl-example-hello_world 'h'
throws:
terminate called after throwing an instance of 'tao::pegtl::parse_error'
what(): parse error matching hello::prefix
Aborted (core dumped)
Beta Was this translation helpful? Give feedback.
All reactions