public override void Parse(TokensStack sTokens) { /* * "(" + Operator + " " + Operand1 + " " + Operand2 + ")" */ Token t; StackIsNotEmpty(sTokens); if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == "(") { t = sTokens.Pop(); //( } else { throw new SyntaxErrorException("Expected ( Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek()); } StackIsNotEmpty(sTokens); if (sTokens.Peek() is Operator) { Token tOperator = sTokens.Pop(); //Operator Operator = ((Operator)tOperator).Name.ToString(); } else { throw new SyntaxErrorException("Expected Operator received: " + sTokens.Peek().ToString(), sTokens.Peek()); } StackIsNotEmpty(sTokens); //Operand1 Operand1 = Expression.Create(sTokens); if (Operand1 == null) { Token tOp1 = new Token(); throw new SyntaxErrorException("Invalid Expression Exception", tOp1); } Operand1.Parse(sTokens); //Operand2 Operand2 = Expression.Create(sTokens); if (Operand2 == null) { Token tOp2 = new Token(); throw new SyntaxErrorException("Invalid Expression Exception", tOp2); } Operand2.Parse(sTokens); StackIsNotEmpty(sTokens); if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == ")") { t = sTokens.Pop(); //) } else { throw new SyntaxErrorException("Expected ) Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek()); } }
//parsing by the format operand-expression-expression public override void Parse(TokensStack sTokens) { Token t = sTokens.Pop(); //( if (sTokens.Count < 3 || !(sTokens.Peek(0) is Operator)) { throw new SyntaxErrorException("Expected Operator, recieved ", sTokens.Peek(0)); } Operator = ((Operator)sTokens.Pop()).Name.ToString(); Operand1 = Expression.Create(sTokens); Operand1.Parse(sTokens); Operand2 = Expression.Create(sTokens); Operand2.Parse(sTokens); t = sTokens.Pop(); //) }
public override void Parse(TokensStack sTokens) { if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals('(')) { Token t1 = sTokens.Pop(); } else { throw new SyntaxErrorException("missing ( ", sTokens.Peek()); } if (sTokens.Peek() is Operator) { Operator = "" + ((Operator)sTokens.Pop()).Name; } else { throw new SyntaxErrorException("no operator", sTokens.Peek()); } Operand1 = Expression.Create(sTokens); if (Operand1 != null) { Operand1.Parse(sTokens); } else { throw new SyntaxErrorException("Bad Ope1", new Token()); } Operand2 = Expression.Create(sTokens); if (Operand2 != null) { Operand2.Parse(sTokens); } else { throw new SyntaxErrorException("Bad Ope2", new Token()); } if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals(')')) { Token t2 = sTokens.Pop(); } else { throw new SyntaxErrorException("misssing (", sTokens.Pop()); } }
public override void Parse(TokensStack sTokens) { Token open = sTokens.Pop(); if (!(open is Parentheses) || ((Parentheses)open).Name != '(') { throw new SyntaxErrorException("$Expected (" + open, open); } Operand1 = Expression.Create(sTokens); Operand1.Parse(sTokens); // while (!(sTokens.Peek() is Operator)) // { // sTokens.Pop(); // } Token tOperator = sTokens.Pop(); if (!(tOperator is Operator)) { throw new SyntaxErrorException("$Expected operator ", tOperator); } else { Operator = ((Operator)tOperator).Name.ToString(); } Operand2 = Expression.Create(sTokens); Operand2.Parse(sTokens); //while (!(sTokens.Peek() is Parentheses)) // { // sTokens.Pop(); // } Token close = sTokens.Pop(); if (!(close is Parentheses) || ((Parentheses)close).Name != ')') { throw new SyntaxErrorException("$Expected )", close); } }
// //recode Operand1, shitty code // public override void Parse(TokensStack sTokens) { //check for '(' Token t = sTokens.Pop(); if (!(t is Parentheses) || ((Parentheses)t).Name != '(') { throw new SyntaxErrorException("'(' expected, received " + t, t); } //define operand1 Operand1 = Expression.Create(sTokens); Operand1.Parse(sTokens); //define operator t = sTokens.Pop(); if (t is Operator) { if (((Operator)t).Name == '+') { Operator = "+"; } else if (((Operator)t).Name == '-') { Operator = "-"; } else if (((Operator)t).Name == '<') { Operator = "<"; } else if (((Operator)t).Name == '>') { Operator = ">"; } else if (((Operator)t).Name == '=') { Operator = "="; } else if (((Operator)t).Name == '|') { Operator = "|"; } else if (((Operator)t).Name == '&') { Operator = "&"; } else if (((Operator)t).Name == '*') { Operator = "*"; } else if (((Operator)t).Name == '/') { Operator = "/"; } else { throw new SyntaxErrorException("Binary operator expected, received " + t, t); } } else { throw new SyntaxErrorException("Binary operator expected, received " + t, t); } //define operand2 Operand2 = Expression.Create(sTokens); Operand2.Parse(sTokens); //check for ')' t = sTokens.Pop(); if (!(t is Parentheses) || ((Parentheses)t).Name != ')') { throw new SyntaxErrorException("')' expected, received " + t, t); } }
public override void Parse(TokensStack sTokens) // ( operator opnd1 opnd2 ) { Token tParentheses = sTokens.Pop(); // ( if (!(tParentheses is Parentheses)) { throw new SyntaxErrorException("Expected Parentheses '(', recived " + tParentheses, tParentheses); } NumOfParent++; Token tOperator = sTokens.Pop(); // operator if (!(tOperator is Operator)) { throw new SyntaxErrorException("Expected Binary Operator, recived " + tOperator, tOperator); } Operator = tOperator.ToString(); TokensStack temp = new TokensStack(); Operand1 = Expression.Create(sTokens); Operand1.Parse(sTokens); Operand2 = Expression.Create(sTokens); Operand2.Parse(sTokens); Token tE_Parentheses = sTokens.Pop(); if (sTokens.Count < 1) { throw new Exception("Code Terminated"); } if (!(tE_Parentheses is Parentheses) && (tE_Parentheses).ToString().Equals(")")) { throw new SyntaxErrorException("Expected Parentheses ')', recived " + tE_Parentheses, tE_Parentheses); } /*while (NumOfParent != 0) * { * if (sTokens.Peek(0) is Parentheses && (((Parentheses)sTokens.Peek(0)).Name == '(')) * NumOfParent++; * if (sTokens.Peek(0) is Parentheses && (((Parentheses)sTokens.Peek(0)).Name == ')')) * NumOfParent--; * temp.Push(sTokens.Pop()); * } * if (sTokens.Peek(0) is Separator && ((Separator)sTokens.Peek(0)).Name == ';') // if ';' exist OR it's BinaryEXPRESSION in functionCall ~ foo(BinEXP) ~ foo((*xy)) * { * Token tEnd = temp.Pop(); * } * Token tParenthesesEnd = temp.Pop(); // ')' last one * if (!(tParenthesesEnd is Parentheses && (((Parentheses)tParenthesesEnd).Name == ')'))) * throw new SyntaxErrorException("Expected Parentheses ')', recived " + tParenthesesEnd, tParenthesesEnd); * if (temp.Peek(0) is Parentheses && (((Parentheses)temp.Peek(0)).Name == ')')) // if opnd2 is Expression - ')' because we ineserted backward from sTokens to temp stack * { * TokensStack Operand2Stack = new TokensStack(); * Operand2Stack.Push(temp.Pop()); // opernad2 stack <-- ')' * while (!(temp.Peek(0) is Parentheses)) * Operand2Stack.Push(temp.Pop()); // operand2 stack <-- 'operator opnd1 opnd2' * Operand2Stack.Push(temp.Pop()); // operand2 stack <-- '(' * Operand2 = Expression.Create(Operand2Stack); * Operand2.Parse(Operand2Stack); * } * else if (!(temp.Peek(0) is Parentheses))// if opnd2 is number or variable * { * if (temp.Peek(0) is Identifier) * { * Operand2 = new VariableExpression(); * Operand2.Parse(temp); * } * else if (temp.Peek(0) is Number) * { * Operand2 = new NumericExpression(); * Operand2.Parse(temp); * } * } * if (!(temp.Peek(0) is Parentheses))// if opnd1 is Number or variable * { * if (temp.Peek(0) is Identifier) * { * Operand1 = new VariableExpression(); * Operand1.Parse(temp); * } * else if (temp.Peek(0) is Number) * { * Operand1 = new NumericExpression(); * Operand1.Parse(temp); * } * } * else // if opnd1 is EXPRESSION * { * TokensStack Operand1Stack = new TokensStack(); * while (!(temp.Count < 0)) // reverse EXPRESSION from temp stack to Operand1Stack to hold the legal expression for opnd1 * Operand1Stack.Push(temp.Pop()); * Operand1 = Expression.Create(Operand1Stack); * Operand1.Parse(Operand1Stack); * }*/ }