antlr4

Skipping unmatched input in Antlr

[亡魂溺海] 提交于 2019-12-13 02:11:20
问题 Is there a way to specify in a grammar that I want to skip all input that doesnt match any of the rules (that would otherwise throw a recognition error)? 回答1: Yes. Implementation depends on where you need/want to do the skipping. In the lexer, a last rule like: Unknown : . -> skip ; // or -> channel(HIDDEN) ; will consume any otherwise unmatched input characters yet keep them from being tokenized and considered by the parser. You do want to match a single character at a time so that at every

ANTLRWorks 2.1: generated Lexer+Parser in Eclipse causing UUID exception

我只是一个虾纸丫 提交于 2019-12-13 01:15:18
问题 I'm new to ANTLR and ANTLRWorks, so I'm quite puzzled by this: I'm using ANTLRWorks 2.1 for grammar creation and subsequent creation of the lexer and parser (Java target). I have then created a small Eclipse project and imported the ANTLR4 jars from ANTLRWorks to manually call the lexer and parser as described in the ANTLR book examples. When running the application I get the following exception: Caused by: java.lang.UnsupportedOperationException: java.io.InvalidClassException: org.antlr.v4

Extraneous input '-' expecting in ANTLR4

烂漫一生 提交于 2019-12-12 23:18:34
问题 Could you please tell me what could be the problem? It's expect {<EOF>, '(', NUMBER, VARIABLE} , but if i insert this rules after - , i got same error. grammar Grammar; @header { package parser; } program: line* EOF ; line: (expression | assignment) ('\n' | EOF); assignment : VARIABLE '=' expression ; expression : '(' expression ')' #parenthesisExpression | left=expression OP1 right=expression #firstPriorityExpression | left=expression OP2 right=expression #secondPriorityExpression | number

ParseTree in AnTLR4 C#

余生颓废 提交于 2019-12-12 17:02:22
问题 I am currently creating a grammar using AnTLR4 targeting C# but I am facing a problem while developing a visitor. I can't find the class ParseTree referred in the book. In the book we have: LabeledExprLexer lexer = new LabeledExprLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); LabeledExprParser parser = new LabeledExprParser(tokens); ParseTree tree = parser.prog(); // parse but I can't see the equivalent C# code. Can you please help? 回答1: Interfaces in C# have the

ANTLR4 lexer rule with @init block

给你一囗甜甜゛ 提交于 2019-12-12 12:11:33
问题 I have this lexer rule defined in my ANTLR v3 grammar file - it maths text in double quotes. I need to convert it to ANTLR v4. ANTLR compiler throws an error 'syntax error: mismatched input '@' expecting COLON while matching a lexer rule' (in @init line). Can lexer rule contain a @init block ? How this should be rewritten ? DOUBLE_QUOTED_CHARACTERS @init { int doubleQuoteMark = input.mark(); int semiColonPos = -1; } : ('"' WS* '"') => '"' WS* '"' { $channel = HIDDEN; } { RecognitionException

Does ANTLR allow multiple variable definitions in the locals clause?

可紊 提交于 2019-12-12 11:28:06
问题 in a Parser Grammar I'd like to define several variables in the locals clause. A simplified example looks like this: body locals [ Map<String, String> bodyContent = new HashMap<String, String>(); long counter = 0; ] : BODY_CAPTION NEWLINE line ; line : key='MyKey' TAB value='MyValue' NEWLINE { $body::bodyContent.put($key.text, $value.text); $body::counter++; } ; This gives the error: unknown attribute 'counter' for rule 'body' in '$body::counter' If I swap the lines in the locals clause like

Gradle can't find Antlr token file

流过昼夜 提交于 2019-12-12 10:38:20
问题 I created a file MyLexer.g4 inside myproject/src/main/antlr/com/mypackage like: lexer grammar MyLexer; DIGIT : '0' .. '9' ; ... WS : [ \t\r\n]+ -> skip ; and then trying to write parser in MyParser.g4 in the same directory: grammar MyParser; options { tokenVocab = MyLexer; } SHORT_YEAR: DIGIT DIGIT; unfortunately, when I run gradle task of generateGrammarSource , the following error occurs: error(160): com\mypackage\MyParser.g4:4:18: cannot find tokens file MYPROJECT\build\generated-src\antlr

antlr4: how to know which alternative is chosen given a context

微笑、不失礼 提交于 2019-12-12 08:49:57
问题 Assume there is a rule about 'type'. It is either a predefined type (referred by IDENTIFIER) or a typeDescriptor. type : IDENTIFIER | typeDescriptor ; In my program, I have got an instance of typeContext 'ctx'. How do I know if the path IDENTIFIER is chosen, or typeDescriptor is chosen. I recognise one way which is to test ctx.IDENTIFIER() == null and ctx.typeDescriptor() == null . But it seems not working very well when there are a lot more alternatives. Is there a way to return an index to

Converting from Antlr3 to Antlr4

匆匆过客 提交于 2019-12-12 06:32:44
问题 I am in the process of converting an antlr3 to antlr4 grammar. I have stripped out all the syntactic predicates. I am struggling to make a correct conversion of this relaxed_date_month_first : relaxed_day_of_week? relaxed_month COMMA? WHITE_SPACE relaxed_day_of_month (relaxed_year_prefix relaxed_year)? -> ^(EXPLICIT_DATE relaxed_day_of_month relaxed_month relaxed_day_of_week? relaxed_year?) to antlr4 grammar. Everytime the antlr4 tool runs into "->" character it says "extraneous input '->'