-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reimplement deyaccify mutation in Rascal; fix Subgrammar
- Loading branch information
1 parent
24401f5
commit 5e81ccc
Showing
2 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI} | ||
module mutate::DeYaccify | ||
|
||
import syntax::BGF; | ||
import transform::library::Util; | ||
import transform::library::Yacc; | ||
|
||
// NB: the deyaccification mutation works on horizontal productions, | ||
// while the deyaccification transformation works on vertical ones!!! | ||
|
||
public BGFGrammar deyaccify_m(BGFGrammar g) | ||
= visit(g) | ||
{ | ||
// N = N X | X => N = X+ | ||
case production(label, lhs, choice([sequence([nonterminal(n),x]),x])) | ||
=> production(label, lhs, plus(x)) | ||
// N = X | N X => N = X+ | ||
case production(label, lhs, choice([x,sequence([nonterminal(n),x])])) | ||
=> production(label, lhs, plus(x)) | ||
// N = X N | X => N = X+ | ||
case production(label, lhs, choice([sequence([x,nonterminal(n)]),x])) | ||
=> production(label, lhs, plus(x)) | ||
// N = X | X N => N = X+ | ||
case production(label, lhs, choice([x,sequence([x,nonterminal(n)])])) | ||
=> production(label, lhs, plus(x)) | ||
// N = X N | e => N = X* | ||
case production(label, lhs, choice([sequence([x,nonterminal(n)]),epsilon()])) | ||
=> production(label, lhs, star(x)) | ||
// N = e | X N => N = X* | ||
case production(label, lhs, choice([epsilon(),sequence([x,nonterminal(n)])])) | ||
=> production(label, lhs, star(x)) | ||
// N = N X | e => N = X* | ||
case production(label, lhs, choice([sequence([nonterminal(n),x]),epsilon()])) | ||
=> production(label, lhs, star(x)) | ||
// N = e | N X => N = X* | ||
case production(label, lhs, choice([epsilon(),sequence([nonterminal(n),x])])) | ||
=> production(label, lhs, star(x)) | ||
// N = N X | Y => N = Y X* | ||
case production(label, lhs, choice([sequence([nonterminal(n),x]),y])) | ||
=> production(label, lhs, sequence([y,star(x)])) | ||
// N = Y | N X => N = Y X* | ||
case production(label, lhs, choice([y,sequence([nonterminal(n),x])])) | ||
=> production(label, lhs, sequence([y,star(x)])) | ||
// N = X N | Y => N = X* Y | ||
case production(label, lhs, choice([sequence([x,nonterminal(n)]),y])) | ||
=> production(label, lhs, sequence([star(x),y])) | ||
// N = Y | X N => N = X* Y | ||
case production(label, lhs, choice([y,sequence([x,nonterminal(n)])])) | ||
=> production(label, lhs, sequence([star(x),y])) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,24 @@ | ||
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI} | ||
module mutate::Subgrammar | ||
|
||
import lib::Rascalware; | ||
import syntax::BGF; | ||
import transform::library::Util; | ||
|
||
public BGFGrammar subgrammar(BGFGrammar g, str root) | ||
{ | ||
BGFProdList ps = []; | ||
set[str] covered = {}, uncovered = {root}; | ||
while(!isEmpty(uncovered)) | ||
for(str nt <- uncovered) | ||
{ | ||
newps = [p | p:production(_,nt,_) <- g.prods]; | ||
newnts = {n | /nonterminal(n) := newps}; | ||
ps += newps; | ||
covered += newnts; | ||
uncovered += (newnts - covered); | ||
uncovered -= nt; | ||
covered += nt; | ||
//println("Covered: <covered>\nUncovered: <uncovered>"); | ||
} | ||
return grammar([root],ps); | ||
} |