Permalink
Browse files

a tool for presenting a subgrammar; complete with a test set

git-svn-id: https://slps.svn.sourceforge.net/svnroot/slps@890 ab42f6e0-554d-0410-b580-99e487e6eeb2
  • Loading branch information...
grammarware committed Dec 7, 2010
1 parent ead2fbb commit f874a2b1b7613795a114550e8bd424cd3c1bde65
@@ -0,0 +1,11 @@
test:
../../../shared/tools/xsd2bgf ../../fl/xsd/fl.xsd fl.bgf
echo 'Executing test cases...'
ls -1 tests/ | xargs -n1 -I _ basename _ .baseline.bgf | xargs -n1 -I _ ./subgrammar.py fl.bgf _ fl._.bgf
echo 'Syntax checking test results...'
ls -1 *.bgf | xargs -n1 checkxml bgf
echo 'Validating test results...'
ls -1 tests/ | xargs -n1 -I _ basename _ .baseline.bgf | xargs -n1 -I _ ../../../shared/tools/gdt fl._.bgf tests/_.baseline.bgf
clean:
rm -f *.bgf
@@ -0,0 +1,38 @@
#!/usr/local/bin/python
import os
import sys
sys.path.append('../../../shared/python')
import slpsns
import elementtree.ElementTree as ET
names = []
if __name__ == "__main__":
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>'
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:
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])
sys.exit(0)
@@ -0,0 +1,168 @@
<?xml version="1.0"?>
<bgf:grammar xmlns:bgf="http://planet-sl.org/bgf">
<root>Apply</root>
<bgf:production>
<nonterminal>Apply</nonterminal>
<bgf:expression>
<sequence>
<bgf:expression>
<selectable>
<selector>name</selector>
<bgf:expression>
<value>string</value>
</bgf:expression>
</selectable>
</bgf:expression>
<bgf:expression>
<plus>
<bgf:expression>
<selectable>
<selector>arg</selector>
<bgf:expression>
<nonterminal>Expr</nonterminal>
</bgf:expression>
</selectable>
</bgf:expression>
</plus>
</bgf:expression>
</sequence>
</bgf:expression>
</bgf:production>
<bgf:production>
<nonterminal>Expr</nonterminal>
<bgf:expression>
<choice>
<bgf:expression>
<nonterminal>Literal</nonterminal>
</bgf:expression>
<bgf:expression>
<nonterminal>Argument</nonterminal>
</bgf:expression>
<bgf:expression>
<nonterminal>Binary</nonterminal>
</bgf:expression>
<bgf:expression>
<nonterminal>IfThenElse</nonterminal>
</bgf:expression>
<bgf:expression>
<nonterminal>Apply</nonterminal>
</bgf:expression>
</choice>
</bgf:expression>
</bgf:production>
<bgf:production>
<nonterminal>Literal</nonterminal>
<bgf:expression>
<selectable>
<selector>info</selector>
<bgf:expression>
<value>int</value>
</bgf:expression>
</selectable>
</bgf:expression>
</bgf:production>
<bgf:production>
<nonterminal>Argument</nonterminal>
<bgf:expression>
<selectable>
<selector>name</selector>
<bgf:expression>
<value>string</value>
</bgf:expression>
</selectable>
</bgf:expression>
</bgf:production>
<bgf:production>
<nonterminal>Binary</nonterminal>
<bgf:expression>
<sequence>
<bgf:expression>
<selectable>
<selector>ops</selector>
<bgf:expression>
<nonterminal>Ops</nonterminal>
</bgf:expression>
</selectable>
</bgf:expression>
<bgf:expression>
<selectable>
<selector>left</selector>
<bgf:expression>
<nonterminal>Expr</nonterminal>
</bgf:expression>
</selectable>
</bgf:expression>
<bgf:expression>
<selectable>
<selector>right</selector>
<bgf:expression>
<nonterminal>Expr</nonterminal>
</bgf:expression>
</selectable>
</bgf:expression>
</sequence>
</bgf:expression>
</bgf:production>
<bgf:production>
<nonterminal>IfThenElse</nonterminal>
<bgf:expression>
<sequence>
<bgf:expression>
<selectable>
<selector>ifExpr</selector>
<bgf:expression>
<nonterminal>Expr</nonterminal>
</bgf:expression>
</selectable>
</bgf:expression>
<bgf:expression>
<selectable>
<selector>thenExpr</selector>
<bgf:expression>
<nonterminal>Expr</nonterminal>
</bgf:expression>
</selectable>
</bgf:expression>
<bgf:expression>
<selectable>
<selector>elseExpr</selector>
<bgf:expression>
<nonterminal>Expr</nonterminal>
</bgf:expression>
</selectable>
</bgf:expression>
</sequence>
</bgf:expression>
</bgf:production>
<bgf:production>
<nonterminal>Ops</nonterminal>
<bgf:expression>
<choice>
<bgf:expression>
<selectable>
<selector>Equal</selector>
<bgf:expression>
<epsilon/>
</bgf:expression>
</selectable>
</bgf:expression>
<bgf:expression>
<selectable>
<selector>Plus</selector>
<bgf:expression>
<epsilon/>
</bgf:expression>
</selectable>
</bgf:expression>
<bgf:expression>
<selectable>
<selector>Minus</selector>
<bgf:expression>
<epsilon/>
</bgf:expression>
</selectable>
</bgf:expression>
</choice>
</bgf:expression>
</bgf:production>
</bgf:grammar>
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<bgf:grammar xmlns:bgf="http://planet-sl.org/bgf">
<root>Argument</root>
<bgf:production>
<nonterminal>Argument</nonterminal>
<bgf:expression>
<selectable>
<selector>name</selector>
<bgf:expression>
<value>string</value>
</bgf:expression>
</selectable>
</bgf:expression>
</bgf:production>
</bgf:grammar>
Oops, something went wrong.

0 comments on commit f874a2b

Please sign in to comment.