I use ANTLR 2.7 to create a grammar to parse simple expressions like
1
2%
(3%)
(4)
((5))
((6%))
It's simple numbers which can have paranthesis and optional percent characters
My grammar looks like this:
class MyParser extends Parser;
options {
k=2;
buildAST = true;
}
parexpr
: variable
| LPAREN^ parexpr RPAREN!
;
variable
: (NUMBER PCT) => NUMBER PCT^
| NUMBER
;
class MyLexer extends Lexer;
options {
k=6;
}
LPAREN: '(';
RPAREN: ')';
DOT: '.';
PCT: '%';
protected
DIGIT: '0'..'9';
NUMBER: (DIGIT)+ (DOT (DIGIT)+)*;
I can successfully parse e.g.
(1)
(2%)
But it fails to parse a single number:
1
If I remove either | LPAREN^ parexpr RPAREN!
or : (NUMBER PCT) => NUMBER PCT^
then I can parse a single number (but obviously not the other expressions).
I do not understand why I cannot parse a single number with this grammar..?
Note that I am working on a project that was implemented with this version a long time ago, and I'm just extending the existing things.