antlr4

Initialising my Lexer throws an error in Antlr4

醉酒当歌 提交于 2019-12-12 05:30:06
问题 Hi Team, I'm new to Antlr and I have spent 4 days trying to learn, install, run tutorials and integrate with my IDE. :( I can run this [tutorial][1] in the Terminal successfully. My goal now is to run the same tutorial in Netbeans with AntlrWorks2 I have cannibalised the Main from [Here][2]. The code compiles, but when I run I get an "java.lang.ExceptionInInitializerError" from init of the Lexer. 1: http://www.antlr.org/wiki/display/ANTLR4/Getting+Started+with+ANTLR+v4 2: http://www.certpal

ANTLR4.7 listener for a rule when sub rules are labeled

假如想象 提交于 2019-12-12 05:03:25
问题 I have an antlr4.7 grammar like this, where all sub rules are labeled. date_expr : attr op=( '+' | '-' ) dt_interval=ISO8601_INTERVAL #dateexpr_Op | DATETIME_NAME #dateexpr_Named | d=( DATETIME_LITERAL | DATE_LITERAL | TIME_LITERAL ) #dateexpr_Literal | attr #dateexpr_Attr | '(' date_expr ')' #dateexpr_Paren ; I would like to annotate the tree when a date_expr rule completes. However, looking at the generated listener class, I see no exitDate_expr . How can I add this? Or, do I have to use a

ANTLR recognize single character

筅森魡賤 提交于 2019-12-12 04:47:34
问题 I'm pretty sure this isn't possible, but I want to ask just in case. I have the common ID token definition: ID: LETTER (LETTER | DIG)*; The problem is that in the grammar I need to parse, there are some instructions in which you have a single character as operand, like: a + 4 but ab + 4 is not possible. So I can't write a rule like: sum: (INT | LETTER) ('+' (INT | LETTER))* Because the lexer will consider 'a' as an ID, due to the higher priority of ID. (And I can't change that priority

Understanding ANTLR4 Tokens

試著忘記壹切 提交于 2019-12-12 03:54:50
问题 I'm pretty new to ANTLR and I'm trying to understand what exactly Token is in ATNLR4. Consider the following pretty nonsensical grammar: grammar Tst; init: A token=('+'|'-') B; A: .+?; B: .+?; ADD: '+'; SUB: '-'; ANTLR4 generates the following TstParser.InitContext for it: public static class InitContext extends ParserRuleContext { public Token token; //<---------------------------- HERE public TerminalNode A() { return getToken(TstParser.A, 0); } public TerminalNode B() { return getToken

ANTLR4 AST Creation - How to create an AstVistor

杀马特。学长 韩版系。学妹 提交于 2019-12-12 03:52:44
问题 With the help of this SO question How to create AST with ANTLR4? I was able to create the AST Nodes, but I'm stuck at coding the BuildAstVisitor as depicted in the accepted answer's example. I have a grammar that starts like this: mini: (constDecl | varDef | funcDecl | funcDef)* ; And I can neither assign a label to the block (antlr4 says label X assigned to a block which is not a set ), and I have no idea how to visit the next node. public Expr visitMini(MiniCppParser.MiniContext ctx) {

antlr grammar avoiding angle brackets

北城以北 提交于 2019-12-12 03:34:09
问题 In this question I asked about extracting tags from arbitrary text. The solution provided worked well, but there's one edge case I'd like to handle. To recap, I'm parsing arbitrary user-entered text and would like to have any occurrence of < or > to conform to valid tag syntax. Where an angle bracket isn't part of a valid tag, it should be escaped as < or > . The syntax I'm looking for is <foo#123> where foo is text from a fixed list of entries and 123 is a number [0-9]+ . The parser: parser

Detect tokens triggering rules in ANTLR4

雨燕双飞 提交于 2019-12-12 03:13:45
问题 I have this very simple grammar: grammar DLR; dlr : c 'sub' c ; c : CN | 'not' c | c 'and' c | c 'or' c ; CN : [A-Z]+ ; WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines When I generate the java code with antlr4 command, I got the java interface DLRBaseListener: public class DLRBaseListener implements DLRListener { @Override public void enterDlr(DLRParser.DlrContext ctx) { } @Override public void exitDlr(DLRParser.DlrContext ctx) { } @Override public void enterC(DLRParser.CContext ctx)

Antlr Lexer exclude a certain pattern

孤者浪人 提交于 2019-12-12 02:50:15
问题 In Antlr Lexer, How can I achieve parsing a token like this: A word that contains any non-space letter but not '.{' inside it. Best I can come up with is using a semantics predicate. WORD: WL+ {!getText().contains(".{")}; WL: ~[ \n\r\t]; I'm a bit worried to use semantics predicate though cause WORD here will be lexed millions of times I would think to put a semantics predicate will hit the performance. This is coming from the requirement that I need to parse something like: TOKEN_ONE.{TOKEN

N-ary operator parsing

依然范特西╮ 提交于 2019-12-12 02:46:24
问题 I'm trying to match an operator of variable arity (e.g. "1 < 3 < x < 10" yields true, given that 3 < x < 10) within a mathematical expression. Note that this is unlike most languages would parse the expression) The (simplified) production rule is: expression: '(' expression ')' # parenthesisExpression | expression ('*' | '/' | '%') expression # multiplicationExpression | expression ('+' | '-') expression # additionExpression | expression (SMALLER_THAN expression)+ # smallerThanExpression |

ANTLR: parse NULL as a function name and a parameter

断了今生、忘了曾经 提交于 2019-12-12 02:14:08
问题 I would like to be able to use 'NULL' as both a parameter (the value null ) and a function name in my grammar. See this reduced example : grammar test; expr : value # valueExpr | FUNCTION_NAME '(' (expr (',' expr)* )* ')' # functionExpr ; value : INT | 'NULL' ; FUNCTION_NAME : [a-zA-Z] [a-zA-Z0-9]* ; INT: [0-9]+; Now, trying to parse: NULL( 1 ) Results in the parse tree failing because it parses NULL as a value, and not a function name. Ideally, I should even be able to parse NULL(NULL) ..