I am using ANTLR4 and have written lexer and parser grammar for a new language that I am designing. I'd like to create a Java POJO by parsing that language.
Language
{dept dept-name="human resources"}
{emp name="john doe" age=23 address="123 Main St, Spring Field, CO 12345" /}
{/dept}
Java POJOs
public class Department {
private final class name;
private final List<Employee>
}
public class Employee {
private final String name;
private final int age;
private final Address address;
}
public class Address {
private final String streetAddress;
private final String city;
private final String state;
private final int zip;
private final int zipExt;
}
I've been able to define the grammar correctly. I was able to use the Antlr tool to generate the Visitor class. The Visitor class takes a generic type, T, and returns an instance of type T while visiting every node. I need to return an Address while visiting the Address section of the AST and an Employee while visiting the Employee section of the AST. So I am not sure what the type T should be for the Visitor implementation.
I am confused about how to go about construct the above Department POJO by implementing the Visitor given out by ANTLR.
PS: Please note that I cannot change the Department, Employee, Address classes. Also, please don't suggest using XML or JSON. I am just trying to learn how such a problem is solved in ANTLR.
Thanks!
DLSParser.FileContext fileContext = parser.file();
Here the root rule in my project is calledfile
so I am gettingDLSParser.FileContext
. Then you just recursively visit this parse tree from its root and generate your POJO along the way. – Wang Sheng