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?
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.
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.
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
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.
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)
;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With