Skip to content

Commit

Permalink
smarter extractor ⇒ easier convergence
Browse files Browse the repository at this point in the history
git-svn-id: https://slps.svn.sourceforge.net/svnroot/slps@1159 ab42f6e0-554d-0410-b580-99e487e6eeb2
  • Loading branch information
grammarware committed Jan 31, 2012
1 parent f0b8fd4 commit 5ca8708
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 142 deletions.
198 changes: 98 additions & 100 deletions topics/convergence/fl/xbgf/unerase.xbgf
Original file line number Diff line number Diff line change
@@ -1,101 +1,99 @@
<xbgf:sequence
xmlns:bgf="http://planet-sl.org/bgf"
xmlns:xbgf="http://planet-sl.org/xbgf">

<xbgf:narrow>
<bgf:expression>
<any/>
</bgf:expression>
<bgf:expression>
<nonterminal>function</nonterminal>
</bgf:expression>
<in>
<nonterminal>program</nonterminal>
</in>
</xbgf:narrow>

<xbgf:narrow>
<bgf:expression>
<star>
<bgf:expression>
<nonterminal>function</nonterminal>
</bgf:expression>
</star>
</bgf:expression>
<bgf:expression>
<plus>
<bgf:expression>
<nonterminal>function</nonterminal>
</bgf:expression>
</plus>
</bgf:expression>
<in>
<nonterminal>program</nonterminal>
</in>
</xbgf:narrow>

<xbgf:narrow>
<bgf:expression>
<any/>
</bgf:expression>
<bgf:expression>
<value>string</value>
</bgf:expression>
<in>
<nonterminal>function</nonterminal>
</in>
</xbgf:narrow>

<xbgf:narrow>
<bgf:expression>
<star>
<bgf:expression>
<value>string</value>
</bgf:expression>
</star>
</bgf:expression>
<bgf:expression>
<plus>
<bgf:expression>
<value>string</value>
</bgf:expression>
</plus>
</bgf:expression>
<in>
<nonterminal>function</nonterminal>
</in>
</xbgf:narrow>

<xbgf:narrow>
<bgf:expression>
<any/>
</bgf:expression>
<bgf:expression>
<nonterminal>expr</nonterminal>
</bgf:expression>
<in>
<nonterminal>apply</nonterminal>
</in>
</xbgf:narrow>

<xbgf:narrow>
<bgf:expression>
<star>
<bgf:expression>
<nonterminal>expr</nonterminal>
</bgf:expression>
</star>
</bgf:expression>
<bgf:expression>
<plus>
<bgf:expression>
<nonterminal>expr</nonterminal>
</bgf:expression>
</plus>
</bgf:expression>
<in>
<nonterminal>apply</nonterminal>
</in>
</xbgf:narrow>

