// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections; public enum TokenType { Name = 1, Value = 2, Plus = 3, Minus = 4 } public class Token { private TokenType type; private int value; private string name; public Token(char op) { if(op == '+') { type = TokenType.Plus; } else if(op == '-') { type = TokenType.Minus; } else { throw new Exception("Illegal operator"); } } public Token(string name) { type = TokenType.Name; this.name = name; } public Token(int value) { type = TokenType.Value; this.value = value; } public TokenType Type { get { return type; } } public int Value { get { return value; } } public string Name { get { return name; } } public override string ToString() { switch(type) { case TokenType.Name: return "[type=name," + name + "]"; case TokenType.Value: return "[type=value," + value + "]"; case TokenType.Plus: return "[type=+]"; case TokenType.Minus: return "[type=-]"; default: throw new Exception("Illegal token type"); } } } class Scanner { private string expr; private int ix; private Token next; public Scanner(string expr) { this.expr = expr; ix = 0; next = null; } public Token GetNextToken() { Token res; if(next == null) { res = ParseNextToken(); } else { res = next; next = null; } return res; } public void PushBackToken(Token tok) { next = tok; return; } public Token ParseNextToken() { while((ix < expr.Length) && (expr[ix] == ' ')) { ix++; } if(ix >= expr.Length) { return null; } if(expr[ix] == '+') { ix++; return new Token('+'); } else if(expr[ix] == '-') { ix++; return new Token('-'); } else if(Char.IsDigit(expr[ix])) { int start = ix; int end = start; while((end < expr.Length) && Char.IsDigit(expr[end])) { end++; } ix = end; return new Token(Int32.Parse(expr.Substring(start, end-start))); } else if(expr[ix] == '{') { int start = ix + 1; int end = start; while((end < expr.Length) && (expr[end] != '}')) { end++; } ix = end + 1; return new Token(expr.Substring(start, end-start)); } else { throw new Exception("Invalid expression"); } } } public class Parser { private Scanner scan; public Parser(string expr) { scan = new Scanner(expr); } public int Evaluate(Hashtable vars) { return Expr(vars); } private int Expr(Hashtable vars) { return RestExpr(Term(vars), vars); } private int RestExpr(int e, Hashtable vars) { Token tok = scan.GetNextToken(); if(tok == null) { scan.PushBackToken(tok); return e; } else if(tok.Type == TokenType.Plus) { return RestExpr(e + Term(vars), vars); } else if(tok.Type == TokenType.Minus) { return RestExpr(e - Term(vars), vars); } else { scan.PushBackToken(tok); return e; } } private int Term(Hashtable vars) { Token tok = scan.GetNextToken(); if(tok.Type == TokenType.Value) { return tok.Value; } else if(tok.Type == TokenType.Name) { return (int)vars[tok.Name]; } else { throw new Exception("Unexpected terminal found"); } } } class MainClass { public static void Main(string[] args) { Parser pars = new Parser("{a} + 2 - {b} - 1"); Hashtable vars = new Hashtable(); vars.Add("a", 7); vars.Add("b", 3); Console.WriteLine(pars.Evaluate(vars)); } }