Token tok = (Token) ctx.getChild(1).getPayload();
if (tok.getType() == Parser.Plus) {...}
// or
if (ctx.getChild(1).getText().equals('+')) {...}
I am using the C.g4 grammar from the antlr/grammars-v4 Github repo to parse C files, but some of the rules use string literals and I don't know how to properly access them in my visitors.Example Rule:additiveExpression: multiplicativeExpression| additiveExpression '+' multiplicativeExpression| additiveExpression '-' multiplicativeExpression;So in order to distinguish between addition and subtraction i need to check for the '+' or '-' literal. I can do:Token tok = (Token) ctx.getChild(1).getPayload();
if (tok.getType() == Parser.Plus) {...}
// or
if (ctx.getChild(1).getText().equals('+')) {...}
This doesn't seem right, am I doing something wrong? Most examples advise against using getChild()...is there a better way?Or is this an error in the C grammar? These tokens are defined later in the same file...Plus : '+';Minus : '-';Should the above literals be replaced with these?
On Fri, Nov 14, 2014 at 10:19 AM, firyice <steve...@gmail.com> wrote:
additiveExpression: multiplicativeExpression| additiveExpression op=(Plus|Minus) multiplicativeExpression;
Token tok = (Token) ctx.getChild(1).getPayload();
if (tok.getType() == Parser.Plus) {...}
// or
if (ctx.getChild(1).getText().equals('+')) {...}