Ejemplo n.º 1
0
Archivo: rsg.py Proyecto: shafaq/RSG
    def generate(self):
        """ Generate the expansion of the context free grammar.

        Under the hood it starts from the <start> nonterminal and returns the
        expansion. A production is chosen randomly from the multiproduction
        non terminal. 
        """

        utility.debug_print(self.grammar["start"][0], "start=")
        return self.__rgenerate(self.get_random_production("start"))
Ejemplo n.º 2
0
Archivo: rsg.py Proyecto: shafaq/RSG
    def generate(self):
        """ Generate the expansion of the context free grammar.

        Under the hood it starts from the <start> nonterminal and returns the
        expansion. A production is chosen randomly from the multiproduction
        non terminal. 
        """

        utility.debug_print(self.grammar["start"][0], "start=")
        return self.__rgenerate(self.get_random_production("start"))
Ejemplo n.º 3
0
Archivo: rsg.py Proyecto: shafaq/RSG
    def __init__(self, nonterminal_start, nonterminal_end, cfgGrammar):
        """ Inititalize the CFG instance. 

        The method takes three arguments:
        nonterminal_start -- the starting delimiter of the non terminal
        nonterminal_end -- the ending delimiter of the non terminal
        cfgGrammar -- recieves a grammar as dictionary object. Program exits 
        if not not provided
        """
        if not cfgGrammar:
            print "Grammar not provided."
            sys.exit(0)
        self.grammar = cfgGrammar
        self.nonterminal_start = nonterminal_start
        self.nonterminal_end = nonterminal_end
        utility.debug_print(self.grammar, "grammar")
Ejemplo n.º 4
0
Archivo: rsg.py Proyecto: shafaq/RSG
    def __init__(self, nonterminal_start, nonterminal_end, cfgGrammar):
        """ Inititalize the CFG instance. 

        The method takes three arguments:
        nonterminal_start -- the starting delimiter of the non terminal
        nonterminal_end -- the ending delimiter of the non terminal
        cfgGrammar -- recieves a grammar as dictionary object. Program exits 
        if not not provided
        """
        if not cfgGrammar:
            print "Grammar not provided."
            sys.exit(0) 
        self.grammar = cfgGrammar
        self.nonterminal_start = nonterminal_start
        self.nonterminal_end = nonterminal_end
        utility.debug_print(self.grammar, "grammar")
Ejemplo n.º 5
0
Archivo: rsg.py Proyecto: shafaq/RSG
 def __rgenerate(self, production):
     pattern_string = (self.nonterminal_start +
                       '(?P<nonterminal>\\b.+?\\b)' + self.nonterminal_end)
     utility.debug_print(pattern_string, "pattern")
     pattern_nt = re.compile(pattern_string)
     #find a match for a not terminal in the given production
     match = pattern_nt.search(production)
     matched_nonterminal = match.group('nonterminal')
     utility.debug_print(match.group('nonterminal'),
                         "matched non terminal = ")
     #get a random production for the matched non terminal
     substitution = self.get_random_production(matched_nonterminal)
     utility.debug_print(substitution, "substitution")
     #split the string around found non terminal
     lst = pattern_nt.split(production, 1)
     # substitute the random production for non terminal
     new_production = lst[0] + substitution + lst[2]
     utility.debug_print(new_production, "new_production")
     # if new production still has more non terminals
     if not pattern_nt.search(new_production):
         return new_production
     return self.__rgenerate(new_production)
Ejemplo n.º 6
0
Archivo: rsg.py Proyecto: shafaq/RSG
 def __rgenerate(self, production):
     pattern_string = (self.nonterminal_start +
                      '(?P<nonterminal>\\b.+?\\b)' +
                      self.nonterminal_end)
     utility.debug_print(pattern_string, "pattern")
     pattern_nt = re.compile(pattern_string)
     #find a match for a not terminal in the given production
     match = pattern_nt.search(production)
     matched_nonterminal = match.group('nonterminal')
     utility.debug_print(match.group('nonterminal'),
                        "matched non terminal = ")
     #get a random production for the matched non terminal
     substitution = self. get_random_production(matched_nonterminal)
     utility.debug_print(substitution, "substitution")
     #split the string around found non terminal
     lst = pattern_nt.split(production, 1)
     # substitute the random production for non terminal
     new_production = lst[0] + substitution + lst[2]
     utility.debug_print(new_production, "new_production")
     # if new production still has more non terminals
     if not pattern_nt.search(new_production):
         return new_production
     return self.__rgenerate(new_production)