📄 program.cs
字号:
using System;
using Antlr.Runtime;
using Antlr.Runtime.Tree;
namespace ANTLRTutorial
{
class Program
{
static void Main(string[] args)
{
// sets the input string
ANTLRStringStream strStream = new ANTLRStringStream(
@" a=3
a+5
");
// creates the lexer
ExprLexer lexer = new ExprLexer(strStream);
// creates the parser with a stream of tokens that will be extracted from the lexer to it
CommonTokenStream tokStream = new CommonTokenStream(lexer);
ExprParser parser = new ExprParser(tokStream);
// parse the input string and get the results
ExprParser.prog_return returnValue = parser.prog();
// creates the tree parser with a stream of tokens that will be extracted from the AST
CommonTreeNodeStream treeNodeStream = new CommonTreeNodeStream((CommonTree)returnValue.Tree);
EvalTreeParser treeWalker = new EvalTreeParser(treeNodeStream);
// walk the AST executing any associated code
treeWalker.prog();
// get the AST and wrap it in our tree node class in order to visualize it with StructsViz when debugging
// you can download an evaluation version of StructsViz here: http://www.manuelabadia.com/products
//AntlrDebugCommonTree AST = new AntlrDebugCommonTree((CommonTree)returnValue.Tree);
// to see the parse tree the parser has to be created with the -debug option, and call the parser
// constructor with a second parameter. The value to pass to the second parameter should be an instance
// of the ParseTreeBuilder class. The parse tree can be visualized in StructsViz using its related wrapper:
// AntlrDebugParseTree parseTree = new AntlrDebugParseTree(parseTreeBuilder.GetTree());
// beware that ANTLR 3.0b5 does not generate 100% working code when the using -debug. You need to change
// a few things. This should be hopefully fixed in the next version.
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -