protected void processFunctionNameLexeme(Lexeme fName) { IToken next = pollNextInput(); if (next is ParenthesesToken) { int nArguments = properties.getFunctionArgumentsCount(fName.Value); ArgumentListNode arguments = parseArgumentList(next as ParenthesesToken, nArguments); if (arguments.Count != nArguments) { throw new ParseException( "Number of arguments given doesn't match function declaration for function: " + fName.Value); } FunctionApplyNode.FunctionBody funcDefinition = properties.getFunctionDefinition(fName.Value); FunctionApplyNode funcApplyNode = new FunctionApplyNode(arguments, funcDefinition, fName.Value); output.Enqueue(funcApplyNode); lastProcessedElement = funcApplyNode; } else if (next is Lexeme && (next as Lexeme).Type == Lexeme.LexemeType.LEFT_PAREN) { openedArgumentLists++; operatorStack.Push(fName); operatorStack.Push(next as Lexeme); lastProcessedElement = next as Lexeme; } else { throw new ParseException("Missing argument list for function call: " + fName.Value + " " + next.simpleRepresentation()); } }
protected void reset() { input.Clear(); output.Clear(); operatorStack.Clear(); lastProcessedElement = null; openedArgumentLists = 0; }
public BaseOXMLParser(ParseProperties properties) { this.properties = properties; this.textRunTokenizer = new Tokenizer(properties); this.input = new Queue <IToken>(); this.output = new Queue <ISyntaxUnit>(); this.operatorStack = new Stack <Lexeme>(); this.lastProcessedElement = null; this.openedArgumentLists = 0; }
protected void pushValueProducerToOutput(IToken t) { ISyntaxUnit processed; if (t is Lexeme) { processed = processValueProducerLexeme(t as Lexeme); } else { if (t is FractionToken) { processed = processFraction(t as FractionToken); } else if (t is FunctionApplyToken) { processed = processFuncApplyToken(t as FunctionApplyToken); } else if (t is ParenthesesToken) { processed = processParenthesesToken(t as ParenthesesToken); } else if (t is SuperscriptToken) { processed = processSuperscriptToken(t as SuperscriptToken); } else if (t is RadicalToken) { processed = processRadicalToken(t as RadicalToken); } else if (t is SubscriptToken) { // TODO : implementiraj za SubscriptedIdentifierLexeme throw new ParseException("Given token cannot be pushed into the output queue as a value producer."); } else { throw new ParseException("Given token cannot be pushed into the output queue as a value producer."); } } output.Enqueue(processed); lastProcessedElement = processed; }
protected SyntaxNode buildSyntaxTree(List <ISyntaxUnit> postfixForm) { Queue <ISyntaxUnit> inputQueue = new Queue <ISyntaxUnit>(postfixForm); Stack <SyntaxNode> operandStack = new Stack <SyntaxNode>(); while (inputQueue.Count > 0) { ISyntaxUnit input = inputQueue.Dequeue(); if (input is Lexeme) { Lexeme token = input as Lexeme; Lexeme.LexemeType ttype = token.Type; if (properties.IsVariable(token.Value)) { VariableIdentifierNode variable = new VariableIdentifierNode(token.Value); operandStack.Push(variable); } else if (properties.IsConstant(token.Value)) { double constantValue = properties.getConstantValue(token.Value); ConstantIdentifierNode constant = new ConstantIdentifierNode(token.Value, constantValue); operandStack.Push(constant); } else if (properties.IsFunctionName(token.Value)) { int nArguments = properties.getFunctionArgumentsCount(token.Value); FunctionApplyNode.FunctionBody funcBody = properties.getFunctionDefinition(token.Value); ArgumentListNode argumentList = new ArgumentListNode(); try { for (int i = 0; i < nArguments; i++) { argumentList.addArgument(operandStack.Pop()); } } catch (InvalidOperationException ex) { throw new ParseException("Not enough operands on operand stack for function call."); } FunctionApplyNode functionCall = new FunctionApplyNode(argumentList, funcBody, token.Value); operandStack.Push(functionCall); } else if (ttype == Lexeme.LexemeType.REAL_VALUE) { double value; if (!double.TryParse(token.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out value)) { throw new ParseException("Couldn't parse literal value: " + token.Value); } LiteralNode literal = new LiteralNode(value); operandStack.Push(literal); } else if (ttype == Lexeme.LexemeType.OP_PLUS) { try { SyntaxNode right = operandStack.Pop(); SyntaxNode left = operandStack.Pop(); AdditionNode addition = new AdditionNode(left, right); operandStack.Push(addition); } catch (InvalidOperationException ex) { throw new ParseException("Missing operand(s) for addition."); } } else if (ttype == Lexeme.LexemeType.OP_MINUS) { try { SyntaxNode right = operandStack.Pop(); SyntaxNode left = operandStack.Pop(); SubtractionNode subtraction = new SubtractionNode(left, right); operandStack.Push(subtraction); } catch (InvalidOperationException ex) { throw new ParseException("Missing operand(s) for subtraction."); } } else if (ttype == Lexeme.LexemeType.OP_MUL) { try { SyntaxNode right = operandStack.Pop(); SyntaxNode left = operandStack.Pop(); MultiplicationNode multiplication = new MultiplicationNode(left, right); operandStack.Push(multiplication); } catch (InvalidOperationException ex) { throw new ParseException("Missing operand(s) for multiplication."); } } else if (ttype == Lexeme.LexemeType.OP_DIV) { try { SyntaxNode right = operandStack.Pop(); SyntaxNode left = operandStack.Pop(); DivisionNode division = new DivisionNode(left, right); operandStack.Push(division); } catch (InvalidOperationException ex) { throw new ParseException("Missing operand(s) for division."); } } else if (ttype == Lexeme.LexemeType.OP_POW) { try { SyntaxNode exponent = operandStack.Pop(); SyntaxNode baseNode = operandStack.Pop(); PowerNode power = new PowerNode(baseNode, exponent); operandStack.Push(power); } catch (InvalidOperationException ex) { throw new ParseException("Missing operand(s) for exponentiation."); } } else if (ttype == Lexeme.LexemeType.EQ_SIGN) { try { SyntaxNode right = operandStack.Pop(); SyntaxNode left = operandStack.Pop(); EqualsNode eqNode = new EqualsNode(left, right); operandStack.Push(eqNode); } catch (InvalidOperationException ex) { throw new ParseException("Missing operand(s) for assignment."); } } else if (ttype == Lexeme.LexemeType.OP_PLUS_UNARY) { try { SyntaxNode child = operandStack.Pop(); UnaryPlusNode unaryPlus = new UnaryPlusNode(child); operandStack.Push(unaryPlus); } catch (InvalidOperationException ex) { throw new ParseException("Missing operand for unary plus."); } } else if (ttype == Lexeme.LexemeType.OP_MINUS_UNARY) { try { SyntaxNode child = operandStack.Pop(); UnaryMinusNode unaryMinus = new UnaryMinusNode(child); operandStack.Push(unaryMinus); } catch (InvalidOperationException ex) { throw new ParseException("Missing operand for unary minus."); } } else { throw new ParseException("Unexpected token in postfix expression: " + token.simpleRepresentation()); } } else if (input is SyntaxNode) { operandStack.Push(input as SyntaxNode); } else { throw new ParseException("Unexpected object type in postfix expression."); } } if (operandStack.Count == 1) { return(operandStack.Pop()); } else { throw new ParseException("Too many operands in postfix expression."); } }
protected void pushOperator(Lexeme op) { if (!op.IsOperator()) { throw new ParseException("Cannot push a non-operator token onto the operator stack!"); } if (op.IsRightAssociative()) { // pushing a right-associative operator // pop the top of the stack into the output queue as long as it isn't // an opening parenthesis or its precedence is lower or equal to that of // the operator being pushed onto the stack try { while (true) { Lexeme stackTop = operatorStack.Peek(); if (stackTop.Type == Lexeme.LexemeType.LEFT_PAREN) { break; } else if (stackTop.IsHigherPrecedenceThan(op)) { output.Enqueue(stackTop); operatorStack.Pop(); } else { break; } } } catch (InvalidOperationException ex) { // operator stack is empty, continue with pushing operator } lastProcessedElement = op; operatorStack.Push(op); } else { // pushing a left-associative operator // pop the top of the stack into the output queue as long as it isn't // an opening parenthesis or its precedence is lower to that of // the operator being pushed onto the stack try { while (true) { Lexeme stackTop = operatorStack.Peek(); if (stackTop.Type == Lexeme.LexemeType.LEFT_PAREN) { break; } else if (!stackTop.IsLowerPrecedenceThan(op)) { output.Enqueue(stackTop); operatorStack.Pop(); } else { break; } } } catch (InvalidOperationException ex) { // operator stack is empty, continue with pushing operator } lastProcessedElement = op; operatorStack.Push(op); } }