antlr4

ANTLRInputStream and ANTLRFileStream are deprecated, what are the alternatives?

▼魔方 西西 提交于 2019-12-01 19:48:23
If I use ANTLRFileStream antlrFileStream = new ANTLRFileStream("myfile.testlang"); or ANTLRInputStream input = new ANTLRInputStream( new FileInputStream("myfile.testlang") ); Compiler shows deprecation error for both the classes what is alternative? Ameer Tamboli You can use the CharStreams class instead of the deprecated classes as below. CharStream codePointCharStream = CharStreams.fromFileName("myfile.testlang"); TESTLANGLexer lexer = new TESTLANGLexer(codePointCharStream); TESTLANGParser parser = new TESTLANGParser(new CommonTokenStream(lexer)); parser.addParseListener(new

ANTLRInputStream and ANTLRFileStream are deprecated, what are the alternatives?

人走茶凉 提交于 2019-12-01 18:52:36
问题 If I use ANTLRFileStream antlrFileStream = new ANTLRFileStream("myfile.testlang"); or ANTLRInputStream input = new ANTLRInputStream( new FileInputStream("myfile.testlang") ); Compiler shows deprecation error for both the classes what is alternative? 回答1: You can use the CharStreams class instead of the deprecated classes as below. CharStream codePointCharStream = CharStreams.fromFileName("myfile.testlang"); TESTLANGLexer lexer = new TESTLANGLexer(codePointCharStream); TESTLANGParser parser =

Parsing fortran-style .op. operators

元气小坏坏 提交于 2019-12-01 14:21:07
I'm trying to write an ANTLR4 grammar for a fortran-inspired DSL. I'm having difficulty with the 'ole classic ".op." operators: if (1.and.1) then where both "1"s should be intepreted as integer. I looked at the OpenFortranParser for insight, but I can't make sense out of it. Initially, I had suitable definitions for INTEGER and REAL in my lexer. Consequently, the first "1" above always parsed as a REAL, no matter what I tried. I tried moving things into the parser, and got it to the point where I could reliably recognize the ".and." along with numbers around it as appropriately INTEGER or REAL

ANTLR - mismatched input error

徘徊边缘 提交于 2019-12-01 13:19:46
I have a grammar which looks like this consisting of comment and control statements of a particular language: Grammar: grammar DD; ddlist: (ddstmt| jclcomment)+; ddstmt: dd1 | dd2 | dd3 | dd4 ; dd1: JCLBEGIN ddname DDWORD 'DUMMY'; dd2: JCLBEGIN ddname DDWORD 'DYNAM'; dd3: JCLBEGIN ddname DDWORD NAME'=' ('*'|NAME); dd4: JCLBEGIN ddname DDWORD '*' inlinerec INLINESTMTEND?; inlinerec: (INLINEDATA )+ ; fragment INLINEDATA: (~[\r\n])*; ddname: NAME; jclcomment: JCLCOMMENT+; JCLCOMMENT: COMMENTBEGIN ~[\r\n]*; DDWORD: 'DD'; JCLBEGIN: '//' ; COMMENTBEGIN: '//*' ; INLINESTMTEND: '/*' ; NAME : [A-Z#]

Parsing fortran-style .op. operators

南楼画角 提交于 2019-12-01 13:10:57
问题 I'm trying to write an ANTLR4 grammar for a fortran-inspired DSL. I'm having difficulty with the 'ole classic ".op." operators: if (1.and.1) then where both "1"s should be intepreted as integer. I looked at the OpenFortranParser for insight, but I can't make sense out of it. Initially, I had suitable definitions for INTEGER and REAL in my lexer. Consequently, the first "1" above always parsed as a REAL, no matter what I tried. I tried moving things into the parser, and got it to the point

ANTLR String interpolation

三世轮回 提交于 2019-12-01 08:25:44
问题 I'm trying to write an ANTLR grammar that parses string interpolation expressions such as: my.greeting = "hello ${your.name}" The error I get is: line 1:31 token recognition error at: 'e' line 1:34 no viable alternative at input '<EOF>' MyParser.g4: parser grammar MyParser; options { tokenVocab=MyLexer; } program: variable EQ expression EOF; expression: (string | variable); variable: (VAR DOT)? VAR; string: (STRING_SEGMENT_END expression)* STRING_END; MyLexer.g4: lexer grammar MyLexer; START

What to use in ANTLR4 to resolve ambiguities (instead of syntactic predicates)?

放肆的年华 提交于 2019-12-01 08:10:55
问题 In ANTLR v3, syntactic predicates could be used to solve e.g., the dangling else problem. ANTLR4 seems to accept grammars with similar ambiguities, but during parsing it reports these ambiguities (e.g., "line 2:29 reportAmbiguity d=0 (e): ambigAlts={1, 2}, input=..."). It produces a parse tree, despite these ambiguities (by chosing the first alternative, according to the documentation). But what can I do, if I want it to chose some other alternative? In other words, how can I explicitly

ANTLR4: TokenStreamRewriter output doesn't have proper format (removes whitespaces)

拈花ヽ惹草 提交于 2019-12-01 05:59:37
I am using Antlr4 and java7 grammar ( source ) for modifying an input Java Source file. More specifically, I am using the TokenStreamRewriter class to modify some tokens. The following code is a sample that shows how the tokens are modified: public class TestListener extends JavaBaseListener { private TokenStreamRewriter rewriter; rewriter = new TokenStreamRewriter(tokenStream); rewriter.replace(ctx.getStart(), ctx.getStop(), "someText"); } When I print the altered source code, the white spaces and tabs are removed and the new source file's format is like this: importjava.util.ArrayList

How to generate AST in ANTLR4?

大憨熊 提交于 2019-12-01 05:05:58
问题 I'm working on a project in which I have to generate Abstract Syntax Tree for a given program. Here program can be in any mainstream programming languages. What should be the standard way of generating AST in ANTLR4? I know only basics of ANTLR4 and I'm able to generate Parse tree for a given program. 回答1: ANTLR 4 automatically generates parse trees instead of relying on manually-structured ASTs. This decision was made after observing years of development with prior approaches encountering

Using ANTLR Parser and Lexer Separatly

孤街醉人 提交于 2019-12-01 04:12:51
I used ANTLR version 4 for creating compiler.First Phase was the Lexer part. I created "CompilerLexer.g4" file and putted lexer rules in it.It works fine. CompilerLexer.g4: lexer grammar CompilerLexer; INT : 'int' ; //1 FLOAT : 'float' ; //2 BEGIN : 'begin' ; //3 END : 'end' ; //4 To : 'to' ; //5 NEXT : 'next' ; //6 REAL : 'real' ; //7 BOOLEAN : 'bool' ; //8 . . . NOTEQUAL : '!=' ; //46 AND : '&&' ; //47 OR : '||' ; //48 POW : '^' ; //49 ID : [a-zA-Z]+ ; //50 WS : ' ' -> channel(HIDDEN) //50 ; Now it is time for phase 2 which is the parser.I created "CompilerParser.g4" file and putted grammars