Parse a file using ANTLR4


I have a Lexer and a Parser called y86 Lexer and Parser which work as far as I know. But I have a file with y86 commands and I want to parse them using Java. So far I have code as follows.

y86Lexer y86 = null;
CommonTokenStream tokenStream = null;
y86Parser y86p = null;

    y86 = new y86Lexer(CharStreams.fromFileName("C:UserssaigbomianDocuments"
            + "LearnANTLRsrcsum.ys"));
    tokenStream = new CommonTokenStream(y86);
    y86p = new y86Parser(tokenStream);

catch (IOException e)
    log.error("Error occured while reading from file");

I’m not sure how to do the parsing. I have seen people use something like y86Parser.CompilationUnitContext but I can seem to find that class. I have tried printing from the Listeners antlr creates but I don’t know how to trigger these listeners


Need Help? Chat with us