-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Awesome grammar extractor from Rascal ADTs
- Loading branch information
1 parent
66b0200
commit d724f83
Showing
9 changed files
with
101 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters