Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
yet another tool (very straightforward behaviour for now: actually mo…
…r of "concatenate" than of "merge" kind); will improve later git-svn-id: https://slps.svn.sourceforge.net/svnroot/slps@1100 ab42f6e0-554d-0410-b580-99e487e6eeb2
- Loading branch information
1 parent
d55aee9
commit 5f27523
Showing
2 changed files
with
55 additions
and
0 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,15 @@ | ||
#! /bin/sh | ||
|
||
# Get our hands on basedir | ||
LOCAL=${PWD} | ||
cd `dirname $0` | ||
cd ../.. | ||
SLPS=${PWD} | ||
cd ${LOCAL} | ||
|
||
if [ $# -lt 3 ]; then | ||
echo "Usage: mergebgf <BGF-input> [<BGF-input>...] <BGF-output>" | ||
exit 1 | ||
else | ||
${SLPS}/topics/transformation/merge/merge.py $* | ||
fi |
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,40 @@ | ||
#!/Library/Frameworks/Python.framework/Versions/3.1/bin/python3 | ||
# -*- coding: utf-8 -*- | ||
import os, sys | ||
import xml.etree.ElementTree as ET | ||
sys.path.append(os.getcwd().split('slps')[0]+'slps/shared/python') | ||
import BGF3 | ||
from functools import reduce | ||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) < 4: | ||
print('Usage:') | ||
print(' merge.py input1.bgf [inputX.bgf ...] output.bgf') | ||
sys.exit(-1) | ||
bgfs = [] | ||
names = [] | ||
nts = [] | ||
for fname in sys.argv[1:-1]: | ||
g = BGF3.Grammar() | ||
g.parse(fname) | ||
bgfs.append(g) | ||
names.append(fname) | ||
print(len(bgfs),'grammars read.') | ||
for i in range(0,len(bgfs)): | ||
n = [] | ||
for p in bgfs[i].prods: | ||
if p.nt not in n: | ||
n.append(p.nt) | ||
nts.append(n) | ||
print('\t',names[i],'has',len(bgfs[i].prods),'productions,',len(nts[i]),'nonterminals and',len(bgfs[i].roots),'roots') | ||
# FINAL STEP: compose BGF | ||
allbgf = BGF3.Grammar() | ||
# roots are combined | ||
for g in bgfs: | ||
for r in g.roots: | ||
allbgf.addRoot(r) | ||
# TODO: write a real merge, not just concatenate | ||
for p in g.prods: | ||
allbgf.addProd(p) | ||
ET.ElementTree(allbgf.getXml()).write(sys.argv[-1]) | ||
print('Merged BGF written to',sys.argv[-1]) |