Skip to content

Commit

Permalink
Guided convergence: demarking inside separator lists, ANF matured
Browse files Browse the repository at this point in the history
  • Loading branch information
grammarware committed Jun 24, 2012
1 parent 22cc245 commit 4fac462
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
7 changes: 6 additions & 1 deletion shared/rascal/src/analyse/Metrics.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module analyse::Metrics

import syntax::BGF;
import List;

@doc{All nonterminals in a grammar: defined or used}
public set[str] allNs(list[BGFProduction] ps) = definedNs(ps) + usedNs(ps);
Expand Down Expand Up @@ -37,7 +38,11 @@ public set[str] usedNRNs(BGFGrammar g) = usedNRNs(g.prods);
public set[str] definedNs(list[BGFProduction] ps) = {s | production(_,str s,_) <- ps};
public set[str] definedNs(BGFGrammar g) = definedNs(g.prods);
@doc{All nonterminals defined in a grammar by one production rule each}
public set[str] definedOnceNs(list[BGFProduction] ps) = {x | str x <- definedNs(ps), size(prodsOfN(x,ps))==1 };
public set[str] definedOnceNs(BGFGrammar g) = definedOnceNs(g.prods);
public rel[str,str] calls(list[BGFProduction] ps) = {<n1,n2> | production(_,n1,rhs) <- ps, /nonterminal(n2) := rhs};
public set[str] calls(str x, list[BGFProduction] ps) = {n2 | production(_,x,rhs) <- ps, /nonterminal(n2) := rhs};
public list[BGFProduction] prodsOfN(str x, list[BGFProduction] ps) = [p | p <- ps, production(_,x,_) := p];
public list[BGFProduction] prodsOfN(str x, list[BGFProduction] ps) = [p | p <- ps, production(_,x,_) := p];
1 change: 1 addition & 0 deletions shared/rascal/src/io/WriteCBGF.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Node cbgf2xml(CBGFCommand step)
case fold_unfold(str s, XBGFScope w): return element(namespace("cbgf","http://planet-sl.org/cbgf"),"fold-unfold",[element(none(),"nonterminal",[charData(s)]),element(none(),"in",[context2xml(w)])]);
case horizontal_vertical(XBGFScope w): return element(namespace("cbgf","http://planet-sl.org/cbgf"),"horizontal-vertical",[context2xml(w)]);
case inject_project(BGFProduction p): return element(namespace("cbgf","http://planet-sl.org/cbgf"),"inject-project",[prod2xml(p)]);
case inline_extract(BGFProduction p, globally()): return element(namespace("cbgf","http://planet-sl.org/cbgf"),"inline-extract",[prod2xml(p)]);
case inline_extract(BGFProduction p, XBGFScope w): return element(namespace("cbgf","http://planet-sl.org/cbgf"),"inline-extract",[prod2xml(p),element(none(),"in",[context2xml(w)])]);
case introduce_eliminate(list[BGFProduction] ps): return element(namespace("cbgf","http://planet-sl.org/cbgf"),"introduce-eliminate",[prod2xml(p) | p <- ps]);
case iterate_assoc(BGFProduction p): return element(namespace("cbgf","http://planet-sl.org/cbgf"),"iterate-assoc",[prod2xml(p)]);
Expand Down
27 changes: 20 additions & 7 deletions shared/rascal/src/normal/ANF.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Set;
import List;
import IO; //debug
import io::WriteBGF; // batch
import io::WriteCBGF; // batch
import io::ReadBGF; // batch

CBGFSequence normalise(BGFGrammar g)
Expand All @@ -26,15 +27,21 @@ CBGFSequence normStage2(CBGFSequence cbgf, BGFGrammar g)
{
list[BGFProduction] afterps = transform(forward(cbgf),g).prods;
set[str] used = usedNs(afterps);
set[str] defined = definedOnceNs(afterps);
set[str] epsilons = {};
//iprintln(afterps);
for(q <- afterps)
{
if (choice(_) := q.rhs)
cbgf += vertical_horizontal(innt(q.lhs));
if (epsilon() := q.rhs)
epsilons += q.lhs;
}
switch(q.rhs)
{
case choice(_) :
cbgf += vertical_horizontal(innt(q.lhs));
case epsilon() :
epsilons += q.lhs;
case nonterminal(str n) :
if (n in defined && n notin usedNs(afterps - q))
cbgf += (n == q.lhs)? abridge_detour(q) : unchain_chain(q);
// default?
}
//println(epsilons);
for (n <- epsilons)
if (n in used)
Expand Down Expand Up @@ -76,8 +83,14 @@ public void main()
{
for (src <- ["antlr","dcg","ecore","emf","jaxb","om","python","rascal-a","rascal-c","sdf","txl","xsd"])
{
println("Reading <src>...");
BGFGrammar g = readBGF(|home:///projects/slps/topics/convergence/guided/bgf/<src>.bgf|);
g = transform(forward(normalise(g)),g);
CBGFSequence c = normalise(g);
println("Writing the normalising trafo <src>...");
writeCBGF(c,|home:///projects/slps/topics/convergence/guided/bgf/<src>.normalise.cbgf|);
println("Transforming <src>...");
g = transform(forward(c),g);
println("Writing output to <src>...");
writeBGF(g,|home:///projects/slps/topics/convergence/guided/bgf/<src>.normal.bgf|);
}
}
5 changes: 5 additions & 0 deletions shared/rascal/src/transform/library/Util.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public BGFProduction demark (BGFProduction p1)
case sequence([*L1,marked(BGFExpression e),*L2]) => sequence(L1 + L2)
case choice([*L1,marked(BGFExpression e),*L2]) => choice(L1 + L2)
case production(l,n,marked(_)) => production(l,n,epsilon())
case selectable(s,marked(_)) => selectable(s,epsilon())
case seplistplus(marked(_),s) => star(s)
case seplistplus(e,marked(_)) => plus(e)
case sepliststar(marked(_),s) => star(s)
case sepliststar(e,marked(_)) => star(e)
}
return p2;
}
Expand Down

0 comments on commit 4fac462

Please sign in to comment.