Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TS2339: Property 'compilationUnit' does not exist on type 'Python3Parser'. #545

Open
paris0120 opened this issue May 8, 2023 · 1 comment

Comments

@paris0120
Copy link

paris0120 commented May 8, 2023

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'.

@nsx07
Copy link

nsx07 commented Mar 24, 2024

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 ;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants