蚂蚁贪婪选择 [英] Antlr greedy-option
问题描述
(我根据@Bart Kiers的第一条评论对问题进行了编辑-谢谢!)
(I edited my question based on the first comment of @Bart Kiers - thank you!)
我有以下语法:
SPACE : (' '|'\t'|'\n'|'\r')+ {$channel = HIDDEN;};
START : 'START:';
STRING_LITERAL : ('"' .* '"')+;
rule : START STRING_LITERAL;
并且我想解析如下语言:'START:"abcd" START:"img src =" test.jpg"(字符串文字可以在字符串文字内).
如果字符串文字中包含字符串文字,则上面定义的语法将不起作用,因为对于语言"START:"img src =" test.jpg",词法分析器会将其转换为以下标记:START('START:') STRING_LITERAL("img src =")test.jpg.
有什么方法可以定义适合我的问题的语法吗?
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: "img src="test.jpg""' the lexer translates it into the following tokens: START('START:') STRING_LITERAL("img src=") test.jpg.
Is there any way to define a grammar which is fine for my problem?
推荐答案
这里有几处错误:
- 您不能在解析器规则中使用
fragment
规则.您的语法将永远不会创建START
令牌;
解析器规则中的 - a
.
char(DOT-char)匹配任何令牌,而词法分析器规则中的.
char(DOT-char)匹配任何字符; - 如果让
.*
贪婪地匹配(并且您定义了匹配字符串文字的适当词法分析器规则),则输入START: "abcd" START: "img src="test.jpg""
中将有一个大字符串:"abcd" START: "img src="test.jpg""
(第一个和最后一个)报价会匹配).
- you cannot use
fragment
rules inside parser rules. You grammar will never create aSTART
token; - a
.
char (DOT-char) inside a parser rule matches any token, while inside a lexer rule, it matches any character; - if you let
.*
match greedily (and you had defined a proper lexer rule that matches a string literal), the inputSTART: "abcd" START: "img src="test.jpg""
would then have one large string in it:"abcd" START: "img src="test.jpg""
(the first and the last quote would be matched).
因此,您不能使用相同的引号将字符串文字嵌入字符串文字中.词法分析器无法确定引号是否打算关闭字符串,或者它是否是(新)嵌入字符串的开始.您将需要在语法中进行更改.
So, you cannot embed string literals inside string literals using the same quotes. The lexer is not able to determine if a quote is meant to close the string, or if it's the start of a (new) embedded string. You will need to change that in your grammar.
这篇关于蚂蚁贪婪选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!