def yyparse(self, lexfile): """ Args: lexfile (str): Flex file to be parsed Returns: DFA: A dfa automaton """ temp = tempfile.gettempdir() self.outfile = temp + '/' + ''.join( random.choice(string.ascii_uppercase + string.digits) for _ in range(5)) + '_lex.yy.c' self._create_automaton_from_regex(lexfile) states_num, delta = self._create_delta() states = self._create_states(states_num) accepted_states = self._read_accept_states() if self.alphabet != []: alphabet = self.alphabet else: alphabet = createalphabet() mma = DFA(alphabet) for state in states: if state != 0: for char in alphabet: nextstate = delta(state, char) mma.add_arc(state - 1, nextstate - 1, char) if state in accepted_states: mma[state - 1].final = True if os.path.exists(self.outfile): os.remove(self.outfile) return mma
def main(): """ Testing function for PDA - DFA Diff Operation """ if len(argv) < 2: print 'Usage: ' print ' Get A String %s CFG_fileA FST_fileB' % argv[ 0] return alphabet = createalphabet() cfgtopda = CfgPDA(alphabet) print '* Parsing Grammar:', mma = cfgtopda.yyparse(argv[1]) print 'OK' flex_a = Flexparser(alphabet) print '* Parsing Regex:', mmb = flex_a.yyparse(argv[2]) print mmb print 'OK' print '* Minimize Automaton:', mmb.minimize() print 'OK' print mmb print '* Diff:', ops = PdaDiff(mma, mmb, alphabet) mmc = ops.diff() print 'OK' print '* Get String:', print ops.get_string()
def __init__(self, alphabet=createalphabet()): """ Args: alphabet (list): pyfst input symbol list Returns: None """ isyms = None self.alphabet = alphabet fst.StdAcceptor.__init__(self, isyms) num = 1 for char in self.alphabet: self.isyms.__setitem__(char, num) num = num + 1
def __init__(self, alphabet=createalphabet()): """ Args: alphabet (list): The imput alphabet Returns: None """ self.states = [] self.alphabet = alphabet self.nfa = False num = 1 self.isyms = syms() self.osyms = syms() for char in alphabet + [EPSILON]: self.isyms.__setitem__(char, num) self.osyms.__setitem__(char, num) num = num + 1
def __init__(self, input_fst_a, alphabet=None): """ Args: input_fst_a (DFA): The DFA states alphabet (list): the input alphabet Returns: None """ if alphabet is None: alphabet = createalphabet() self.mma = DFA(self.alphabet) self.mma.init_from_acceptor(input_fst_a) self.alphabet = alphabet self.l_transitions = {} self.epsilon = '' self.empty = None
def __init__(self, input_fst_a, alphabet=None): """ Args: input_fst_a (DFA): The DFA states alphabet (list): the input alphabet Returns: None """ if alphabet is None: alphabet = createalphabet() self.alphabet = alphabet self.mma = DFA(self.alphabet) self.mma.init_from_acceptor(input_fst_a) # a is a matrix that holds all transitions # If there is a transition from state 0 to state 1 with the symbol x # then a[0][1]=x self.A = {} # B[n] holds the regular expression that describes how a final state # can be reached from state n self.B = {} self.epsilon = '' self.empty = None
def __init__(self, alphabet=createalphabet()): """ Args: alphabet (list): The imput alphabet Returns: None """ self.automaton = fst.Fst() id = self.add_state() self.automaton.set_start(id) self.alphabet = alphabet num = 1 self.isyms = syms() self.osyms = syms() insymbols = fst.SymbolTable() outsymbols = fst.SymbolTable() for char in alphabet: self.isyms.__setitem__(char, num) self.osyms.__setitem__(char, num) insymbols.add_symbol(char, num) outsymbols.add_symbol(char, num) num = num + 1 self.automaton.set_input_symbols(insymbols) self.automaton.set_output_symbols(outsymbols)
def __init__(self, alphabet=createalphabet()): self.alphabet = alphabet super(DFA, self).__init__(alphabet)
def main(): """ Function for PDA to CNF Operation :type argv: list :param argv: Parameters """ if len(argv) < 3: print 'Usage for getting CFG: %s CFG_fileA CFG ' % argv[0] print 'Usage for getting STR: %s CFG_fileA STR ' \ 'Optimize[0 or 1] splitstring[0 or 1] ' % argv[0] print '' print 'For example: python pdacnf.py grammar.y STR 1 0' print ' python pdacnf.py grammar.y STR 1 1' print ' python pdacnf.py grammar.y CFG' return alphabet = createalphabet() mode = argv[2] optimized = 0 splitstring = 0 if mode == 'STR': optimized = int(argv[3]) splitstring = int(argv[4]) cfgtopda = CfgPDA(alphabet) print '* Parsing Grammar:', mma = cfgtopda.yyparse(argv[1]) print 'OK' print ' - Total PDA states are ' + repr(len(mma.s)) print '* Simplify State IDs:', simple_a = SimplifyStateIDs() mma.s, biggestid, newaccepted = simple_a.get(mma.s) if newaccepted: print 'OK' else: print 'OK' print '* Eliminate READ states:', replace = ReadReplace(mma.s, biggestid) mma.s = replace.replace_read() print 'OK' print ' - Total PDA states now are ' + repr(len(mma.s)) maxstate = replace.nextstate() - 1 print '* Reduce PDA:', simple_b = ReducePDA() mma.s = simple_b.get(mma.s) print 'OK' print ' - Total PDA states now are ' + repr(len(mma.s)) print '* PDA to CFG transformation:', cnfgenerator = PdaCnf(mma.s) grammar = cnfgenerator.get_rules(optimized) print 'OK' print ' - Total CFG rules generated: ' + repr(len(grammar)) if mode == 'STR': gen = CFGGenerator(CNFGenerator(grammar), optimized=optimized, splitstring=splitstring, maxstate=maxstate) print gen.generate() else: print grammar