我的问题是关于处理 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]*
?