antlr4

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:

Ambiguous call expression in ANTLR4 grammar

耗尽温柔 提交于 2019-11-27 08:25:52
问题 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

syntactic predicates - Upgrading from Antlr 3 to Antlr 4

℡╲_俬逩灬. 提交于 2019-11-27 08:09:21
问题 I have syntactic predicated that I have to convert into the Antlr 4. The grammar is not written my me so I have no idea how to convert them in a meaningful way. These are the main variations of the grammar that I have to convert. 1. simpleSelector : elementName ((esPred)=>elementSubsequent)* | ((esPred)=>elementSubsequent)+ ; esPred : HASH | DOT | LBRACKET | COLON ; elementSubsequent : HASH | cssClass | attrib | pseudo ; 2. fragment EMS :; // 'em' fragment EXS :; // 'ex' fragment LENGTH :; //

ANTLR 4 Parser Grammar

巧了我就是萌 提交于 2019-11-27 08:05:07
问题 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

Compiling sample ANTRL4 output

房东的猫 提交于 2019-11-27 08:03:23
问题 From the Definitive ANTLR4 reference I have run through the first example and it has generated the JAVA target. In the directory C:\JavaLib I have antlr-4.5-complete.jar When I attempt to compile it with; javac -classpath C:\JavaLib *.java It creates the following error messages; helloBaseListener.java:13: error: class HelloBaseListener is public, should be declared in a file named HelloBaseListener.java public class HelloBaseListener implements HelloListener { ^ helloListener.java:9: error:

ANTLR4: Using non-ASCII characters in token rules

放肆的年华 提交于 2019-11-27 06:53:12
问题 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 ŭ

Handling errors in ANTLR4

被刻印的时光 ゝ 提交于 2019-11-27 06:31:55
The default behavior when the parser doesn't know what to do is to print messages to the terminal like: line 1:23 missing DECIMAL at '}' This is a good message, but in the wrong place. I'd rather receive this as an exception. I've tried using the BailErrorStrategy , but this throws a ParseCancellationException without a message (caused by a InputMismatchException , also without a message). Is there a way I can get it to report errors via exceptions while retaining the useful info in the message? Here's what I'm really after--I typically use actions in rules to build up an object: dataspec

ANTLR 4 tree inject/rewrite operator

空扰寡人 提交于 2019-11-27 04:26:17
问题 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? 回答1: 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

Antlr 4.5 parser error during runtime

别说谁变了你拦得住时间么 提交于 2019-11-27 02:13:01
I'm building simple grammar for programming laguange for learning purposes. I run into strange error that make no sense to me. line 1:0 missing {'void', 'int', 'bool', 'string', 'union'} at 'void' I'm using prebuild lexer and parser from this grammar: grammar ProgrammingLanguage; function_definition : type_specifier IDENTIFIER '(' parameter_list_opt ')' compound_statement ; type_specifier : VOID | INT | BOOL | STRING | UNION ; compound_statement : '{' declaration_list statement_list '}' ; statement_list : statement | statement statement_list | ; statement : compound_statement | selection