1
我不确定标题是否清楚了我想要执行的操作。解析输入并在解析时构建Dictionary/HashMap
我的解析器输入包含有关C源文件的调试信息。一些输入看起来像例如像这样:
L:C$main.c$41$1$10:C0C5
这基本上意味着在源文件main.c
在该行10
对应与所述存储器地址C0C5
。
这里有一个例子就是我AST的样子:
表示输入:
M:main
L:C$main.c$29$1$0:C09C
L:C$main.c$30$1$10:C0A2
M:divide
L:C$divice.c$31$1$10:C5A9
我想要的是两个散列的地图,这样我可以访问这些信息在运行时很快。但是,我现在如何构建这样的哈希映射,并且可以在parsetime处做到这一点?
这是我想用我的解析器:
public CDBFileParser getFileParser(String cdbFilePath)
{
Stream stream = File.OpenRead(cdbFilePath);
ANTLRInputStream inputStream = new ANTLRInputStream(stream);
CDBFileLexer lexer = new CDBFileLexer(inputStream);
CommonTokenStream tokens = new CommonTokenStream(lexer);
CDBFileParser parser = new CDBFileParser(tokens);
try
{
parser.TreeAdaptor = new CommonTreeAdaptor();
parser.parseCDBFile();
// All this works so far. Here comes the part I am looking for:
Modules[] modules = parser.getModules();
Dictionary<int, int> lineToAddress = modules[0].getLineToAddressMap();
Dictionary<int, int> addressToLine = modules[0].getAddressToLineMap();
int address = 0xC09C;
System.Out.WriteLine("Address 0xC09C is at line " + addressToLine.get(address) + " in " + modules[0].getName());
}
catch (Exception e)
{
printException(e);
}
return parser;
}
预期输出:
Address 0xC09C is at line 29 in main
有人能帮忙吗?
此致敬礼。