Skip to content

Commit

Permalink
reimplement the subgrammar mutation in Rascal
Browse files Browse the repository at this point in the history
  • Loading branch information
grammarware committed Jan 17, 2013
1 parent 994e208 commit 04d4514
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
5 changes: 3 additions & 2 deletions shared/rascal/src/export/BNF.rsc
Expand Up @@ -11,8 +11,9 @@ public str pp(BGFProdList ps) = mapjoin(pp,ps,"\n");

public str pp(set[BGFProduction] ps) = mapjoin(pp,ps,"\n");

public str pp(production("",str lhs, BGFExpression rhs)) = "<lhs> ::= <pptop(rhs)> ;";
public default str pp(BGFProduction p) = "[<p.label>] <p.lhs> ::= <pptop(p.rhs)> ;";
public str pp(BGFProduction p) = "<ppl(p.label)><p.lhs> ::= <pptop(p.rhs)> ;";

str ppl(str s) = (s=="")?"":"[<s>] ";

str pptop(sequence(BGFExprList es)) = pp(es);
default str pptop(BGFExpression e) = pp(e);
Expand Down
2 changes: 2 additions & 0 deletions shared/rascal/src/export/XBNF.rsc
Expand Up @@ -78,3 +78,5 @@ str pp(XBGFScope::innt(str x)) = "innt(<x>)";
str pp(XBGFScope::notinnt(str x)) = "notinnt(<x>)";
str pp(XBGFScope::comboscope(XBGFScope w1, XBGFScope w2)) = "comboscope(<pp(w1)>,<pp(w2)>)";
default str pp(XBGFScope smth) = "??<smth>??";

str pp(list[str] ss) = joinStrings(ss,",");
20 changes: 20 additions & 0 deletions shared/rascal/src/mutate/Subgrammar.rsc
@@ -0,0 +1,20 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module mutate::Subgrammar

import syntax::BGF;
import transform::library::Util;

public BGFGrammar subgrammar(BGFGrammar g, str root)
{
BGFProdList ps = [];
set[str] covered = {}, uncovered = {root};
for(str nt <- uncovered)
{
newps = [p | p:production(_,nt,_) <- g.prods];
newnts = {n | /nonterminal(n) := newps};
ps += newps;
covered += newnts;
uncovered -= nt;
}
return grammar([root],ps);
}

0 comments on commit 04d4514

Please sign in to comment.