Ejemplo n.º 1
0
 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
Ejemplo n.º 2
0
    def concretize(self):
        """
        Transforms the SFA into a DFA
        Args:
            None
        Returns:
            DFA: The generated DFA
        """
        dfa = DFA(self.alphabet)
        for state in self.states:
            for arc in state.arcs:
                for char in arc.guard:
                    dfa.add_arc(arc.src_state, arc.dst_state, char)

        for i in xrange(len(self.states)):
            if self.states[i].final:
                dfa[i].final = True
        return dfa