ANTLR:具有或和一次的语法

huangapple go评论50阅读模式
英文:

ANTLR: syntax with or and once

问题

我想使用ANTLR创建语法,并允许语法中有3个部分:

toAddress | fromAddress | subject

用户可以选择只写其中一个,但顺序没有限制,例如:

  1. to: email1@compaby.com from: email2@company.com --> 可行
  2. from: email1@company.com --> 可行
  3. 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:

  1. to: email1@compaby.com from: email2@company.com --> ok
  2. from: email1@company.com --> ok
  3. to: 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 (&#39;:&#39;);
FROM: F R O M (&#39;:&#39;);
SUBJECT: S U B J E C T (&#39;:&#39;);
SPACES
    : [ \u000B\t\r\n] -&gt; skip
    ;

addressValue    : (ANYTHING | NAME)?;
anyValue        : (ANYTHING | NAME)+;
subjectValue    : SUBJECT_TYPE+;
NAME            : (&#39;a&#39;..&#39;z&#39; | &#39;A&#39;..&#39;Z&#39; | &#39;0&#39;..&#39;9&#39; | &#39;-&#39; | &#39;_&#39; | &#39;@&#39; | &#39;/&#39; | &#39;#&#39; | &#39;.&#39; | &#39;\u0080&#39;..&#39;\uFFFF&#39;)+;
ANYTHING        : [A-Za-z][0-9A-Za-z\u0080-\uFFFF_]+;
SUBJECT_TYPE: &#39;&quot;&#39; ~&#39;&quot;&#39;* &#39;&quot;&#39;;

答案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.

huangapple
  • 本文由 发表于 2023年7月31日 23:45:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76805192.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定