antlr4

Gradle ANTLR4 plugin in Android app: build fails

妖精的绣舞 提交于 2020-01-25 12:37:27
问题 I spent few days spotting this issue, and I'd like to post my finding here. I'm building an Android app with several modules, one of them using ANTLR plugin. While the module with ANTLR builds OK, as it is a Java module, the android module fails in transformClassesWithDexForDebug task: * What went wrong: Execution failed for task ':Android:transformClassesWithDexForDebug'. > com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util

How can I parse nested source files with ANTLR4 - Trying one more time

混江龙づ霸主 提交于 2020-01-25 10:17:08
问题 I found the code (reproduced below) in an article from Terrence Parr showing how INCLUDE files could be handled in ANTLR3 for Java. I tried to add this to a grammar I use with ANTLR4 (with a C++ target) but when I tried to generate a parser, I got the errors error(50): : syntax error: '^' came as a complete surprise to me error(50): : syntax error: mismatched input '->' expecting SEMI while matching a rule error(50): : syntax error: '^' came as a complete surprise to me error(50): : syntax

Getting token start character position relative to the beginning of file

走远了吗. 提交于 2020-01-24 05:44:26
问题 Is there any reliable way with antlr4 API to get Token start character position relative to the beginning of file, not line? After doing some research the only way I found is to use some custom implementation of IntStream, that does not treat '\n' as line terminators, but perhaps I'm missing some easier way? I'm using Visitor API if it matters. The application I'm working on parses source files and provides insertion coordinates for another application that uses provided coordinates to insert

ANTLR Grammar Mutually Left Recursive

狂风中的少年 提交于 2020-01-24 01:08:11
问题 I do know that this question has been asked a lot of times. I am trying to build a grammar using ANTLR. Predicate : LOWERCASE | Predicate VarChars ; VarChars : LOWERCASE | UPPERCASE; fragment LOWERCASE : [a-z] ; fragment UPPERCASE : [A-Z] ; I am getting the following error :"The following sets of rules are mutually left-recursive [Predicate]" Please show me how this is fixed. How to remove the mutual left recursion in my antlr grammar. 回答1: Get rid of the recursive occurrence of "Predicate"

Generate source code from AST with Antlr4 and StringTemplates

梦想与她 提交于 2020-01-21 11:37:39
问题 If I have an AST and modify it, can I use StringTemplates to generate the source code for the modified AST? I have successfully implemented my grammar for Antlr4. It generates the AST of a source code and I use the Visitor Class to perform the desired actions. I then modify something in the AST and I would like to generate the source code for that modified AST. (I believe it is called pretty-printing?). Does Antlr's built in StringTemplates have all the functionality to do this? Where should

ANTLR4 parsing RegEx

你离开我真会死。 提交于 2020-01-17 06:04:31
问题 I am tryingo to parse RegEx and specifically the following: [A-Z0-9]{1,20} The problem is, i don't know how to make the following grammar work beacuse the Char and Int tokens are both recognizing the digit. grammar RegEx; regEx : (character count? )+ ; character : Char | range ; range : '[' (rangeChar|rangeX)+ ']' ; rangeX : rangeStart '-' rangeEnd ; rangeChar : Char ; rangeStart : Char ; rangeEnd : Char ; count : '{' (countExact | (countMin ',' countMax) ) '}' ; countMin : D+ ; countMax :

ANTLR4 parsing RegEx

痞子三分冷 提交于 2020-01-17 06:04:05
问题 I am tryingo to parse RegEx and specifically the following: [A-Z0-9]{1,20} The problem is, i don't know how to make the following grammar work beacuse the Char and Int tokens are both recognizing the digit. grammar RegEx; regEx : (character count? )+ ; character : Char | range ; range : '[' (rangeChar|rangeX)+ ']' ; rangeX : rangeStart '-' rangeEnd ; rangeChar : Char ; rangeStart : Char ; rangeEnd : Char ; count : '{' (countExact | (countMin ',' countMax) ) '}' ; countMin : D+ ; countMax :

ANTLR parsing and code generation with “-” operator and “-” number sign

岁酱吖の 提交于 2020-01-16 18:53:45
问题 This is my grammar: grammar FOOL; @header { import java.util.ArrayList; } @lexer::members { public ArrayList<String> lexicalErrors = new ArrayList<>(); } /*------------------------------------------------------------------ * PARSER RULES *------------------------------------------------------------------*/ prog : exp SEMIC #singleExp | let exp SEMIC #letInExp | (classdec)+ SEMIC (let)? exp SEMIC #classExp ; classdec : CLASS ID ( EXTENDS ID )? (LPAR (vardec ( COMMA vardec)*)? RPAR)? (CLPAR (

Antlr lexer semantic predicate on an alternative

一个人想着一个人 提交于 2020-01-16 12:19:07
问题 Given the grammar: grammar Test; words: (WORD|SPACE|DOT)+; WORD : ( LD |DOT {_input.LA(1)!='.'}? ) + ; DOT: '.'; SPACE: ' '; fragment LD: ~[.\n\r ]; with Antlr4 generated Lexer, for an input: test. test.test test..test The token sequence is like: [@0,0:4='test.',<1>,1:0] [@1,5:5=' ',<3>,1:5] [@2,6:14='test.test',<1>,1:6] [@3,15:15=' ',<3>,1:15] [@4,16:19='test',<1>,1:16] [@5,20:20='.',<2>,1:20] [@6,21:25='.test',<1>,1:21] [@7,26:25='<EOF>',<-1>,1:26] What puzzles why the last piece of text

Antlr to parse python setup file

时光毁灭记忆、已成空白 提交于 2020-01-15 11:41:34
问题 I have a java program that has to parse a python setup.py file to extract info from it. I sorta have something working, but I hit a wall. I am starting with a simple raw file first, once i get that running, then i will worry about stripping out the noise that i don't want to make it reflect an actual file. So here's my grammer grammar SetupPy ; file_input: (NEWLINE | setupDeclaration)* EOF; setupDeclaration : 'setup' '(' method ')'; method : setupRequires testRequires; setupRequires : 'setup