antlr4

Antlr - Parsing Multiline #define for C.g4

我是研究僧i 提交于 2019-12-24 07:37:40
问题 I am using Antlr4 to parse C code. I want to parse multiline #defines alongwith C.g4 provided in C.g4 But the grammar mentioned in the link above does not support preprocessor directives, so I have added the following new rules to support preprocessing. Link to my previous question Whitespace : [ \t]+ -> channel(HIDDEN) ; Newline : ( '\r' '\n'? | '\n' ) -> channel(HIDDEN) ; BlockComment : '/*' .*? '*/' ; LineComment : '//' ~[\r\n]* ; IncludeBlock : '#' Whitespace? 'include' ~[\r\n]* ;

ANTLR 4 How to handle unary/negative numbers

痴心易碎 提交于 2019-12-24 07:04:22
问题 I'm trying to setup a simple calculator with Antlr 4. Grammar: grammar calcGrammar; input : expression EOF; expression : MINUS expression #unaryMinusExpr | expression op=(MULTIPLY | DIVIDE) expression #multiplicationExpr | expression op=(MINUS | ADD) expression #additiveExpr | NUMBER #num ; NUMBER : [0-9]+ ; DOUBLE : NUMBER '.' NUMBER; LPAR : '('; RPAR : ')'; ADD : ('+'); MINUS : ('-'); DIVIDE : ('/'); MULTIPLY : ('*'); Java Code: public class Listener extends ListenerBaseVisitor { @Override

How To Terminate a Lexer in ANTLR4

帅比萌擦擦* 提交于 2019-12-24 06:34:18
问题 Is there a simple way to terminate a lexer? There are some tokens that I do not want to handle yet. But I also want the Lexer to sound an alarm if the input does contain those tokens. My simple strategy is throwing a RuntimeException in an action: CHARIZING: '#@' {throw new RuntimeException("charizing op not supported yet");}; But the action produces compilation error since the generated Lexer has a break command after the action and the Java compiler complains the break is an unreachable

my lexer token action is not invoked

给你一囗甜甜゛ 提交于 2019-12-24 05:36:10
问题 I use antlr4 with javascript target. Here is a sample grammar: P : T ; T : [a-z]+ {console.log(this.text);} ; start: P ; When I run the generated parser, nothing is printed, although the input is matched. If I move the action to the token P , then it gets invoked. Why is that? 回答1: Actions are ignored in referenced rules. This was the original behavior of ANTLR 4, back when the lexer only supported a single action per token (and that action must appear at the end of the token). Several

Migration tool for ANTLR grammar

蹲街弑〆低调 提交于 2019-12-24 01:54:44
问题 Suppose I have a following simple grammar (query DSL): grammar TestGrammar; term : textTerm ; textTerm : 'Text' '(' T_VALUE '=' STRING+ ')' ; T_VALUE : 'value' ; STRING : '"' .+? '"' ; WS : [ \t\r\n]+ -> skip ; Then at some point I decide that text term format needs to be changed, for example: Text(value = "123") -> MyText(val = "123") How should I approach migrating existing data that users have generated with previous version of grammar? 回答1: Assumption Let's make one simplification of your

ANTLR4 performance issue

不羁的心 提交于 2019-12-24 01:04:21
问题 There has been some discussion on the performance of ANTL4 parsing e.g: Antlr 4 parsing large c file takes forever Is there any good ways to improve the parser's performance generated using antlr4? and there is an open bug report: https://github.com/antlr/antlr4/issues/994 that calls for using two phase strategy to improve the performance of ANTLR4. To do so you first call the parser in SLL mode and only on failure the slower LL mode is used. This works nicely for small grammars like the one

Umlauts in JSON files lead to errors in Python code created by ANTLR4

点点圈 提交于 2019-12-23 17:51:52
问题 I've created python modules from the JSON grammar on github / antlr4 with antlr4 -Dlanguage=Python3 JSON.g4 I've written a main program "JSON2.py" following this guide: https://github.com/antlr/antlr4/blob/master/doc/python-target.md and downloaded the example1.json also from github. python3 ./JSON2.py example1.json # works perfectly, but python3 ./JSON2.py bookmarks-2017-05-24.json # the bookmarks contain German Umlauts like "ü" ... File "/home/xyz/lib/python3.5/site-packages/antlr4

How to recognise start-of-line in an Antlr grammar?

三世轮回 提交于 2019-12-23 17:46:05
问题 In the language I work with, some keywords must be at the start of the line. This is mainly because string values within the language can go over multiple lines, and strings could easily contain these keywords. The old yacc/lex grammar implementation I have easily deals with this because the lexer uses normal regexes to match text patterns, e.g. ^description { actions } matches ' description ' at the start of a line and then does actions. How can I do this in Antlr4? It does not appear to

The selected wizard could not be started in eclipse for Anltr 4 .How to solve?

人盡茶涼 提交于 2019-12-23 10:49:13
问题 The selected wizard could not be started. Failed to create injector for com.github.jknack.antlr4ide.Antlr4 ExtensionFactory: com.github.jknack.antlr4ide.ui.Antlr4ExecutableExtensionFactory Failed to create injector for com.github.jknack.antlr4ide.Antlr4 How do I solve this problem? 回答1: I have solved the problem by installing xtext 2.7.3: Download In Eclipse > Help > Install New Software... Add... > Archive > Select downloaded .zip file Check Xtext from the list > Continue installing Restart

antlr 4.2.2 output to console warning (157)

坚强是说给别人听的谎言 提交于 2019-12-23 09:58:19
问题 I downloaded latest release of ANTLR - 4.2.2 (antlr-4.2.2-complete.jar) When I use it to generate parsers for grammar file Java.g4 it prints me some warnings like: "Java.g4:525:16: rule 'expression' contains an 'assoc' terminal option in an unrecognized location" Files was generated but didn't compile Previous version works fine. Whats wrong? 回答1: The <assoc> should now be moved left of the "expression". It must be placed always right to the surrounding | : Look here: https://theantlrguy