我猜你正在使用这个语法。
尽管该语法表明创建了正确的 AST,但事实并非如此。它使用一些内联运算符从解析树中排除某些标记,但它从不为树创建任何根,从而产生一个完全平坦的解析树。由此,您无法以合理的方式获得所有全局变量。
您需要稍微调整语法:
options { ... }
在语法文件顶部的下方添加以下内容:
tokens
{
VARIABLE;
FUNCTION;
}
现在替换以下规则:functionDeclaration
,functionExpression
并variableDeclaration
用这些:
functionDeclaration
: 'function' LT* Identifier LT* formalParameterList LT* functionBody
-> ^(FUNCTION Identifier formalParameterList functionBody)
;
functionExpression
: 'function' LT* Identifier? LT* formalParameterList LT* functionBody
-> ^(FUNCTION Identifier? formalParameterList functionBody)
;
variableDeclaration
: Identifier LT* initialiser?
-> ^(VARIABLE Identifier initialiser?)
;
现在生成了更合适的树。如果您现在解析源:
var a = 1; function foo() { var b = 2; } var c = 3;
生成以下树:

您现在所要做的就是遍历树根的子节点,当您偶然发现一个VARIABLE
标记时,您知道它是一个“全局”,因为所有其他变量都将位于FUNCTION
节点下。
以下是如何做到这一点:
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
public class Main {
public static void main(String[] args) throws Exception {
String source = "var a = 1; function foo() { var b = 2; } var c = 3;";
ANTLRStringStream in = new ANTLRStringStream(source);
JavaScriptLexer lexer = new JavaScriptLexer(in);
CommonTokenStream tokens = new CommonTokenStream(lexer);
JavaScriptParser parser = new JavaScriptParser(tokens);
JavaScriptParser.program_return returnValue = parser.program();
CommonTree tree = (CommonTree)returnValue.getTree();
for(Object o : tree.getChildren()) {
CommonTree child = (CommonTree)o;
if(child.getType() == JavaScriptParser.VARIABLE) {
System.out.println("Found a global var: "+child.getChild(0));
}
}
}
}
产生以下输出:
Found a global var: a
Found a global var: c