如果您了解antlr,您可能有兴趣探索http://www.antlr.org/wiki/display/ANTLR3/ANTLR3JavaScriptTarget,它会生成 javascript 词法分析器和解析器
如果您将目标语言设置为 JavaScript
语法 Tg
grammar T;
options {
language=JavaScript;
[other options]
}
...
...
解析器.html
<script type="text/javascript" src="lib/antlr3-all-min.js"></script>
<script type="text/javascript" src="TLexer.js"></script>
<script type="text/javascript" src="TParser.js"></script>
<script type="text/javascript" src="TWalker.js"></script>
<script type="text/javascript">
var input = "...what you want to feed into the parser...",
cstream = new org.antlr.runtime.ANTLRStringStream(input),
lexer = new TLexer(cstream),
tstream = new org.antlr.runtime.CommonTokenStream(lexer),
parser = new TParser(tstream),
r = parser.entry_rule();
var nodes = new org.antlr.runtime.tree.CommonTreeNodeStream(r.getTree());
nodes.setTokenStream(tstream);
var walker = new TWalker(nodes);
walker.tree_entry_rule();
</script>