1
votes

I'm trying to use Antlr v4 to generate a lexer and parser for a simple custom grammar. The issue is that when I run the Antlr .jar utility, it generates a Lexer file but not a Parser file too, as I think it should.

Simple grammar

// Define a grammar called Hello
grammar Hello;
r  : 'hello' ID ;         // match keyword hello followed by an identifier
ID : [a-z]+ ;             // match lower-case identifiers
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines

Antlr tool
Following along with these instructions: https://github.com/antlr/antlr4/blob/master/doc/tool-options.md. The Java tool can be downloaded from http://www.antlr.org/download/index.html (I selected the antlr-4.7-complete.jar).

Let's generate the lexer and parser: antlr.jar -o outDir -Dlangage=JavaScript -visitor -listener test.g4

Actual output

  • HelloLexer.js
  • HelloLexer.tokens

Desired output

  • HelloLexer.js
  • HelloParser.js
  • HelloListener.js
  • HelloVisitor.js

I'd like the parser because once I have lexed the input, I want to parse and generate a tree which I can then traverse, as illustrated in this tutorial:

 var input = "your text to parse here"
 var chars = new antlr4.InputStream(input);
 var lexer = new MyGrammarLexer.MyGrammarLexer(chars);
 var tokens  = new antlr4.CommonTokenStream(lexer);
 var parser = new MyGrammarParser.MyGrammarParser(tokens);
 // ^ [!] notice here how I don't have an analogous "HelloParser.js" to run my tokens through!
 parser.buildParseTrees = true;
 var tree = parser.MyStartRule();

https://github.com/antlr/antlr4/blob/master/doc/javascript-target.md#how-do-i-run-the-generated-lexer-andor-parser

How do I get the Antlr tool to generate the HelloParser.js file I want? Right now it is only generating the Lexer, and the tutorials I have followed (links above) do not have any details for the situation I am in.

1
Could it be that you confuse JavaScript with Java? - lumio
On face value I see that using the Java tool to generate JavaScript may look strange, but according to Antlr - How to create a lexer or parser I need to create the lexer and parser before continuing to the next step of reading input and eventually using its syntax tree. - ChiefOfGxBxL
Did you eventually get an error from ANTLR? Your approach is totally correct, so I think there might be something that caused ANTLR to abort the generation. As a test try a different target (e.g. C++) to see if there's something with the JS target that causes the trouble. - Mike Lischke

1 Answers

1
votes

The issue was solved by re-trying the Antlr tool (Java/.jar), but being extra-certain the CLASSPATH on Windows was being set correctly.

Following the Getting started for Windows doc, the CLASSPATH environment variable has to be set to include the Jar file's path. I originally did this using the System Properties > Environment Variables window, but for some reason it wasn't being registered (I've re-checked for typos and still see no issue).

Next time around I run the command SET CLASSPATH=.;C:\Javalib\antlr-4.5.3-complete.jar;%CLASSPATH% manually and the lexer and parser are now being generated.

So the issue is that if CLASSPATH is set incorrectly, the Java tool silently fails. Without any warnings or errors, it was difficult to diagnose the error.