antlr

How to Display ANTLR Tree GUI

余生长醉 提交于 2019-11-29 21:20:44
问题 How do you display the AST GUI if you have code like this, both of console or swing? My ANTLR is version 3. CharStream stream = new ANTLRStringStream("program XLSample1 =\n" + "constant one : Integer := 1;\n" + "constant two : Integer := 2;\n" + "var a, b,c : Integer := 42;\n" + "begin\n" + " x:= (12 + 6) - (7 * 41) - x mod y;\n" + " y := 21;\n" + "\n" + "if x < 10 then\n" + " y :=2;\n" + " elseif x < 20 then\n" + " y := 20;\n" + " else\n" + " y := 30;\n" + "end if; \n" + "end XLSample1.");

Is there a simple example of using antlr4 to create an AST from java source code and extract methods, variables and comments? [closed]

允我心安 提交于 2019-11-29 20:52:19
Can someone provide a detailed example as to how I can do this using antlr4? Instructions right from installing antlr4 and its dependencies would be highly appreciated. Here it is. First, you're gonna buy the ANTLR4 book ;-) Second, you'll download antlr4 jar and the java grammar ( http://pragprog.com/book/tpantlr2/the-definitive-antlr-4-reference ) Then, you can change the grammar a little bit, adding these to the header (...) grammar Java; options { language = Java; } // starting point for parsing a java file compilationUnit (...) I'll change a little thing in the grammar just to illustrate

In antlr4 lexer, How to have a rule that catches all remaining “words” as Unknown token?

十年热恋 提交于 2019-11-29 18:08:24
问题 I have an antlr4 lexer grammar. It has many rules for words, but I also want it to create an Unknown token for any word that it can not match by other rules. I have something like this: Whitespace : [ \t\n\r]+ -> skip; Punctuation : [.,:;?!]; // Other rules here Unknown : .+? ; Now generated matcher catches '~' as unknown but creates 3 '~' Unknown tokens for input '~~~' instead of a single '~~~' token. What should I do to tell lexer to generate word tokens for unknown consecutive characters.

ANTLR4: Unexpected behavior that I can't understand

限于喜欢 提交于 2019-11-29 18:04:36
I'm very new to ANTLR4 and am trying to build my own language. So my grammar starts at program: <EOF> | statement | functionDef | statement program | functionDef program; and my statement is statement: selectionStatement | compoundStatement | ...; and selectionStatement : If LeftParen expression RightParen compoundStatement (Else compoundStatement)? | Switch LeftParen expression RightParen compoundStatement ; compoundStatement : LeftBrace statement* RightBrace; Now the problem is, that when I test a piece of code against selectionStatement or statement it passes the test, but when I test it

ANTLR4 mutual left recursion grammar

房东的猫 提交于 2019-11-29 16:44:31
I have read many questions here on StackOverflow about mutual left-recursion issues in LL(k) parsers. I found the general algorithm for removing left-recursion: A : Aa | b ; becomes A : bR ; R : (aA)? ; However, I cannot figure out how to apply it to my situation. I have left_exp: IDENT | exp DOT IDENT ; exp : handful | of | other rules | left_exp ; The "handful of other rules" all contain regular recursion, such as exp : exp PLUS exp , etc. and have no issues. The issue is with left_exp and exp being mutually recursive. I thought about just adding IDENT and exp DOT IDENT to the exp rules, but

Is there a way to generate unit test to test my grammar

点点圈 提交于 2019-11-29 16:00:26
I created my grammar using antlr4 but I want to test robustess is there an automatic tool or a good way to do that fast Thanks :) The only way I found to create unit tests for a grammar is to create a number of examples from a written spec of the given language. This is neither fast, nor complete, but I see no other way. You could be tempted to create test cases directly from the grammar (writing a tool for that isn't that hard). But think a moment about this. What would you test then? Your unit tests would always succeed, unless you use generated test cases from an earlier version of the

Island grammar antlr3

大城市里の小女人 提交于 2019-11-29 15:44:06
What are and how to use the "island grammar" in antlr3? 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 at all?". The positive benefits: you don't have to write a full, complete grammar for the language you want

ANTLR Grammar for expressions

牧云@^-^@ 提交于 2019-11-29 15:10:17
I'm trying to implement a expression handling grammar (that deals with nested parenthesis and stuff). I have the following so far, but they can't deal with some cases (successful/failure cases appear after the following code block). Anyone know what's going on? Note: The varname += and varname = stuff are just some additional AST generation helper stuff in XText. Don't worry about them for now. ... NilExpression returns Expression: 'nil'; FalseExpression returns Expression: 'false'; TrueExpression returns Expression: 'true'; NumberExpression returns Expression: value=Number; StringExpression

Is it feasible to use Antlr for source code completion?

淺唱寂寞╮ 提交于 2019-11-29 14:59:58
问题 I don't know, if this question is valid since i'm not very familiar with source code parsing. My goal is to write a source code completion function for one existing programming language (Language "X") for learning purposes. Is Antlr(v4) suitable for such a task or should the necessary AST/Parse Tree creation and parsing be done by hand, assuming no existing solutions exists? I haven't found much information about that specific topic, except a list of compiler books, except a compiler is not

How to control error handling and synchronization in Antlr 4 / c#

点点圈 提交于 2019-11-29 12:05:08
I'm using Antlr 4 with c# target. Here is a subset of my grammar: /* * Parser Rules */ text : term+ EOF; term : a1 a2 a3; a1: .... ... ... I want to accept valid data blocks as (term)s, when error exists I want to search for the next valid term and print out the whole text which caused the error for user to analyze manually. How to synchronize input to the next valid term? and How to get the ignored text? You will need to create your own implementation of IAntlrErrorStrategy for this, and then set the Parser.ErrorHandler property to an instance of your error strategy. The documentation for the