Skip to content

Commit

Permalink
implement several new mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
grammarware committed Jan 20, 2013
1 parent b9ef08e commit d975329
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 5 deletions.
2 changes: 1 addition & 1 deletion shared/rascal/src/mutate/DeYaccify.rsc
Expand Up @@ -7,7 +7,7 @@ import syntax::BGF;
// NB: the deyaccification mutation works on horizontal productions,
// while the deyaccification transformation works on vertical ones!!!

public BGFGrammar deyaccify_m(BGFGrammar g)
public BGFGrammar deyaccifyAll(BGFGrammar g)
= visit(g)
{
// N = N X | X => N = X+
Expand Down
4 changes: 1 addition & 3 deletions shared/rascal/src/mutate/InlineChains.rsc
Expand Up @@ -2,11 +2,9 @@
@wiki{UnchainAll}
module mutate::InlineChains

import IO;
import syntax::BGF;
import export::BNF;

public BGFGrammar unchain_m(BGFGrammar g)
public BGFGrammar unchainAll(BGFGrammar g)
{
ps = g.prods;
for (tp:production(_,_,nonterminal(chained)) <- [p | p:production(_,_,nonterminal(x)) <- g.prods])
Expand Down
28 changes: 28 additions & 0 deletions shared/rascal/src/mutate/InlineLazy.rsc
@@ -0,0 +1,28 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
@wiki{InlineLazy}
module mutate::InlineLazy

import lib::Rascalware;
import syntax::BGF;
import syntax::XBGF;
import transform::library::Util;
import transform::XBGF;
import diff::GDT;

// if a nonterminal has one production rule and one occurrence in the grammar, it is inlined
public BGFGrammar inlineLazy(BGFGrammar g)
{
ns = allNs(g.prods);
BGFGrammar g1 = grammar([],[]), g2 = g;
// keep rewriting
while(!gdts(g1,g2))
{
g1 = g2;
lazy = {nt | nt <- ns, len({p | p:production(_,nt,_) <- g1.prods})==1} // defined by one prod
& {nt | nt <- ns, len([1 | /nonterminal(nt) := g1.prods])==1}; // occur once
g2 = vtransform([inline(nt) | nt <- lazy],g1);
println("-----second stage------");
}
// TODO: might want to change to non-verbal transform if bored to see messages
return g1;
}
31 changes: 31 additions & 0 deletions shared/rascal/src/mutate/InlinePlus.rsc
@@ -0,0 +1,31 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
@wiki{InlinePlus}
module mutate::InlinePlus

import lib::Rascalware;
import syntax::BGF;
import syntax::XBGF;
import transform::library::Util;
import transform::XBGF;
import IO;

// (1) inline all nonterminals defined as pluses of other nonterminals
// (2) massage x+? to x* if necessary
public BGFGrammar inlinePlus(BGFGrammar g)
{
ns = allNs(g.prods);
todo = ({nt | p:production(_,nt,plus(nonterminal(_))) <- g.prods} // defined as plus
& {nt | nt <- ns, len({p | p:production(_,nt,_) <- g.prods})==1}) // not defined otherwise
- toSet(g.roots);
//println({nt | p:production(_,nt,plus(nonterminal(_))) <- g.prods});
//println({nt | nt <- ns, len({p | p:production(_,nt,_) <- g.prods})==1});
//println(todo);
//println(g.roots);
//g2 = vtransform([inline(nt) | nt <- todo],g);
// TODO: might want to change to non-verbal transform if bored to see messages
return visit(vtransform([inline(nt) | nt <- todo],g))
{
case optional(plus(nonterminal(nt))) => star(nonterminal(nt))
};
}

25 changes: 25 additions & 0 deletions shared/rascal/src/mutate/Mutations.rsc
@@ -0,0 +1,25 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
@wiki{SubGrammar, DeYaccifyAll, UnchainAll, InlineLazy, InlinePlus}
module mutate::Mutations

extend mutate::SubGrammar;
extend mutate::DeYaccify;
extend mutate::InlineChains;
extend mutate::InlineLazy;
extend mutate::InlinePlus;

import syntax::BGF;
import normal::BGF;
import IO;

alias MutationList = list[BGFGrammar(BGFGrammar)];

public BGFGrammar mutate(MutationList script, BGFGrammar g)
{
for (m <- script)
{
println("[Mutation] <m>");
g = normalise(m(g));
}
return g;
}
@@ -1,6 +1,6 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
@wiki{SubGrammar}
module mutate::Subgrammar
module mutate::SubGrammar

import lib::Rascalware;
import syntax::BGF;
Expand Down

0 comments on commit d975329

Please sign in to comment.