antlr4

how to use antlr4 visitor

不打扰是莪最后的温柔 提交于 2019-12-09 04:44:39
问题 I am a beginner of antlr. I was trying to use visitor in my code and following the instruction on the net. However, I found out that the visitor was not entering the method I create at all. May anyone tell me what I did wrong? This is my visitor: import java.util.LinkedList; import org.antlr.v4.runtime.misc.NotNull; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author Sherwood */ public class ExtractMicroBaseVisitor extends

Using ANTLR Parser and Lexer Separatly

若如初见. 提交于 2019-12-09 03:08:00
问题 I used ANTLR version 4 for creating compiler.First Phase was the Lexer part. I created "CompilerLexer.g4" file and putted lexer rules in it.It works fine. CompilerLexer.g4: lexer grammar CompilerLexer; INT : 'int' ; //1 FLOAT : 'float' ; //2 BEGIN : 'begin' ; //3 END : 'end' ; //4 To : 'to' ; //5 NEXT : 'next' ; //6 REAL : 'real' ; //7 BOOLEAN : 'bool' ; //8 . . . NOTEQUAL : '!=' ; //46 AND : '&&' ; //47 OR : '||' ; //48 POW : '^' ; //49 ID : [a-zA-Z]+ ; //50 WS : ' ' -> channel(HIDDEN) //50

ANTLR4- dynamically inject token

前提是你 提交于 2019-12-08 19:56:27
So I'm writing a python parser and I need to dynamically generate INDENT and DEDENT tokens (because python doesn't use explicit delimiters) according to the python grammar specification . Basically I have a stack of integers representing indentation levels. In an embedded Java action in the INDENT token, I check if the current level of indentation is higher than the level on top of the stack; if it is, I push it on; if not, I call skip() . The problem is, if the current indentation level matches a level multiple levels down in the stack, I have to generate multiple DEDENT tokens, and I can't

ANTLR4 Lexer error reporting (length of offending characters)

笑着哭i 提交于 2019-12-08 18:50:31
I'm developing a small IDE for some language using ANTLR4 and need to underline erroneous characters when the lexer fails to match them. The built in org.antlr.v4.runtime.ANTLRErrorListener implementation outputs a message to stderr in such cases, similar to this: line 35:25 token recognition error at: 'foo\n' I have no problem understanding how information about line and column of the error is obtained (passed as arguments to syntaxError callback), but how do I get the 'foo\n' string inside the callback? When a parser is the source of the error, it passes the offending token as the second

antlr4: ATN version 2 expected 3

主宰稳场 提交于 2019-12-08 17:31:28
问题 When trying to use a generated grammar and lexer I get: org.antlr.v4.runtime.atn.ATN; Could not deserialize ATN with version 2 (expected 3). What's wrong? 回答1: Your parser was generated with ANTLR 4.0, but you are trying to execute it with ANTLR 4.1. The most likely cause of this is using ANTLRWorks 2.0 to generate the parser, which internally uses ANTLR 4.0. I'm in the process of releasing ANTLRWorks 2.1 which will correct this mismatch. 来源: https://stackoverflow.com/questions/18180103

Is it possible to throw an exception if the input isn't valid?

◇◆丶佛笑我妖孽 提交于 2019-12-08 15:16:48
问题 I have a simple ANLTR grammar and accompanying Visitor. Everything works great, unless the input is invalid. If the input is invalid, the errors get swallowed and my calculator comes out with the wrong output. I've tried implementing an error listener, over riding the Recover method of the lexer, and.. well... half a dozen other things today. Can someone show me how to simply throw an error instead of swallowing bad "tokens"? (I use quotes because they're not tokens at all. The characters are

How to write visitor classes for collections?

僤鯓⒐⒋嵵緔 提交于 2019-12-08 10:05:06
问题 The example LabeledExpr.g4 in the book describes how to use the visitor classes for singletons. But if I want to visit a class which is a collection, how do I do it? e.g. for the grammar: prog: stat+ ; stat: expr NEWLINE # printExpr ; The visitor function for print is shown as: public Integer visitPrintExpr(LabeledExprParser.PrintExprContext ctx) { Integer value = visit(ctx.expr()); // evaluate the expr child System.out.println(value); // print the result return 0; // return dummy value }

Using ANTLR to generate lexer/parser as streams

坚强是说给别人听的谎言 提交于 2019-12-08 08:47:14
问题 Can I use the ANTLR Java API to generate the lexer/parser as streams and save them somewhere other than some files? Also, is there a simple example of using the API to generate the required files from a given grammar? thanks 回答1: I am not 100% sure if I understand your question correctly, but you might want to have a look at https://stackoverflow.com/a/38052798/5068458. This is an in-memory compiler for antlr grammars that generates lexer and parser for a given grammar in-memory. You do not

Bind ANTLR4 subrules of a rule

狂风中的少年 提交于 2019-12-08 08:01:26
I've a grammar like that: living : search EOF ; search : K_SEARCH entity ( K_QUERY expr )? ( K_FILTER expr )? ( K_SELECT1 expr ( COMMA expr )* )? ( K_SELECT2 expr ( COMMA expr )* )? ( K_SELECT3 expr ( COMMA expr )* )? ; As you can see I've two optional expr. I've created my Visitor, and I'm able to get access to entity, K_QUERY and K_FILTER. SearchContext provides a List in order to get a list of all expr. However, how can I bind which expression is for K_QUERY , K_FILTER ? What about the exprs of K_SELECT1 , K_SELECT2 , K_SELECT3 . public class LivingQueryVisitor extends LivingDSLBaseVisitor

ANTLR4- dynamically inject token

自作多情 提交于 2019-12-08 07:17:33
问题 So I'm writing a python parser and I need to dynamically generate INDENT and DEDENT tokens (because python doesn't use explicit delimiters) according to the python grammar specification. Basically I have a stack of integers representing indentation levels. In an embedded Java action in the INDENT token, I check if the current level of indentation is higher than the level on top of the stack; if it is, I push it on; if not, I call skip() . The problem is, if the current indentation level