antlr3

Python+ANTLR4: No module named antlr4

两盒软妹~` 提交于 2019-12-13 12:34:55
问题 I would like to use ANTLR4 with Python 2.7 and for this I did the following: I installed the package antlr4-4.6-1 on Arch Linux with sudo pacman -S antlr4 . I wrote a MyGrammar.g4 file and successfully generated Lexer and Parser Code with antlr4 -Dlanguage=Python2 MyGrammar.g4 Now executing for example the generated Lexer code with python2 MyGrammarLexer.py results in the error ImportError: No module named antlr4 . What could to be the problem? FYI: I have both Python2 and Python3 installed -

Replace token in ANTLR

一笑奈何 提交于 2019-12-13 12:13:16
问题 I want to replace a token using ANTLR. I tried with TokenRewriteStream and replace, but it didn't work. Any suggestions? ANTLRStringStream in = new ANTLRStringStream(source); MyLexer lexer = new MyLexer(in); TokenRewriteStream tokens = new TokenRewriteStream(lexer); for(Object obj : tokens.getTokens()) { CommonToken token = (CommonToken)obj; tokens.replace(token, "replacement"); } The lexer finds all occurences of single-line comments, and i want to replace them in the original source too.

Storing line number in ANTLR Parse Tree

一笑奈何 提交于 2019-12-13 05:31:37
问题 Is there any way of storing line numbers in the created parse tree, using ANTLR 4? I came across this article: http://puredanger.github.io/tech.puredanger.com/2007/02/01/recovering-line-and-column-numbers-in-your-antlr-ast/ ,which does it but i think it's for older ANTLR version, because parser.setASTFactory(factory); does not seem to be applicable for ANTLR 4. I am thinking of having something like treenode.getLine() , just like we can have treenode.getChild() 回答1: With Antlr4, you normally

ANTLR from Java to C#

无人久伴 提交于 2019-12-13 02:13:43
问题 I'm planning to create something that would do automated translation from Java to C# (and in reverse afterwards). What I need is something that you could use to translate Java source code into C# source code. I ran across ANTLR, but I'm not exactly sure of how go about using it for my task. I know ANTLR has a strong support for both Java and C#, and there are already existing grammars for both of them, the lexing/parsing process, then the AST creation, and then finally the tree walker.

Ignore some part of input when parsing with ANTLR

泪湿孤枕 提交于 2019-12-12 09:02:27
问题 I'm trying to parse a language by ANTLR (ANTLRWorks-3.5.2). The goal is to enter complete input but Antlr gives a parse tree of defined parts in grammar and ignore the rest of inputs, for example this is my grammar : grammar asap; project : '/begin PROJECT' name module+ '/end PROJECT'; module : '/begin MODULE'name '/end MODULE'; name : IDENT ; IDENT : ('a'..'z'|'A'..'Z')('a'..'z'|'A'..'Z'|'0'..'9'|'_'|'.'|':'|'-')*; Given input: /begin PROJECT HybridSailboat_2 /begin MODULE engine /begin A2ML

antlr global rule scope declaration vs @members declaration

人走茶凉 提交于 2019-12-12 08:34:30
问题 Which one would you prefer to declare a variable in which case, global scope or @members declaration? It seems to me that they can serve for same purpose? UPDATE here is a grammar to explain what i mean. grammar GlobalVsScope; scope global{ int i; } @lexer::header{package org.inanme.antlr;} @parser::header{package org.inanme.antlr;} @parser::members { int j; } start scope global; @init{ System.out.println($global::i); System.out.println(j); }:R EOF; R:'which one'; 回答1: Note that besides

Converting from Antlr3 to Antlr4

匆匆过客 提交于 2019-12-12 06:32:44
问题 I am in the process of converting an antlr3 to antlr4 grammar. I have stripped out all the syntactic predicates. I am struggling to make a correct conversion of this relaxed_date_month_first : relaxed_day_of_week? relaxed_month COMMA? WHITE_SPACE relaxed_day_of_month (relaxed_year_prefix relaxed_year)? -> ^(EXPLICIT_DATE relaxed_day_of_month relaxed_month relaxed_day_of_week? relaxed_year?) to antlr4 grammar. Everytime the antlr4 tool runs into "->" character it says "extraneous input '->'

Why does Antlr not generate a lexer java file?

时间秒杀一切 提交于 2019-12-12 03:09:28
问题 Antlr3 does not generate Mylexer.java. I use AntlrWorks... when I have grammar starting like grammar mylexer; It does generate myParser.java It looks like a simple thing.. I wonder what may be the reason.. and the solution... I get no error message. 回答1: I found a way. AntlrWorks 1.4 seems to have a bug. When I pressed ctrl+shift+G to generate it did not give an error message (in fact, I got that compilation was ok) and generated a parser file only. The cursor was not on the first line of the

Generated Antlr Parser in Java: Not all inputs are read

时光总嘲笑我的痴心妄想 提交于 2019-12-12 00:21:14
问题 I am working on my Antlr grammar to parse polynomial functions in multiple variables using Java. Examples for legal input are 42; X; +42X; Y^42; 1337HelloWorld; 13,37X^42; The following grammar does compile without warnings or errors: grammar Function; parseFunction returns [java.util.List<java.util.List<Object>> list] : { list = new java.util.ArrayList(); } ( f=functionPart { list.add($f.list); } )+ | { list = new java.util.ArrayList(); } ( fb=functionBegin ) { list.add($fb.list); } ( f

Mediawiki parsing in ANTLR: processing ' tokens

狂风中的少年 提交于 2019-12-11 23:13:51
问题 I'm trying to write a grammar to parse Media wiki's wiki syntax, and after this the Creole syntax too (unfortunately an existing Creole grammar doesn't work in Antlr 3). My issue right now is being able to capture a bold rule when I'm already inside an italic rule, or visa versa. For example '' this text is bold '''now it's italic''' and just bold again'' I've got a lot of help from this question but I'm stuck. The goal is to produce HTML inside the grammar using actions, or possibly an AST -