public void ParserShouldHandleSimpleExpressions() { ECalcLexer lexer = new ECalcLexer(new ANTLRStringStream("3*(5+2)")); ECalcParser parser = new ECalcParser(new CommonTokenStream(lexer)); parser.expression(); }
public CommonTree Parse(string expression) { ECalcLexer lexer = new ECalcLexer(new ANTLRStringStream(expression)); ECalcParser parser = new ECalcParser(new CommonTokenStream(lexer)); return((CommonTree)parser.expression().Tree); }
protected CommonTree Parse(string expression) { ECalcLexer lexer = new ECalcLexer(new ANTLRStringStream(expression)); ECalcParser parser = new ECalcParser(new CommonTokenStream(lexer)); try { RuleReturnScope rule = parser.expression(); if (parser.HasError) { //modify by wsl to translate //throw new EvaluationException(parser.ErrorMessage + " " + parser.ErrorPosition); throw new EvaluationException("语法严重错误,系统无法解释计算公式表达式"); } return(rule.Tree as CommonTree); } catch (EvaluationException) { throw; } catch (Exception e) { throw new EvaluationException(e.Message, e); } }
public void ParserShouldHandleTree() { ECalcLexer lexer = new ECalcLexer(new ANTLRStringStream(" 2 * (3 + 5)")); ECalcParser parser = new ECalcParser(new CommonTokenStream(lexer)); CommonTree tree = (CommonTree)parser.expression().Tree; Console.WriteLine(tree.ToStringTree()); }
public void ParserShouldHandleTree() { ECalcLexer lexer = new ECalcLexer(new ANTLRStringStream(" 2 * (3 + 5)")); ECalcParser parser = new ECalcParser(new CommonTokenStream(lexer)); CommonTree tree = (CommonTree)parser.expression().Tree; //Console.WriteLine(tree.ToStringTree()); //modify by wsl Assert.AreEqual("(* 2 (+ 3 5))", tree.ToStringTree()); }
public void ParserShouldHandleBasExpressions() { ECalcLexer lexer = new ECalcLexer(new ANTLRStringStream("3*(5+2")); ECalcParser parser = new ECalcParser(new CommonTokenStream(lexer)); try { parser.expression(); Assert.Fail(); } catch (Exception e) { Console.Error.WriteLine("Parser exception: " + e.Message); } }