【发布时间】:2014-04-11 14:10:19
【问题描述】:
Rascal 植根于术语重写。它是否具有对术语重写中通常定义的术语/节点位置的内置支持,以便我可以查询子术语在术语中的位置或其他方式?
【问题讨论】:
标签: rascal
Rascal 植根于术语重写。它是否具有对术语重写中通常定义的术语/节点位置的内置支持,以便我可以查询子术语在术语中的位置或其他方式?
【问题讨论】:
标签: rascal
我不相信显式位置通常在术语重写的语义中定义,但是 Rascal 定义了所有类型的术语操作,使得位置是显式的或可以显式的。也请在http://www.rascal-mpl.org查看他的手册
对术语的主要操作是使用普通一阶同余、深度(高阶)匹配、否定匹配、析取匹配等的模式匹配:
if (and(and(_, _), _) := and(and(true(),false()), false())) // pattern match operator :=
println("yes!");
and(true(), b) = b; // function definition, aka rewrite rule
and(false(), _) = false();
[ a | and(a,b) <- booleanList]; // comprehension with pattern as filter on a generator
innermost visit (t) { // explicit automated traversal with strategies
case and(a,b) => or(a,b)
}
b.leftHandSide = true(); // assign new child term to the leftHandSide field of the term assigned to the b variable (non-destructively, you get a new b)
b[0] = false(); // same but to the anonymous first child.
然后是正常的投影运算符,如果使用字段标签定义了许多排序的术语签名,则在子 term[0] 和子名称上的索引:term.myChildName。
如果你想知道子孩子在哪个位置,我可能会这样写:
int getPos(node t, value child) = [*pre, child, *_] := getChildren(t) ? size(pre) : -1;
但还有其他方法可以达到同样的效果。
Rascal 没有指向术语父项的指针。
【讨论】:
getPos 所示) ,说一个函数myGetPos,这样myGetPos(and(and(true(), false()), false()), true()) == [0, 1],0是根节点的位置。
Traversal,尤其是getTraversalContextNodes(),它在visit 的上下文中会为您提供所有当前父母的列表。