public void E1(Expression p) { Token t = _tokens.LookAhead(); if (t.Type == TokenType.OP_PLUS || t.Type == TokenType.OP_MINUS) { t = _tokens.Read(); p.Left = new Expression(); p.Right = new Expression(); p.Type = t.Type; T(p.Left); E1(p.Right); } }
//E -> TE' //E' -> +TE' | e //T -> TF' //T' -> *FT' | e //V -> FV' //V' -> -V' | +V' | !V' | e //F -> (E) | id private void E(Expression p) { p.Left = new Expression(); p.Right = new Expression(); T(p.Left); E1(p.Right); }
private void PruneTree(Expression n, Stack<Expression> items) { if (n == null) return; PruneTree(n.Left, items); PruneTree(n.Right, items); if (n.Type != TokenType.NULL) { if (n.Class == TokenClass.OPERATOR) { if (n.OperatorType == OperatorType.BINARY) { Expression r = items.Pop(); Expression l = items.Pop(); Expression t = new Expression(); t.Type = n.Type; t.Left = l; t.Right = r; items.Push(t); } else if (n.OperatorType == OperatorType.UNARY) { Expression l = items.Pop(); Expression t = new Expression(); t.Type = n.Type; t.Left = l; items.Push(t); } } else { Expression t = new Expression(); t.Type = n.Type; t.Value = n.Value; items.Push(t); } } }
public void T1(Expression p) { Token t = _tokens.LookAhead(); if (t.Type == TokenType.OP_MUL || t.Type == TokenType.OP_DIV || t.Type == TokenType.OP_MOD) { t = _tokens.Read(); p.Left = new Expression(); p.Right = new Expression(); p.Type = t.Type; V(p.Left); T1(p.Right); } }
//public void V(Expression p) //{ // p.Left = new Expression(); // p.Right = new Expression(); // F(p); // V1(p); //} public void V(Expression p) { Token t = _tokens.LookAhead(); switch (t.Type) { case TokenType.OP_MINUS: p.Type = TokenType.OP_UMINUS; t = _tokens.Read(); p.Right = new Expression(); F(p.Right); break; case TokenType.OP_PLUS: p.Type = TokenType.OP_UPLUS; t = _tokens.Read(); p.Right = new Expression(); F(p.Right); break; case TokenType.OP_NOT: p.Type = TokenType.OP_UNOT; t = _tokens.Read(); p.Right = new Expression(); F(p.Right); break; } F(p); }
public Expression Parse() { Expression n = new Expression(); E(n); Stack<Expression> ns = new Stack<Expression>(); PruneTree(n, ns); n = ns.Pop(); return n; }
public void T(Expression p) { p.Left = new Expression(); p.Right = new Expression(); V(p.Left); T1(p.Right); }
static void EvaluateInternal(Expression n, Stack<Expression> items) { if (n == null) return; EvaluateInternal(n.Left, items); EvaluateInternal(n.Right, items); if (n.Class == TokenClass.OPERATOR) { Expression l; Expression r; Expression v; switch (n.Type) { case TokenType.OP_PLUS: l = items.Pop(); r = items.Pop(); v = new Expression(); v.Value = r.Value + l.Value; items.Push(v); break; case TokenType.OP_MINUS: l = items.Pop(); r = items.Pop(); v = new Expression(); v.Value = r.Value - l.Value; items.Push(v); break; case TokenType.OP_MUL: l = items.Pop(); r = items.Pop(); v = new Expression(); v.Value = r.Value * l.Value; items.Push(v); break; case TokenType.OP_DIV: l = items.Pop(); r = items.Pop(); v = new Expression(); v.Value = r.Value / l.Value; items.Push(v); break; case TokenType.OP_MOD: l = items.Pop(); r = items.Pop(); v = new Expression(); v.Value = r.Value % l.Value; items.Push(v); break; case TokenType.OP_UNOT: r = items.Pop(); v = new Expression(); v.Value = !r.Value; items.Push(v); break; case TokenType.OP_UMINUS: r = items.Pop(); v = new Expression(); v.Value = -r.Value; items.Push(v); break; case TokenType.OP_UPLUS: r = items.Pop(); v = new Expression(); v.Value = +r.Value; items.Push(v); break; } } if (n.Class == TokenClass.LITERAL) { items.Push(n); } }
public void F(Expression p) { Token t = _tokens.LookAhead(); switch (t.Type) { case TokenType.PM_LPARN: t = _tokens.Read(); E(p); t = _tokens.LookAhead(); if (t.Type != TokenType.PM_RPARN) { throw new Exception("Syntax Error! Expect ')'"); } t = _tokens.Read(); break; case TokenType.ID: t = _tokens.Read(); p.Type = t.Type; p.Value = t.Value; break; default: if (t.Class == TokenClass.LITERAL) { t = _tokens.Read(); p.Type = t.Type; p.Value = t.Value; } break; } }
static Expression Evaluate(Expression n) { Stack<Expression> s = new Stack<Expression>(); EvaluateInternal(n, s); return s.Pop(); }
static void PrintTree(Expression n, int level) { if (n == null) return; PrintTree(n.Left, level + 1); if (n.Type != TokenType.NULL) { Console.WriteLine(indent(level) + n); } PrintTree(n.Right, level + 1); }
static int NodesCount(Expression n) { if (n == null) return 0; return 1 + NodesCount(n.Left) + NodesCount(n.Right); }
static Expression NewValueToken(Expression left, Expression right) { Expression t = new Expression(); if (left == null && right != null) { t.Type = right.Type; return t; } if (left != null && right == null) { t.Type = left.Type; return t; } return t; }
private string ConvertToSubName(Expression e) { if (e.ExpressionType == ExpressionType.ID) return ((IdentifierExpr)e).Name; if (e.ExpressionType == ExpressionType.CONST) { if (e.Value.DataType == ElementType.STRING) { string name = (string)e.Value; if (!IsIdentifier(name)) throw new Exception("'" + name + "' is not a valid identifier."); } else { throw new Exception("The sub-names of a qualified name must be of string type."); } } throw new Exception("Invalid sub-name type."); }