gpt4 book ai didi

eclipse - 从语法构建符号表

转载 作者:行者123 更新时间:2023-12-01 10:55:01 24 4
gpt4 key购买 nike

关闭。这个问题需要更多focused .它目前不接受答案。












想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post .

5年前关闭。




Improve this question




我正在尝试从我的 grammar 构建一个符号表(用 antlr 完成)通过使用 eclipse。但是我不知道从什么开始。我想我在某处读到你需要 antlr 生成的解析器和词法分析器来做到这一点。有人知道一个简单的例子,以便我可以理解它是如何工作的吗?

最佳答案

符号表只是 id 到值的版本化映射。这是一种解决方案,使用范围的推送和弹出作为版本控制机制——在定义规则的范围的入口处推送范围并在退出时弹出。

package net.certiv.metal.symbol;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

import net.certiv.metal.types.ScopeType;
import net.certiv.metal.util.Strings;

public class Scope {

public final int genId;

public ScopeType type;
public Scope enclosingScope;
protected Map<String, Symbol> symbolMap = new LinkedHashMap<String, Symbol>();

public Scope(ScopeType type, final int genId, Scope enclosingScope) {
this.type = type;
this.genId = genId;
this.enclosingScope = enclosingScope;
}

/**
* Define a new variable in the current scope
* This is the entry point for adding new variables
*/
public void define(String name, ArrayList<String> parameters) {
String params = Strings.asString(parameters, true, ".");
Symbol symbol = new Symbol(null, name + params, null);
define(symbol);
}

/** Define a symbol in the current scope */
private void define(Symbol symbol) {
symbol.setScope(this);
symbolMap.put(symbol.name, symbol);
}

/**
* Look up the symbol name in this scope and, if not found,
* progressively search the enclosing scopes.
* Return null if not found in any applicable scope.
*/
private Symbol resolve(String name) {
Symbol symbol = symbolMap.get(name);
if (symbol != null) return symbol;
if (enclosingScope != null) return enclosingScope.resolve(name);
return null; // not found
}
/**
* Lookup a variable starting in the current scope.
* This is the entry point for lookups
*/
public Symbol resolve(String name, ArrayList<String> parameters) {
String params = Strings.asString(parameters, true, ".");
return resolve(name + params);
}

/** Where to look next for symbols */
public Scope enclosingScope() {
return enclosingScope;
}

public String toString() {
return symbolMap.keySet().toString();
}
}


package net.certiv.metal.types;

public enum ScopeType {
GLOBAL,
LOCAL;
}



package net.certiv.metal.symbol;

import net.certiv.metal.converter.BaseDescriptor;
import net.certiv.metal.types.ValueType;

public class Symbol {

protected Scope scope; // the owning scope
protected BaseDescriptor descriptor;
protected String name;
protected ValueType type;

public Symbol(BaseDescriptor descriptor, String name, ValueType type) {
this.descriptor = descriptor;
this.name = name;
this.type = type;
}

public BaseDescriptor getDescriptor() {
return descriptor;
}

public String getName() {
return name;
}

public ValueType getType() {
return type;
}

public void setScope(Scope scope) {
this.scope = scope;
}

public Scope getScope() {
return scope;
}

public int genId() {
return scope.genId;
}

public String toString() {
if (type != null) return '<' + getName() + ":" + type + '>';
return getName();
}
}

package net.certiv.metal.symbol;

import java.util.ArrayList;
import java.util.Stack;

import net.certiv.metal.types.ScopeType;
import net.certiv.metal.util.Log;

public class SymbolTable {

protected Stack<Scope> scopeStack;
protected ArrayList<Scope> allScopes;
protected int genId;

public SymbolTable() {
init();
}

protected void init() {
scopeStack = new Stack<>();
allScopes = new ArrayList<>();
genId = 0;

Scope globals = new Scope(ScopeType.GLOBAL, nextGenId(), null);
scopeStack.push(globals);
allScopes.add(globals);
}

public Scope pushScope() {
Scope enclosingScope = scopeStack.peek();
Scope scope = new Scope(ScopeType.LOCAL, nextGenId(), enclosingScope);
scopeStack.push(scope);
allScopes.add(scope);
return scope;
}

public void popScope() {
scopeStack.pop();
}

public Scope currentScope() {
if (scopeStack.size() > 0) {
return scopeStack.peek();
}
Log.error(this, "Unbalanced scope stack.");
return allScopes.get(0);
}

public Scope getScope(int genId) {
for (Scope scope : scopeStack) {
if (scope.genId == genId) return scope;
}
return null;
}

private int nextGenId() {
genId++;
return genId;
}

public String toString() {
StringBuilder sb = new StringBuilder();
for (Scope scope : scopeStack.subList(0, scopeStack.size() - 1)) {
sb.append(scope.toString());
}
return sb.toString();
}
}

关于eclipse - 从语法构建符号表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15588452/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com