antlr

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

烈酒焚心 提交于 2019-11-28 15:44:23
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 proper scoping. If possible I would rather NOT do this in the grammar file as that would require mixing

Ambiguous call expression in ANTLR4 grammar

被刻印的时光 ゝ 提交于 2019-11-28 14:32:00
I have a simple grammar (for demonstration) grammar Test; program : expression* EOF ; expression : Identifier | expression '(' expression? ')' | '(' expression ')' ; Identifier : [a-zA-Z_] [a-zA-Z_0-9?]* ; WS : [ \r\t\n]+ -> channel(HIDDEN) ; Obviously the second and third alternatives in the expression rule are ambiguous. I want to resolve this ambiguity by permitting the second alternative only if an expression is immediately followed by a '(' . So the following bar(foo) should match the second alternative while bar (foo) should match the 1st and 3rd alternatives (even if the token between

ANTLR 4 Parser Grammar

你说的曾经没有我的故事 提交于 2019-11-28 14:07:15
How can I improve my parser grammar so that instead of creating an AST that contains couple of decFunc rules for my testing code. It will create only one and sum becomes the second root. I tried to solve this problem using multiple different ways but I always get a left recursive error. This is my testing code : f :: [Int] -> [Int] -> [Int] f x y = zipWith (sum) x y sum :: [Int] -> [Int] sum a = foldr(+) a This is my grammar: This is the image that has two decFunc in this link http://postimg.org/image/w5goph9b7/ prog : stat+; stat : decFunc | impFunc ; decFunc : ID '::' formalType ( ARROW

How to collect errors during run time given by a parser in Antlr4

时间秒杀一切 提交于 2019-11-28 13:36:24
I have upgraded from Antlr 3 to Antlr 4. I was using this code to catch exceptions using this code. But this is not working for Antlr 4. partial class XParser { public override void ReportError(RecognitionException e) { base.ReportError(e); Console.WriteLine("Error in Parser at line " + ":" + e.OffendingToken.Column + e.OffendingToken.Line + e.Message); } } This is the error that appears 'Parser.ReportError(Antlr4.Runtime.RecognitionException)': no suitable method found to override In Antlr 4 what is the expected way of accumulating errors that occurs in the input stream. I was unable to find

ANTLR not throwing errors on invalid input

杀马特。学长 韩版系。学妹 提交于 2019-11-28 13:19:15
I'm using ANTLR to parse logical expressions in a Java tool I'm writing, and I'm having issues because passing invalid input strings to the generated ANTLR lexer and parser doesn't cause any exceptions. Instead of throwing a RecognitionException, like I would expect, the generated files just print the error message to the console and return as if no errors occurred, causing my program to crash when it runs into the empty data later. I used ANTLRWorks version 1.4.3 to generate the files, and it seems like there should be some sort of option to have it actually throw errors rather than print to

ANTLR4: Using non-ASCII characters in token rules

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 12:16:49
On page 74 of the ANTRL4 book it says that any Unicode character can be used in a grammar simply by specifying its codepoint in this manner: '\uxxxx' where xxxx is the hexadecimal value for the Unicode codepoint. So I used that technique in a token rule for an ID token: grammar ID; id : ID EOF ; ID : ('a' .. 'z' | 'A' .. 'Z' | '\u0100' .. '\u017E')+ ; WS : [ \t\r\n]+ -> skip ; When I tried to parse this input: Gŭnter ANTLR throws an error, saying that it does not recognize ŭ . (The ŭ character is hex 016D, so it is within the range specified) What am I doing wrong please? ANTLR is ready to

Catching (and keeping) all comments with ANTLR

蹲街弑〆低调 提交于 2019-11-28 11:42:35
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 either {skip();} or by sending the text to the hidden channel. With either of those options set, ANTLR

Why are antlr3 c# parser methods private?

有些话、适合烂在心里 提交于 2019-11-28 10:55:49
I'm building a parser in antlr which compiles to a working java target. When I retarget for c#2 it produces a parser in which all of the parse methods are private but marked with a [GrammarRule("rulename")] attribute. What is the approved means to actually invoke the parser? I am using ANTLR 3.3 Nov 30, 2010 12:45:30 Thanks, Andy Make at least one parser rule "public" like this: grammar T; options { language=CSharp2; } public parse : privateRule+ EOF ; privateRule : Token+ ; // ... You can then call parse() on the generated parser. protected and private (the default if nothing is specified)

Island grammar antlr3

孤者浪人 提交于 2019-11-28 10:04:28
问题 What are and how to use the "island grammar" in antlr3? 回答1: An island grammar is one that treats most of a language as a blob of text ("water") and picks out the part of the langauge of interest to parse using grammar rules ("island"). For instance, you might choose to build an island grammar to pick out all the expressions found in a C# program, and ignore the variable/method/class declarations and the statement syntax (if, while, ...). The real question is, "Should you use island grammars

Dynamically create lexer rule

♀尐吖头ヾ 提交于 2019-11-28 09:33:24
Here is a simple rule: NAME : 'name1' | 'name2' | 'name3'; Is it possible to provide alternatives for such rule dynamically using an array that contains strings? Yes, dynamic tokens match IDENTIFIER rule In that case, simply do a check after the Id has matched completely to see if the text the Id matched is in a predefined collection. If it is in the collection (a Set in my example) change the type of the token. A small demo: grammar T; @lexer::members { private java.util.Set<String> special; public TLexer(ANTLRStringStream input, java.util.Set<String> special) { super(input); this.special =