public static void Main(string[] args) { try { CalcLexer lexer = new CalcLexer(new CharBuffer(Console.In)); lexer.setFilename("<stdin>"); CalcParser parser = new CalcParser(lexer); parser.setFilename("<stdin>"); // Parse the input expression parser.expr(); CommonAST t = (CommonAST)parser.getAST(); // Print the resulting tree out in LISP notation Console.Out.WriteLine(t.ToStringTree()); CalcTreeWalker walker = new CalcTreeWalker(); // Traverse the tree created by the parser float r = walker.expr(t); Console.Out.WriteLine("value is " + r); } catch (TokenStreamException e) { Console.Error.WriteLine("exception: " + e); } catch (RecognitionException e) { Console.Error.WriteLine("exception: " + e); } }
protected CommonAST(CommonAST another) { // don't include child/sibling pointers in Clone()/dup() //down = another.down; //right = another.right; ttype = another.ttype; text = (another.text == null) ? null : String.Copy(another.text); }
protected CommonAST(CommonAST another) { // don't include child/sibling pointers in Clone()/dup() //down = another.down; //right = another.right; ttype = another.ttype; text = (another.text==null) ? null : String.Copy(another.text); }
public static void Main(string[] args) { try { ExprLexer lexer = new ExprLexer(new CharBuffer(Console.In)); ExprParser parser = new ExprParser(lexer); // set the type of tree node to create; this is default action // so it is unnecessary to do it here, but demos capability. parser.setASTNodeClass("antlr.CommonAST"); parser.expr(); antlr.CommonAST ast = (antlr.CommonAST)parser.getAST(); if (ast != null) { Console.Out.WriteLine(ast.ToStringList()); } else { Console.Out.WriteLine("null AST"); } } catch(Exception e) { Console.Error.WriteLine("exception: "+e); } }