Question

I am not sure if the title makes it clear what I want to do.

My input for my parser contains debug information about C source files. Some of the input looks e.g. like this:

L:C$main.c$41$1$10:C0C5

Which basically means that line 10 in the source file main.c corresponds with the memory address C0C5.

Here is an example what my AST looks like:

enter image description here

Which represents the input:

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

What I want are two Hash-Maps such that I can access these information quickly at runtime. But how can I now build such Hash-Maps and is it possible to do that at parsetime?

This is how I would like to use my parser:

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;
}

Expected Output:

Address 0xC09C is at line 29 in main

Can anybody help?

Best regards.

Was it helpful?

Solution

I was about to delete my question but maybe somebody else comes along to this post. I just made the transition to ANTLR4 and it really is much simpler (so far at least).

In ANTLR4 an interface (e.g. ICDBFileListener) is built for you which one can use to catch all information at parsetime:

namespace Parser
{
    public class CDBFileParserListener : ICDBFileListener
    {
        public void ExitModule_name(CDBFileParser.Module_nameContext context)
        {
            Console.WriteLine("ModuleName: " + context.GetText());
            // Add module to module-map and remember 
            // that current module is context.GetText()
        }

        public void ExitLine_number(CDBFileParser.Line_numberContext context)
        {
            Console.WriteLine("LineNumber: " + context.GetText());
            // Remember line number
        }

        public void ExitMemory_address(CDBFileParser.Memory_addressContext context)
        {
            Console.WriteLine("MemoryAddress: " + context.GetText());
            // Add linenumber <-> memoryaddress to maps
        }

        public Modules[] getModules() 
        {
            return m_modules;
        }
    }


} 

And this is how it can be used:

    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
        {
            CDBFileParserListener listener = new CDBFileParserListener();
            parser.AddParseListener(listener);
            System.Diagnostics.Debug.WriteLine(parser.parseCDBFile().ToStringTree());

            Dictionary<String, Module> modules = listener.Modules;

            Module main;
            modules.TryGetValue("main", out main);

            long line = main.getLineFromAddress(0xC09C);
            Console.WriteLine("0xC09C maps to " + line + " in  main.c");
        }
        catch (Exception e)
        {
            printException(e);
        }

        return parser;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top