6
votes

I found a simple grammar to start learning ANTLR. I put it in the myGrammar.g file. here is the grammar:

grammar myGrammar;
/* This will be the entry point of our parser. */
eval
    :    additionExp
    ;

/* Addition and subtraction have the lowest precedence. */
additionExp
    :    multiplyExp 
         ( '+' multiplyExp 
         | '-' multiplyExp
         )* 
    ;

/* Multiplication and division have a higher precedence. */
multiplyExp
    :    atomExp
         ( '*' atomExp 
         | '/' atomExp
         )* 
    ;
atomExp
    :    Number
    |    '(' additionExp ')'
    ;

/* A number: can be an integer value, or a decimal value */
Number
    :    ('0'..'9')+ ('.' ('0'..'9')+)?
    ;

/* We're going to ignore all white space characters */
WS  
    :   (' ' | '\t' | '\r'| '\n') {$channel=HIDDEN;}
    ;

when I use this command :

java -jar /usr/local/...(antlr path) /home/ali/Destop/...(myGrammar.g path)

I have this error:

myGrammar.g:39:36: attribute references not allowed in lexer actions: $channel

what is the problem and what should I do?

2

2 Answers

12
votes

it looks like you are using antlr4, so please replace {$channel=HIDDEN;} with -> channel(HIDDEN).

example:

/* We're going to ignore all white space characters */
WS  
    :   (' ' | '\t' | '\r'| '\n') -> channel(HIDDEN)
    ;
2
votes

I also replace

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

with this:

WS : (' ' | '\t' | '\r'| '\n') -> skip;

and it worked.