Skip to content

Commit

Permalink
XBGF transformation framework redone for Rascal: first setup
Browse files Browse the repository at this point in the history
  • Loading branch information
grammarware committed Jun 1, 2012
1 parent 8953e20 commit c9c1433
Show file tree
Hide file tree
Showing 8 changed files with 716 additions and 6 deletions.
16 changes: 16 additions & 0 deletions shared/rascal/Makefile
@@ -0,0 +1,16 @@
all:
echo "Use Rascal"

build:
make clean
cp ../../topics/convergence/xbgf/*/xbgf/*.xbgf tests/xbgf/
cp ../../topics/transformation/xbgf/tests/*.xbgf tests/xbgf/

check:
echo "Validating input files..."
ls -1 tests/xbgf/*.xbgf | xargs -n1 ../tools/validate xbgf
echo "Validating output files..."
ls -1 res/xbgf-res/*.xbgf | xargs -n1 ../tools/validate xbgf

clean:
rm -f tests/*/*.xbgf
19 changes: 13 additions & 6 deletions shared/rascal/src/Main.rsc
@@ -1,25 +1,33 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module Main

import String;
import IO;
import syntax::BGF;
import syntax::XBGF;
import io::ReadBGF;
import io::ReadXBGF;
import io::WriteXBGF;
import transform::XBGF;
//import lang::xml::DOM;

public void main()
{
XBGFSequence x;
x = readXBGF(|project://slps/tests/long.xbgf|);
iprintln(x);
writeXBGF(x,|project://slps/res/long.xbgf|);
x = readXBGF(|project://slps/tests/all/abridge.xbgf|);
println(x);
g1 = readBGF(|project://slps/tests/all/abridge.bgf|);
g2 = readBGF(|project://slps/tests/all/abridge.baseline|);
//writeXBGF(x,|project://slps/tests/all/abridge.bgf|);
g3 = transform(x,g1);
println(g3);
println(g2);
}

public bool tryAll()
{
loc base = |project://slps/tests|;
loc outp = |project://slps/res|;
loc base = |project://slps/tests/xbgf|;
loc outp = |project://slps/tests/xbgf-res|;
for (f <- listEntries(base))
{
if (f == ".gitignore") continue;
Expand All @@ -28,4 +36,3 @@ public bool tryAll()
}
return true;
}

37 changes: 37 additions & 0 deletions shared/rascal/src/Sync.rsc
@@ -0,0 +1,37 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
@doc{This simple program reads is the whole test suite for XBGF from the corresponding SLPS directory
and generates a Rascal file that contains identically behaving test functions which can be invoked with
:test
command.}
module Sync

import String;
import IO;
import io::ReadBGF;
import io::ReadXBGF;

public void main()
{
loc base = |home:///projects/slps/topics/transformation/xbgf/tests|;
int cx = 1;
str buffer = "";
for (f <- listEntries(base), endsWith(f,".xbgf"))
{
xbgf = readXBGF(base+f);
bgf = readBGF(base+replaceLast(f,".xbgf",".bgf"));
bl = readBGF(base+replaceLast(f,".xbgf",".baseline"));
buffer += "test bool x<cx>() { return transform(<xbgf>,<bgf>)==<bl>; }\n";
cx+=1;
}
writeFile(|project://slps/src/transform/XBGFTest.rsc|,
"@contributor{Super Awesome Automated XBGF Test Suite Synchroniser}
'@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
'module transform::XBGFTest
'
'import syntax::BGF;
'import syntax::XBGF;
'import transform::XBGF;
'
'"+replaceAll(buffer,"import","\\import"));
}

55 changes: 55 additions & 0 deletions shared/rascal/src/normal/BGF.rsc
@@ -0,0 +1,55 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module normal::BGF

import syntax::BGF;

public BGFGrammar normalise(grammar (list[str] roots, list[BGFProduction] prods))
= grammar (roots, [normalise(p) | p <- prods]);

public BGFProduction normalise(production (str label, str lhs, BGFExpression rhs))
= production (label, lhs, normalise(rhs));

public BGFExpression normalise(BGFExpression e)
{
// Algebraic normalisations; TODO: others needed?
return innermost visit(e)
{
case sequence([]) => epsilon()
case sequence([BGFExpression e]) => e
case choice([]) => empty()
case choice([BGFExpression e]) => e
case optional(epsilon()) => epsilon()
case plus(epsilon()) => epsilon()
case star(epsilon()) => epsilon()
case sequence([*L1,sequence(L),*L2]) => sequence(L1+L+L2)
case sequence([*L1,epsilon(),*L2]) => sequence(L1+L2)
case choice([*L1,choice(L),*L2]) => choice(L1+L+L2)
case choice([*L1,X1,*L2,X1,*L3]) => choice([*L1,X1,*L2,*L3])
};
}

public BGFGrammar tw(BGFExpression e)
{
return grammar([],[production("","foo",e)]);
}

test bool n1() {return normalise(tw(sequence([])))==tw(epsilon());}
test bool n2() {return normalise(tw(sequence([terminal("1")])))==tw(terminal("1"));}
test bool n3() {return normalise(tw(choice([])))==tw(empty());}
test bool n4() {return normalise(tw(choice([terminal("1")])))==tw(terminal("1"));}
test bool n5() {return normalise(tw(optional(epsilon())))==tw(epsilon());}
test bool n6() {return normalise(tw(plus(epsilon())))==tw(epsilon());}
test bool n7() {return normalise(tw(star(epsilon())))==tw(epsilon());}
test bool n8() {return normalise(tw(sequence([terminal("1"),sequence([terminal("2"),terminal("3")])])))
==tw(sequence([terminal("1"),terminal("2"),terminal("3")]));}
test bool n9() {return normalise(tw(sequence([sequence([terminal("1"),terminal("2")]),terminal("3")])))
==tw(sequence([terminal("1"),terminal("2"),terminal("3")]));}
test bool n10(){return normalise(tw(sequence([terminal("1"),epsilon(),terminal("2"),epsilon()])))
==tw(sequence([terminal("1"),terminal("2")]));}
test bool n11(){return normalise(tw(choice([terminal("1"),choice([terminal("2"),terminal("3")])])))
==tw(choice([terminal("1"),terminal("2"),terminal("3")]));}
test bool n12(){return normalise(tw(choice([choice([terminal("1"),terminal("2")]),terminal("3")])))
==tw(choice([terminal("1"),terminal("2"),terminal("3")]));}
test bool n13(){return normalise(tw(choice([terminal("1"),terminal("2"),terminal("2"),terminal("3")])))
==tw(choice([terminal("1"),terminal("2"),terminal("3")]));}

0 comments on commit c9c1433

Please sign in to comment.