TS2339: Property 'compilationUnit' does not exist on type 'Python3Parser'.
paris0120 opened this issue · comments
paris0120 commented
I'm trying to follow the readme with python3.g4.
import { ANTLRInputStream, CommonTokenStream } from 'antlr4ts';
// Create the lexer and parser
let inputStream = new ANTLRInputStream("text");
let lexer = new MyGrammarLexer(inputStream);
let tokenStream = new CommonTokenStream(lexer);
let parser = new MyGrammarParser(tokenStream);
// Parse the input, where `compilationUnit` is whatever entry point you defined
let tree = parser.compilationUnit();
every thing is ok but the last one. I got TS2339: Property 'compilationUnit' does not exist on type 'Python3Parser'.
Felipe Pires commented
the 'compilationUnit' isnt related to antlr itself, its vary based on your grammar
import { CharStreams, CommonTokenStream, ParserRuleContext } from 'antlr4ts';
import { ParseTreeWalker } from 'antlr4ts/tree/ParseTreeWalker';
import { ExprContext, calcParser } from './generated/calcParser';
import { calcListener } from './generated/calcListener';
import { calcLexer } from './generated/calcLexer';
import * as fs from "fs";
// Create the lexer and parser
const code = fs.readFileSync('EXAMPLE.txt', 'utf8');
const inputStream = CharStreams.fromString(code);
const lexer = new calcLexer(inputStream);
const tokenStream = new CommonTokenStream(lexer);
const parser = new calcParser(tokenStream);
const tree = parser.program();
in my case my entrypoint parser is "program" method, the grammar used is the AntlrLab Sample
grammar calc;
program
: stat EOF
| def EOF
;
stat: ID '=' expr ';'
| expr ';'
;
def : ID '(' ID (',' ID)* ')' '{' stat* '}' ;
expr: ID
| INT
| func
| 'not' expr
| expr 'and' expr
| expr 'or' expr
;
func : ID '(' expr (',' expr)* ')' ;
AND : 'and' ;
OR : 'or' ;
NOT : 'not' ;
EQ : '=' ;
COMMA : ',' ;
SEMI : ';' ;
LPAREN : '(' ;
RPAREN : ')' ;
LCURLY : '{' ;
RCURLY : '}' ;
INT : [0-9]+ ;
ID: [a-zA-Z_][a-zA-Z_0-9]* ;
WS: [ \t\n\r\f]+ -> skip ;