antlr4

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>

Semantic predicates in ANTLR4?

倖福魔咒の 提交于 2019-12-17 11:02:14
问题 How would you translate this portion of code written in ANTLR 3 into ANTLR 4? expr: (Identifier '.')=> (refIdentifier) | (Identifier '!')=> (refIdentifier) | (Identifier '=>')=> (lambdaExpression); I mean this kind of semantic predicate does not seem to exist now. What could I use Instead? 回答1: In ANTLR v4, there are no longer gated semantic predicates , { ... }?=> , and there are also no longer syntactic predicates , ( ... )=> , because the parsing algorithm used in v4 can resolve the

Antlr4 grammar ambiguity

霸气de小男生 提交于 2019-12-14 04:08:21
问题 I have the following grammar ( minimized for SO) grammar Hello; odataIdentifier : identifierLeadingCharacter identifierCharacter*; identifierLeadingCharacter : Alpha| UNDERSCORE; identifierCharacter : identifierLeadingCharacter | Digit; identifierUnreserved : identifierCharacter | (MINUS | DOT | TILDE); Digit : ZERO_TO_FIVE |[6-9]; ONEHUNDRED_TO_ONEHUNDREDNINETYNINE : '1' Digit Digit; // 100-199 TWOHUNDRED_TO_TWOHUNDREDFOURTYNINE : '2' ZERO_TO_FOUR Digit; // 200-249 TWOHUNDREDFIFTY_TO

Traversal of tokens using ParserRuleContext in listener - ANTLR4

独自空忆成欢 提交于 2019-12-14 03:48:57
问题 While iterating over the tokens using a Listener, I would like to know how to use the ParserRuleContext to peek at the next token or the next few tokens in the token stream? In the code below I am trying to peek at all the tokens after the current token till the EOF: @Override public void enterSemicolon(JavaParser.SemicolonContext ctx) { Token tok, semiColon = ctx.getStart(); int currentIndex = semiColon.getStartIndex(); int reqInd = currentIndex+1; TokenSource tokSrc= semiColon

antlr4/java: pretty print parse tree to stdout

喜夏-厌秋 提交于 2019-12-14 00:17:04
问题 Beginners question: how do I print a readable version of the parse tree to stdout? CharStream input = CharStreams.fromFileName("testdata/test.txt"); MyLexer lexer = new MyLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); MyParser parser = new MyParser(tokens); parser.setBuildParseTree(true); RuleContext tree = parser.record(); System.out.println(tree.toStringTree(parser)); this prints the whole tree on a single line delimited by brackets '()'. (record (husband <4601>

antlr4 import maven eclipse combination does not work can't find or load grammar

六月ゝ 毕业季﹏ 提交于 2019-12-13 19:19:53
问题 With ANTLR 4.7 ANTLR 4.7 maven plugin Eclipse Neon 4.6.2 i am trying to use the grammar import feature of ANTLR4. see https://github.com/BITPlan/com.bitplan.antlr with the example iri grammar https://github.com/BITPlan/com.bitplan.antlr/tree/master/src/main/antlr4/com/bitplan/iri That grammar is basically the attempt to split the https://github.com/antlr/grammars-v4/blob/master/iri/IRI.g4 into three parts: parser lexer imported basic lexer parts At this point I am getting the error message:

ANTLRv4: How can I check efficiently whether a sentence satisfies a given grammar?

时间秒杀一切 提交于 2019-12-13 18:40:22
问题 This question is basic enough. If I have a sentence and a grammar how can I check efficiently whether this sentence is part of the grammar. By efficient way I mean a way which uses minimum amount of computations/resources. Here's what I did. I implemented a class ErrorListener which extends the already defined BaseErrorListener . This class ErrorListener has a single field boolean error which tells us whether a parse error was encountered during the parsing phase. At the start error is set to

ANTLR4 extraneous input

守給你的承諾、 提交于 2019-12-13 12:37:11
问题 I have a problem with my ANTLR4. I'm trying to print AST from python 3 code but there are some errors and I don't know how to fix them. I wrote simple code for test: a=(1,2,3) print(a) I ran the program but this errors appeared: line 1:1 extraneous input '=' expecting {<EOF>, '.', '*', '(', '**', '[', '|', '^', '&', '<<', '>>', '+', '-', '/', '%', '//', '@'} line 2:0 extraneous input '\n' expecting {<EOF>, '.', '*', '(', '**', '[', '|', '^', '&', '<<', '>>', '+', '-', '/', '%', '//', '@'}

Python+ANTLR4: No module named antlr4

两盒软妹~` 提交于 2019-12-13 12:34:55
问题 I would like to use ANTLR4 with Python 2.7 and for this I did the following: I installed the package antlr4-4.6-1 on Arch Linux with sudo pacman -S antlr4 . I wrote a MyGrammar.g4 file and successfully generated Lexer and Parser Code with antlr4 -Dlanguage=Python2 MyGrammar.g4 Now executing for example the generated Lexer code with python2 MyGrammarLexer.py results in the error ImportError: No module named antlr4 . What could to be the problem? FYI: I have both Python2 and Python3 installed -

Using ANTLR4 to generate modified code

半城伤御伤魂 提交于 2019-12-13 08:47:57
问题 I'm trying to use antlr4 to parse C files using the C.g4 grammar. I was wondering if there's a way to change the text inside a parse tree node and then generate a new .c file starting from the edited tree. My goal is basically to replace all the function names with <FUNC> 回答1: A function name -- looks to be an 'Identifier' -- will be represented as a TerminalNode (extends ParseTree, which is the basic parse-tree node). So, in the proper context, TerminalNode id = (TerminalNode) node;