英文:
ANTLR: syntax with or and once
问题
我想使用ANTLR创建语法,并允许语法中有3个部分:
toAddress | fromAddress | subject
用户可以选择只写其中一个,但顺序没有限制,例如:
to: email1@compaby.com from: email2@company.com
--> 可行from: email1@company.com
--> 可行to: email1@company.com to: email2@company.com
--> 错误
任何帮助都将不胜感激。
searchQueryLang
: (toAddress | fromAddress | subject)*
| freeText
;
toAddress: TO addressValue;
fromAddress: FROM addressValue;
subject: SUBJECT subjectValue;
freeText: anyValue;
TO: T O (':');
FROM: F R O M (':');
SUBJECT: S U B J E C T (':');
SPACES
: [ \u000B\t\r\n] -> skip
;
addressValue : (ANYTHING | NAME)?;
anyValue : (ANYTHING | NAME)+;
subjectValue : SUBJECT_TYPE+;
NAME : ('a'..'z' | 'A'..'Z' | '0'..'9' | '-' | '_' | '@' | '/' | '#' | '.' | '\u0080'..'\uFFFF')+;
ANYTHING : [A-Za-z][0-9A-Za-z\u0080-\uFFFF_]+;
SUBJECT_TYPE: '"' ~'"'* '"';
请注意,我已经将ANTLR语法中的单引号字符(')和角括号(<和>)转换为HTML实体编码,以避免解释为HTML标记。
英文:
I want to create grammar with ANTLR and to allow 3 parts in the syntax:
toAddress | fromAddress | subject
The user can choose to write any of them just once but the order is not restricted, examples:
to: email1@compaby.com from: email2@company.com
--> okfrom: email1@company.com
--> okto: email1@company.com to: email2@company.com
--> error
any help will be appreciated.
searchQueryLang
: (toAddress | fromAddress | subject)*
| freeText
;
toAddress: TO addressValue;
fromAddress: FROM addressValue;
subject: SUBJECT subjectValue;
freeText: anyValue;
TO: T O (':');
FROM: F R O M (':');
SUBJECT: S U B J E C T (':');
SPACES
: [ \u000B\t\r\n] -> skip
;
addressValue : (ANYTHING | NAME)?;
anyValue : (ANYTHING | NAME)+;
subjectValue : SUBJECT_TYPE+;
NAME : ('a'..'z' | 'A'..'Z' | '0'..'9' | '-' | '_' | '@' | '/' | '#' | '.' | '\u0080'..'\uFFFF')+;
ANYTHING : [A-Za-z][0-9A-Za-z\u0080-\uFFFF_]+;
SUBJECT_TYPE: '"' ~'"'* '"';
答案1
得分: 0
这个请求定期出现。在ANTLR中无法指定“多个值以任意顺序但只出现一次”的规则。
只需使用你已经拥有的内容,然后编辑生成的解析树以去除重复项。(额外好处,你可以生成自己的易于理解的错误消息。)
英文:
This request comes up periodically. It’s not possible to specify the “multiple values in any order but only once” rule in ANTLR.
Just use what you have and edit the resulting parse tree for duplicates. (Bonus, you gat produce your own, easily understood error message.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论