我正在使用 jison 文件并使用 python PLY 的 lex 模块将其转换为解析器生成器。
我注意到在这个 jison 文件中,某些令牌有多个与之关联的规则。例如,对于 token CONTENT
,文件指定了以下三个规则:
[^\x00]*?/("{{") {
if(yytext.slice(-2) === "\\\\") {
strip(0,1);
this.begin("mu");
} else if(yytext.slice(-1) === "\\") {
strip(0,1);
this.begin("emu");
} else {
this.begin("mu");
}
if(yytext) return 'CONTENT';
}
[^\x00]+ return 'CONTENT';
// marks CONTENT up to the next mustache or escaped mustache
<emu>[^\x00]{2,}?/("{{"|"\\{{"|"\\\\{{"|<<EOF>>) {
this.popState();
return 'CONTENT';
}
COMMENT
在另一种情况下,令牌有多个规则:
<com>[\s\S]*?"--}}" strip(0,4); this.popState(); return 'COMMENT';
<mu>"{{!--" this.popState(); this.begin('com');
<mu>"{{!"[\s\S]*?"}}" strip(3,5); this.popState(); return 'COMMENT';
当它们适用于不同的州时,区分规则似乎很容易,但是当它们适用于同一个州时呢?
如何使用 ply.lex 将此 jison 转换为 python 规则?
编辑
如果有帮助,这个 jison 文件是 handlebars.js 源代码的一部分。见:https ://github.com/wycats/handlebars.js/blob/master/src/handlebars.l