antlr4

How can I import an ANTLR lexer grammar into another grammar using Gradle 2.10?

浪尽此生 提交于 2019-12-06 06:05:11
I've been learning about ANTLR 4 with Terence Parr's The Definitive ANTLR 4 Reference , which I've been following so far using Gradle 2.10 and its built-in ANTLR plugin. However I'm having some trouble getting some code which I adapted from Chapter 4, pp. 38-41 to work properly with my Gradle build script. (The reason I'm using Gradle, rather than ANTLR directly, is because I want to eventually integrate ANTLR into a Java web application which I'm making for my dissertation, and I'd strongly prefer to use a build tool for this so I can automate the ANTLR-to-Java code generation process and

Antlr4 language translation - separating template logic from visitor class?

微笑、不失礼 提交于 2019-12-06 05:56:23
问题 I’m looking at pragmatically translating huge amounts of relatively simple TSQL code to Groovy code. There are a number of reasons sure, but the driving reason is just to see if it can be done and in the process learn about compilers/grammers/ etc. Antlr4 seems like the ideal tool for this problem (Java is a plus). Tokenizing / parsing the TSQL (using a grammar file), and reading the tree using the generated Listener/Visitor is pretty straight forward. I know I could just create the string

ANTLR chaining 1 to 1 grammar rules together to solve conditionals

被刻印的时光 ゝ 提交于 2019-12-06 03:52:21
If you look at the ObjectiveC antlr v3 grammars ( http://www.antlr3.org/grammar/1212699960054/ObjectiveC2ansi.g ), and many of the other popular grammars out there they do a similar structure to this for solving conditionals conditional_expression : logical_or_expression ('?' logical_or_expression ':' logical_or_expression)? ; constant_expression : conditional_expression ; logical_or_expression : logical_and_expression ('||' logical_and_expression)* ; logical_and_expression : inclusive_or_expression ('&&' inclusive_or_expression)* ; inclusive_or_expression : exclusive_or_expression ('|'

What is the replacement for $type attribute in ANTLR 4?

此生再无相见时 提交于 2019-12-06 03:01:52
Here is the example. This ($type) is not recognised by ANTLR4. Number //options { backtrack=true; } : IntegerLiteral { $type = IntegerLiteral; } | FloatLiteral { $type = FloatLiteral; } | IntegerLiteral { $type = IntegerLiteral; } ; What could this be replaced by? Thank you. In ANTLR v4, do: Number : IntegerLiteral {setType(IntegerLiteral);} | ... In ANTLR 4, this is the new syntax: Foo : Bar -> type(SomeType) | ... ; However, for the rule you have above you should just remove the Number rule and make sure the FloatLiteral and IntegerLiteral rules are not fragment rules. 来源: https:/

Antlr Extraneous Input

混江龙づ霸主 提交于 2019-12-06 00:23:59
问题 I have a grammar file BoardFile.g4 that has (relevant parts only): grammar Board; //Tokens GADGET : 'squareBumper' | 'circleBumper' | 'triangleBumper' | 'leftFlipper' | 'rightFlipper' | 'absorber' | 'portal' ; NAME : [A-Za-z_][A-Za-z_0-9]* ; INT : [0-9]+ ; FLOAT : '-'?[0-9]+('.'[0-9]+)? ; COMMENT : '#' ~( '\r' | '\n' )*; WHITESPACE : [ \t\r\n]+ -> skip ; KEY : [a-z] | [0-9] | 'shift' | 'ctrl' | 'alt' | 'meta' | 'space' | 'left' | 'right' | 'up' | 'down' | 'minus' | 'equals' | 'backspace' |

Antlr4: How can I both hide and use Tokens in a grammar

强颜欢笑 提交于 2019-12-05 19:15:58
I'm parsing a script language that defines two types of statements; control statements and non control statements. Non control statements are always ended with ';' , while control statements may end with ';' or EOL ('\n'). A part of the grammar looks like this: script : statement* EOF ; statement : control_statement | no_control_statement ; control_statement : if_then_control_statement ; if_then_control_statement : IF expression THEN end_control_statment ( statement ) * ( ELSEIF expression THEN end_control_statment ( statement )* )* ( ELSE end_control_statment ( statement )* )? END IF end

Antlr Error Strategy to skip tokens until rule matches again

不羁岁月 提交于 2019-12-05 16:12:31
I tried this solution but it didn't seem to work for me Here's an excerpt from my grammer: module : BEGIN MODULE IDENT STRING module_element* END MODULE ; module_element : element_1 | element_2 | element_3 | ... ; There is a bigger tree below each element. Now when a RecognitionException occurs I want to consume tokens until either the next module_element matches or the parent END MODULE matches. Any hints on how to do this inside a class inheriting from DefaultErrorStrategy? edit: Here is a MCVE: Program.cs namespace AntlrExample { class Program { static void Main(string[] args) { var

how print parse-tree using python2 runtime with antlr4

这一生的挚爱 提交于 2019-12-05 15:12:06
I'm trying to use antlr4 version 4.4 and the python2 runtime . The grammar is from the antlr4 book, page 6, file: Hello.g4 : grammar Hello; r : 'hello' ID ; ID : [a-z]+ ; WS : [ \t\r\n]+ -> skip ; and I generate lexer and parser with command antlr4 -Dlanguage=Python2 Hello.g4 the files HelloLexer.py , HelloParser.py and HelloListener.py among other, are then generated. I make a main program test.py to test the generated python parser: from antlr4 import * from HelloLexer import HelloLexer from HelloParser import HelloParser def main(argv): input = FileStream(argv[1]) lexer = HelloLexer(input)

Rule variables in ANTLR4

佐手、 提交于 2019-12-05 11:44:08
I'm trying to convert my grammar from v3 to v4 and having some trouble. In v3 I have rules like this: dataspec[DataLayout layout] returns [DataExtractor extractor] @init { DataExtractorBuilder builder = new DataExtractorBuilder(layout); } @after { extractor = builder.create(); } : first=expr { builder.addAll(first); } (COMMA next=expr { builder.addAll(next); })* ; expr returns [List<ValueExtractor> ext] ... However, with rules in v4 returning these custom context objects instead of what I explicitly told them to return, things are all messed up. What's the v4 way to do this? There are multiple

ANTLR doesn't give correct output tokens for Scala Grammar

送分小仙女□ 提交于 2019-12-05 05:43:31
I am new to Scala and I am trying to parse Scala files with the use of Scala Grammar and ANTLR. Below is the code for Scala Grammar which I got from the git hub link: https://github.com/antlr/grammars-v4/tree/master/scala There are chances of repo to be moved so I am pasting the Scala grammar code here: grammar Scala; literal : '-'? IntegerLiteral | '-'? FloatingPointLiteral | BooleanLiteral | CharacterLiteral | StringLiteral | SymbolLiteral | 'null' ; qualId : Id ('.' Id)* ; ids : Id (',' Id)* ; stableId : (Id | (Id '.')? 'this') '.' Id | (Id '.')? 'super' classQualifier? '.' Id ;