I'm using antlr4 (4.13) JS parser with node.js support and when I run the code, I get an error like this:
const tree = parser.MyStartRule();
^
TypeError: parser.MyStartRule is not a function
I don't know how to fix it. Thats my code:
Grammar: https://github.com/antlr/grammars-v4/blob/master/angelscript/angelscript.g4 Just for test (Dont change anything, only replace "grammar angelscript;" to "grammar fns;")
Code to run: (parse.js)
import antlr4 from 'antlr4';
import fnsLexer from './fnsLexer.js';
import fnsParser from './fnsParser.js';
import MyGrammarListener from './fnsListener.js';
const input = "func name(x) { say(x); } name("Hello world");";
const chars = new antlr4.InputStream(input);
const lexer = new fnsLexer(chars);
const tokens = new antlr4.CommonTokenStream(lexer);
const parser = new fnsParser(tokens);
const tree = parser.MyStartRule();
Run command: node parse.js
And my package.json:
{
"name": "fns",
"version": "1.0.0",
"description": "",
"main": "babel.config.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"antlr4": "^4.13.1-patch-1"
}
}
I tried after this error to use chat-gpt to figure it out, and even downloaded the 4.9.0 version (this is the latest version that chat has caught), nothing helped.
As kaby mentioned in the comments: MyStartRule()
is not a parser rule of your grammar. It is script
:
grammar angelscript;
script
: (
import_
| enum_
| typdef
| class_
| mixin_
| interface_
| funcdef
| virtprop
| var_
| func_
| namespace
| ';'
)+ EOF
;
Use it instead:
const tree = parser.script();