[antlr-interest] Possible bug?

Amal Khailtash akhailtash at gmail.com
Sat Feb 2 13:08:19 PST 2008


I have the following grammar, other than the problem with the fragment
(I don't know why unreachable).  Can I not use greedy=false on
fragment?

It seems, ANTLR declares variables c and e twice!

---BEGIN Test.g-------------------------------------
grammar Test;

tokens
{
  TEXT;
  COMMENT = '$comment';
  END     = '$end';
}

@lexer::members {
List tokens = new ArrayList();

public void emit( Token token )
{
  super.token = token;
  tokens.add(token);
}

public Token nextToken()
{
  super.nextToken();
  if ( tokens.size()==0 )
  {
    return Token.EOF_TOKEN;
  }
  return (Token)tokens.remove(0);
}
}

@header {
// Header
import java.io.*;
}

@members {
public static void main( String[] args ) throws Exception
{
  TestLexer lexer = new TestLexer( new ANTLRFileStream(args[0]) );
  CommonTokenStream tokens = new CommonTokenStream( lexer );
  TestParser parser = new TestParser( tokens );

/**/
  try {
    Token token;
    while( (token = lexer.nextToken())!=Token.EOF_TOKEN )
    {
      System.out.println( "------------------------------------------------"
);
      System.out.println( "Token: [" + token.getText() + "]" );
    }
  } catch( Throwable t ) {
    System.out.println( "Exception: " + t );
    t.printStackTrace();
  }
/**/

  try {
    parser.start();
  } catch( Exception e ) {
    System.err.println( "Exception: " + e );
    e.printStackTrace();
  }
}
}

start
  : comment+
  ;

comment
//      : '$comment' TEXT '$end'
//      : '$comment' (options {greedy=false;} : .)* '$end'
        : COMMENT_T
        ;

COMMENT_T
//      : c='$comment' t=(options {greedy=false;} : .)* e='$end'
        : c='$comment' t=TEXT_T e='$end'
        {
          $c.setType(COMMENT);
          emit($c);
          $t.setType(TEXT);
          emit($t);
          $e.setType(END);
          emit($e);
        }
        ;

fragment
TEXT_T
        :       (options {greedy=false;} : .)*
//      :       (~('$'|'e'|'n'|'d'))*
//  : 't'
        ;

//NEWLINE : '\r'? '\n' ;
WS
  : ( ' '
    | '\t'
    | '\r'? '\n'
    )+
    //{skip();}
    {$channel=HIDDEN;}
  ;
---END Test.g-------------------------------------

---BEGIN errors-------------------------------------
[20:10:34] warning(201): Test.g:91:32: The following alternatives are
unreachable: 1
[20:10:34] warning(201): Test.g:91:32: The following alternatives are
unreachable: 1
[20:10:38] TestLexer.java:93: c is already defined in mCOMMENT_T()
[20:10:38]             Token c = new CommonToken(input,
Token.INVALID_TOKEN_TYPE, Token.DEFAULT_CHANNEL, cStart,
getCharIndex()-1);
[20:10:38]                   ^
[20:10:38] TestLexer.java:99: e is already defined in mCOMMENT_T()
[20:10:38]             Token e = new CommonToken(input,
Token.INVALID_TOKEN_TYPE, Token.DEFAULT_CHANNEL, eStart,
getCharIndex()-1);
[20:10:38]                   ^
[20:10:38] Note: TestLexer.java uses unchecked or unsafe operations.
[20:10:38] Note: Recompile with -Xlint:unchecked for details.
[20:10:38] 2 errors
---END errors-------------------------------------
-- Amal
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080202/0246e883/attachment.html 


More information about the antlr-interest mailing list