help-bison
[Top][All Lists]
Advanced

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

Re: text is not parsed correctly due to shift/reduce conflict


From: Christian Schoenebeck
Subject: Re: text is not parsed correctly due to shift/reduce conflict
Date: Mon, 26 Jul 2021 14:07:06 +0200

On Sonntag, 25. Juli 2021 14:48:11 CEST Hans Åberg wrote:
> Indeed, there is an example in the Bison manual, sec. 1.5.2, on how to use
> GLR resolve C++ style ambiguities. C++ is not LALR, so if using that, one
> has to write a grammar for a larger language and cut it down in the
> actions.
> > On 25 Jul 2021, at 09:45, Alex Shkotin <alex.shkotin@gmail.com> wrote:
> > 
> > or try %glr-parser command - it helps in my case:-)
> > 
> > сб, 24 июл. 2021 г. в 21:16, Hans Åberg <haberg-1@telia.com>:
> > > On 24 Jul 2021, at 16:34, Guenther Sohler <guenther.sohler@gmail.com>
> > > wrote:
> > > 
> > > When trying to code a c language parser I got a issue with shift/reduce
> > > conflict in bison, which actually hurts me.
> > 
> > You might check the LALR(1) grammars for C and C++ others have done. Two
> > examples:
> > https://isocpp.org/wiki/faq/compiler-dependencies#yaccable-grammar
> > http://www.quut.com/c/ANSI-C-grammar-y.html

Actually in Guenther's particular case his grammar definition is simply wrong, 
i.e. there is no need for grabbing the GLR hammer just for that.

On Samstag, 24. Juli 2021 16:34:15 CEST Guenther Sohler wrote:
> my rules are like this:
> 
> functiondefinition:
> type_ptr identifier '(' vardeclarationlist ')' compound_statement ;

That would assume the following code would be valid C:

int myFunction(int a, b /*wrong*/, c /*wrong*/) {
}

Which is not. You have to separate in the grammar definition between variable 
declaration inside a scope/block, example:

        void foo() {
                int a, b, c; // allowed here
        }

and declaration of function arguments. E.g. change your grammar definition to 
something like (not tested):

functiondefinition:
        type_ptr identifier '(' arglist ')' compound_statement ;

arglist :
        arg |
        arglist | arg;

arg :
        type_ptr identifier;

> vardeclarationlist :
> vardeclaration |
> vardeclarationlist ',' vardeclaration  ;
> example: int a, int b=2
> 
> vardeclaration:
> type_ptr identifierlist  ;
> example: int *b
> 
> identifierlist :
> identifierwithdefault |
> identifierlist ',' identifierwithdefault
> 
> identifierwithdefault:
> identifier |
> identifier '=' expression |
> identifier '[' INTNUM ']'
> 
> 
> type_ptr :
> type_ptr '*' |
> type ;
> example: int

Best regards,
Christian Schoenebeck





reply via email to

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