@Deprecated
public final class LeftAssociative
extends java.lang.Object
higherPrecendenceExpressionRule.is( lowerPrecedenceExpressionRule, o2n(highPrecedenceOperator, lowerPrecedenceExpressionRule) ).skipIfOneChild();Let's take as an example a simple calculator with the binary subtraction as only operator:
primaryExpression.is(INTEGER_LITERAL); substractionExpression.is( primaryExpression, o2n("-", primaryExpression) ).skipIfOneChild();The interpretation of the subtraction is made easy by this class, and is done as follows:
public int evaluate(AstNode node) { if (node.getType() == primaryExpression) { return evaluatePrimaryExpression(node); } else if (node.getType() == substractionExpression) { return evaluateSubtractionExpression(node); } else { throw new IllegalArgumentException("Unsupported node type " + node.getType()); } } private int evaluatePrimaryExpression(AstNode node) { return Integer.parseInt(node.getTokenOriginalValue()); } private int evaluateSubtractionExpression(AstNode node) { int accumulator = evaluate(node.getChild(0)); for (LeftAssociative.OperatorAndOperand operatorAndOperand : new LeftAssociative.Iterator(node)) { // If several binary operators have the same precedence, use operatorAndOperand.getOperator() accumulator -= evaluate(operatorAndOperand.getOperand()); } return accumulator; }
Modifier and Type | Class and Description |
---|---|
static class |
LeftAssociative.Iterator
Deprecated.
|
static class |
LeftAssociative.OperatorAndOperand
Deprecated.
|
Copyright © 2012 SonarSource. All Rights Reserved.