antlr4 generated javascript code results in error on "this._input.LT(1)" when run

593 views
Skip to first unread message

Clive

unread,
Apr 20, 2015, 5:08:01 PM4/20/15
to antlr-di...@googlegroups.com
Hi, I'm not sure if i'm doing something stupid since i'm a javascript n00b, but i can't seem to get the javascript generated output of antlr to work correctly. I've added a copy of my lexer and parser below. The code is so simple, i feel like i must be doing something wrong in the way i run it. I'm trying to simply invoke the rule on the parser - just to see if it will throw parse errors. Any help would be much appreciated - i've been banging my head against this for a while now.

I installed the javascript runtime from NPM - it says it installed v 4.5.2 (ant...@4.5.2 node_modules/antlr4)

I'm running on OS X, node v. 0.12.2 and npm v. 2.7.5 installed using brew. Using the antlr-4.5-complete.jar file to generate code.

When i run this script below, I get:

$ node runner.js
/Users/clive/node_modules/antlr4/Parser.js:433
this._ctx.start = this._input.LT(1);
                             ^
TypeError: undefined is not a function
    at cParser.Parser.enterRule (/Users/clive/node_modules/antlr4/Parser.js:433:32)
    at cParser.jline (/Users/clive/parser/js/cParser.js:184:10)
    at Object.<anonymous> (/Users/clive/parser/js/runner.js:11:19)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3



Here's my running script mostly cut and pasted from the antlr confluence :

var input = "a"
var antlr4 = require("antlr4");

var cLexer = require("./cLexer.js")
var cParser = require("./cParser.js")

var chars = new antlr4.InputStream(input);
var lexer = new cLexer.cLexer(chars);
var tokens = antlr4.CommonTokenStream(lexer);
var parser = new cParser.cParser(tokens);
parser.buildParseTrees = true ;

var tree = parser.jline();
--------------------------


The lexer grammar:
lexer grammar cLexer ;


SPC : ' ' ;
UNQUOTED_REF_CHARS : ~[ \t\r\n$\[\]{}\'"\(\)] ;
WS : [\t\r\n] -> skip ;

----------------------

The Parser Grammar:
parser grammar cParser ;

options {
  tokenVocab = cLexer ;
}

cfile : jline+ ;
jline : SPC* key ;

key : UNQUOTED_REF_CHARS+ ;


----------------------------

Clive

unread,
Apr 20, 2015, 5:15:27 PM4/20/15
to antlr-di...@googlegroups.com
Forgot to add - i'm generating the grammar by saying:

antlr4 -Dlanguage=JavaScript cLexer.g4
antlr4 -Dlanguage=JavaScript cParser.g4

where antlr4 is the usual alias from the book - 

alias antlr4 = "java -jar /Users/clive/antlr4/antlr-4.5-complete.jar"

thanks
Clive

Eric Vergnaud

unread,
Apr 21, 2015, 11:11:21 AM4/21/15
to antlr-di...@googlegroups.com
shouldn't 
var tokens = antlr4.CommonTokenStream(lexer);
be
var tokens = new antlr4.CommonTokenStream(lexer);
?

Clive

unread,
Apr 21, 2015, 7:13:10 PM4/21/15
to antlr-di...@googlegroups.com
Duh, that's quite possible, i'll try it. Thank you very much for the response Eric !

clive
Reply all
Reply to author
Forward
0 new messages