Skip to content

Commit

Permalink
Awesome grammar extractor from Rascal ADTs
Browse files Browse the repository at this point in the history
  • Loading branch information
grammarware committed Jun 14, 2012
1 parent 66b0200 commit d724f83
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 10 deletions.
63 changes: 63 additions & 0 deletions shared/rascal/src/extract/RascalADT2BGF.rsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module extract::RascalADT2BGF

import IO;
import List;
import String;
import syntax::BGF;
import normal::BGF;
import io::WriteBGF;

layout LO = [\ \t\n\r]* !>> [\ \t\n\r];
syntax DataDef = Name "=" {DataExpr "|"}+ ";";
lexical Name = [a-zA-Z_0-9]+ !>> [a-zA-Z_0-9] ;
syntax DataExpr
= Name "(" {DataExpr ","}* ")"
| Name "[" DataExpr "]" Name?
| Name Name?
;

public void main(list[str] args)
{
writeBGF(process(|cwd:///|+args[0]),|cwd:///|+args[1]);
println("Extraction completed.");
}

public void main()
{
iprintln(process(|project://fl/src/Abstract.rsc|));
}

BGFGrammar process(loc src)
{
str gs = readFile(src);
defs = split("data ",gs);
list[BGFProduction] ps = [];
for (d <- slice(defs,1,size(defs)-1))
{
str name = trim(split("=",d)[0]);
println("Parsing <name>...");
ps += def2prod(parse(#DataDef,trim(d)));
}
return normalise(syntax::BGF::grammar([],ps));
}
list[BGFProduction] def2prod((DataDef)`<Name n> = <DataExpr d> ;`)
= [production("","<n>",expr2expr(d))];
default list[BGFProduction] def2prod((DataDef)`<Name n> = <{DataExpr "|"}+ ds> ;`)
= [production("","<n>",expr2expr(d)) | d <- ds];
BGFExpression type2expr((Name)`str`) = val(string());
BGFExpression type2expr((Name)`int`) = val(integer());
default BGFExpression type2expr(Name n) = nonterminal("<n>");
BGFExpression expr2expr((DataExpr)`<Name t>`) = nonterminal("<t>");
BGFExpression expr2expr((DataExpr)`<Name t><Name n>`) = selectable("<n>",type2expr(t));
BGFExpression expr2expr((DataExpr)`<Name n>()`) = selectable("<n>",epsilon());
BGFExpression expr2expr((DataExpr)`<Name n>(<DataExpr e>)`) = selectable("<n>",expr2expr(e));
BGFExpression expr2expr((DataExpr)`<Name n>(<{DataExpr ","}* es>)`) = selectable("<n>",sequence([expr2expr(e) | e <- es]));
BGFExpression expr2expr((DataExpr)`list[<DataExpr e>]`) = star(expr2expr(e));
BGFExpression expr2expr((DataExpr)`list[<DataExpr e>] <Name n>`) = selectable("<n>",star(expr2expr(e)));
BGFExpression expr2expr((DataExpr)`set[<DataExpr e>]`) = star(expr2expr(e));
BGFExpression expr2expr((DataExpr)`set[<DataExpr e>] <Name n>`) = selectable("<n>",star(expr2expr(e)));
default BGFExpression expr2expr(DataExpr e) = terminal("UNK<e>");
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module extract::Rascal2BGF
module extract::RascalSyntax2BGF

import lang::rascal::\syntax::RascalRascal;
import lang::rascal::grammar::definition::Modules;
Expand All @@ -20,7 +20,7 @@ public void main(list[str] args)
println("Extraction completed.");
}

public void mn()
public void main()
{
Module pt = parse(#Module,|project://fl/src/Concrete.rsc|);
Grammar g = modules2grammar("Concrete", {pt});
Expand Down
5 changes: 4 additions & 1 deletion shared/rascal/src/normal/BGF.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ public BGFGrammar normalise(grammar (list[str] roots, list[BGFProduction] prods)
public list[BGFProduction] normalise(list[BGFProduction] prods)
= [normalise(p) | p <- prods];
public BGFProduction normalise(production (str label, str lhs, BGFExpression rhs))
public BGFProduction normalise(production ("", str lhs, selectable(str label,BGFExpression rhs)))
= production (label, lhs, normalise(rhs));
public default BGFProduction normalise(production (str label, str lhs, BGFExpression rhs))
= production (label, lhs, normalise(rhs));
public BGFExpression normalise(BGFExpression e)
Expand Down
2 changes: 1 addition & 1 deletion shared/rascal/src/syntax/BGF.rsc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module syntax::BGF
module \syntax::BGF

data BGFGrammar =
grammar (list[str] roots, list[BGFProduction] prods)
Expand Down
4 changes: 2 additions & 2 deletions shared/rascal/src/syntax/XBGF.rsc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module syntax::XBGF
module \syntax::XBGF

import syntax::BGF;
import \syntax::BGF;

alias XBGFSequence = list[XBGFCommand];

Expand Down
21 changes: 21 additions & 0 deletions shared/tools/rscd2bgf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh

LOCAL=${PWD}
cd `dirname $0`
cd ../..
SLPS=${PWD}
cd ${LOCAL}

if [ $# -ne 2 ]; then
echo "This tool extracts a BGF grammar from a Rascal abstract data type."
echo "Usage: rscs2rsc <input-src> <output-bgf>"
exit 1
elif [ ! -r $1 ]; then
echo "Oops: $1 not found or not readable."
exit 1
fi

cp $1 ${SLPS}/shared/rascal/src/src.rsc
cd ${SLPS}/shared/rascal/src && java -Xmx1G -Xss32m -jar ${SLPS}/download/rascal.jar extract::RascalADT2BGF src.rsc tgt.bgf
mv ${SLPS}/shared/rascal/src/tgt.bgf ${LOCAL}/$2
rm ${SLPS}/shared/rascal/src/src.rsc
2 changes: 1 addition & 1 deletion shared/tools/rscs2bgf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ elif [ ! -r $1 ]; then
fi

cp $1 ${SLPS}/shared/rascal/src/src.rsc
cd ${SLPS}/shared/rascal/src && java -Xmx1G -Xss32m -jar ${SLPS}/download/rascal.jar extract::Rascal2BGF src.rsc tgt.bgf
cd ${SLPS}/shared/rascal/src && java -Xmx1G -Xss32m -jar ${SLPS}/download/rascal.jar extract::RascalSyntax2BGF src.rsc tgt.bgf
mv ${SLPS}/shared/rascal/src/tgt.bgf ${LOCAL}/$2
rm ${SLPS}/shared/rascal/src/src.rsc
5 changes: 4 additions & 1 deletion topics/extraction/rascal/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
all:

test:
../../../shared/tools/rscs2bgf ../../fl/rascal/src/Concrete.rsc rsc.bgf
../../../shared/tools/rscs2bgf ../../fl/rascal/src/Concrete.rsc rsc1.bgf
../../../shared/tools/rscd2bgf ../../fl/rascal/src/Abstract.rsc rsc2.bgf
../../../shared/tools/rsc2bgf ../../fl/rascal/src/Concrete.rsc rsc3.bgf
ls -1 *.bgf | xargs -n1 ../../../shared/tools/validate bgf

clean:
rm -f *.bgf *.bnf *.html
5 changes: 3 additions & 2 deletions topics/fl/rascal/src/Abstract.rsc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module Abstract

data FLPrg = prg(list[FLFun]);
data FLPrg = prg(list[FLFun] fs);
data FLFun = fun(str f, list[str] args, FLExpr body);
data FLExpr
= binary(FLExpr e1, FLOp op, FLExpr e2)
Expand All @@ -10,4 +10,5 @@ data FLExpr
| argument(str a)
| literal(int i)
;
data FLOp = minus() | plus() | equal();
data FLOp = minus() | plus() | equal();

0 comments on commit d724f83

Please sign in to comment.