antlr4

Drawing parse tree in ANTLR4 using Java

谁说胖子不能爱 提交于 2019-12-11 10:12:26
问题 I am new to ANTLR4, when I was first trying it out in command line I was using the grun with gui parameter. Now I am developing a Java application and I want to display the same dialog while executing my Java program. I generated the ParseTree successfully, and I can navigate through it. But I want to display it as well. I think it has something to do with TreeViewer class but I couldn't figure out how to use it. Thanks 回答1: TreeViewer is a Swing Component so you should be able to add it to

Antlr 4 parsing large c file takes forever

a 夏天 提交于 2019-12-11 09:39:44
问题 I have a large c-code file (>9000 LoC) and attempt to parse it using this grammar: https://github.com/antlr/grammars-v4/blob/master/c/C.g4 I waited for over an hour before aborting. The machine is a Core 2 Duo L9400 with 4GB of ram. Maximum java vm-heap-size is set to 2GB. It does not produce any parse errors, but it simply doesn't finish. After doing some research, I set the prediction mode to SLL, which produces a "no viable alternative at input" within seconds. Next, I set the prediction

Antlr Parser operator priority

只愿长相守 提交于 2019-12-11 09:26:00
问题 Consider the following grammar. I have issues with the operator priority, for instance: res=2*a+b has a similar parse tree as res=2*(a+b). I know where the problem is, but no "beautiful" solution without mutual left recursion comes to my mind. Can you please help me out a little. The grammar is used with a custom visitor. grammar Math; expression: expression add=('+'|'-') expression # expressionAddExpression | expression mult='*' expression # expressionMultExpression |'(' expression ')' #

antlr 4: Should all of these tokens be showing up in the AST?

大城市里の小女人 提交于 2019-12-11 08:58:46
问题 My ultimate goal is to parse a structured file as a tree of in-memory objects that I can then manipulate. The file format that I'm using is fairly sophisticated with about 200 keywords/tags, and this seemed like a good reason to learn about parser/lexer frameworks. Unfortunately, there are so many concepts (and hundreds of tutorials and guides) that the learning process so far feels like trying to drink from a fire hose. So I'm taking some very meager baby steps, starting with this example. I

move to a new state after cosuming tokens in antlr4

自古美人都是妖i 提交于 2019-12-11 07:44:49
问题 i have following grammar root : sql_statements EOF | EOF ; sql_statements :( sql_statement )+ ; sql_statement : ddl_statement semicolon ; ddl_statement : alter_statement ; This is a initial snapshot of my grammar i have following test case aleter table user;alter table group_user; first statement has error hence i am getting error for both statements under sql_statements rule Failed to parse at line 1:1 due to mismatched input 'aleter' expecting ALTER Now i want to use Defaulterrorhandler

ANTLR Parsing - Matches Wrong Thing but Fix is EOF?

怎甘沉沦 提交于 2019-12-11 07:38:23
问题 I try to parse: return 3 + 4; My parse code (see below) parses it as the second rule ( statement statements program ) instead of the first rule. If I comment out the second rule, then the parser interprets it as the third rule ( functions statement program ). My "fix" that has worked so far is to make: master : program EOF ; Why does that fix my issue? Relevant Parse Code: program : statement { System.out.println("STATEMENT PROGRAM"); } | statement statements program { System.out.println(

write a grammar rule name in unicode [ANTLR 4]

穿精又带淫゛_ 提交于 2019-12-11 06:38:54
问题 I am still a beginner in ANTLR 4 and I was wondering if there is a way to write a grammar rule name in unicode. For example, the following rule is fine: atomExp returns [double value] : n=Number {$value = Double.parseDouble($n.text);} | '(' exp=additionExp ')' {$value = $exp.value;} ; However, let's say I want to write the same rule but instead of writing its name as "atomExp" , I want to write the name as an Arabic word "تعبير" تعبير returns [double value] : n=Number {$value = Double

ANTLR4 RegEx lexer modes

一曲冷凌霜 提交于 2019-12-11 06:21:23
问题 I am working on a Regx parser for RegEx inside XSD. My previous problem was descrived here: ANTLR4 parsing RegEx I have split the Lexer and Parser since than. Now I have a problem parsing parantheses inside brackets. They should be treated as characters inside the brackets and as grouping tokens outside. This is my lexer grammar: lexer grammar RegExLexer; Char : ALPHA ; Int : DIGIT ; LBrack : '[' ;//-> pushMode(modeRange) ; RBrack : ']' ;//-> popMode ; LBrace : '(' ; RBrace : ')' ; Semi : ';'

Antlr Skip text outside tag

我们两清 提交于 2019-12-11 06:05:42
问题 Im trying to skip/ignore the text outside a custom tag: This text is a unique token to skip < ?compo \5+5\ ?> also this < ?compo \1+1\ ?> I tried with the follow lexer: TAG_OPEN : '<?compo ' -> pushMode(COMPOSER); mode COMPOSER; TAG_CLOSE : ' ?>' -> popMode; NUMBER_DIGIT : '1'..'9'; ZERO : '0'; LOGICOP : OR | AND ; COMPAREOP : EQ | NE | GT | GE | LT | LE ; WS : ' '; NEWLINE : ('\r\n'|'\n'|'\r'); TAB : ('\t'); ... and parser: instructions : (TAG_OPEN statement TAG_CLOSE)+?; statement : if

How to debug ANTLR4 grammar extraneous / mismatched input error

依然范特西╮ 提交于 2019-12-11 05:49:36
问题 I want to parse the Rulebook "demo.rb" files like below: rulebook Titanic-Normalization { version 1 meta { description "Test" source "my-rules.xslx" user "joltie" } rule remove-first-line { description "Removes first line when offset is zero" when(present(offset) && offset == 0) then { filter-row-if-true true; } } } I wrote the ANTLR4 grammar file Rulebook.g4 like below. For now, it can parse the *.rb files generally well, but it throw unexpected error when encounter the "expression" /