Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use antlr4 visitor

I am a beginner of antlr. I was trying to use visitor in my code and following the instruction on the net. However, I found out that the visitor was not entering the method I create at all. May anyone tell me what I did wrong?

This is my visitor:

import java.util.LinkedList;
import org.antlr.v4.runtime.misc.NotNull;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Sherwood
 */
public class ExtractMicroBaseVisitor extends MicroBaseVisitor<Integer> {
    //LinkedList<IR> ll = new LinkedList<IR>();
    //MicroParser parser;
    //System.out.println("11");

    @Override 
    public Integer visitPgm_body(@NotNull MicroParser.Pgm_bodyContext ctx){
        System.out.println(ctx.getText());
        return 467;
    }

    @Override
    public Integer visitProgram(@NotNull MicroParser.ProgramContext ctx){
        System.out.println("11");
        return 456;
    }

}

As you can see, the stdout should print 11 when the method "visitProgram" was entered. But the output screen gave me nothing (a null type).

This is my main code:

import java.io.IOException;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
/**
 *
 * @author Sherwood
 */
public class Compiler {

    /**
     * @param args the command line arguments    
     */
    public static void main(String[] args) {
        // TODO code application logic here 
        //SymbolTable table = new SymbolTable();
        try {
            ANTLRFileStream reader = new ANTLRFileStream("TestCases/step4_testcase3.micro");
            MicroLexer lexer  = new MicroLexer((CharStream)reader);
                    TokenStream tokens = new CommonTokenStream(lexer);
                    MicroParser parser = new MicroParser(tokens);
                    parser.setErrorHandler(new MyErrorStrategy());
                    ParseTree tree = parser.program();

                    //ParseTreeWalker walker = new ParseTreeWalker(); // create standard walker
                    //ExtractMicroBaseListener extractor = new ExtractMicroBaseListener(parser);
                    //walker.walk(extractor, tree); // initiate walk of tree with listener
                    ExtractMicroBaseVisitor visitor = new ExtractMicroBaseVisitor();
                    int t = visitor.visit(tree);
                    //for(String str : extractor.table.checkDuplicate()){
                    //    System.out.println("SHADOW WARNING " + str);
                    //}
                    //System.out.println(extractor.table.checkDuplicate().toString());
                    //System.out.println(extractor.table.toString());
                    //System.out.println("Accepted");
    }catch (IOException e) { 
                System.out.println("Not Accepted");
            }catch(IllegalArgumentException e){
                System.out.println(e.getMessage());
            }
    }
}

This is my grammar file(partially):

grammar Micro;

options {
  language = Java;
 }

//Program 
program           : ('PROGRAM' id 'BEGIN' pgm_body 'END')
; 

id                : IDENTIFIER;
pgm_body          : (decl func_declarations);
decl          : (string_decl_list | var_decl_list)* ;
like image 266
Sherwood Lee Avatar asked Oct 20 '13 01:10

Sherwood Lee


People also ask

What is a visitor in ANTLR?

ANTLR Visitors The difference between listener and visitor mechanisms is listener methods are called by the ANTLR-provided walker object, whereas visitor methods must walk their children with explicit visit calls. Forgetting to invoke visit() on a node's children means those subtrees don't get visited.

What is the use of antlr4?

ANTLR v4. ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files. It's widely used to build languages, tools, and frameworks.

What is a listener in ANTLR?

Antlr - Parse Tree Listener The parse tree listener (or listener) is a class that implements callback methods that are called by the parser when it creates the parse tree. You can overwrite this class to get information when the parser enter or exit a rule (ie as found a pattern)


1 Answers

You have to call super if you want ANTLR4 to visit children. Like this:

@Override 
public Integer visitPgm_body(@NotNull MicroParser.Pgm_bodyContext ctx){
    super.visitPgm_body(ctx);
    System.out.println(ctx.getText());
    return 467;
}

@Override
public Integer visitProgram(@NotNull MicroParser.ProgramContext ctx){
    super.visitProgram(ctx);
    System.out.println("11");
    return 456;
}

You have to think about where to put your logic: before super or after.

like image 70
mikea Avatar answered Oct 11 '22 21:10

mikea