Skip to content

Commit

Permalink
subgrammar analysis now allows for multiple roots (and uses BGF frame…
Browse files Browse the repository at this point in the history
…work)

git-svn-id: https://slps.svn.sourceforge.net/svnroot/slps@938 ab42f6e0-554d-0410-b580-99e487e6eeb2
  • Loading branch information
grammarware committed Feb 3, 2011
1 parent 59ee138 commit 5fa220b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 35 deletions.
6 changes: 3 additions & 3 deletions shared/tools/subgrammar
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ cd ../..
SLPS=${PWD}
cd ${LOCAL}

if [ $# -ne 3 ]; then
if [ $# -lt 3 ]; then
echo "This tool extracts a portion of a grammar that starts at a given root nonterminal and includes all referenced nonterminals as well."
echo "Usage: subgrammar <bgf-input> <new-root> <bgf-output>"
echo "Usage: subgrammar <bgf-input> <new-root> [<new-root>...] <bgf-output>"
exit 1
elif [ ! -r $1 ]; then
echo "Oops: $1 not found or not readable."
exit 1
fi

${SLPS}/topics/presentation/subgrammar/subgrammar.py $1 $2 $3
${SLPS}/topics/investigation/subgrammar/subgrammar.py $@
61 changes: 29 additions & 32 deletions topics/investigation/subgrammar/subgrammar.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
#!/usr/local/bin/python
import os
import sys
sys.path.append('../../../shared/python')
import slpsns
import os,sys
import elementtree.ElementTree as ET
sys.path.append(os.getcwd().split('slps')[0]+'slps/shared/python')
import BGF
import metrics

names = []
processed = []

def addProductionsOf(r,cg,src,tgt):
if r in processed:
return
for p in src.prods:
if p.nt == r:
tgt.addProd(p)
processed.append(r)
for n in cg[r]:
addProductionsOf(n,cg,src,tgt)
return

if __name__ == "__main__":
if len(sys.argv) != 4:
if len(sys.argv) < 4:
print 'This tool extracts a portion of a grammar that starts at a given root nonterminal and includes all referenced nonterminals as well.'
print 'Usage:'
print ' subgrammar <bgf-input> <new-root> <bgf-output>'
print ' subgrammar <bgf-input> <new-root> [<new-root>...] <bgf-output>'
sys.exit(1)
slpsns.init(ET)
bgf = ET.parse(sys.argv[1])
grammar = {}
for prod in bgf.findall('//'+slpsns.bgf_('production')):
nt = prod.findtext('nonterminal')
if nt in grammar.keys():
grammar[nt].append(prod)
else:
grammar[nt]=[prod]
newBgf = ET.Element(slpsns.bgf_('grammar'))
nts = [sys.argv[2]]
ET.SubElement(newBgf,'root').text = nts[0]
oldnts = []
while nts:
if nts[0] not in grammar.keys():
print 'Nonterminal not found:',nts[0]
else:
for prod in grammar[nts[0]]:
newBgf.append(prod)
for nt in prod.findall('.//nonterminal'):
if (nt.text not in oldnts) and (nt.text not in nts):
nts.append(nt.text)
oldnts.append(nts[0]) # car
nts = nts[1:] # cdr
ET.ElementTree(newBgf).write(sys.argv[3])
bgf = BGF.Grammar()
newBgf = BGF.Grammar()
bgf.parse(sys.argv[1])
roots = sys.argv[2:-1]
print 'Setting root(s) to',roots
for r in roots:
newBgf.addRoot(r)
cg = metrics.getCallGraph(bgf)
for r in roots:
addProductionsOf(r,cg,bgf,newBgf)
ET.ElementTree(newBgf.getXml()).write(sys.argv[-1])
sys.exit(0)

0 comments on commit 5fa220b

Please sign in to comment.