예제 #1
0
파일: mcc.py 프로젝트: siwiwit/slps
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import os,sys
sys.path.append(os.getcwd().split('slps')[0]+'slps/shared/python')
import BGF
import metrics

if __name__ == "__main__":
	if len(sys.argv) != 2:
		print 'This tool calculates an MCC metric for any given BGF grammar.'
		print 'Usage:'
		print '      '+sys.argv[0]+' <bgf-input>'
		sys.exit(1)
	bgf = BGF.Grammar()
	bgf.parse(sys.argv[1])
	print metrics.MCC(bgf)
	sys.exit(0)
예제 #2
0
파일: baby.py 프로젝트: siwiwit/slps
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import elementtree.ElementTree as ET
import os, sys
sys.path.append(os.getcwd().split('slps')[0] + 'slps/shared/python')
import BGF

if __name__ == "__main__":
    g = BGF.Grammar()
    g.addRoot('foo')
    p = BGF.Production()
    p.setNT('foo')
    t = BGF.Terminal()
    t.setName('bar')
    p.setExpr(BGF.Expression(t))
    g.addProd(p)
    ET.ElementTree(g.getXml()).write('baby.bgf')
예제 #3
0
파일: monster.py 프로젝트: siwiwit/slps
def ntd(x, d):
    nt = BGF.Nonterminal()
    nt.setName(x)
    w = d()
    w.setExpr(BGF.Expression(nt))
    return BGF.Expression(w)
예제 #4
0
파일: monster.py 프로젝트: siwiwit/slps
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import elementtree.ElementTree as ET
import os, sys

sys.path.append(os.getcwd().split('slps')[0] + 'slps/shared/python')
import BGF

singles = (BGF.Empty(), BGF.Epsilon(), BGF.Any())


def ntd(x, d):
    nt = BGF.Nonterminal()
    nt.setName(x)
    w = d()
    w.setExpr(BGF.Expression(nt))
    return BGF.Expression(w)


if __name__ == "__main__":
    g = BGF.Grammar()
    prod = 400
    term = 150
    for i in range(0, prod):
        p = BGF.Production()
        p.setNT('nt' + str(i))
        p.setExpr(BGF.Expression(singles[i % 3]))
        g.addProd(p)
        p = BGF.Production()
        p.setNT('nt' + str(i))
        a = ntd('nt' + str((i + 1) % prod), BGF.Plus)
예제 #5
0
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:
        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> [<new-root>...] <bgf-output>'
        sys.exit(1)
    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)
예제 #6
0
             grammar[nt].append(tokens)
             endOfAlt = False
         elif len(grammar[nt]) > 0:
             grammar[nt][-1].extend(tokens)
         else:
             grammar[nt].append(tokens)
     pass
     if tokens[-1][-1] == ';':
         if nt:
             print '[' + str(cx) + ']', 'Done with nonterminal', nt
         nt = ''
 # NOW TO PROCESS TOKENS
 #print 'Command:'
 #for s in grammar['Command']:
 #	print '	',s
 bgf = BGF.Grammar()
 bgf.roots = start
 prevline = []
 curly = 0
 # going through the sorted list of nonterminals
 print grammarkeys
 for nt in grammarkeys:
     print nt, '::=', grammar[nt]
 for nt in grammarkeys:
     if nt in lexicals:
         print 'Lexical nonterminal', nt, 'is disregarded'
         continue
     if nt in layouts:
         print 'Layout-related nonterminal', nt, 'is disregarded'
         continue
     for alt in grammar[nt]:
예제 #7
0
파일: present_bgf.py 프로젝트: siwiwit/slps
#!/usr/bin/python
import os, sys
sys.path.append(os.getcwd().split('projects')[0] +
                'projects/slps/shared/python')
import BGF, bench

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print 'This tool presents an overview of a BGF file.'
        print 'Usage:'
        print '      checkbgf <bgf>'
        sys.exit(1)
    grammar = BGF.Grammar()
    grammar.parse(sys.argv[1])
    mb = bench.MeasurementBench(grammar)
    print 'Productions:         ', len(grammar.prods)
    print 'Top alternatives:    ', mb.PROD()
    print 'Nonterminals defined:', mb.DEFD()
    pa = lambda a: reduce(lambda x, y: x + ', ' + str(y), a[1:], '(' + str(a[
        0])) + ')'
    if mb.UNDEF():
        print '%20s:' % 'bottom', mb.UNDEF(), pa(mb.getBottoms())
    else:
        print '%20s:' % 'bottom', mb.UNDEF()
    if mb.ROOT():
        print '%20s:' % 'root', mb.ROOT(), pa(grammar.roots)
    else:
        print '%20s:' % 'root', mb.ROOT()
    if mb.DEAD():
        print '%20s:' % 'dead top', mb.DEAD(), pa(mb.getDeadTops())
    else: