Symbol.Type 在 Antlr4 中无法识别
Symbol.Type unrecognized in Antlr4
我目前正在学习 Antlr4。
我尝试 运行:
文档中有一个示例
DefPhase.java.
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.ParseTreeProperty;
public class DefPhase extends CymbolBaseListener {
ParseTreeProperty<Scope> scopes = new ParseTreeProperty<Scope>();
GlobalScope globals;
Scope currentScope; // define symbols in this scope
public void enterFile(CymbolParser.FileContext ctx) {
globals = new GlobalScope(null);
currentScope = globals;
}
public void exitFile(CymbolParser.FileContext ctx) {
System.out.println(globals);
}
public void enterFunctionDecl(CymbolParser.FunctionDeclContext ctx) {
String name = ctx.ID().getText();
int typeTokenType = ctx.type().start.getType();
Symbol.Type type = CheckSymbols.getType(typeTokenType);
// push new scope by making new one that points to enclosing scope
FunctionSymbol function = new FunctionSymbol(name, type, currentScope);
currentScope.define(function); // Define function in current scope
saveScope(ctx, function); // Push: set function's parent to current
currentScope = function; // Current scope is now function scope
}
void saveScope(ParserRuleContext ctx, Scope s) { scopes.put(ctx, s); }
public void exitFunctionDecl(CymbolParser.FunctionDeclContext ctx) {
System.out.println(currentScope);
currentScope = currentScope.getEnclosingScope(); // pop scope
}
public void enterBlock(CymbolParser.BlockContext ctx) {
// push new local scope
currentScope = new LocalScope(currentScope);
saveScope(ctx, currentScope);
}
public void exitBlock(CymbolParser.BlockContext ctx) {
System.out.println(currentScope);
currentScope = currentScope.getEnclosingScope(); // pop scope
}
public void exitFormalParameter(CymbolParser.FormalParameterContext ctx) {
defineVar(ctx.type(), ctx.ID().getSymbol());
}
public void exitVarDecl(CymbolParser.VarDeclContext ctx) {
defineVar(ctx.type(), ctx.ID().getSymbol());
}
void defineVar(CymbolParser.TypeContext typeCtx, Token nameToken) {
int typeTokenType = typeCtx.start.getType();
Symbol.Type type = CheckSymbols.getType(typeTokenType);
VariableSymbol var = new VariableSymbol(nameToken.getText(), type);
currentScope.define(var); // Define symbol in current scope
}
}
由于某种原因 Symbol.Type 无法识别。翻了一下好像之前的Antlr有个库叫symtab。最新的是 1.0.8。该库包含所有相关定义,但仍不包含 Symbol.Type。
Anltr4 是否有专门的符号表?
关于这个问题我真的需要一些帮助
我想你说的这个例子是这个:https://github.com/remenska/Grammars/blob/master/book-examples/listeners/DefPhase.java
如果您查看它所在的包,您会发现类型 Symbol (https://github.com/remenska/Grammars/blob/master/book-examples/listeners/Symbol.java) 并且它具有字段类型。也许你可以以某种方式使用它。所以它似乎真的来自这本书的例子。
我目前正在学习 Antlr4。 我尝试 运行:
文档中有一个示例DefPhase.java.
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.ParseTreeProperty;
public class DefPhase extends CymbolBaseListener {
ParseTreeProperty<Scope> scopes = new ParseTreeProperty<Scope>();
GlobalScope globals;
Scope currentScope; // define symbols in this scope
public void enterFile(CymbolParser.FileContext ctx) {
globals = new GlobalScope(null);
currentScope = globals;
}
public void exitFile(CymbolParser.FileContext ctx) {
System.out.println(globals);
}
public void enterFunctionDecl(CymbolParser.FunctionDeclContext ctx) {
String name = ctx.ID().getText();
int typeTokenType = ctx.type().start.getType();
Symbol.Type type = CheckSymbols.getType(typeTokenType);
// push new scope by making new one that points to enclosing scope
FunctionSymbol function = new FunctionSymbol(name, type, currentScope);
currentScope.define(function); // Define function in current scope
saveScope(ctx, function); // Push: set function's parent to current
currentScope = function; // Current scope is now function scope
}
void saveScope(ParserRuleContext ctx, Scope s) { scopes.put(ctx, s); }
public void exitFunctionDecl(CymbolParser.FunctionDeclContext ctx) {
System.out.println(currentScope);
currentScope = currentScope.getEnclosingScope(); // pop scope
}
public void enterBlock(CymbolParser.BlockContext ctx) {
// push new local scope
currentScope = new LocalScope(currentScope);
saveScope(ctx, currentScope);
}
public void exitBlock(CymbolParser.BlockContext ctx) {
System.out.println(currentScope);
currentScope = currentScope.getEnclosingScope(); // pop scope
}
public void exitFormalParameter(CymbolParser.FormalParameterContext ctx) {
defineVar(ctx.type(), ctx.ID().getSymbol());
}
public void exitVarDecl(CymbolParser.VarDeclContext ctx) {
defineVar(ctx.type(), ctx.ID().getSymbol());
}
void defineVar(CymbolParser.TypeContext typeCtx, Token nameToken) {
int typeTokenType = typeCtx.start.getType();
Symbol.Type type = CheckSymbols.getType(typeTokenType);
VariableSymbol var = new VariableSymbol(nameToken.getText(), type);
currentScope.define(var); // Define symbol in current scope
}
}
由于某种原因 Symbol.Type 无法识别。翻了一下好像之前的Antlr有个库叫symtab。最新的是 1.0.8。该库包含所有相关定义,但仍不包含 Symbol.Type。 Anltr4 是否有专门的符号表?
关于这个问题我真的需要一些帮助
我想你说的这个例子是这个:https://github.com/remenska/Grammars/blob/master/book-examples/listeners/DefPhase.java
如果您查看它所在的包,您会发现类型 Symbol (https://github.com/remenska/Grammars/blob/master/book-examples/listeners/Symbol.java) 并且它具有字段类型。也许你可以以某种方式使用它。所以它似乎真的来自这本书的例子。