Skip to content
This repository
Browse code

now using __import__ to make a portable main function, just name a le…

…xer and we can import it yourclass.scanner is preferable
  • Loading branch information...
commit 75782c27e566a80c9ccf83943e03d15496f52c09 1 parent 28df4e9
Nehemiah I. Dacres authored
2  bnflexar.py
... ... @@ -1,7 +1,7 @@
1 1 from lexar import Token, Scanner
2 2 # we can impliment the grammar parser inside the scanner lexer
3 3 # 3 out of 4 methods are going to be overloaded by the child does the nested class inheret?
4   -class BnfLexar(Scanner): #should I subclass this or not
  4 +class Lexer(Scanner): #should I subclass this or not
5 5 def rule(self,match):
6 6 result=match.string.strip('\n',)
7 7 self.tokenStream.append(Token("nonterminal",result) )
2  cyk.py
@@ -2,7 +2,7 @@
2 2 import sys
3 3 from lexar import Token
4 4 from grammar import Grammar
5   -from bnflexar import BnfLexar
  5 +from bnflexar import Lexer
6 6
7 7 class CYKChart(object):
8 8 def __init__(self):
4 grammar.py
... ... @@ -1,5 +1,5 @@
1 1 from lexar import Token
2   -from bnflexar import BnfLexar
  2 +from bnflexar import Lexer
3 3
4 4 class Grammar(object):
5 5 """grammar is simply a list of rules with at least one start symbole"""
@@ -21,7 +21,7 @@ def __findbreaks( stack ,i, left):# we need to text this function
21 21 newrule.rightHand=stack[i:]
22 22 self.rules.append(newrule)
23 23
24   - lex=BnfLexar()
  24 + lex=Lexer()
25 25 if verbose:
26 26 lex.setVerbose()
27 27 lex.scanFile(source)
9 main.py
@@ -2,7 +2,8 @@
2 2 from cyk import CYKChart
3 3 from viz import genDot
4 4 from os import system
5   -def main(lexarname, gramarspec, inputfile, outputfile=None):
  5 +def main(args**):
  6 + lexarname, gramarspec, inputfile, outputfile=None
6 7 if outputfile == None:
7 8 outputfile = inputfile
8 9 G = Grammar()
@@ -10,7 +11,8 @@ def main(lexarname, gramarspec, inputfile, outputfile=None):
10 11 G.generate(source)
11 12 G.bnf2cnf()
12 13 print "grammer==",G
13   - lexer=__import__(lexarname)
  14 + lexerclass=__import__(lexarname)
  15 + lexer=lexerclass.Lexer()
14 16 lexer.scan(inputfile)
15 17 S=lexer.getStream()
16 18 print "stream ===",S
@@ -19,9 +21,10 @@ def main(lexarname, gramarspec, inputfile, outputfile=None):
19 21 print C
20 22 print C.graph
21 23 genDot(C,outputfile)
22   - system("dot -Tjpg %s -o %s "%(outputfile, outputfile))
  24 + system("dot -Tjpg %s -o %s "%(outputfile, outputfile))
23 25 print "%s ted"%(outputfile)
24 26
25 27 if __name__ == '__main__':
26 28 import sys
27 29 main(sys.argv)
  30 +
2  viz.py
... ... @@ -1,4 +1,4 @@
1   -from treecyk import CYKChart
  1 +from cyk import CYKChart
2 2 class genDot(object):
3 3
4 4 def genTree(self,G,here,visited):

0 comments on commit 75782c2

Please sign in to comment.
Something went wrong with that request. Please try again.