The purpose is to be able to parse a R program and then generate the abstract syntax tree (AST).
The parser is generated from a JavaCC grammar, whose productions have been derived from the R grammar file. Later I have tuned those productions to eliminate left recursions and to adapt them to JavaCC.
- Pull the repo.
- Open a console at the repo source dir.
- Run
mvn install
. The parser class (R) should have been generated at this point. - Now in your project, invoke it this way:
public static void main(String[] args) {
try {
File file = new File(args[0]); // suppose you are passing the file path as the first argument
InputStream inputStream = new FileInputStream(file);
R rParser = new R(inputStream);
SimpleNode root = rParser.parse();
// Now do something useful with the AST :)
} catch (Exception e) {
System.out.println("Parser error");
e.printStackTrace();
}
}
In my tests I've observed parsing times between 5-10ms per file.
- Fix typos.
- Fix grammar bugs.
- Add more test cases.
- Any help would be appreciated :).
José Antonio Garcel ([email protected])
Sept 13 2020