1

我的问题是关于处理 Rascal 语法定义中的保留关键字。采用以下 Rascal 模块,该模块源自在线文档:

module Test
// Taken from http://tutor.rascal-mpl.org/Rascal/Declarations/SyntaxDefinition/SyntaxDefinition.html

start syntax Top = Identifier | ReservedKeyword;

layout MyLayout = [\t\n\ \r\f]*;

// Is OK, parse(#Top,"if") succeeds, parse(#Identifier,"if") fails
lexical Identifier = [a-z] !<< [a-z]+ !>> [a-z] \ MyKeywords;

// Goes wrong, parse(#Top,"if") fails, parse(#Identifier,"if") succeeds, 
// so "if" is not exluded
//lexical Identifier = [a-z0-9] !<< [a-z][a-z0-9]* !>> [a-z0-9] \ MyKeywords;

keyword MyKeywords = "if" | "then" | "else" | "fi";

syntax ReservedKeyword = "if" | "then" | "else" | "fi";

关键是保留关键字构造\ MyKeywords仅在词法定义为 时才有效[a-z] !<< [a-z]+ !>> [a-z]。如果词汇变得稍微复杂[a-z0-9] !<< [a-z][a-z0-9]* !>> [a-z0-9],则不再排除关键字。

我在这里做错了什么?在标识符的情况下,如何排除关键字[a-z][a-z0-9]*

4

1 回答 1

0

问题的原因是诸如[a-z]next to之类的符号的附加位置[a-z0-9]+绑定不如\运算符和!>>运算符强。

所以这里我们只保留标识符MyKeywords的尾部[a-z0-9]+

lexical Identifier = [a-z] !<< [a-z][a-z0-9]+ \ MyKeywords;

要解决此问题,您可以添加括号以MyKeywords从整个序列中删除:

lexical Identifier = [a-z] !<< ([a-z][a-z0-9]+) \ MyKeywords;

然后您可以再次添加限制:

lexical Identifier = [a-z] !<< ([a-z][a-z0-9]+) \ MyKeywords !>> [a-z];

或者像这样,等效地:

lexical Identifier = [a-z] !<< ([a-z][a-z0-9]+) !>> [a-z] \ MyKeywords;
于 2016-03-24T11:37:55.913 回答