Generate Java/Python parser for syntax error checking in the browser

80 views
Skip to first unread message

Abdullah Hilson

unread,
May 7, 2018, 8:41:22 AM5/7/18
to antlr-discussion
Hello,

I am trying to use ANTLR's grammars from https://github.com/antlr/grammars-v4 to create a linter for an online editor.

I could generate a java parser and python3 parser for javascript target following documentation.

Now I try to use the parser in a javascript script, I don't get any rules in the parser created:

const antlr4 = require('../antlr4-js/index')
const JavaLexer = require('./JavaLexer')
const JavaParser = require('./JavaParser')

var input = `
public class Main {
   public static void main(String[] args) {
       System.out.println("Hello, World!");
   }
}
`;
var chars = new antlr4.InputStream(input);
var lexer = new JavaLexer.JavaLexer(chars);
var tokens = new antlr4.CommonTokenStream(lexer);
var parser = new JavaParser.JavaParser(tokens);
parser.buildParseTrees = true;
console.log(Object.keys(parser))
// parser.parseMyRule();





I get these properties on the parser object:
[ '_listeners',
  '_interp',
  '_stateNumber',
  '_input',
  '_errHandler',
  '_precedenceStack',
  '_ctx',
  'buildParseTrees',
  '_tracer',
  '_parseListeners',
  '_syntaxErrors',
  'ruleNames',
  'literalNames',
  'symbolicNames' ]

I don't get compilationUnit or anything that looks like a rule function here.

I pushed my antlr-js and my code on a fork of the grammar here :
One can clone the repository and run `node java/test.js` to see what I get.

I am stuck for 2 days on this. Am I missing something ?

Thank you very much

Abdullah Hilson

unread,
May 9, 2018, 4:14:48 AM5/9/18
to antlr-discussion
Really I don't understand this, it should have generated rules for compilationUnit, packageDeclaration, etc... https://github.com/abumalick/grammars-v4/blob/master/java/JavaParser.g4#L34

I don't see anything in the parser object..

Did anyone generate a parser from public grammars to javascript target ?

I tried with 4 or 5 grammars and every time I get no rule generated.

Abdullah Hilson

unread,
May 10, 2018, 11:29:20 AM5/10/18
to antlr-discussion
Okay I understood now thanks to eric vergnaud here: https://github.com/antlr/antlr4/issues/2286#issuecomment-388080617

we have to look for the names of the rules in the generated Parser, so here it works with `parser.compilationUnit()`
Reply all
Reply to author
Forward
0 new messages