help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Resolving shift/reduce conflicts?


From: Evan Lavelle
Subject: Re: Resolving shift/reduce conflicts?
Date: Tue, 2 Feb 2021 10:36:41 +0000
User-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.7.0

I don't know how familiar you are with this, so apologies if I'm stating
the obvious.

General observations:

1 - start by writing a skeleton with no actions; this makes it easier to
find conflicts. When you're done, add the actions

2 - this is *way*, *way* too verbose. You don't need tokens for ';',
'{', '}', etc - just write them explicitly in the description; this
makes it much easier to read. Get rid of all the $<str>$. Move all the
common body actions out to routines at the bottom of the file, etc.
yyGetParser->SetCurrentCombine("") is just noise.

3 - if you have an optional ';' in your description, try not to repeat
the body twice, once with a ';', and once without:

MethodDeclaration
 : MethodHeader jp_SEMICOL
 | MethodHeader MethodBody
 | MethodHeader MethodBody jp_SEMICOL

this is asking for conflicts, and is too verbose. Try (4) for the
conflicts, and/or this to reduce verbosity:

MethodDeclaration
 : MethodHeader jp_SEMICOL
 | MethodHeader MethodBody opt_semicolon

See also ConstructorDeclaration, etc.

4 - I think your fundamental problem is that your semicolon handling is
too complicated; I'm surprised that you only have 4 conflicts. Statement
terminators are always difficult. My procedure is to move this to the
top level, as far as possible. I can't easily explain why, but this does
seem to make it much easier to write without conflicts (and to handle
error recovery). If you're lucky, you end up with something like this,
with no other terminators in your description:

statement
 : statement_a ';'
 | statement_b ';'
 | statement_c ';'
 | /* empty */ ';'
 | ...
 ;

5 - turn the counter examples into real source code that demonstrates
the problem - that makes it much easier for us to see the issue, and may
make the resolution clearer.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]