antlr

String Template: make all variable declaration global

元气小坏坏 提交于 2019-12-07 13:53:12
问题 I am trying to implement a translator using ANTLR+StringTemplate. I have a starting language that is java like and multiple destination language. I used the example: http://www.antlr.org/wiki/display/ST/Language+Translation+Using+ANTLR+and+StringTemplate One of my destination language needs all variables to be declared globally. I wrote a grammar that recognizes variables, but i cannot find e way in my template for making a local variable to be declared globally. Of course if I would have

ANTLR: how to parse a region within matching brackets with a lexer

别来无恙 提交于 2019-12-07 09:00:42
问题 i want to parse something like this in my lexer: ( begin expression ) where expressions are also surrounded by brackets. it isn't important what is in the expression, i just want to have all what's between the (begin and the matching ) as a token. an example would be: (begin (define x (+ 1 2))) so the text of the token should be (define x (+ 1 2))) something like PROGRAM : LPAREN BEGIN .* RPAREN; does (obviously) not work because as soon as he sees a ")", he thinks the rule is over, but i

ANTLR Implicit Multiplication

非 Y 不嫁゛ 提交于 2019-12-07 07:07:06
问题 I'm new to ANTLR, and I'm trying to expand upon the example of a simple calculator presented here. Specifically, I've tried adding some simple functions, negative numbers and so on, to familiarize myself with ANTLR. However, I've run into a bit of a problem trying to implement "implicit" multiplication (for example, 3cos(2)sin(2) would be interpreted as 3*cos(2)*sin(2)). I've found a question on Stack Overflow with the same kind of problem (here). The general form of the solution to that

Antlr Error Strategy to skip tokens until rule matches again

大憨熊 提交于 2019-12-07 07:01:23
问题 I tried this solution but it didn't seem to work for me Here's an excerpt from my grammer: module : BEGIN MODULE IDENT STRING module_element* END MODULE ; module_element : element_1 | element_2 | element_3 | ... ; There is a bigger tree below each element. Now when a RecognitionException occurs I want to consume tokens until either the next module_element matches or the parent END MODULE matches. Any hints on how to do this inside a class inheriting from DefaultErrorStrategy? edit: Here is a

Error handeling in antlr 3.0

左心房为你撑大大i 提交于 2019-12-07 05:45:30
I need to Report customized error when ever user input does not match our defined rules. Here is my code: grammar second1; @lexer::members { @Override public void reportError(RecognitionException e) { System.out.println("Throwing Exception: "+ e.getMessage()); throw new IllegalArgumentException(e); } } @parser::members { private boolean inbounds(Token t, int min, int max, String methodName) { int n = Integer.parseInt(t.getText()); if(n >= min && n <= max) { return true; } else { System.out.println("The range for value accepted by " + methodName+" is "+min +"-" + max ); return false; } } } expr

Antlr lexer tokens that match similar strings, what if the greedy lexer makes a mistake?

空扰寡人 提交于 2019-12-07 05:37:06
问题 It seems that sometimes the Antlr lexer makes a bad choice on which rule to use when tokenizing a stream of characters... I'm trying to figure out how to help Antlr make the obvious-to-a-human right choice. I want to parse text like this: d/dt(x)=a a=d/dt d=3 dt=4 This is an unfortunate syntax that an existing language uses and I'm trying to write a parser for. The "d/dt(x)" is representing the left hand side of a differential equation. Ignore the lingo if you must, just know that it is not

What does ^ and ! stand for in ANTLR grammar

时光怂恿深爱的人放手 提交于 2019-12-07 05:04:31
问题 I was having difficulty figuring out what does ^ and ! stand for in ANTLR grammar terminology. 回答1: Have a look at the ANTLR Cheat Sheet: ! don't include in AST ^ make AST root node And ^ can also be used in rewrite rules: ... -> ^( ... ) . For example, the following two parser rules are equivalent: expression : A '+'^ A ';'! ; and: expression : A '+' A ';' -> ^('+' A A) ; Both create the following AST: + / \ A A In other words: the + is made as root, the two A 's its children, and the ; is

ANTLR - NoViableAltException

流过昼夜 提交于 2019-12-07 04:39:25
问题 I'm trying to learn ANTLR by writing a grammer (I'm using eclipse with the plugins for ANTLR), and it was going alright until I ran into the error: NoViableAltException: line 0:-1 no viable alternative at input '<EOF>' When I try to test my args parser rule; typedident : (INT|CHAR) IDENT; args : (typedident ( COMMA typedident)*)?; An ident is a letter followed by any character, this works, I've tested it. typedident also works for the test. I'm using the input of int a12q2efwe, char a12eqdsf

“FOLLOW_set_in_”… is undefined in generated parser

北慕城南 提交于 2019-12-07 01:40:22
问题 I have written a grammar for vaguely Java-like DSL. While there are still some issues with it (it doesn't recognize all the inputs as I would want it to), what concerns me most is that the generated C code is not compilable. I use AntlrWorks 1.5 with Antlr 3.5 (Antlr 4 apparently does not support C target). The problem is with expression rules. I have rules prio14Expression to prio0Expression which handle operator precedence. To problem is at priority 2, which evaluates prefix and postfix

How can i see the live parse tree using Antlr4 Ide in Eclipse?

。_饼干妹妹 提交于 2019-12-06 19:31:28
问题 I'm new using Antlr4 but I know that exist a plugin for Eclipse. I have a simple question...After I created the g4 file how can I visualize the live parse tree in order to see the tree of an input expression? Thanks 回答1: Currently there is no provision for viewing live parse tree in ANTLR 4 IDE for Eclipse. Meanwhile, you can see the parse tree using -gui switch in the command line. It also provides a feature of saving the parse tree as PNG. 回答2: After installing Antlr4Ide plugin in Eclipse: