[antlr-interest] expecting IDENT, found 'x'

mzukowski at yci.com mzukowski at yci.com
Mon Mar 8 09:12:24 PST 2004


You are using ":" (in the parser) as a literal which it really shouldn't be,
unless it can be part of an identifier.  

Instead you should have this in your lexer:
COLON        :     ':'   ;     

And in the parser you should use COLON, not ":".

Read the docs on literals to get an idea of what was happening and when to
use "something" in the parser.

Monty

-----Original Message-----
From: jc_lelann [mailto:jc.lelann at wanadoo.fr] 
Sent: Saturday, March 06, 2004 6:58 AM
To: antlr-interest at yahoogroups.com
Subject: [antlr-interest] expecting IDENT, found 'x'

Hi !

Could someone explain me why my parser returns the following message ?

java Test < test.txt
line 1:10: expecting IDENT, found 'x'
exception: line 1:12: unexpected char: ':'

I am new to antlr.
Thanks

JC



My grammar :

// content of my test file :

// constant x : integer := 4;
// constant x,y,z : integer := 5;
// constant x,y,z : integer := 5+5*2;


class TestParser extends Parser;
options {
    buildAST = true;
    defaultErrorHandler = true;     // Generate parser error handlers
}

mytext : 
        (constant_declaration)* EOF! ;

constant_declaration : 
        CONSTANT! identifier_list ":"! subtype_indication ":="!
expression ";"!
;
identifier_list : 
        identifier (","! identifier)*
;
identifier : 
        IDENT
;
subtype_indication : 
        IDENT
;
expression:     
        additiveExpression
;
// addition/subtraction
additiveExpression:     
        multiplicativeExpression((PLUS^ | MINUS^)
multiplicativeExpression)*
;
// multiplication/division   
multiplicativeExpression:     
        powerExpression ( (STAR^ | DIV^ ) powerExpression )*
;
powerExpression: 
        unaryExpression ( POWER^ unaryExpression)*
;
unaryExpression:
        MINUS^ unaryExpression
      |     primaryExpression
;
primaryExpression:
        NUMBER
      |     LPAREN! additiveExpression RPAREN!
      ;

class TestLexer extends Lexer;
options {
	exportVocab=Test;      // call the vocabulary "Java"
}

tokens {
    CONSTANT ="constant";
}

WS    :(    ' ' | '\t' | '\n' | '\r' )
        { _ttype = Token.SKIP; }
;
//-------------
// OPERATORS
//-------------
LPAREN      :     '('   ;     
RPAREN      :     ')'   ;
SEMI        :     ';'   ;     
DOT         :     '.'   ;
DIV         :     '/'   ;     
PLUS        :     '+'   ;
MINUS       :     '-'   ;     
STAR        :     '*'   ;
POWER       :     '^'   ;    

// Rule for IDENTIFIER: testLiterals is set to true.
IDENT
	options {testLiterals=true;}
            : ( 'a'..'z' )+
	;

//for both integer and real number

NUMBER : (DIGIT)+ ( '.' (DIGIT)+ )? ;

//for numbers
protected
DIGIT :     '0'..'9' ;







 
Yahoo! Groups Links



 


 
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 at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list