antlr4

Antlr : beginner 's mismatched input expecting ID

会有一股神秘感。 提交于 2019-11-28 02:13:41
问题 As a beginner, when I was learning ANTLR4 from the The Definitive ANTLR 4 Reference book, I tried to run my modified version of the exercise from Chapter 7: /** * to parse properties file * this example demonstrates using embedded actions in code */ grammar PropFile; @header { import java.util.Properties; } @members { Properties props = new Properties(); } file : { System.out.println("Loading file..."); } prop+ { System.out.println("finished:\n"+props); } ; prop : ID '=' STRING NEWLINE {

build AST in antlr4

独自空忆成欢 提交于 2019-11-28 01:23:12
问题 I was wondering whether we could build an AST using Antlr version 4. I couldn't find any reference on building it using antlr4. One SO answer says that it would be easy to use antlr4 which produces only parse tree but my question is what about the efficiency ? It forces us to crawl whole parse tree instead of an abstract syntax tree which is not efficient way to walk through the whole tree and perform task using visitors. 回答1: There are two key items I'd like to point out first: Efficiency

ANTLR 4 tree inject/rewrite operator

谁说胖子不能爱 提交于 2019-11-27 20:13:07
In ANTLR 3 you could just do the following: andExpression : (andnotExpression -> andnotExpression) (AND? a=andnotExpression -> ^(AndNode $andExpression $a))* ; Any idea how to do it in the new version? As mentioned by Sam (280Z28), ANTLR 4 does not have rewrite operators. When generating the parser, ANTLR 4 creates some listener classes that you can use to listen for "enter" and "exit" events of all parser rules. Also, ANTLR 4 supports "direct left recursive rules", so your expression rules can be defined in a single rule as demonstrated below: grammar Expr; parse : expression EOF ; expression

ANTLR4 visitor pattern on simple arithmetic example

痴心易碎 提交于 2019-11-27 20:08:07
I am a complete ANTLR4 newbie, so please forgive my ignorance. I ran into this presentation where a very simple arithmetic expression grammar is defined. It looks like: grammar Expressions; start : expr ; expr : left=expr op=('*'|'/') right=expr #opExpr | left=expr op=('+'|'-') right=expr #opExpr | atom=INT #atomExpr ; INT : ('0'..'9')+ ; WS : [ \t\r\n]+ -> skip ; Which is great because it will generate a very simple binary tree that can be traversed using the visitor pattern as explained in the slides, e.g., here's the function that visits the expr : public Integer visitOpExpr(OpExprContext

ANTLR4: Matching all input alternatives exaclty once

让人想犯罪 __ 提交于 2019-11-27 14:50:05
问题 How can I make a rule to match all of its alternatives only once in any order, in ANTLR? i.e. rule: ('example\r\n' | 'example2\r\n') nextRule I would like 'example' and 'example2' to match only once before moving onto the next rule. Should match inputs of: example example2 or example2 example but not inputs of: example example example2 回答1: How can I make a rule to match all of its alternatives only once in any order, in ANTLR? "All of its alternatives only once" is simply rule: altA altB

Semantic predicates in ANTLR4?

心已入冬 提交于 2019-11-27 14:06:12
How would you translate this portion of code written in ANTLR 3 into ANTLR 4? expr: (Identifier '.')=> (refIdentifier) | (Identifier '!')=> (refIdentifier) | (Identifier '=>')=> (lambdaExpression); I mean this kind of semantic predicate does not seem to exist now. What could I use Instead? Bart Kiers In ANTLR v4, there are no longer gated semantic predicates , { ... }?=> , and there are also no longer syntactic predicates , ( ... )=> , because the parsing algorithm used in v4 can resolve the ambiguities (the need for such predicates are no longer needed). So, this should just work for you:

Antlr4 Listeners and Visitors - which to implement?

こ雲淡風輕ζ 提交于 2019-11-27 12:55:18
问题 I'm reading "The Definitive Antlr 4 Reference" and get the idea in relation to how Listeners and Visitors work. The book explains particularly well how Listeners relate to SAX parsers and makes it obvious when methods are going to be called during the implementation of each. I can see also that listeners are quite good for transforming input to output but I would appreciate a short explanation/example as to when to use a Listener and when to use a Visitor (or should they both be used in

antlr4 sql grammar

佐手、 提交于 2019-11-27 11:41:15
问题 Does ANTLR4 have a sql grammar available? If so, where can I find it? There is a link from the ANTLR wiki, but the link is broken: grammar list 回答1: No, at the time of this writing, there is no v4 SQL grammar. All v4 grammar will be put into the following Github repository (as far as I can remember from the ANTLR mailing list): https://github.com/antlr/grammars-v4 The v3 grammars here now: http://antlr3.org/grammar/list.html EDIT - April 2018 There is now a user contributed MySQL grammar here

ANTLR 4.5 - Mismatched Input 'x' expecting 'x'

♀尐吖头ヾ 提交于 2019-11-27 11:35:02
I have been starting to use ANTLR and have noticed that it is pretty fickle with its lexer rules. An extremely frustrating example is the following: grammar output; test: FILEPATH NEWLINE TITLE ; FILEPATH: ('A'..'Z'|'a'..'z'|'0'..'9'|':'|'\\'|'/'|' '|'-'|'_'|'.')+ ; NEWLINE: '\r'? '\n' ; TITLE: ('A'..'Z'|'a'..'z'|' ')+ ; This grammar will not match something like: c:\test.txt x Oddly if I change TITLE to be TITLE: 'x' ; it still fails this time giving an error message saying "mismatched input 'x' expecting 'x'" which is highly confusing. Even more oddly if I replace the usage of TITLE in test

How can I build an AST using ANTLR4? [duplicate]

安稳与你 提交于 2019-11-27 09:29:36
This question already has an answer here: How to create AST with ANTLR4? 2 answers I have an ANTLR3 grammar that builds an abstract syntax tree. I'm looking into upgrading to ANTLR4. However, it appears that ANTLR4 only builds parse trees and not abstract syntax trees. For example, the output=AST option is no longer recognized. Furthermore neither "AST" nor "abstract syntax" appears in the text of "The Definitive ANTLR4 reference" . I'm wondering if I'm missing something. My application currently knows how to crawl over the AST produced by ANTLR3. Changing it to process a parse tree isn't