1

I need to create a rule via boost spirit that should match situations like

return foo;

and

return (foo);

I tried smth like this:

start %= "return" >> -boost::spirit::qi::char_('(') >> identifier >> -boost::spirit::qi::char_(')') >> ';';

but this will succeeded even in cases like

return (foo;

and

return foo);

How can I solve it?

FrozenHeart
  • 19,844
  • 33
  • 126
  • 242
  • What about `statement = ("return" >> identifier >> ';') | ("return" >> '(' >> identifier >> ')' >> ';')`? –  Feb 13 '14 at 15:09
  • @faranwath Yep, I already thought about it, but it looks horrible, especially in case when I have a lot of rules with the similar structure – FrozenHeart Feb 13 '14 at 15:21
  • Horrible? I guess we differ in what makes a production rule look nice, but it's your work, so fair enough. –  Feb 13 '14 at 15:23
  • Since you seem to have many of those rules, you could use the relevant parser (here identifier) as an argument to a parser handling the parenthesis. However, parsers as arguments to parsers have some problems, there is a nice post here on SO by sehe but I cannot find it right away. – Mike M Feb 13 '14 at 15:41
  • Also I need to have the same amount of open and close parentheses and I could even have two or more parentheses pairs in one expression – FrozenHeart Feb 13 '14 at 15:45

1 Answers1

4

Your example only looks pathological, because you are using an overly specific example.

In practice, you don't "return" >> identifier;. Usually, the thing that's returned is just an expression. So, you'd say

expr = literal | variable | function_call;

Now the general way to cater for parenthesized expressions in on fell swoop is simply:

expr = literal | variable | function_call
     | ('(' >> expr >> ')')
     ;

Bam. Done. It handles the balancing. It handles nested parentheses. It handles (((foo))) even. Not a whistle was given that day.

I don't think there is /anything/ wrong at all. I've posted probably over 20 recursive different expression grammars in answers on this site. They should provide motivating examples (showing operator precedence and overruling them with these parentheses).

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Can you give me an idea why does the following code crash with "stack overflow" error? http://pastie.org/8732392 Input data - http://pastie.org/8732447 I guess that it's a result of adding recursion in my rules. Am I right? How can I fix it? – FrozenHeart Feb 14 '14 at 07:22
  • Left recursion and PEG grammars :) they don't mix – sehe Feb 14 '14 at 07:24
  • Can you rephrase, please? What should I do in case of boost spirit? – FrozenHeart Feb 14 '14 at 07:30
  • This was just to fix the compilation issues (rules shadowing type names): http://coliru.stacked-crooked.com/a/25bbfd4abfc719f1 – sehe Feb 14 '14 at 07:43
  • Ok, thanks. I'm using MSVC-11.0 and boost 1.54 and it doesn't give me any errors at all. Can you answer my question about recursion in rules and stack overflow, please? – FrozenHeart Feb 14 '14 at 07:45
  • Can you post it as a proper question please? (I have the answer sitting here, but since you seem to be in a hurry...) – sehe Feb 14 '14 at 07:49
  • Ok, done - http://stackoverflow.com/questions/21773660/boost-spirit-recursion-and-stack-overflow – FrozenHeart Feb 14 '14 at 07:53