antlr

ANTLR Parse tree modification

三世轮回 提交于 2019-12-18 13:16:13
问题 I'm using ANTLR4 to create a parse tree for my grammar, what I want to do is modify certain nodes in the tree. This will include removing certain nodes and inserting new ones. The purpose behind this is optimization for the language I am writing. I have yet to find a solution to this problem. What would be the best way to go about this? 回答1: While there is currently no real support or tools for tree rewriting, it is very possible to do. It's not even that painful. The ParseTreeListener or

How to match a string, but case-insensitively?

独自空忆成欢 提交于 2019-12-18 12:13:32
问题 Let's say that I want to match "beer", but don't care about case sensitivity. Currently I am defining a token to be ('b'|'B' 'e'|'E' 'e'|'E' 'r'|'R') but I have a lot of such and don't really want to handle 'verilythisisaverylongtokenindeedomyyesitis'. The antlr wiki seems to suggest that it can't be done (in antlr) ... but I just wondered if anyone had some clever tricks ... 回答1: How about define a lexer token for each permissible identifier character, then construct the parser token as a

ANTLR4: Unexpected behavior that I can't understand

我只是一个虾纸丫 提交于 2019-12-18 09:45:30
问题 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

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

岁酱吖の 提交于 2019-12-18 07:14:31
问题 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? 回答1: You will need to create your own implementation of IAntlrErrorStrategy for this,

ANTLR resolving non-LL(*) problems and syntactic predicates

99封情书 提交于 2019-12-18 05:27:04
问题 consider following rules in the parser: expression : IDENTIFIER | (...) | procedure_call // e.g. (foo 1 2 3) | macro_use // e.g. (xyz (some datum)) ; procedure_call : '(' expression expression* ')' ; macro_use : '(' IDENTIFIER datum* ')' ; and // Note that any string that parses as an <expression> will also parse as a <datum>. datum : simple_datum | compound_datum ; simple_datum : BOOLEAN | NUMBER | CHARACTER | STRING | IDENTIFIER ; compound_datum : list | vector ; list : '(' (datum+ ( '.'

ANTLR duplicate a tree

给你一囗甜甜゛ 提交于 2019-12-18 05:21:38
问题 I use ANTLR to build a tree (CommonTree) like follwing (language: JAVA): Parser.prog_return r = parser.prog(); CommonTree t = (CommonTree) r.getTree(); Now, I need to pass "t" as a parameter, and make some changes without affecting the original tree. However, with Java's pointer, this could not been done, so I need to duplicate the tree. I have been search on the internet, the cloested thing I could find is the dupTree() method of ASTFactory class. Any suggestion or advises on how to achive

ANTLR duplicate a tree

回眸只為那壹抹淺笑 提交于 2019-12-18 05:21:04
问题 I use ANTLR to build a tree (CommonTree) like follwing (language: JAVA): Parser.prog_return r = parser.prog(); CommonTree t = (CommonTree) r.getTree(); Now, I need to pass "t" as a parameter, and make some changes without affecting the original tree. However, with Java's pointer, this could not been done, so I need to duplicate the tree. I have been search on the internet, the cloested thing I could find is the dupTree() method of ASTFactory class. Any suggestion or advises on how to achive

Good parser generator (think lex/yacc or antlr) for .NET? Build time only? [closed]

六月ゝ 毕业季﹏ 提交于 2019-12-17 23:12:39
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . Is there a good parser generator (think lex/yacc or antlr) for .NET? Any that have a license that would not scare lawyers? Lot’s of LGPL but I am working on embedded components and some organizations are not comfortable with me taking an LGPL dependency. I've heard that Oslo may provide this functionality but I

How to collect errors during run time given by a parser in Antlr4

淺唱寂寞╮ 提交于 2019-12-17 20:47:38
问题 I have upgraded from Antlr 3 to Antlr 4. I was using this code to catch exceptions using this code. But this is not working for Antlr 4. partial class XParser { public override void ReportError(RecognitionException e) { base.ReportError(e); Console.WriteLine("Error in Parser at line " + ":" + e.OffendingToken.Column + e.OffendingToken.Line + e.Message); } } This is the error that appears 'Parser.ReportError(Antlr4.Runtime.RecognitionException)': no suitable method found to override In Antlr 4

Dynamically create lexer rule

偶尔善良 提交于 2019-12-17 19:48:09
问题 Here is a simple rule: NAME : 'name1' | 'name2' | 'name3'; Is it possible to provide alternatives for such rule dynamically using an array that contains strings? 回答1: Yes, dynamic tokens match IDENTIFIER rule In that case, simply do a check after the Id has matched completely to see if the text the Id matched is in a predefined collection. If it is in the collection (a Set in my example) change the type of the token. A small demo: grammar T; @lexer::members { private java.util.Set<String>