Skip to content

Commit

Permalink
yet another tool (very straightforward behaviour for now: actually mo…
Browse files Browse the repository at this point in the history
…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
grammarware committed Jun 29, 2011
1 parent d55aee9 commit 5f27523
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
15 changes: 15 additions & 0 deletions shared/tools/mergebgf
@@ -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
40 changes: 40 additions & 0 deletions topics/transformation/merge/merge.py
@@ -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])

0 comments on commit 5f27523

Please sign in to comment.