I am working on a command-line tool with the following functionality: Parse modified .java files using an extended ANTLR4 Java9 grammar. The syntax in the files is Java, with one modification to the method declaration which includes a purpose, like in this example: public void {marketing} sendEmail() {} Collect and remove all purposes using a visitor. Collection and analysis of
Tag: antlr
ANTLR AST visitor that returns different data types
I finished converting the ANTLR CST into an AST and created a specific Visitor<T> interface that allows me to visit all my AST nodes, but the main issue i’m having is that some visits should return different data types, and i’m not sure how to go about this. For example, for simple arithmetics operations i want to return a double
How to parse Java code in Python using ANTLRv4
I’m trying to parse some Java code in Python using ANTLRv4. I’ve tried to follow this post, but I get the following error: I can’t figure out what I’m doing wrong. The file I’m trying to parse is proper Java, it’s extracted from the docker-maven-plugin package. I’ve tried with other files, but I get the same error. Any idea ?
Separate definitions of decimal number and word in ANTLR grammar
I’m working on defining a grammar in ANTLR4 which includes words and numbers separately. Numbers are described: and words are described: The simplified grammar below describes the addition between either a word or a letter (and needs to be defined recursively like this): The issue is that when I enter ‘d3’ into the parser, I get a returned instance of
Can Antlr ignore some of the keyword?
I’m new to Antlr but I’m trying to change some of the Grammar from my existing project. The example of the grammar looks like this The grammar is supposed to parse something like this a=10 AND s=1 AND s=2 or ((a=9 AND s=1) AND s=2). The question is is it possible to ignore anything that’s not s={ANYTHING}. So from the
How to instruct the grammar to NOT generate certain methods in the ANTLR listener?
I have this grammar: The listener interface, which ANTLR generates, contains these four methods: Two of them are not needed for me: exitFoo() and enterBar(). Is it possible to tell ANTLR somehow to NOT generate them in the interface? I would actually prefer to find a way to somehow tell the grammar which grammar rules need those enter/exit methods, and
Hierarchical Visitor Pattern and State Management
I am trying to find a good way to manage state with the hierarchical visitor pattern and ANTLRs autogenerated base visitor class. While the example below is something silly I made up, I believe it helps get the point across about which concepts I would like to address. As an example lets say we have a class: Now note that
ANTLR Visitor Implementation For ArrayInitVisitorImpl
I am trying to build a visitor implementation that will convert a string array to unicode. I am using the following grammar: I have gotten as far as this: and I’m not sure how to handle the nested visiting. Any assistance is appreciated. Edit: Thank to help from Bart, I was able to come up with a final solution for
How to force ANTLR to parse all input CharStream
I’m using ANTLR4 to parse a syntax file. When I use BaseErrorListener to detect errors, I got a problem. When faced with an illegal input string, ANTLR automatically matches the appropriate branch and then ignores the subsequent stream of characters even if it contains errors. And I want to detect that error. Here are my g4 file and java file.
Nested Boolean Expression Parser using ANTLR
I’m trying to parse a Nested Boolean Expression and get the individual conditions within the expression separately. For e.g., if the input string is: (A = a OR B = b OR C = c AND ((D = d AND E = e) OR (F = f AND G = g))) I would like to get the conditions with the