antlr4

ANTLR4: Unrecognized constant value in a lexer command

断了今生、忘了曾经 提交于 2019-12-11 02:17:02
问题 I am learning how to use the "more" lexer command. I typed in the lexer grammar shown in the ANTLR book, page 281: lexer grammar Lexer_To_Test_More_Command ; LQUOTE : '"' -> more, mode(STR) ; WS : [ \t\r\n]+ -> skip ; mode STR ; STRING : '"' -> mode(DEFAULT_MODE) ; TEXT : . -> more ; Then I created this simple parser to use the lexer: grammar Parser_To_Test_More_Command ; import Lexer_To_Test_More_Command ; test: STRING EOF ; Then I opened a DOS window and entered this command: antlr4 Parser

Ambiguous ANTLR parser rule

爷,独闯天下 提交于 2019-12-11 02:13:12
问题 I have a very simple example text which I want to parse with ANTLR, and yet I'm getting wrong results due to ambiguous definition of the rule. Here is the grammar: grammar SimpleExampleGrammar; prog : event EOF; event : DEFINE EVT_HEADER eventName=eventNameRule; eventNameRule : DIGIT+; DEFINE : '#define'; EVT_HEADER : 'EVT_'; DIGIT : [0-9a-zA-Z_]; WS : ('' | ' ' | '\r' | '\n' | '\t') -> channel(HIDDEN); First text example: #define EVT_EX1 Second text example: #define EVT_EX1 #define EVT_EX2

Antlr4 Javascript target - issue with Visitor and labeled alternative

馋奶兔 提交于 2019-12-11 01:51:08
问题 I'm using antlr4 (4.5.3) with Javascript target, and trying to implement a visitor. Following the antlr4 book's calculator example (great book BTW) I'm trying to create a similar grammar: ... expr: expr op=(​'*'​|​'/'​) expr # MulDiv | expr op=(​'+'​|​'-'​) expr # AddSub | INT # int | ​'('​ expr ​')'​ # parens ; ... The issue: visitor methods are created for the labeled alternatives (for example visitMulDiv) however 2 thing are missing: Implementation for visitExpr in the base visitor

Antlr4 Visitor several rule contexts

故事扮演 提交于 2019-12-11 01:37:14
问题 I've a grammar like that: search : K_SEARCH entity ( K_QUERY expr )? ( K_FILTER 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<ExprContext> in order to get a list of all expr . However, how can I know with expression is a K_QUERY expr or a K_FILTER expr ? public class LivingQueryVisitor extends LivingDSLBaseVisitor<Void> { @Override public Void visitSearch(SearchContext ctx)

Getting “An expression is too long or complex to compile” when using c# target

荒凉一梦 提交于 2019-12-11 01:34:37
问题 I've finally got my full grammar up earlier today and ran into the "String is too long" Java issue. I understand that this problem has been resolved but I was afraid of running into other limitations so I decided to switch to using the C# target. I'm an experienced C++ programmer and learned Java to be able to use Antlr4, switching to C# is no big deal, just a new syntax to learn. I am now getting the message: 1>CSC : fatal error CS1647: An expression is too long or complex to compile There's

ANTLR4 Mutual left recursion

痞子三分冷 提交于 2019-12-11 01:08:49
问题 I just ran into a strange problem with ANTLR 4.2.2: Consider a (simplified) java grammar. This does not compile: classOrInterfaceType : (classOrInterfaceType) '.' Identifier | Identifier ; ANTLR outputs the following error: error(119): Java.g4::: The following sets of rules are mutually left-recursive [classOrInterfaceType] Yes, I also see a left recursion. But I do not see a mutual left recursion, only a usual one. When I remove the parenthesis around (classOrInterfaceType) , then it

Parsing single line comments

泪湿孤枕 提交于 2019-12-11 00:54:30
问题 I am trying to write a grammar for parsing single line comments. Comments starts with '--' can appear anywhere in the file. My basic grammar looks like below. Grammar (aa.g4): grammar aa; statement : commentStatement* ifStatement | commentStatement* returnStatement ; ifStatement : 'if' '(' expression ')' returnStatement+ ; returnStatement : 'return' expression ';' ; commentStatement : '--' (.+?) '\\n'? ; expression : IDENTIFIER ; IDENTIFIER : [a-z]([A-Za-z0-9\-\_])* ; NEWLINE : '\r'? '\n' ->

ANTLR behaviour with conflicting tokens

独自空忆成欢 提交于 2019-12-11 00:48:28
问题 How is ANTLR lexer behavior defined in the case of conflicting tokens? Let me explain what I mean by "conflicting" tokens. For example, assume that the following is defined: INT_STAGE : '1'..'6'; INT : '0'..'9'+; There is a conflict here, because after reading a sequence of digits, the lexer would not know whether there is one INT or many INT_STAGE tokens (or different combinations of both). After a test, it looks like that if INT is defined after INT_STAGE, the lexer would prefer to find INT

Mutual Left Recursion ANTLR 4

荒凉一梦 提交于 2019-12-11 00:38:30
问题 I'm sorry to ask yet another question on mutual left recursion, I feel like mine is unique to my situation, or at least I can't figure out enough to relate it to everyone else's grammars. I'm a bit new to the comp sci world (I'm self taught in java, which is my target language, and now ANTLR4) so if possible please describe things in layperson terms, not CS major terms. I'm writing a program that requires algebra and symbolic derivatives, and of course that requires that things be parsed, and

Parse string antlr

百般思念 提交于 2019-12-10 23:44:56
问题 I have strings as a parser rule rather than lexer because strings may contain escapes with expressions in them, such as "The variable is \(variable)" . string : '"' character* '"' ; character : escapeSequence | . ; escapeSequence : '\(' expression ')' ; IDENTIFIER : [a-zA-Z][a-zA-Z0-9]* ; WHITESPACE : [ \r\t,] -> skip ; This doesn't work because . matches any token rather than any character, so many identifiers will be matched and whitespace will be completely ignored. How can I parse strings