【发布时间】:2015-08-31 20:28:51
【问题描述】:
我用一个不知道如何解决的问题构建了一个简单的语法:
start syntax Prog = prog: Type Id;
syntax Dot = {Id "."}+ ;
syntax Id =
id: [A-Z_a-z] !>> [0-9A-Z_a-z] \ KW
| Rv
;
syntax Rv = rv: "$" [a-z_A-Z][a-z_A-Z0-9]* ;
syntax Type =
Rv
| Ref
;
syntax Ref =
Dot
| s: "str"
;
keyword KW = "str" ;
layout LAYOUTLIST = LAYOUT* !>> [\t-\n\r\ /] ;
lexical LAYOUT = [\t-\n\r\ ] ;
问题在于我们可以通过三种不同的方式解决rv (Id -> Rv) 和 (Type -> Rv) 和 (Type -> Ref -> Dot -> Id -> Rv)。问题是我需要Types 和Ids 才能成为Rvs。所以,给定一个简单的程序:
$a x
我的想法是我可以通过将Type 的规则更改为:
syntax Type =
Rv
> Ref
;
希望解析器将Rv 与Type 关联,然后检查它是否可以在Ref 分支中解析。我已经运行了一些模棱两可的诊断,但我不确定这些是什么:
info(
"Ambiguity cluster with 2 alternatives",
|cwd:///grammar.txt|(0,3,<1,0>,<1,3>)),
info(
"Production unique to the one alternative: Type = Rv ;",
|cwd:///grammar.txt|(0,3,<1,0>,<1,3>)),
info(
"Production unique to the other alternative: Dot = {Id \".\"}+ ;",
|cwd:///grammar.txt|(0,3,<1,0>,<1,3>)),
info(
"Production unique to the other alternative: {Id \".\"}+;",
|cwd:///grammar.txt|(0,3,<1,0>,<1,3>)),
info(
"Production unique to the other alternative: Ref = Dot ;",
|cwd:///grammar.txt|(0,3,<1,0>,<1,3>)),
info(
"Production unique to the other alternative: Type = Ref ;",
|cwd:///grammar.txt|(0,3,<1,0>,<1,3>)),
info(
"Production unique to the other alternative: Id = Rv ;",
|cwd:///grammar.txt|(0,3,<1,0>,<1,3>)),
info(
"The alternatives have different productions at the top, one has \n Type = Rv \nwhile the other has\n Type = Ref ",
|cwd:///grammar.txt|(0,3,<1,0>,<1,3>)),
warning(
"You should give this production a good label: \n Ref = Dot ]",
|cwd:///grammar.txt|(0,3,<1,0>,<1,3>)),
error(
"To fix this issue, you could restrict the nesting of\n Ref = Dot \nunder\n Type = Ref \nusing the ! operator on argument 0: !labelX\nHowever, you should realize that you are introducing a restriction that makes the language smaller",
|cwd:///grammar.txt|(0,3,<1,0>,<1,3>))
所以,我需要某种方式使Rvs 在解析时同时成为Types 和Ids,而不会产生歧义。这可能吗?
谢谢!
【问题讨论】: