antlr

ANTLR lexer can't lookahead at all

老子叫甜甜 提交于 2020-01-11 09:23:10
问题 I have the following grammar: rule: 'aaa' | 'a' 'a'; It can successfully parse the string 'aaa', but it fails to parse 'aa' with the following error: line 1:2 mismatched character '<EOF>' expecting 'a' FYI, it is the lexer's problem not the parser's because I don't even call the parser. The main function looks like: @members { public static void main(String[] args) throws Exception { RecipeLexer lexer = new RecipeLexer(new ANTLRInputStream(System.in)); for (Token t = lexer.nextToken(); t

How to avoid building intermediates and useless AST nodes with ANTLR3?

被刻印的时光 ゝ 提交于 2020-01-11 07:09:15
问题 I wrote an ANTLR3 grammar subdivided into smaller rules to increase readability. For example: messageSequenceChart: 'msc' mscHead bmsc 'endmsc' end ; # Where mscHead is a shortcut to : mscHead: mscName mscParameterDecl? timeOffset? end mscInstInterface? mscGateInterface ; I know the built-in ANTLR AST building feature allows the user to declare intermediate AST nodes that won't be in the final AST. But what if you build the AST by hand? messageSequenceChart returns [msc::MessageSequenceChart*

ANTLR Is it possible to make grammar with embed grammar inside?

ⅰ亾dé卋堺 提交于 2020-01-11 05:26:07
问题 ANTLR: Is it possible to make grammar with embed grammar (with it's own lexer) inside? For example in my language I have ability to use embed SQL language: var Query = [select * from table]; with Query do something ....; Is it possible with ANTLR? 回答1: Is it possible to make grammar with embed grammar (with it's own lexer) inside? If you mean whether it is possible to define two languages in a single grammar (using separate lexers), then the answer is: no, that's not possible. However, if the

if then else conditional evaluation

对着背影说爱祢 提交于 2020-01-11 02:20:06
问题 I have a language which basically is meant to map columns to a new structure in an array. The language is meant for product managers to define mappings without having to know a lot of programming details. I'm sure there is a lot more to improve here but this is what I have. The language works, mostly. The problem I have is with conditional statements. My parser has the following rule: conditionalexpr : IF^ LPAREN! (statement) RPAREN! THEN! LCURLY! statement RCURLY! (ELSE! LCURLY! statement

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

Catching (and keeping) all comments with ANTLR

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-09 10:55:13
问题 I'm writing a grammar in ANTLR that parses Java source files into ASTs for later analysis. Unlike other parsers (like JavaDoc) I'm trying to keep all of the comments. This is difficult comments can be used literally anywhere in the code. If a comment is somewhere in the source code that doesn't match the grammar, ANTLR can't finish parsing the file. Is there a way to make ANTLR automatically add any comments it finds to the AST? I know the lexer can simply ignore all of the comments using

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

Ignore tokens in the token characters?

不想你离开。 提交于 2020-01-07 06:39:57
问题 I have the following token definition in my lexer defining a CharacterString (e.g. 'abcd'): CharacterString: Apostrophe (Alphanumeric)* Apostrophe ; Is it possible to ignore the two apostrophes to then be able to get the token string without them in the lexer (via $CharacterString.text->chars)? I tried ... CharacterString: Apostrophe { $channel = HIDDEN; } (Alphanumeric)* Apostrophe { $channel = HIDDEN; } ; ... without success... This case does not even match my string anymore (e.g. 'oiu'

什么是自举?

≡放荡痞女 提交于 2020-01-07 02:27:50
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 我一直看到在应用程序开发的讨论中提到的“bootstrapping”。 它似乎既广泛又重要,但我还没有看到一个关于实际引导的错误解释; 相反,似乎每个人都应该知道这意味着什么。 不过,我没有。 我可以说,它与启动时应用程序所需的初始化任务有关,但我可能完全错了。 任何人都可以帮我理解这个想法吗? #1楼 请参阅关于 bootstrapping 的维基百科文章。 有一节和链接解释了它在 计算中的 含义。 它在该领域有四种不同的用途。 以下是一些引用,但有关更深入的解释和其他含义,请参阅上面的链接。 “...是一种简单的计算机程序激活更复杂的程序系统的技术。” “对bootstrapping这个术语的另一种用法是使用编译器来编译自己,首先用现有语言编写新编程语言的一小部分编译器来编译用新语言编写的新编译器的更多程序。” #2楼 “Bootstrapping”来自于“通过自己的引导来提升自己”这个术语。 你可以从 维基百科 那里得到多少。 在计算中,引导加载程序是机器启动时运行的第一段代码,负责加载操作系统的其余部分。 在现代计算机中,它存储在ROM中,但我记得PDP-11上的引导过程,您可以通过前面板开关戳位以将特定磁盘段加载到内存中,然后运行它。 不用说,引导加载程序通常非常小。 “Bootstrapping