antlr

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

Get original text of an Antlr rule

耗尽温柔 提交于 2019-11-27 18:34:01
问题 I am an ANTLR beginner and want to calculate a SHA1-Hash of symbols. My simplified example grammar: grammar Example; method @after{calculateSha1($text); }: 'call' ID; ID: 'A'..'Z'+; WS: (' '|'\n'|'\r')+ {skip(); } COMMENT: '/*' (options {greedy=false;}: .)* '*/' {$channel=HIDDEN} As the lexer removes all whitespaces the different strings callABC , call /* DEF */ ABC unfortunately get the same SHA1-Hash value. Is it possible to get the "original" text of a rule between the start- and end-token

Why is .NET exception not caught by try/catch block?

本秂侑毒 提交于 2019-11-27 18:19:48
I'm working on a project using the ANTLR parser library for C#. I've built a grammar to parse some text and it works well. However, when the parser comes across an illegal or unexpected token, it throws one of many exceptions. The problem is that in some cases (not all) that my try/catch block won't catch it and instead stops execution as an unhandled exception. The issue for me is that I can't replicate this issue anywhere else but in my full code. The call stack shows that the exception definitely occurs within my try/catch(Exception) block. The only thing I can think of is that there are a

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:

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

android ANTLR make not working properly

╄→гoц情女王★ 提交于 2019-11-27 11:22:41
问题 I am trying to use ANTLR on Android, and I found this: ANTLR and Android After downloading the AntlrJavaRuntime I am not sure of what to do, I am supposed to make this: 1. lunch the appropriate target 2. make AntlrJavaRuntime 3. verify that AntlrJavaRuntime.xml was placed in /system/etc/permissions and 4. AntlrJavaRuntime.jar was placed in /system/framework 5. after this, you can run a normal make First of all, what does step 1 even means? Secondly, when I try doing: make AntlrJavaRuntime I

Once grammar is complete, what's the best way to walk an ANTLR v4 tree?

喜你入骨 提交于 2019-11-27 09:21:50
问题 Goal I'm working on a project to create a Varscoper for Coldfusion CFscript. Basically, this means checking through source code files to ensure that developers have properly var 'd their variables. After a couple of days of working with ANTLR V4 I have a grammar which generates a very nice parse tree in the GUI view. Now, using that tree I need a way to crawl up and down the nodes programmatically looking for variable declarations and ensure that if they are inside functions they have the

Allow Whitespace sections ANTLR4

醉酒当歌 提交于 2019-11-27 09:10:48
I have an antlr4 grammar designed for an a domain specific language that is embedded into a text template. There are two modes: Text (whitespace should be preserved) Code (whitespace should be ignored) Sample grammar part: template : '{' templateBody '}' ; templateBody : templateChunk* ; templateChunk : code # codeChunk // dsl code, ignore whitespace | text # textChunk // any text, preserve whitespace ; The rule for code may contain a nested reference to the template rule. So the parser must support nesting whitespace/non-whitespace sections. Maybe lexer modes can help - with some drawbacks:

Negating inside lexer- and parser rules

て烟熏妆下的殇ゞ 提交于 2019-11-27 08:39:42
How can the negation meta-character, ~ , be used in ANTLR's lexer- and parser rules? Bart Kiers Negating can occur inside lexer and parser rules . Inside lexer rules you can negate characters, and inside parser rules you can negate tokens (lexer rules). But both lexer- and parser rules can only negate either single characters, or single tokens, respectively. A couple of examples: lexer rules To match one or more characters except lowercase ascii letters, you can do: NO_LOWERCASE : ~('a'..'z')+ ; (the negation-meta-char, ~ , has a higher precedence than the + , so the rule above equals (~('a'..