shift/reduce and reduce/reduce conflict in ANSI C Yacc grammar

View: New views
12 Messages — Rating Filter:   Alert me  

shift/reduce and reduce/reduce conflict in ANSI C Yacc grammar

by osama abdelwahed :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi all
I transformed ANSI C Yacc grammar into sablecc but I got shift/reduce
and reduce/reduce conflicts.
This happened  although I removed if-else definition from the grammar
because I know that there is one shift/reduce conflict in if-else
statement.
Is there is any body can help for that  problem ?
Also by adding if-else definition in the grammar, how can I solve that
one shift/reduce conflict to implement my project( the semantic
analyzer of c language).
Thanks in advance.

--
english

_______________________________________________
SableCC-Discussion mailing list
SableCC-Discussion@...
http://lists.sablecc.org/listinfo/sablecc-discussion

Re: shift/reduce and reduce/reduce conflict in ANSI C Yacc grammar

by osama abdelwahed :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi all , the following is the error messages which I got, maybe help:

shift/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TOrOp in {
        [ PLogicalOrExpr = PLogicalOrExpr * TOrOp PLogicalAndExpr ] (shift),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TOrOp (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TRpar in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TRpar (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TRpar (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TRbra in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TRbra (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TRbra (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TComma in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TComma (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TComma (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TColon in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TColon (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TColon (reduce)
}

shift/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TQuestion in {
        [ PCondExpr = PLogicalOrExpr * TQuestion PExpr TColon
PCondExpr ] (shift),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TQuestion (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TChar in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TChar (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TChar (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TConst in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TConst (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TConst (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TDouble in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TDouble (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TDouble (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TEnum in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TEnum (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TEnum (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TFloat in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TFloat (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TFloat (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TInt in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TInt (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TInt (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TLong in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TLong (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TLong (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TShort in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TShort (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TShort (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TSigned in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TSigned (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TSigned (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TStruct in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TStruct (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TStruct (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TUnion in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TUnion (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TUnion (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on
TUnsigned in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TUnsigned (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TUnsigned (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TVoid in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TVoid (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TVoid (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on
TVolatile in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TVolatile (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TVolatile (reduce)
}
java.lang.RuntimeException:

shift/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TOrOp in {
        [ PLogicalOrExpr = PLogicalOrExpr * TOrOp PLogicalAndExpr ] (shift),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TOrOp (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TRpar in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TRpar (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TRpar (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TRbra in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TRbra (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TRbra (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TComma in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TComma (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TComma (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TColon in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TColon (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TColon (reduce)
}

shift/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TQuestion in {
        [ PCondExpr = PLogicalOrExpr * TQuestion PExpr TColon
PCondExpr ] (shift),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TQuestion (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TChar in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TChar (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TChar (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TConst in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TConst (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TConst (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TDouble in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TDouble (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TDouble (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TEnum in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TEnum (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TEnum (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TFloat in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TFloat (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TFloat (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TInt in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TInt (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TInt (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TLong in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TLong (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TLong (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TShort in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TShort (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TShort (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TSigned in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TSigned (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TSigned (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TStruct in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TStruct (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TStruct (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TUnion in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TUnion (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TUnion (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on
TUnsigned in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TUnsigned (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TUnsigned (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on TVoid in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TVoid (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TVoid (reduce)
}

reduce/reduce conflict in state [stack: TLpar PLogicalOrExpr *] on
TVolatile in {
        [ PCondExpr = PLogicalOrExpr * ] followed by TVolatile (reduce),
        [ PLogicalOrExpr = PLogicalOrExpr * ] followed by TVolatile (reduce)
}
        at org.sablecc.sablecc.GenParser.caseStart(Unknown Source)
        at org.sablecc.sablecc.node.Start.apply(Unknown Source)
        at org.sablecc.sablecc.SableCC.processGrammar(Unknown Source)
        at org.sablecc.sablecc.SableCC.processGrammar(Unknown Source)
        at org.sablecc.sablecc.SableCC.main(Unknown Source)

On Sun, Jan 4, 2009 at 12:03 AM, osama abdelwahed
<osama.abdelwahed@...> wrote:

> Hi all
> I transformed ANSI C Yacc grammar into sablecc but I got shift/reduce
> and reduce/reduce conflicts.
> This happened  although I removed if-else definition from the grammar
> because I know that there is one shift/reduce conflict in if-else
> statement.
> Is there is any body can help for that  problem ?
> Also by adding if-else definition in the grammar, how can I solve that
> one shift/reduce conflict to implement my project( the semantic
> analyzer of c language).
> Thanks in advance.
>
> --
> english
>



--
english

_______________________________________________
SableCC-Discussion mailing list
SableCC-Discussion@...
http://lists.sablecc.org/listinfo/sablecc-discussion

Re: shift/reduce and reduce/reduce conflict in ANSI C Yacc grammar

by Simon Richter-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

> I transformed ANSI C Yacc grammar into sablecc but I got shift/reduce
> and reduce/reduce conflicts.

The C grammar cannot be handled with SableCC because of the

"typedef-name: identifier"

rule in the C standard, which makes the entire grammar context dependent.
In order to parse C code, you need a list of identifiers that are typedef
names and use a separate terminal for typedef-name.

   Simon (who also tried)

_______________________________________________
SableCC-Discussion mailing list
SableCC-Discussion@...
http://lists.sablecc.org/listinfo/sablecc-discussion

Re: shift/reduce and reduce/reduce conflict in ANSI C Yacc grammar

by Hisashi Nakai :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi,

  I tried to translate a C grammar for yacc into sablecc's one.
  The original one I have can be got from

http://www.csd.uwo.ca/~watt/home/courses/2008-09/cs4447a/parts/ansi-c-grammar/gram.y

#  I got it from the internet using ftp some little time ago...

  It includes one shift/reduce conflict, which is made from dangling-
else.

  I translated straightforwardly, but I met some messages about conflicts
maybe they do not look like the same for dangling-else's one. I think
it is an LALR(1) grammar, and, the both yacc and sablecc are based on
LALR(1), then I think the sablecc should accept as yacc (of course,
except for one shift/reduce conflict).

  Can someone help this or tell me the pointer to refer about this
problem ?

  Thanks in advance.

In Message-ID: <20090103185117.GA25981@...>
Simon Richter <Simon.Richter@...> wrote  :

> Hi,
>
> > I transformed ANSI C Yacc grammar into sablecc but I got shift/reduce
> > and reduce/reduce conflicts.
>
> The C grammar cannot be handled with SableCC because of the
>
> "typedef-name: identifier"
>
> rule in the C standard, which makes the entire grammar context dependent.
> In order to parse C code, you need a list of identifiers that are typedef
> names and use a separate terminal for typedef-name.
>
>    Simon (who also tried)
>
> _______________________________________________
> SableCC-Discussion mailing list
> SableCC-Discussion@...
> http://lists.sablecc.org/listinfo/sablecc-discussion
>
--
Nakai, Hisashi
University of Tsukuba
E-mail: nakai@...

_______________________________________________
SableCC-Discussion mailing list
SableCC-Discussion@...
http://lists.sablecc.org/listinfo/sablecc-discussion

Re: shift/reduce and reduce/reduce conflict in ANSI C Yacc grammar

by Etienne M. Gagnon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Hisashi,

Without your translated version, it is hard to tell. If the only LALR(1) conflict in the original grammar is the if/else shift-reduce conflict, then the usual stmt-noif solution should have eliminated all conflicts. So, either there was a little mistake in the solution, either the original grammar had additional conflicts. SableCC 2 and 3 are effectively LALR(1). Actually, SableCC 3 does even automatically resolve some LALR(1) conflicts by inlining conflicting productions.

My recommendation is to check your stmt-noif transformations and also verify that the original Yacc warning report didn't include additional conflicts. SableCC only reports the first conflict it finds.

Have fun!

Etienne


Hisashi Nakai a écrit :
Hi,

  I tried to translate a C grammar for yacc into sablecc's one.
  The original one I have can be got from 

http://www.csd.uwo.ca/~watt/home/courses/2008-09/cs4447a/parts/ansi-c-grammar/gram.y

#  I got it from the internet using ftp some little time ago...

  It includes one shift/reduce conflict, which is made from dangling-
else.

  I translated straightforwardly, but I met some messages about conflicts
maybe they do not look like the same for dangling-else's one. I think
it is an LALR(1) grammar, and, the both yacc and sablecc are based on 
LALR(1), then I think the sablecc should accept as yacc (of course,
except for one shift/reduce conflict).

  Can someone help this or tell me the pointer to refer about this
problem ?

  Thanks in advance.

In Message-ID: 20090103185117.GA25981@...
Simon Richter Simon.Richter@... wrote  :

  
Hi,

    
I transformed ANSI C Yacc grammar into sablecc but I got shift/reduce
and reduce/reduce conflicts.
      
The C grammar cannot be handled with SableCC because of the

"typedef-name: identifier"

rule in the C standard, which makes the entire grammar context dependent.
In order to parse C code, you need a list of identifiers that are typedef
names and use a separate terminal for typedef-name.

   Simon (who also tried)

_______________________________________________
SableCC-Discussion mailing list
SableCC-Discussion@...
http://lists.sablecc.org/listinfo/sablecc-discussion

    
--
Nakai, Hisashi
University of Tsukuba
E-mail: nakai@...

_______________________________________________
SableCC-Discussion mailing list
SableCC-Discussion@...
http://lists.sablecc.org/listinfo/sablecc-discussion

  

-- 
Etienne M. Gagnon, Ph.D.
SableCC:                                            http://sablecc.org


_______________________________________________
SableCC-Discussion mailing list
SableCC-Discussion@...
http://lists.sablecc.org/listinfo/sablecc-discussion

signature.asc (265 bytes) Download Attachment

Re: shift/reduce and reduce/reduce conflict in ANSI C Yacc grammar

by Hisashi Nakai :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi Etienne,

Message-ID: <4964BDE6.2010202@...> の記事にて
"Etienne M. Gagnon" <egagnon@...> さんは書かれました :

> Hi Hisashi,
>
> Without your translated version, it is hard to tell. If the only LALR(1)
> conflict in the original grammar is the if/else shift-reduce conflict,
> then the usual stmt-noif solution should have eliminated all conflicts.
> So, either there was a little mistake in the solution, either the
> original grammar had additional conflicts. SableCC 2 and 3 are
> effectively LALR(1). Actually, SableCC 3 does even automatically resolve
> some LALR(1) conflicts by inlining conflicting productions.
>
> My recommendation is to check your stmt-noif transformations and also
> verify that the original Yacc warning report didn't include additional
> conflicts. SableCC only reports the first conflict it finds.

  I removed the if-else rule and then yacc did not issue any message.
  Of course, my translation may include something wrong.
  Please check my grammar, which I put on

http://nakataf.slis.tsukuba.ac.jp/~nakai/c.grammar

#  If the member does not mind, I'll send the file with mail
#  next time.

> Have fun!
>
> Etienne
>
>
> Hisashi Nakai a écrit :
> > Hi,
> >
> >   I tried to translate a C grammar for yacc into sablecc's one.
> >   The original one I have can be got from
> >
> > http://www.csd.uwo.ca/~watt/home/courses/2008-09/cs4447a/parts/ansi-c-grammar/gram.y
> >
> > #  I got it from the internet using ftp some little time ago...
> >
> >   It includes one shift/reduce conflict, which is made from dangling-
> > else.
> >
> >   I translated straightforwardly, but I met some messages about conflicts
> > maybe they do not look like the same for dangling-else's one. I think
> > it is an LALR(1) grammar, and, the both yacc and sablecc are based on
> > LALR(1), then I think the sablecc should accept as yacc (of course,
> > except for one shift/reduce conflict).
> >
> >   Can someone help this or tell me the pointer to refer about this
> > problem ?
> >
> >   Thanks in advance.
> >
> > In Message-ID: <20090103185117.GA25981@...>
> > Simon Richter <Simon.Richter@...> wrote  :
> >
> >  
> >> Hi,
> >>
> >>    
> >>> I transformed ANSI C Yacc grammar into sablecc but I got shift/reduce
> >>> and reduce/reduce conflicts.
> >>>      
> >> The C grammar cannot be handled with SableCC because of the
> >>
> >> "typedef-name: identifier"
> >>
> >> rule in the C standard, which makes the entire grammar context dependent.
> >> In order to parse C code, you need a list of identifiers that are typedef
> >> names and use a separate terminal for typedef-name.
> >>
> >>    Simon (who also tried)
> >>
> >> _______________________________________________
> >> SableCC-Discussion mailing list
> >> SableCC-Discussion@...
> >> http://lists.sablecc.org/listinfo/sablecc-discussion
> >>
> >>    
> > --
> > Nakai, Hisashi
> > University of Tsukuba
> > E-mail: nakai@...
> >
> > _______________________________________________
> > SableCC-Discussion mailing list
> > SableCC-Discussion@...
> > http://lists.sablecc.org/listinfo/sablecc-discussion
> >
> >  
>
> --
> Etienne M. Gagnon, Ph.D.
> SableCC:                                            http://sablecc.org
>
--
Nakai, Hisashi
University of Tsukuba
E-mail: nakai@...
_______________________________________________
SableCC-Discussion mailing list
SableCC-Discussion@...
http://lists.sablecc.org/listinfo/sablecc-discussion

Re: shift/reduce and reduce/reduce conflict in ANSI C Yacc grammar

by Etienne M. Gagnon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Hisashi,

I see a little discrepancy in your translation. In the Yacc grammar, there are two types of type name:
  1. "type_name": a production.
  2. "TYPE_NAME": a token.
In your version, there is only one "type_name", which is a production. This is most likely why you get a conflict involving pointers and type specifiers using SableCC 3:
shift/reduce conflict in state [stack: TAsterisk *] on TVoid in {
    [ PPointer = TAsterisk * ] followed by TVoid (reduce),
    [ PTypeSpecifier = * TVoid ] (shift)
}
It seems that the Yacc grammar uses the usual hack, letting the lexer distinguish between an identifier and a type name.

Have fun!

Etienne
PS: Please do not CC me. I am already subscribed to the mailing-list!

Hisashi Nakai wrote :
I removed the if-else rule and then yacc did not issue any message.
  Of course, my translation may include something wrong.
  Please check my grammar, which I put on 

http://nakataf.slis.tsukuba.ac.jp/~nakai/c.grammar

  
  I tried to translate a C grammar for yacc into sablecc's one.
  The original one I have can be got from 

http://www.csd.uwo.ca/~watt/home/courses/2008-09/cs4447a/parts/ansi-c-grammar/gram.y

      

-- 
Etienne M. Gagnon, Ph.D.
SableCC:                                            http://sablecc.org


_______________________________________________
SableCC-Discussion mailing list
SableCC-Discussion@...
http://lists.sablecc.org/listinfo/sablecc-discussion

signature.asc (265 bytes) Download Attachment

Re: shift/reduce and reduce/reduce conflict in ANSI C Yacc grammar

by Hisashi Nakai :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi Etienne,

In Message-ID: <49661093.2060403@...>
"Etienne M. Gagnon" <egagnon@...> wrote  :

> Hi Hisashi,
>
> I see a little discrepancy in your translation. In the Yacc grammar,
> there are two types of type name:
>
>    1. "type_name": a production.
>    2. "TYPE_NAME": a token.
>
> In your version, there is only one "type_name", which is a production.
> This is most likely why you get a conflict involving pointers and type
> specifiers using SableCC 3:
>
> shift/reduce conflict in state [stack: TAsterisk *] on TVoid in {
>     [ PPointer = TAsterisk * ] followed by TVoid (reduce),
>     [ PTypeSpecifier = * TVoid ] (shift)
> }

  Thank you very much.
  I got it.
  Now I removed the dangling-else rule and added type_name2 token

  type_name2   = letter (letter | digit)* ;

and now trying to make filter in parser class and lexer class.
 
> It seems that the Yacc grammar uses the usual hack, letting the lexer
> distinguish between an identifier and a type name.

  Yes.
  By the way, in your master thesis, there is the explanation about
node variable in chapter 5, but in version 3.2, it does not seem to
exist in Parser class. I found nodeList variable instead. Is it right
to use it as node variable ?

  One more question.
  Yacc accepts shift/reduce conflict as it takes shift rather than
reduce. In the future, can sablecc treat shift/reduce conflict as
yacc (or user can select) ?

  Thanks in advance.

> Have fun!
>
> Etienne
> PS: Please do not CC me. I am already subscribed to the mailing-list!

  Sorry.

> --
> Etienne M. Gagnon, Ph.D.
> SableCC:                                            http://sablecc.org
>
--
Nakai, Hisashi
University of Tsukuba
E-mail: nakai@...

_______________________________________________
SableCC-Discussion mailing list
SableCC-Discussion@...
http://lists.sablecc.org/listinfo/sablecc-discussion

Re: shift/reduce and reduce/reduce conflict in ANSI C Yacc grammar

by krischan83 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Hisashi, all

  Now I removed the dangling-else rule and added type_name2 token

  type_name2   = letter (letter | digit)* ;

  and now trying to make filter in parser class and lexer class.
 
How do want to realize this?
Just by adding each known type name to your type_name2 rule?

I have the same problem and I'm quite a bit happy about Simon's
statement
which makes the entire grammar context dependent
so I can quit investigation with a clear conscience.

Nevertheless I still have the problem of distinguishing identifiers and type names
(which are in my special case not declared and I can't simulate a preprocessing step like gcc does).

I think about to hack the token stream provided by the lexer...

Does anybody have experiences with such a way?

Best,
Christian

Re: shift/reduce and reduce/reduce conflict in ANSI C Yacc grammar

by Etienne M. Gagnon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Hisashi,

  By the way, in your master thesis, there is the explanation about
node variable in chapter 5, but in version 3.2, it does not seem to
exist in Parser class. I found nodeList variable instead. Is it right
to use it as node variable ?
  
SableCC 3 removed the filter() method from the parser class due to its incompatibility with CST->AST transformations. In other words, SableCC 3's Parser class is not customizable.

I won't go into the details, but it would be a hell to try to fix SableCC 3 to offer a well designed filter() method. SableCC 4 has investigators which lets custom user code investigate subtrees while parsing. It also has semantic selectors to allow user code to select among productions based on semantics. In other words, SableCC 4 has a clean solution for this, but it will not reintroduce the filter method. But it is not yet available...

So, your only immediate solution is to go back to using SableCC 2, which provides the filter() method, which is required for the ANSI C lexer/parser hack.

WARNING: Do not forget that the parser is ALWAYS one token ahead, in SableCC 2. This means that if you want to build a typedef table, you must make sure to get your REDUCE before the ";" of the typedef declaration, so that the typedef is in effect for any identifier that follows the ";". E.g.
...
typedef int myint;
myint x = 3;
...
As the parser is LALR(1), it must look at the next token before making the reduction. The following grammar would fail to achieve what you want:
...
typedef_decl = typedef_keyword ... semicolon;
...
To reduce typedef_decl, the parser asks the lexer for the token that follows semicolon, so it gets "myint", an identifier which is not a typename (not yet in the typedef table)! To avoid this, make sure the the semicolon is the token that follows the reduction used by the filter() method.

As you see, all this is quite tricky. That's why I put a lot of work in designing clean mechanisms to use semantics for parsing decisions in SableCC 4, while protecting users from such low-level worries as the lookahead problem.

  Yacc accepts shift/reduce conflict as it takes shift rather than
reduce. In the future, can sablecc treat shift/reduce conflict as
yacc (or user can select) ?
  

I've already answered this somewhere the archives. In a few words, in SableCC 4 you can write:
stmt = {if} if_token exp then_token stmt Dangling else_stmt |
       ...;

Dangling else_stmt = else_token stmt;

This is different from Yacc in that it is an explicit grammar disambiguation directive, not a parsing-algorithm-specific conflict resolution directive. It is backed on a formal proof that the language is not modified (it only selects among the multiple parse trees of the language). In other words, it does not come with unexpected side effects.


Etienne
-- 
Etienne M. Gagnon, Ph.D.
SableCC:                                            http://sablecc.org


_______________________________________________
SableCC-Discussion mailing list
SableCC-Discussion@...
http://lists.sablecc.org/listinfo/sablecc-discussion

signature.asc (265 bytes) Download Attachment

Re: shift/reduce and reduce/reduce conflict in ANSI C Yacc grammar

by Etienne M. Gagnon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Christian,

If I understood correctly, Hishashi's trying to take the usual approach
of detecting typedef's while parsing, and using this information in the
lexer to discriminate between identifiers and type names. This can be
implemented in SableCC 2 by overriding the filter() methods of both
Lexer and Parser classes. This is tricky, but it works if done carefully.

Etienne

krischan83 wrote:
> I think about to hack the token stream provided by the lexer...
>
> Does anybody have experiences with such a way?
>  

--
Etienne M. Gagnon, Ph.D.
SableCC:                                            http://sablecc.org




_______________________________________________
SableCC-Discussion mailing list
SableCC-Discussion@...
http://lists.sablecc.org/listinfo/sablecc-discussion

signature.asc (265 bytes) Download Attachment

Re: shift/reduce and reduce/reduce conflict in ANSI C Yacc grammar

by Hisashi Nakai :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi Etienne,

In Message-ID: <4967FAF8.8060509@...>
"Etienne M. Gagnon" <egagnon@...> wrote  :

> Hi Hisashi,
>
> >   By the way, in your master thesis, there is the explanation about
> > node variable in chapter 5, but in version 3.2, it does not seem to
> > exist in Parser class. I found nodeList variable instead. Is it right
> > to use it as node variable ?
> >  
> SableCC 3 removed the filter() method from the parser class due to its
> incompatibility with CST->AST transformations. In other words, SableCC
> 3's Parser class is not customizable.

  Ok, I see, but I found the filter method in the emitted code.
  It seems to work. (I am using Sablecc-3.2.)

> I won't go into the details, but it would be a hell to try to fix
> SableCC 3 to offer a well designed filter() method. SableCC 4 has
> /investigators/ which lets custom user code investigate subtrees while
> parsing. It also has /semantic selectors/ to allow user code to select
> among productions based on semantics. In other words, SableCC 4 has a
> clean solution for this, but it will not reintroduce the filter method.
> But it is not yet available...
>
> So, your only immediate solution is to go back to using SableCC 2, which
> provides the filter() method, which is required for the ANSI C
> lexer/parser hack.

  Maybe, with version 3.2, I can.
  I made codes with filter method without deep thinking, at this
point it seems to work. In this version of my code, I omit the
detailed checks like duplicated declarations.

> WARNING: Do not forget that the parser is ALWAYS one token ahead, in
> SableCC 2. This means that if you want to build a typedef table, you
> must make sure to get your REDUCE before the ";" of the typedef
> declaration, so that the typedef is in effect for any identifier that
> follows the ";". E.g.
>
> ...
> typedef int myint;
> myint x = 3;
> ...
>
> As the parser is LALR(1), it must look at the next token before making
> the reduction. The following grammar would fail to achieve what you want:
>
> ...
> typedef_decl = typedef_keyword ... semicolon;
> ...
>
> To reduce typedef_decl, the parser asks the lexer for the token that
> follows semicolon, so it gets "myint", an identifier which is not a
> typename (not yet in the typedef table)! To avoid this, make sure the
> the semicolon is the token that follows the reduction used by the
> filter() method.

  Yes, thank you.
  (I may ask again if I meet some troubles.)

> As you see, all this is quite tricky. That's why I put a lot of work in
> designing clean mechanisms to use semantics for parsing decisions in
> SableCC 4, while protecting users from such low-level worries as the
> lookahead problem.

  I (maybe we) are looking forward to it.

> >   Yacc accepts shift/reduce conflict as it takes shift rather than
> > reduce. In the future, can sablecc treat shift/reduce conflict as
> > yacc (or user can select) ?
> >  
>
> I've already answered this somewhere the archives. In a few words, in
> SableCC 4 you can write:

#  I'll try to search it later.

> stmt = {if} if_token exp then_token stmt Dangling else_stmt |
>        ...;
>
> Dangling else_stmt = else_token stmt;
>
>
> This is different from Yacc in that it is an explicit grammar
> disambiguation directive, not a parsing-algorithm-specific conflict
> resolution directive. It is backed on a formal proof that the language
> is not modified (it only selects among the multiple parse trees of the
> language). In other words, it does not come with unexpected side effects.

  Thank you very much again!

> Etienne
>
> --
> Etienne M. Gagnon, Ph.D.
> SableCC:                                            http://sablecc.org
>
--
Nakai, Hisashi
University of Tsukuba
E-mail: nakai@...

_______________________________________________
SableCC-Discussion mailing list
SableCC-Discussion@...
http://lists.sablecc.org/listinfo/sablecc-discussion