Loading ...
Sorry, an error occurred while loading the content.

Bug in cgrammar

Expand Messages
  • daniel_raichle
    I thing I found a bug in cgrammar. I use GnuCParser.g to lex and parse the input file and GnuCEmitter.g to print it out. If I have the following input: typedef
    Message 1 of 2 , Feb 4, 2004
    • 0 Attachment
      I thing I found a bug in cgrammar.
      I use GnuCParser.g to lex and parse the input file and GnuCEmitter.g
      to print it out.

      If I have the following input:

      typedef struct {
      unsigned char var1 ;
      } name1 ;
      typedef struct {
      unsigned char var2 ;
      } name2 ;

      I get the following output:
      typedef struct 0
      unsigned char var1 ;
      } name1 ;
      typedef struct 1
      unsigned char var2 ;
      } name2 ;

      Increasing numbers will be printed instead of "{".

      I think the bug is in GnuCGrammar.g (and also in StdCGrammar.g) in the
      rule "structOrUnionSpecifier". This rule should be changed like this
      (two statements were outcommented):

      structOrUnionSpecifier
      { String scopeName; }
      : sou:structOrUnion!
      ( ( ID LCURLY )=> i:ID l:LCURLY
      {
      scopeName = #sou.getText() + " " + #i.getText();
      // #l.setText(scopeName);
      pushScope(scopeName);
      }
      ( structDeclarationList )?
      { popScope();}
      RCURLY
      | l1:LCURLY
      {
      scopeName = getAScopeName();
      // #l1.setText(scopeName);
      pushScope(scopeName);
      }
      ( structDeclarationList )?
      { popScope(); }
      RCURLY
      | ID
      )
      { ## = #( #sou, ## );}
      ;

      In GnuCEmitter.g the rule "structOrUnionBody" should be changed like
      this (I changed: 'print("{");' to: 'print(lc1);'):

      structOrUnionBody
      : ( (ID LCURLY) => i1:ID lc1:LCURLY
      { print( i1 ); print ( lc1 ); tabs++; }
      ( structDeclarationList )?
      rc1:RCURLY { tabs--; print( rc1 ); }
      | lc2:LCURLY { print( lc2 ); tabs++; }
      ( structDeclarationList )?
      rc2:RCURLY { tabs--; print( rc2 ); }
      | i2:ID { print( i2 ); }
      );

      It seems that it works correct now but it would be better if someone
      other could check the changes, too.
    • mzukowski@yci.com
      Good catch. The idea was to record the scope name in the text of the LCURLY node. So the other way (the way I intended) to make it work would be to leave the
      Message 2 of 2 , Feb 4, 2004
      • 0 Attachment
        Good catch. The idea was to record the scope name in the text of the LCURLY
        node. So the other way (the way I intended) to make it work would be to
        leave the parser as is and just change the emitter:

        structOrUnionBody
        : ( (ID LCURLY) => i1:ID lc1:LCURLY
        { print( i1 ); print ( "{" ); tabs++; }
        ( structDeclarationList )?
        rc1:RCURLY { tabs--; print( rc1 ); }
        | lc2:LCURLY { print( "{" ); tabs++; }
        ( structDeclarationList )?
        rc2:RCURLY { tabs--; print( rc2 ); }
        | i2:ID { print( i2 ); }
        );

        Monty

        -----Original Message-----
        From: daniel_raichle [mailto:daniel.raichle@...]
        Sent: Wednesday, February 04, 2004 2:59 AM
        To: antlr-interest@yahoogroups.com
        Subject: [antlr-interest] Bug in cgrammar

        I thing I found a bug in cgrammar.
        I use GnuCParser.g to lex and parse the input file and GnuCEmitter.g
        to print it out.

        If I have the following input:

        typedef struct {
        unsigned char var1 ;
        } name1 ;
        typedef struct {
        unsigned char var2 ;
        } name2 ;

        I get the following output:
        typedef struct 0
        unsigned char var1 ;
        } name1 ;
        typedef struct 1
        unsigned char var2 ;
        } name2 ;

        Increasing numbers will be printed instead of "{".

        I think the bug is in GnuCGrammar.g (and also in StdCGrammar.g) in the
        rule "structOrUnionSpecifier". This rule should be changed like this
        (two statements were outcommented):

        structOrUnionSpecifier
        { String scopeName; }
        : sou:structOrUnion!
        ( ( ID LCURLY )=> i:ID l:LCURLY
        {
        scopeName = #sou.getText() + " " + #i.getText();
        // #l.setText(scopeName);
        pushScope(scopeName);
        }
        ( structDeclarationList )?
        { popScope();}
        RCURLY
        | l1:LCURLY
        {
        scopeName = getAScopeName();
        // #l1.setText(scopeName);
        pushScope(scopeName);
        }
        ( structDeclarationList )?
        { popScope(); }
        RCURLY
        | ID
        )
        { ## = #( #sou, ## );}
        ;

        In GnuCEmitter.g the rule "structOrUnionBody" should be changed like
        this (I changed: 'print("{");' to: 'print(lc1);'):

        structOrUnionBody
        : ( (ID LCURLY) => i1:ID lc1:LCURLY
        { print( i1 ); print ( lc1 ); tabs++; }
        ( structDeclarationList )?
        rc1:RCURLY { tabs--; print( rc1 ); }
        | lc2:LCURLY { print( lc2 ); tabs++; }
        ( structDeclarationList )?
        rc2:RCURLY { tabs--; print( rc2 ); }
        | i2:ID { print( i2 ); }
        );

        It seems that it works correct now but it would be better if someone
        other could check the changes, too.




        Yahoo! Groups Links

        To visit your group on the web, go to:
        http://groups.yahoo.com/group/antlr-interest/

        To unsubscribe from this group, send an email to:
        antlr-interest-unsubscribe@yahoogroups.com

        Your use of Yahoo! Groups is subject to:
        http://docs.yahoo.com/info/terms/
      Your message has been successfully submitted and would be delivered to recipients shortly.