Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

attribute references not allowed in lexer actions

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?

like image 218
Ali Salehi Avatar asked Mar 26 '17 15:03

Ali Salehi


People also ask

What is a custom lexer action?

A lexer rule contains a standard lexer command, but the constant value argument for the command is an unrecognized string. As a result, the lexer command will be translated as a custom lexer action, preventing the command from executing in some interpreted modes.

Are multi-character literals allowed in lexer sets?

Compiler Error 144. multi-character literals are not allowed in lexer sets: literal Compiler Error 145. Every lexer mode must contain at least one rule which is not declared with the fragment modifier. Compiler Warning 146. All non-fragment lexer rules must match at least one character.

What are the requirements for non-fragment lexer rules?

Every lexer mode must contain at least one rule which is not declared with the fragment modifier. Compiler Warning 146. All non-fragment lexer rules must match at least one character. The following example shows this error. Whitespace : [ ]+; // ok Whitespace : [ ]; // ok fragment WS : [ ]*; // ok Whitespace : [ ]*; // error 146

When does this warning appear when using the ANTLR 3 syntax?

This warning appears when the tokens block is written using the ANTLR 3 syntax of semicolon-terminated token declarations. NOTE: ANTLR 4 does not allow a trailing comma to appear following the last token declared in the tokens {} block.


1 Answers

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)
    ;
like image 189
Yevgeniy Avatar answered Oct 13 '22 08:10

Yevgeniy