antlr4

Handling scope for single and double quote strings in ANTLR4

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 01:38:46
问题 I am working with ANTLR4 and in the process of writing grammar to handle single and double quoted strings. I am trying to use Lexer modes to scope the strings but that is not working out for me, my grammar is listed below. Is this the right way or how can I properly parse these as tokens instead of parser rules with context. Any insight? An example: 'single quote that contain "a double quote 'that has another single quote'"' Lexer Grammar lexer grammar StringLexer; fragment SQUOTE: '\'';

How to split input according to the grammar

≡放荡痞女 提交于 2019-12-12 01:36:41
问题 We are trying to build a parser for log file generated in the router. We successfully build that and able to print the valid language in particular file. But if the input is not valid according to the grammar, then we want to print it in the different file. We tried something and it's not working properly. Can you please suggest the way by which we can do it? And if possible, kindly give the working example. This is what we have tried. We are not using any specific IDE, just a text editor.

ANTLR4 Tokenizing a Huge Set of Keywords

只愿长相守 提交于 2019-12-12 00:28:21
问题 I want to embed some known identifier names into my grammar e.g. the class names of my project are known and I want to tell the lexer what identifiers are known keywords that actually belongs to the class-name token. But since I have a long list of class names (hundreds of names), I don't want to create a class-name lexer rule by listing all the known class name keywords in the rule, that will make my grammar file too large. Is it possible to place my keywords into a separate file? One

Using parser for OData v4 grammar

╄→尐↘猪︶ㄣ 提交于 2019-12-11 23:14:34
问题 I try to use the OData v4 grammar for Antlr4 provided by the OASIS group. See the following link: https://tools.oasis-open.org/version-control/browse/wsvn/odata/trunk/spec/grammar/ANTLR/#_trunk_spec_grammar_ANTLR_ Based on these files and Antlr v4 Maven plugin, I successfully generated classes to parse OData URLs. I try to use the parser as described below: String expression = "http://192.168.1.1/odata/Category(1)/Products?$top=2&$orderby=name"; ANTLRInputStream in = new ANTLRInputStream

Overlapping rules - mismatched input

爱⌒轻易说出口 提交于 2019-12-11 21:00:08
问题 My grammar (as follows (trimmed down from the original)) requires somewhat overlapping rules grammar NOVIANum; statement : (priorityStatement | integerStatement)* ; priorityStatement : T_PRIO TwoDigits ; integerStatement : T_INTEGER Integer ; WS : [ \t\r\n]+ -> skip ; T_PRIO : 'PRIO' ; T_INTEGER : 'INTEGER' ; Integer: OneToNine Digit* | ZERO ; TwoDigits : Digit Digit ; fragment OneToNine : ('1'..'9') ; fragment Digit: ('0'..'9'); ZERO : [0] ; so "Integer" and "TwoDigits" overlap to a certain

How to create a lexical analyzer in ANTLR 4 that can catch different types of lexical errors

狂风中的少年 提交于 2019-12-11 18:08:22
问题 I am using ANTLR 4 to create my lexer, but I don't how to create a lexical analyzer that catches different types of lexical errors. For example: If I have an unrecognized symbol like ^ the lexical analyzer should a report an error like this "Unrecognized symbol "^" " If I have an invalid identifier like 2n the lexical analyzer should report an error like this "identifier "2n" must begin with a letter" Please can you help me. 回答1: Create an error token rule for each known error and an

Adding the ParserRuleContext when there is a Syntax error in the source code

折月煮酒 提交于 2019-12-11 17:58:34
问题 Given below is the code i written. I need to add the parserRulecontext that cause the syntax error when ever there is a error in the code. I am using the antlr4 as well. if (tree instanceof TerminalNodeImpl) { Token token = ((TerminalNodeImpl) tree).getSymbol(); map.put("line", token.getLine()); map.put("column", token.getCharPositionInLine()); map.put("type",JavaScriptLexer.VOCABULARY.getSymbolicName(token.getType())); map.put("text", token.getText()); } 来源: https://stackoverflow.com

How to correctly bundle antlr4 generated parsers with antlr4?

柔情痞子 提交于 2019-12-11 17:21:33
问题 I have been trying to use webpack to bundle my parser/lexer/listener .js files with antlr. I am not sure if I am doing it right. Because after getting the bundle.js file, I imported it with importScripts and the browser still claims that the variable is not defined. I have generated parser/lexer/listener file using antlr4 -Dlanguage=JavaScript LPMLN.g4 I wrote a main.js file to refer to the parser/lexer/listener. I configured webpack.config.js file for bundling. I imported the bundle.js file

Why is antlr4 parsing my sentence into two statements?

…衆ロ難τιáo~ 提交于 2019-12-11 17:14:47
问题 I am writing a little parser for expressions. At the moment I just want it to recognize binary multiplications ( myId * myId ) and C-like dereferenced pointers ( *myId ), plus some assignation statements ( myId *= myId ). The input that makes the parser throw errors is: x *= y; ... on which the parser fails with this message and parse tree: [line 1:1 mismatched input ' *' expecting {';', NEWLINE}] (sourceFile (statement (expressionStatement (expression (monoOperatedExpression

ANTLR4 ambiguous grammar

蹲街弑〆低调 提交于 2019-12-11 16:55:25
问题 I want to achieve following behavior: User:class should be parsed to Object - User; Type - class , also Us:er:class should result Object - Us:er; Type - class . I can't make second part work, as soon as I add : as a legal symbol for WORD it parses whole input as an object Object - Us:er:class . My grammar: grammar Sketch; /* * Parser Rules */ input : (object)+ EOF ; object : objectName objectType? NEWLINE ; objectType : ':' TYPE ; objectName : WORD ; /* * Lexer Rules */ fragment LOWERCASE :