antlr4

ANTLR 4 - Tree pattern matching

夙愿已清 提交于 2021-01-28 02:02:14
问题 I am trying to understand parse tree matching in ANTLR 4, so for that I have the following java code: package sampleCodes; public class fruits { public static void main(String[] args){ int a = 10; System.out.println(a); } } I am using ANTLR 4 to create a parse tree of this code. Now, I want to use tree pattern matching function to find "int a = 10;". There is a doc on GitHub: https://github.com/antlr/antlr4/blob/master/doc/tree-matching.md which explains this(something like this) by an

Antlr generated classes access modifier to internal

我只是一个虾纸丫 提交于 2021-01-27 04:18:33
问题 I am building a library which contains certain parsers. These parsers are internally built with ANTLR4. Since the generated classes are all public, users of my library are able to see all the classes they do not need to see. Also the Sandcastle documentation contains all these classes. Is there any way I can tell Antlr to make the generated classes internal instead of public? 回答1: Actually, it's relatively easy to do. Internally, ANTLR uses StringTemplate files to generate code for each of

Antlr generated classes access modifier to internal

纵饮孤独 提交于 2021-01-27 04:18:32
问题 I am building a library which contains certain parsers. These parsers are internally built with ANTLR4. Since the generated classes are all public, users of my library are able to see all the classes they do not need to see. Also the Sandcastle documentation contains all these classes. Is there any way I can tell Antlr to make the generated classes internal instead of public? 回答1: Actually, it's relatively easy to do. Internally, ANTLR uses StringTemplate files to generate code for each of

ANTLR4 errors not being reported to custom lexer / parser error listeners

耗尽温柔 提交于 2021-01-07 04:12:32
问题 This question is about how different kinds of ANTLR4 lexer errors are reported to custom lexer and parser error listeners. From my experiments, it seems that not all ANTLR4 errors are reported internally to both the default and custom lexer/parser error listeners. Some ANTLR4 errors are only reported on the console. Consequently, there is no way for me to detect such errors programmatically. EXAMPLES I have two working grammars (one simple, one complex) and a common ANTLR4 setup in Visual

ANTLR4 errors not being reported to custom lexer / parser error listeners

余生颓废 提交于 2021-01-07 04:10:37
问题 This question is about how different kinds of ANTLR4 lexer errors are reported to custom lexer and parser error listeners. From my experiments, it seems that not all ANTLR4 errors are reported internally to both the default and custom lexer/parser error listeners. Some ANTLR4 errors are only reported on the console. Consequently, there is no way for me to detect such errors programmatically. EXAMPLES I have two working grammars (one simple, one complex) and a common ANTLR4 setup in Visual

How to write a antlr4 visitor

痴心易碎 提交于 2021-01-07 02:31:11
问题 I am trying to write a visitor for a simple antlr4 grammar - I am adapting from the following example from the book: * directory tour * example: LabeledExpr.g4, EvalVisitor.java, Calc.java Based on the java code, I have written the following go code: package main import ( "os" "./parser" "github.com/antlr/antlr4/runtime/Go/antlr" ) type evalVisitor struct { *parser.BaseLabeledExprVisitor } func (v *evalVisitor) VisitAddSub(c *parser.AddSubContext) int { left := v.Visit(c.Expr(0)) right := v

How to make ANTLR consume all available elements using recursion?

孤者浪人 提交于 2021-01-07 02:28:26
问题 This is my grammar: grammar test; text: foo EOF; foo: 'X' | '(' foo ')' | foo '!' | foo tail ; tail: (' ' foo)+; This is the input it perfectly parses: X (X! (X)! (X X X)!!!) X However, the output tree has too many tail elements, as I explained earlier here. Is it possible to fix this? 回答1: Thanks to @kaby76, the solution is found: foo: 'X' tail? | '(' foo ')' tail? | foo '!' tail? ; tail: ( ' ' 'X' | ' ' '(' foo ')' | ' ' foo '!' )+ ; 来源: https://stackoverflow.com/questions/64635366/how-to

How to write a antlr4 visitor

隐身守侯 提交于 2021-01-07 02:28:14
问题 I am trying to write a visitor for a simple antlr4 grammar - I am adapting from the following example from the book: * directory tour * example: LabeledExpr.g4, EvalVisitor.java, Calc.java Based on the java code, I have written the following go code: package main import ( "os" "./parser" "github.com/antlr/antlr4/runtime/Go/antlr" ) type evalVisitor struct { *parser.BaseLabeledExprVisitor } func (v *evalVisitor) VisitAddSub(c *parser.AddSubContext) int { left := v.Visit(c.Expr(0)) right := v

How to make ANTLR consume all visible elements?

邮差的信 提交于 2020-12-26 11:06:20
问题 This is my grammar: grammar test; text: foo EOF; foo: 'X' | foo '!' | foo '?' | foo tail ; tail: (' ' foo)+; I'm parsing this text: X? X! X X This is the tree I'm getting: What should change in the grammar so that I get only one tail element with a collection of all foo elements inside? In the real world the task is way more complex, and using only a scanner is no solution to it. 回答1: As far as I can tell, what you want is this: item: 'X' ('!' | '?')*; // Alternatively to get a tree per