<?xml version="1.0"?>
<xbgf:sequence xmlns:bgf="http://planet-sl.org/bgf" xmlns:xbgf="http://planet-sl.org/xbgf">
<!-- legacy of the old and stupid java2bgf extractor
<xbgf:narrow>
<bgf:expression>
<any/>
</bgf:expression>
<bgf:expression>
<nonterminal>function</nonterminal>
</bgf:expression>
<in>
<nonterminal>program</nonterminal>
</in>
</xbgf:narrow>
-->
<xbgf:narrow>
<bgf:expression>
<star>
<bgf:expression>
<nonterminal>function</nonterminal>
</bgf:expression>
</star>
</bgf:expression>
<bgf:expression>
<plus>
<bgf:expression>
<nonterminal>function</nonterminal>
</bgf:expression>
</plus>
</bgf:expression>
<in>
<nonterminal>program</nonterminal>
</in>
</xbgf:narrow>
<!-- legacy of the old and stupid java2bgf extractor
<xbgf:narrow>
<bgf:expression>
<any/>
</bgf:expression>
<bgf:expression>
<value>string</value>
</bgf:expression>
<in>
<nonterminal>function</nonterminal>
</in>
</xbgf:narrow>
-->
<xbgf:narrow>
<bgf:expression>
<star>
<bgf:expression>
<value>string</value>
</bgf:expression>
</star>
</bgf:expression>
<bgf:expression>
<plus>
<bgf:expression>
<value>string</value>
</bgf:expression>
</plus>
</bgf:expression>
<in>
<nonterminal>function</nonterminal>
</in>
</xbgf:narrow>
<!-- legacy of the old and stupid java2bgf extractor
<xbgf:narrow>
<bgf:expression>
<any/>
</bgf:expression>
<bgf:expression>
<nonterminal>expr</nonterminal>
</bgf:expression>
<in>
<nonterminal>apply</nonterminal>
</in>
</xbgf:narrow>
-->
<xbgf:narrow>
<bgf:expression>
<star>
<bgf:expression>
<nonterminal>expr</nonterminal>
</bgf:expression>
</star>
</bgf:expression>
<bgf:expression>
<plus>
<bgf:expression>
<nonterminal>expr</nonterminal>
</bgf:expression>
</plus>
</bgf:expression>
<in>
<nonterminal>apply</nonterminal>
</in>
</xbgf:narrow>
</xbgf:sequence>
98 changes: 56 additions & 42 deletions topics/extraction/java/slps/java2bgf/Tool.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,33 @@ public class Tool {

private static Document doc;
private static Element root;

// Adds a regular nonterminal or a built-in nonterminal depending on the class
// Detects only String and int
public static void addNT(Class<?> c, Element expr)
{
if (c == int.class)
{
Element v = doc.createElement("value");
expr.appendChild(v);
v.appendChild(doc.createTextNode("int"));
}
else if (c == String.class)
{
Element v = doc.createElement("value");
expr.appendChild(v);
v.appendChild(doc.createTextNode("string"));
}
else
{
Element nt = doc.createElement("nonterminal");
expr.appendChild(nt);
nt.appendChild(doc.createTextNode(c.getSimpleName()));
}
}

public static void main(String[] args) throws Exception {
public static void main(String[] args) throws Exception
{

String dirname = args[0]; // the basedir for the classes to be scanned
String pkgname = args[1]; // the package holding the relevant classes
Expand All @@ -35,20 +60,23 @@ public static void main(String[] args) throws Exception {
String pkgdirname = dirname + File.separatorChar + pkgname;
File pkgdir = new File(pkgdirname);
if (!pkgdir.exists())
throw new RuntimeException("Can't find package directory "
+ pkgdirname + "!");
throw new RuntimeException("Can't find package directory " + pkgdirname + "!");
Collection<Class<?>> classes = new LinkedList<Class<?>>();
for (String f : pkgdir.list()) {
for (String f : pkgdir.list())
{
int len = f.length();
if (len > 5 && f.substring(len - 6).equals(".class")) {
if (len > 5 && f.substring(len - 6).equals(".class"))
{
String classname = pkgname + "." + f.substring(0, len - 6);
try {
try
{
Class<?> clss = Class.forName(classname);
System.out.println("Loaded " + clss.getName() + ".");
classes.add(clss);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Can't load class " + classname
+ "!");
}
catch (ClassNotFoundException e)
{
throw new RuntimeException("Can't load class " + classname + "!");
}
}
}
Expand All @@ -57,7 +85,8 @@ public static void main(String[] args) throws Exception {
throw new RuntimeException("No classes found!");

// Generate one nonterminal per class
for (Class<?> clss : classes) {
for (Class<?> clss : classes)
{
Element rule = doc.createElement("bgf:production");
Element nonterminal = doc.createElement("nonterminal");
Element rhs = doc.createElement("bgf:expression");
Expand All @@ -71,10 +100,12 @@ public static void main(String[] args) throws Exception {
String compositor;
String unit;

if (clss.isEnum()) {
if (clss.isEnum())
{
compositor = "choice";
unit = "empty";
for (Object c : clss.getEnumConstants()) {
for (Object c : clss.getEnumConstants())
{
Element selectable = doc.createElement("selectable");
tmp.add(selectable);
Element selector = doc.createElement("selector");
Expand Down Expand Up @@ -111,9 +142,7 @@ public static void main(String[] args) throws Exception {
for (Field f : clss.getFields())
{
if (List.class.isAssignableFrom(f.getType()))
{
fs.add(new Feature(f.getName(),f.getType(),(Class<?>)((ParameterizedType)f.getGenericType()).getActualTypeArguments()[0]));
}
else
fs.add(new Feature(f.getName(),f.getType()));
}
Expand All @@ -129,54 +158,36 @@ public static void main(String[] args) throws Exception {
fs.add(new Feature(m.getName().substring(3),m.getReturnType()));
}

for (Feature f : fs) {
for (Feature f : fs)
{
Element selectable = doc.createElement("selectable");
tmp.add(selectable);
Element selector = doc.createElement("selector");
selectable.appendChild(selector);
selector.appendChild(doc.createTextNode(f.getName()));
Element expr = doc.createElement("bgf:expression");
selectable.appendChild(expr);
if (f.getType() == LinkedList.class || f.getType() == List.class)
if (List.class.isAssignableFrom(f.getType()))
{
Element coll = doc.createElement("star");
expr.appendChild(coll);
Element inner = doc.createElement("bgf:expression");
coll.appendChild(inner);
if (f.getInnerType() != null)
{
Element nt = doc.createElement("nonterminal");
inner.appendChild(nt);
nt.appendChild(doc.createTextNode(f.getInnerType().getSimpleName()));
}
addNT(f.getInnerType(),inner);
else
inner.appendChild(doc.createElement("any"));
}
else if (f.getType() == int.class)
{
Element v = doc.createElement("value");
expr.appendChild(v);
v.appendChild(doc.createTextNode("int"));
}
else if (f.getType() == String.class)
{
Element v = doc.createElement("value");
expr.appendChild(v);
v.appendChild(doc.createTextNode("string"));
}
else
{
Element nt = doc.createElement("nonterminal");
expr.appendChild(nt);
nt.appendChild(doc.createTextNode(f.getType().getSimpleName()));
}
addNT(f.getType(),expr);
}
}
if (tmp.size() == 0)
rhs.appendChild(doc.createElement(unit));
else if (tmp.size() == 1)
rhs.appendChild(tmp.iterator().next());
else {
else
{
Element sequence = doc.createElement(compositor);
rhs.appendChild(sequence);
for (Element e : tmp) {
Expand All @@ -196,16 +207,19 @@ else if (tmp.size() == 1)

}

static class Feature {
static class Feature
{
private String name;
private Class<?> type;
private Class<?> intype;
public Feature(String name, Class<?> type) {
public Feature(String name, Class<?> type)
{
this.name = name;
this.type = type;
this.intype = null;
}
public Feature(String name, Class<?> type, Class<?> intype) {
public Feature(String name, Class<?> type, Class<?> intype)
{
this.name = name;
this.type = type;
this.intype = intype;
Expand Down
1 change: 1 addition & 0 deletions topics/mutation/anf/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
test:
cp ../../convergence/fl/snapshot/*.bgf tests/
rsc2bgf ../../fl/rascal1/FL.rsc tests/rascal.bgf
ls -1 tests/*.bgf | xargs -n1 ./testperform
rm normal/*.almost.bgf

Expand Down

0 comments on commit 5ca8708

Please sign in to comment.