Skip to content

Commit

Permalink
deLayering is implemented as a grammar analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
grammarware committed Jul 7, 2012
1 parent be4cd69 commit f974d30
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions shared/rascal/src/analyse/Layers.rsc
@@ -0,0 +1,61 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module analyse::Layers

import syntax::BGF;
import syntax::XBGF;
import io::ReadBGF;
import lib::Rascalware;
import analyse::Metrics;
import normal::BGF;
import transform::XBGF;
import export::BNF;
import Relation;

rel[str,str] detectLayers(BGFGrammar g)
{
rel[str,str] res = {};
rel[str,str] dres = {};
for (n1 <- definedNs(g), n2 <- definedNs(g))
{
BGFProdList ps1 = prodsOfN(n1,g.prods);
BGFProdList ps2 = prodsOfN(n2,g.prods);
// n2 chains to n1, but not exclusively
if (
/p:production(_,n2,nonterminal(n1)) := ps2
&& !isEmpty(ps2 - p)
)
res += <n1,n2>;
if (
/production(_,n2,choice([*L1,nonterminal(n1),*L2])) := ps2
&& !isEmpty(L1+L2)
)
res += <n1,n2>;

for (<n1,n2> <- res)
if (/production(_,n1,e) := ps1 && /nonterminal(n2) := e && n1!=n2)
dres += <n1,n2>;
}
return dres;
}

public void main()
{
for (src <- ["antlr","dcg","ecore","emf","jaxb","om","python","rascal-a","rascal-c","sdf","txl","xsd"])
//for (src <- ["python"])
{
println("Reading <src>...");
BGFGrammar g = readBGF(|home:///projects/slps/topics/convergence/guided/bgf/<src>.bgf|);
println("Normalising <src>...");
g = visit(g)
{
case selectable(s,e) => e
case terminal(_) => epsilon()
}
for (n <- [n | n <- usedNs(g), len(prodsOfN(n,g.prods))==1, len([n | /nonterminal(n) := g])==1])
g = transform([inline(n)],g);
println("Analysing <src>...");
rel[str,str] layers = detectLayers(g);
if(!isEmpty(layers))
println("Found <layers>");
}
}

0 comments on commit f974d30

Please sign in to comment.