Replies: 1 comment 1 reply
-
You can access the first and the last terminal rules (actually tokens) by using |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I have a chain of rules that I don't want to 'follow' to get to the 'terminal rule'. Is there a method to return the terminal rule?
For example, I don't want to follow this chain, checking for Nil rules at every step along the way:
ctx -> expression() -> conditionalExpression() -> logicalOrExpression() -> logicalAndExpression() -> equalityExpression() -> relationalExpression() -> additiveExpression() -> multiplicativeExpression() -> exponentExpression() -> unaryExpression() -> functionExpression().
I would rather have: ctx.getTerminalRule() that returns the context of the ultimate rule that is actually matched, in this case, functionExpression.
BTW My use case is an attempt to optimize an expressionStatement. I want to discard any expressionStatements that do not have side effects. Based on the way I've defined my language, only assignmentExpressions and functionExpressions can have side effects. The rule for assignmentExpression is 'close' to my rule for expressionStatement in my grammar, but I don't want to have to 'dive' to check for functionExpression.
In code, my check and discard action would look something like this:
def exitExpressionStatement(self, ctx:MyParser.ExpressionStatementContext):
actualRule = ctx.getTerminalRule().getRuleIndex()
if not (actualRule == MyParser.RULE_assignmentExpression or
actualRule == MyParser.RULE_functionExpression):
ParseTreeRemove(ctx)
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions