antlr3

How to match a string, but case-insensitively?

独自空忆成欢 提交于 2019-12-18 12:13:32
问题 Let's say that I want to match "beer", but don't care about case sensitivity. Currently I am defining a token to be ('b'|'B' 'e'|'E' 'e'|'E' 'r'|'R') but I have a lot of such and don't really want to handle 'verilythisisaverylongtokenindeedomyyesitis'. The antlr wiki seems to suggest that it can't be done (in antlr) ... but I just wondered if anyone had some clever tricks ... 回答1: How about define a lexer token for each permissible identifier character, then construct the parser token as a

Antlr v3 error with parser/lexer rules

走远了吗. 提交于 2019-12-17 21:13:45
问题 I am having problems with my Antlr grammar. I'm trying to write a parser rule for 'typedident' which can accept the following inputs: 'int a' or 'char a' The variable name 'a' is from my lexer rule 'IDENT' which is defined as follows: IDENT : (('a'..'z'|'A'..'Z') | '_') (('a'..'z'|'A'..'Z')|('0'..'9')| '_')*; My 'typedident' parser rule is as follows: typedident : (INT|CHAR) IDENT; INT and CHAR having been defined as tokens. The problem I'm having is that when I test 'typedident' the variable

How to collect errors during run time given by a parser in Antlr4

淺唱寂寞╮ 提交于 2019-12-17 20:47:38
问题 I have upgraded from Antlr 3 to Antlr 4. I was using this code to catch exceptions using this code. But this is not working for Antlr 4. partial class XParser { public override void ReportError(RecognitionException e) { base.ReportError(e); Console.WriteLine("Error in Parser at line " + ":" + e.OffendingToken.Column + e.OffendingToken.Line + e.Message); } } This is the error that appears 'Parser.ReportError(Antlr4.Runtime.RecognitionException)': no suitable method found to override In Antlr 4

Dynamically create lexer rule

偶尔善良 提交于 2019-12-17 19:48:09
问题 Here is a simple rule: NAME : 'name1' | 'name2' | 'name3'; Is it possible to provide alternatives for such rule dynamically using an array that contains strings? 回答1: Yes, dynamic tokens match IDENTIFIER rule In that case, simply do a check after the Id has matched completely to see if the text the Id matched is in a predefined collection. If it is in the collection (a Set in my example) change the type of the token. A small demo: grammar T; @lexer::members { private java.util.Set<String>

Why are antlr3 c# parser methods private?

我的梦境 提交于 2019-12-17 19:33:48
问题 I'm building a parser in antlr which compiles to a working java target. When I retarget for c#2 it produces a parser in which all of the parse methods are private but marked with a [GrammarRule("rulename")] attribute. What is the approved means to actually invoke the parser? I am using ANTLR 3.3 Nov 30, 2010 12:45:30 Thanks, Andy 回答1: Make at least one parser rule "public" like this: grammar T; options { language=CSharp2; } public parse : privateRule+ EOF ; privateRule : Token+ ; // ... You

Can I add Antlr tokens at runtime?

痞子三分冷 提交于 2019-12-17 16:31:15
问题 I have a situation where my language contains some words that aren't known at build time but will be known at run time causing the need to constantly rebuild / redeploy the program to take into account new words. I was wandering if it was possible in Antlr generate some of the tokens from a config file? e.g In a simplified example if I have a rule rule : WORDS+; WORDS : 'abc'; And my language comes across 'bcd' at runntime, I would like to be able to modify a config file to define bcd as a

ANTR3 set the number of accepted characters for a token

五迷三道 提交于 2019-12-14 04:20:51
问题 I have to create a Lexer which will accept for example an integer only if it has a maximum of 8 digits. Is here an alternative to do it rather than just writing it like this? INTEGER : (DIG | DIG DIG | DIG DIG DIG | ...) 回答1: This can be done using a Gated Semantic Predicates like this: INTEGER @init{int n = 1;} : ({n <= 8}?=> DIGIT {n++;})+ ; fragment DIGIT : '0'..'9'; Details about this kind of predicate, see: What is a 'semantic predicate' in ANTLR? 来源: https://stackoverflow.com/questions

Antlr greedy-option

早过忘川 提交于 2019-12-13 17:21:47
问题 (I edited my question based on the first comment of @Bart Kiers - thank you!) I have the following grammar: SPACE : (' '|'\t'|'\n'|'\r')+ {$channel = HIDDEN;}; START : 'START:'; STRING_LITERAL : ('"' .* '"')+; rule : START STRING_LITERAL; and I want to parse languages like: 'START: "abcd" START: "img src="test.jpg""' (string literals could be inside string literals). The grammar defined above does not work if there are string literals inside a string literal because for the language 'START:

sbt and antlr, got simple example?

风格不统一 提交于 2019-12-13 14:33:02
问题 Does anyone have an example of how to set up sbt to build an ANTLR file (to scala) and then compile the resulting code. My file layout src/main/scala/Test.scala // scala test rig src/main/scala/Test.g // antlr grammar build/antlr/TestParser.scala // antlr output files build/antlr/TestLexer.scala What should my sbt contain? I know there's a plugin out there for pulling in the rules for ANTLR, but I haven't been able to make it work. (Still newbie to this world) 回答1: I've written a sbt plugin

Parsing Newlines, EOF as End-of-Statement Marker with ANTLR3

落花浮王杯 提交于 2019-12-13 13:30:24
问题 My question is in regards to running the following grammar in ANTLRWorks: INT :('0'..'9')+; SEMICOLON: ';'; NEWLINE: ('\r\n'|'\n'|'\r'); STMTEND: (SEMICOLON (NEWLINE)*|NEWLINE+); statement : STMTEND | INT STMTEND ; program: statement+; I get the following results with the following input (with program as the start rule), regardless of which newline NL (CR/LF/CRLF) or integer I choose: "; NL " or "32; NL " parses without error. ";" or "45;" (without newlines) result in EarlyExitException. " NL