1

i used antlr version 4.8 and javascript antlr4 runtime version 4.8. grammar(JavaScriptLexer.g4, JavaScriptParser.g4) and JavaScriptLexerBase.js, JavaScriptParserBase.js

https://github.com/antlr/grammars-v4/tree/master/javascript/javascript

When I use the term like

if (true) { i = 0; }

or even

if ( true

I face error 'Maximum call stack size exceeded'.

what should i do? my code: antlr.js

const antlr4 = require("antlr4");
const { CommonTokenStream, InputStream } = antlr4;
const JavaScriptLexer = require("./JavaScriptLexer");
const JavaScriptParser = require("./JavaScriptParser");

const input = "if ( true";
console.log("Starting...");
const chars = new InputStream(input);
const lexer = new JavaScriptLexer.JavaScriptLexer(chars);
const tokens  = new CommonTokenStream(lexer);
const parser = new JavaScriptParser.JavaScriptParser(tokens);
parser.buildParseTrees = true;
const tree = parser.program();
console.log("parsed.");

error: antlr4 Maximum call stack size exceeded error

dmaon
  • 58
  • 9
  • This is good info. The grammar and input works fine in 4.9.1 C#. The call stack looks like it's computing adaptivePredict() gets caught in a cycle in the ATN. It's a bug. Submit an issue to github.com/antlr/antlr4. – kaby76 Feb 03 '21 at 18:46
  • Best to try this with 4.9.1, not the old 4.8 Antlr, before submitting a bug. – kaby76 Feb 03 '21 at 18:56
  • Maybe it's a bug in the JS runtime, but JS as such is pretty limited. So it might be worth to try with Node.js and [increase the stacksize there](https://stackoverflow.com/questions/11332422/how-can-i-increase-the-maximum-call-stack-size-in-node-js). Or even better: use Typescript and the [TS runtime](https://github.com/tunnelvisionlabs/antlr4ts), which I successfully use in both Node.js and a browser environment (with a pretty complex grammar). – Mike Lischke Feb 04 '21 at 07:50
  • 1
    @kaby76 Unfortunately, the structure of the files[JavaScriptLexerBase.js, JavaScriptParserBase.js in official grammar of antlr4] for the syntax is pre - version 4.9 and is not updated. so i can't use antlr4.9.1 with runtime4.9.1 and gets another error. – dmaon Feb 04 '21 at 11:39
  • @NourahmadKhosravi You are right. Antlr 4.9+ is now ES6, not ES5. The code in grammars-v4 needs updating, as well as the doc. Multiple issues here. – kaby76 Feb 04 '21 at 18:21

0 Answers0