[英]Adding a parsing constraint to a DCG
Graphic tokens can serve as Prolog operators that don't require single quotes. 图形标记可以用作不需要单引号的Prolog运算符。
A translation of ISO/IEC 13211-1:1995, 6.4.2 "Syntax.Tokens.Names" is: ISO / IEC 13211-1:1995,6.4.2“ Syntax.Tokens.Names”的翻译是:
graphic_token --> kleene_plus(graphic_token_char).
graphic_token_char --> member("#$&*+-./:<=>?@^~\\").
% some auxiliary code
kleene_plus(NT) --> NT, kleene_star(NT).
kleene_star(NT) --> "" | kleene_plus(NT).
member(Xs) --> [X], { member(X,Xs) }.
Subsection 6.4.1 "Syntax.Tokens.Layout Text" adds the following constraint: 6.4.1小节“ Syntax.Tokens.Layout文本”添加了以下约束:
A graphic token shall not begin with the character sequence
comment open
(ie, "/*
"). 图形标记不得以comment open
的字符序列comment open
(即“/*
”)开头。
Enforcing that restriction in the DCG is no big deal... 在DCG中实施限制并不重要...
graphic_token --> graphic_token_char. % 1 char graphic_token --> % 2+ chars [C1,C2], { phrase((graphic_token_char,graphic_token_char), [C1,C2]) }, { dif([C1,C2], "/*") }, kleene_star(graphic_token_char).
... but quite ugly! ...但是很丑! How do I make it pretty again (and keep it bidirectional)? 如何再次使其漂亮(并保持双向)?
I'm not sure this is prettier, but maybe something like this: 我不确定这是否更漂亮,但也许是这样的:
graphic_token --> kleene_plus_member("#$&*+-.:<=>?@^~\\",0'/).
graphic_token --> "/", kleene_star_member("#$&+-./:<=>?@^~\\", 0'*).
kleene_plus_member(Xs, Code) --> member(Xs), kleene_star(member([Code|Xs])).
kleene_star_member(Xs, Code) --> "" | member(Xs), kleene_star(member([Code|Xs])).
The first clause of graphic_token parses a graphic token that does not begin with /
and the second clause the one which starts with it. graphic_token的第一个子句分析不以/
开头的图形标记,第二个子句以/
开头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.