Skip to content

Commit

Permalink
reimplement deyaccify mutation in Rascal; fix Subgrammar
Browse files Browse the repository at this point in the history
  • Loading branch information
grammarware committed Jan 17, 2013
1 parent 24401f5 commit 5e81ccc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
50 changes: 50 additions & 0 deletions shared/rascal/src/mutate/DeYaccify.rsc
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]))
};
6 changes: 5 additions & 1 deletion shared/rascal/src/mutate/Subgrammar.rsc
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);
}

0 comments on commit 5e81ccc

Please sign in to comment.