You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When [1] produces primaryExpression, accumulate remaining child nodes in [2] to produce the list of postfix expressions. The for loop starts at 1 to account for the first child in the context at index 0 - primaryExpression; this works fine.
postfixExpression.setPrimaryExprNode(visit(ctx.primaryExpression()));
List<Expression> postfixExprList;
//i starts at 1 for [1]->primaryExpressionfor (inti = 1; i < ctx.getChildCount(); i++) {
postfixExprList.add(visit(ctx.getChild(i)));
}
PROBLEM
When [1] produces a compound literal (typeName/initializerList), accumulate remaining child nodes in [2] but offset i to 6 in the for loop since '('typeName')' '{'initializerList'}' accounts for the first 6 child nodes in the context. While this solution works, it's brittle and doesn't feel right; any minor change to this rule in the future breaks the logic.
Is there a cleaner way to iterate child nodes in a context when the start index is variable?
postfixExpression.setCompoundLiteralNode(visit(ctx.typeName()), visit(ctx.initializerList()));
List<Expression> postfixExprList;
//i starts at 6 for [1]->typeName/initializerListfor (inti = 6; i < ctx.getChildCount(); i++) {
postfixExprList.add(visit(ctx.getChild(i)));
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I am building an AST model for C.g4 using visitor strategy but having some difficulty modeling
postfixExpressionrule.postfixExpressionis a two-part production; I added some comments to highlight these parts.[1]primaryExpressionOR compound literal (typeName/initializerList)[2]list of postfix expressions/operators 0..*When
[1]producesprimaryExpression, accumulate remaining child nodes in[2]to produce the list of postfix expressions. Theforloop starts at1to account for the first child in the context at index 0 -primaryExpression; this works fine.PROBLEM
When
[1]produces a compound literal (typeName/initializerList), accumulate remaining child nodes in[2]but offsetito6in theforloop since'('typeName')' '{'initializerList'}'accounts for the first 6 child nodes in the context. While this solution works, it's brittle and doesn't feel right; any minor change to this rule in the future breaks the logic.Is there a cleaner way to iterate child nodes in a context when the start index is variable?
Beta Was this translation helpful? Give feedback.
All reactions