英文:
How to make an ANTLR grammar that matches strings both inside and outside a delimiter?
问题
这个ANTLR4的语法应该将文档分成两种子串类型:wiki和nowiki。
grammar NoWikiText;
nowiki: '<nowiki>' ~'</nowiki>'* '</nowiki>';
wiki: ~'<nowiki>'+;
document: (wiki | nowiki)*;
这是输入:
<nowiki>2</nowiki>4<nowiki></nowiki>
我得到两个nowiki匹配。但应该匹配wiki的文本"4"被忽略了。为什么?
编辑:
这似乎有效:
grammar NoWikiText;
P1: '<nowiki>';
P2: '</nowiki>';
NP: .;
nowiki: P1 NP* P2;
wiki: NP+;
document: (wiki | nowiki)*;
英文:
This grammar for ANTLR4 should break a document up into two types of substring: wiki and nowiki.
grammar NoWikiText;
nowiki: '<nowiki>' ~'</nowiki>'* '</nowiki>';
wiki: ~'<nowiki>'+;
document: (wiki | nowiki)*;
Here's the input:
<nowiki>2</nowiki>4<nowiki></nowiki>
I get two matches for nowiki. But the text "4", which should match wiki, is ignored. Why?
EDIT:
This seems to work:
grammar NoWikiText;
P1: '<nowiki>';
P2: '</nowiki>';
NP: .;
nowiki: P1 NP* P2;
wiki: NP+;
document: (wiki | nowiki)*;
答案1
得分: 1
在你发布的语法中,只会创建2个标记:<nowiki> 和 </nowiki>。否定字符的工作方式与你的期望不同:~'</nowiki>' 的意思是:“匹配除了 </nowiki> 之外的任何标记”(这将匹配标记 <nowiki>)。因此,对于你的输入 <nowiki>2</nowiki>4<nowiki></nowiki>,2 和 4 不被识别为有效的标记。
英文:
In the grammar you posted, only 2 tokens will be created: <nowiki> and </nowiki>. The negations char works differently than you expect: ~'</nowiki>' means: "match any token other than </nowiki>" (so that would match the token <nowiki>). So for your input <nowiki>2</nowiki>4<nowiki></nowiki>, the 2 and 4 are not recognized as valid tokens.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论