antlr4

antlr4 python 3 print or dump tokens from plsql grammar

こ雲淡風輕ζ 提交于 2020-01-15 10:22:50
问题 I am using antlr4 in Python, to read the following grammar : https://github.com/antlr/grammars-v4/tree/master/plsql file grants.sql just has "begin select 'bob' from dual; end;" simple code to print lisp like tree from antlr4 import * from PlSqlLexer import PlSqlLexer from PlSqlParser import PlSqlParser from PlSqlParserListener import PlSqlParserListener input = FileStream('grants.sql') lexer = PlSqlLexer(input) stream = CommonTokenStream(lexer) parser = PlSqlParser(stream) tree = parser.sql

ANTLR4 lexer not resolving ambiguity in grammar order

烂漫一生 提交于 2020-01-15 04:50:14
问题 Using ANTLR 4.2, I'm trying a very simple parse of this test data: RRV0#ABC Using a minimal grammar: grammar Tiny; thing : RRV N HASH ID ; RRV : 'RRV' ; N : [0-9]+ ; HASH : '#' ; ID : [a-zA-Z0-9]+ ; WS : [\t\r\n]+ -> skip ; // match 1-or-more whitespace but discard I expect the lexer RRV to match before ID, based on the excerpt below from Terence Parr's Definitive ANTLR 4 reference: BEGIN : 'begin' ; // match b-e-g-i-n sequence; ambiguity resolves to BEGIN ID : [a-z]+ ; // match one or more

Parse sql query using antlr parsetree to mongo bson document in Java

纵然是瞬间 提交于 2020-01-14 05:37:11
问题 I have a SQL like query example: Select id,name from employee where age > 30 and department = 'IT' limit 200 The SQL query grammer is defined in an ANTLR4 grammar file. Is there any implementation that converts the parse tree of this query to a bson document? The bson document will then be used to query a mongo db. 回答1: In one of my previous jobs I did something similar: got a query (not an sql, but pretty similar) and translated it to mongo query with antlr. I don't have a code to share,

Bind ANTLR4 subrules of a rule

纵饮孤独 提交于 2020-01-14 03:07:48
问题 I've a grammar like that: living : search EOF ; search : K_SEARCH entity ( K_QUERY expr )? ( K_FILTER expr )? ( K_SELECT1 expr ( COMMA expr )* )? ( K_SELECT2 expr ( COMMA expr )* )? ( K_SELECT3 expr ( COMMA expr )* )? ; As you can see I've two optional expr. I've created my Visitor, and I'm able to get access to entity, K_QUERY and K_FILTER. SearchContext provides a List in order to get a list of all expr. However, how can I bind which expression is for K_QUERY , K_FILTER ? What about the

How can the PL/SQL grammar included with ANTLR4 be tested?

我是研究僧i 提交于 2020-01-13 20:35:06
问题 I'm just getting started using ANTLR, and want to try test parsing some simple PL/SQL statements using the plsql.g4 grammar. I am following the format used in Getting Started with ANTLR v4. The following commands execute without issue: antlr4 plsql.g4 java org.antlr.v4.Tool plsql.g4 javac plsql*.java In the getting started example, they run the following command: grun Hello r -tree Where 'Hello' is the name of the grammar and 'r' is one of its production rules. The grammar indicates that the

ANTLR4 API to Display Arbitrary ParseTree

守給你的承諾、 提交于 2020-01-13 06:22:12
问题 The current TestRig tool of ANTLR4 supports the -gui option to parse the whole input file and display the whole resultant parse tree graphically. Is it possible for us to obtain the parse tree first, modify it and call some APIs to display graphically a subset/subtree of the parse tree. My input source file is large and the standard parse tree displayed by TestRig is impossible to view. What's more, i want to filter out a lot of irrelevant grammar and focus on verifying those grammar I need

Access Channels in ANTLR 4 and Parse them separately

瘦欲@ 提交于 2020-01-11 11:58:17
问题 I have included my comments in to a separate channel in ANTLR 4. in my case it is channel 2. This is my lexer grammar. COMMENT: '/*' .*? '*/' -> channel(2) ; I want to access this channel 2 and do a parse on this channel to accumulate comments. So I included that as in parsing grammar as below comment :COMMENT ; In the program string s = " paring string" AntlrInputStream input = new AntlrInputStream(s); CSSLexer lexer = new CSSLexer(input); CommonTokenStream tokens = new CommonTokenStream

ANTLR4 mutual left recursion grammar

醉酒当歌 提交于 2020-01-10 05:45:06
问题 I have read many questions here on StackOverflow about mutual left-recursion issues in LL(k) parsers. I found the general algorithm for removing left-recursion: A : Aa | b ; becomes A : bR ; R : (aA)? ; However, I cannot figure out how to apply it to my situation. I have left_exp: IDENT | exp DOT IDENT ; exp : handful | of | other rules | left_exp ; The "handful of other rules" all contain regular recursion, such as exp : exp PLUS exp , etc. and have no issues. The issue is with left_exp and

How do I get the original text that an antlr4 rule matched?

不羁的心 提交于 2020-01-09 06:52:27
问题 Using the Java 7 grammar https://github.com/antlr/grammars-v4/blob/master/java7/Java7.g4 I want to find methods with a specific name and then just print out that method. I see that I can use the methodDeclaration rule when I match. So I subclass Java7BaseListener and override this listener method: @Override public void enterMethodDeclaration(Java7Parser.MethodDeclarationContext ctx) { } How do I get the original text out? ctx.getText() gives me a string with all the whitespace stripped out. I

How do I get the original text that an antlr4 rule matched?

為{幸葍}努か 提交于 2020-01-09 06:52:18
问题 Using the Java 7 grammar https://github.com/antlr/grammars-v4/blob/master/java7/Java7.g4 I want to find methods with a specific name and then just print out that method. I see that I can use the methodDeclaration rule when I match. So I subclass Java7BaseListener and override this listener method: @Override public void enterMethodDeclaration(Java7Parser.MethodDeclarationContext ctx) { } How do I get the original text out? ctx.getText() gives me a string with all the whitespace stripped out. I