[antlr-interest] Getting Unexpected token @ as errors building examples

atif azad atif.azad at hotmail.com
Mon Aug 20 13:33:54 PDT 2007


Dears friends
I am a masters student and  I need little help .
When i try to run the examples from the antlr.org website i get the 
following  errors while running the java example SimpleC.g grammer
Although my class path contains all the jars from version 2.7.7 and version 
3 also. I am listing the errors here when i run the command

java antlr.Tool SimpleC.g




ANTLR Parser Generator   Version 2.7.7 (20070812)   1989-2005
SimpleC.g:1:1: unexpected token: grammar
error: Token stream error reading grammar(s):
SimpleC.g:33:1: unexpected char: '@'
SimpleC.g:1:1: rule grammar trapped:
SimpleC.g:1:1: unexpected token: grammar
TokenStreamException: unexpected char: '@'



I will be greatful if some one helps me.

here is the  grammer file I am running

grammar SimpleC;

program
    :   declaration+
    ;

/** In this rule, the functionHeader left prefix on the last two
*  alternatives is not LL(k) for a fixed k.  However, it is
*  LL(*).  The LL(*) algorithm simply scans ahead until it sees
*  either the ';' or the '{' of the block and then it picks
*  the appropriate alternative.  Lookhead can be arbitrarily
*  long in theory, but is <=10 in most cases.  Works great.
*  Use ANTLRWorks to see the lookahead use (step by Location)
*  and look for blue tokens in the input window pane. :)
*/
declaration
    :   variable
    |   functionHeader ';'
	{System.out.println($functionHeader.name+" is a declaration");}
    |   functionHeader block
	{System.out.println($functionHeader.name+" is a definition");}
    ;

variable
    :   type declarator ';'
    ;

declarator
    :   ID
    ;

functionHeader returns [String name]
@init {
    name=null; // for now you must init here rather than in 'returns'
}
    :   type ID '(' ( formalParameter ( ',' formalParameter )* )? ')'
	{$name = $ID.text;}
    ;

formalParameter
    :   type declarator
    ;

type
    :   'int'
    |   'char'
    |   'void'
    |   ID
    ;

block
    :   '{'
            variable*
            stat*
        '}'
    ;

stat: forStat
    | expr ';'
    | block
    | assignStat ';'
    | ';'
    ;

forStat
    :   'for' '(' assignStat ';' expr ';' assignStat ')' block
    ;

assignStat
    :   ID '=' expr
    ;

expr:   condExpr
    ;

condExpr
    :   aexpr ( ('==' | '<') aexpr )?
    ;

aexpr
    :   atom ( '+' atom )*
    ;

atom
    : ID
    | INT
    | '(' expr ')'
    ;

ID  :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
    ;

INT :	('0'..'9')+
    ;

WS  :   (   ' '
        |   '\t'
        |   '\r'
        |   '\n'
        )+
        { $channel=HIDDEN; }
    ;



Regards
Atif Azad

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/



More information about the antlr-interest mailing list