Ejemplo n.º 1
0
    def makeregex(self, j):

        regex = re.escape(
            self.parsedtokens[j].val[1:-1])  #eliminate the ["..."]

        label = "__" + self.current_rule + "[" + regex + "]__"

        self.tokens.append((regex, label))

        thisnode = Token("TERMINAL", label, 0)
        self.production_rules[self.current_rule][-1].append(thisnode)
Ejemplo n.º 2
0
    def explode_unit_rules(self, rule, emptykeys):
        if len(rule) != 1:
            return False, []

        fixedrules = []

        op = rule[0]
        if op.val in emptykeys:
            fixedrules = [[Token("EMPTY", '""', op.pos)]]
        else:
            fixedrules = [rule]
        return True, fixedrules
Ejemplo n.º 3
0
 def binarizerule(self, normalForm, key, rule):
     if len(rule) <= 2:
         normalForm[key].append(rule)
     else:
         newKey = "/".join([r.val.strip('.') for r in rule[1:]])
         #newKey = key + "-".join ([r.val for r in rule[1:]])
         if not (newKey in normalForm.keys()):
             normalForm[newKey] = []
         newProdRule = rule[1:]
         normalForm[key].append([rule[0], Token("NONTERMINAL", newKey, 0)])
         normalForm[newKey].append(newProdRule)
     return normalForm
Ejemplo n.º 4
0
 def checkruleforterminals(self, key, rule):
     newRule = []
     for i in range(0, len(rule)):
         operand = rule[i]
         if operand.type == "TERMINAL":
             newKey = operand.val + "."
             if not (newKey in self.normalForm.keys()):
                 self.normalForm[newKey] = []
             newRule.append(Token("NONTERMINAL", newKey, 0))
             operand.label = ""
             self.normalForm[newKey].append([operand])
         else:
             newRule.append(operand)
     self.normalForm[key].append(newRule)
Ejemplo n.º 5
0
 def unfold(self, parent=None):
     if DEBUG:
         if self.iscompacted() or parent == self.nodetype:
             return self.unit.unfold(self.nodetype)
         else:  #if parent != None :
             return " {} = [\n {} \n]".format(
                 self.nodetype,
                 self.unit.unfold(self.nodetype),
             )
     else:
         if self.iscompacted() or parent == self.nodetype:
             return self.unit.unfold(self.nodetype)
         else:  #if parent != None :
             return [
                 Token(self.nodetype, self.unit.unfold(self.nodetype), 0)
             ]
Ejemplo n.º 6
0
 def unfold(self, parent=None):
     if DEBUG:
         if self.iscompacted() or parent == self.nodetype:
             return "{} \n {}".format(  # make it merge two dicts
                 self.left.unfold(self.nodetype),
                 self.right.unfold(self.nodetype),
             )
         else:
             return "{} = [\n {} \n {} \n]".format(
                 self.nodetype,
                 self.left.unfold(self.nodetype),
                 self.right.unfold(self.nodetype),
             )
     else:
         if self.iscompacted() or parent == self.nodetype:
             return self.left.unfold(self.nodetype) + self.right.unfold(
                 self.nodetype)
         else:
             return [
                 Token(
                     self.nodetype,
                     self.left.unfold(self.nodetype) +
                     self.right.unfold(self.nodetype), 0)
             ]
Ejemplo n.º 7
0
 def makelist(self):
     thisnode = Token("NONTERMINAL", self.current_rule, 0)
     eps = Token("EMPTY", '""', 0)
     self.production_rules[self.current_rule][-1] = [thisnode, thisnode]
     self.production_rules[self.current_rule].append([eps])
Ejemplo n.º 8
0
 def addempty(self, fixedrules):
     fixedrules.append([Token("EMPTY", '""', 0)])
     return fixedrules
Ejemplo n.º 9
0
 def unfold(self, parent=None):
     if DEBUG:
         return "{key}({val})".format(key=self.nodetype, val=self.val)
     else:
         return [Token(self.nodetype, self.val, 0)]