Skip to content

Commit

Permalink
now using __import__ to make a portable main function, just name a le…
Browse files Browse the repository at this point in the history
…xer and we can import it yourclass.scanner is preferable
  • Loading branch information
dacresni committed May 31, 2010
1 parent 28df4e9 commit 75782c2
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion bnflexar.py
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,7 @@
from lexar import Token, Scanner from lexar import Token, Scanner
# we can impliment the grammar parser inside the scanner lexer # we can impliment the grammar parser inside the scanner lexer
# 3 out of 4 methods are going to be overloaded by the child does the nested class inheret? # 3 out of 4 methods are going to be overloaded by the child does the nested class inheret?
class BnfLexar(Scanner): #should I subclass this or not class Lexer(Scanner): #should I subclass this or not
def rule(self,match): def rule(self,match):
result=match.string.strip('\n',) result=match.string.strip('\n',)
self.tokenStream.append(Token("nonterminal",result) ) self.tokenStream.append(Token("nonterminal",result) )
Expand Down
2 changes: 1 addition & 1 deletion cyk.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys import sys
from lexar import Token from lexar import Token
from grammar import Grammar from grammar import Grammar
from bnflexar import BnfLexar from bnflexar import Lexer


class CYKChart(object): class CYKChart(object):
def __init__(self): def __init__(self):
Expand Down
4 changes: 2 additions & 2 deletions grammar.py
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
from lexar import Token from lexar import Token
from bnflexar import BnfLexar from bnflexar import Lexer


class Grammar(object): class Grammar(object):
"""grammar is simply a list of rules with at least one start symbole""" """grammar is simply a list of rules with at least one start symbole"""
Expand All @@ -21,7 +21,7 @@ def __findbreaks( stack ,i, left):# we need to text this function
newrule.rightHand=stack[i:] newrule.rightHand=stack[i:]
self.rules.append(newrule) self.rules.append(newrule)


lex=BnfLexar() lex=Lexer()
if verbose: if verbose:
lex.setVerbose() lex.setVerbose()
lex.scanFile(source) lex.scanFile(source)
Expand Down
9 changes: 6 additions & 3 deletions main.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
from cyk import CYKChart from cyk import CYKChart
from viz import genDot from viz import genDot
from os import system from os import system
def main(lexarname, gramarspec, inputfile, outputfile=None): def main(args**):
lexarname, gramarspec, inputfile, outputfile=None
if outputfile == None: if outputfile == None:
outputfile = inputfile outputfile = inputfile
G = Grammar() G = Grammar()
source = open(gramarspec,'r') source = open(gramarspec,'r')
G.generate(source) G.generate(source)
G.bnf2cnf() G.bnf2cnf()
print "grammer==",G print "grammer==",G
lexer=__import__(lexarname) lexerclass=__import__(lexarname)
lexer=lexerclass.Lexer()
lexer.scan(inputfile) lexer.scan(inputfile)
S=lexer.getStream() S=lexer.getStream()
print "stream ===",S print "stream ===",S
Expand All @@ -19,9 +21,10 @@ def main(lexarname, gramarspec, inputfile, outputfile=None):
print C print C
print C.graph print C.graph
genDot(C,outputfile) genDot(C,outputfile)
system("dot -Tjpg %s -o %s "%(outputfile, outputfile)) system("dot -Tjpg %s -o %s "%(outputfile, outputfile))
print "%s ted"%(outputfile) print "%s ted"%(outputfile)


if __name__ == '__main__': if __name__ == '__main__':
import sys import sys
main(sys.argv) main(sys.argv)

2 changes: 1 addition & 1 deletion viz.py
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
from treecyk import CYKChart from cyk import CYKChart
class genDot(object): class genDot(object):


def genTree(self,G,here,visited): def genTree(self,G,here,visited):
Expand Down

0 comments on commit 75782c2

Please sign in to comment.