Пример #1
0
 def p_check(self, p):
     """check : CHECK expr THEN NL stat_group
             | CHECK expr THEN NL stat_group OTHERWISE NL stat_group"""
     if len(p) == 6:
         p[0] = Tree('if_then', children={'condition': p[2], 'body': p[5]}, lineno=p.lineno(1))
     else:
         p[0] = Tree('if_th_el', children={'condition': p[2], 'body_1': p[5], 'body_2': p[8]}, lineno=p.lineno(1))
Пример #2
0
def logic_monomial1(string,i, tree):
    mine = i.val()
    if len(string) <= i.val():
        return False
    child = Tree(cargo='&')
    tree.childs.append(child)
    if string[i.val()] == '&':
        i.inc()
        child = Tree(cargo='second logic expression')
        tree.childs.append(child)
        if second_logic_expression(string,i, child):
            i.inc()
            child = Tree(cargo='logic monomial 1')
            tree.childs.append(child)
            if logic_monomial1(string,i, child):
                print(f'({i.val()})log mon -> & sec log exp   log mon')
                return True
            else:
                tree.childs.remove(child)
                print(f'!({i.val()})log mon -> & sec log exp')
                i.dec()
                return True
        else: print(f'Position {i.val()} It`s not second logic expression')
    else: print(f'Position {i.val()} It`s not `&`')
    return False
Пример #3
0
def logic_expression1(string, i, tree):
    mine = i.val()
    if len(string) <= i.val():
        return False
    child = Tree(cargo='!')
    tree.childs.append(child)
    if string[i.val()] == '!':
        i.inc()
        child = Tree(cargo='logic monomial')
        tree.childs.append(child)
        if logic_monomial(string,i, child):
            i.inc()
            child = Tree(cargo='logic expression 1')
            tree.childs.append(child)
            if logic_expression1(string,i, child):
                print(f'({i.val()})log exp 1 -> ! log mon    log exp 1')
                return True
            else:
                tree.childs.remove(child)
                i.dec()
                print(f'({i.val()})log exp 1 -> ! log mon')
                return True
        else: print(f'Position {i.val()} It`s not logic monomial')
    else: print(f'Position {i.val()} It`s not `!`')
    return False
Пример #4
0
 def p_stat_group(self, p):
     """stat_group : L_FIGBRACKET NL stat_list R_FIGBRACKET NL
                 | statement"""
     if p[1] == '{':
         p[1] = Tree('border', value=p[1], lineno=p.lineno(1))
         p[4] = Tree('border', value=p[4], lineno=p.lineno(1))
         p[0] = Tree('group_stat', children=[p[1], p[3], p[4]], lineno=p.lineno(2) + 1)
     else:
         p[0] = p[1]
Пример #5
0
 def p_routing(self, p):
     """routing : type ROUTING VARIABLE var_list NL stat_group RETURN expr ENDSTR NL"""
     if p[3] in self.functions.keys():
         p[0] = Tree('error', value='Function declared earlier', lineno=p.lineno(1))
         sys.stderr.write(f'<<<<<Redeclared function\n')
     else:
         p[0] = Tree('function', value=str(p[3]), children={'parameters': p[4],
                                                            'body': p[6],
                                                            'return': p[8]}, lineno=p.lineno(1))
         self.functions[p[3]] = p[0]
Пример #6
0
    def operator_precedence_parsing(self, grammar, string):
        if string[0] == self.marker:
            return True
        s = ['' for _ in range(10)]
        s[0] = self.marker
        t = 0  # for stack
        i = 0  # for string
        n = 0
        prn = []
        ch = string[i]
        f_error = False
        tree_list = []
        while t > 0 or ch != self.marker:
            chose = self.matrix[self.ind(s[t])][self.ind(ch)]
            if chose in ['<', '=']:  # перенос
                tree_list.append(Tree(ch))
                t = t + 1
                s[t] = ch
                i = i + 1
                ch = string[i]
                print(f'Shift: {s[0:t+1]}')
            elif chose == '>':  # свертка
                while True:
                    length = 0
                    prn.append(s[t])
                    for rule in grammar.rules:
                        if s[t] in rule.right_part:
                            length = len(rule.right_part)
                            break
                    tree1 = Tree('S ' + str(n))
                    n = n + 1
                    for ind_tr in range(length):
                        tree1.childs.append(tree_list[len(tree_list) - length +
                                                      ind_tr])
                    for _ in range(length):
                        tree_list.pop()
                    tree_list.append(tree1)

                    t = t - 1
                    print(f'Reduce: {s[0:t+1]}')
                    if self.matrix[self.ind(s[t])][self.ind(s[t + 1])] == '<':
                        break
            else:
                f_error = True
                break

        if not f_error:
            print(f'Postfix: {prn}')
            return tree_list[0]
        else:
            print(f'Postfix: {prn}')
            print(self.errors_dict[chose])
            return None
Пример #7
0
 def p_math_expr(self, p):
     """math_expr : expr PLUS expr
                 | expr MINUS expr
                 | expr MUL expr
                 | expr DIV expr
                 | expr AND expr
                 | DENY expr
                 | MOST expr"""
     if len(p) == 3:
         p[0] = Tree('calculation', value=p[1], children=p[2], lineno=p.lineno(2))
     else:
         p[0] = Tree('calculation', value=p[2], children=[p[1], p[3]], lineno=p.lineno(2))
Пример #8
0
 def p_stat_list(self, p):
     """stat_list : PLEASE statement THANK YOU
                 | stat_list statement
                 | statement
                 | NL"""
     if len(p) == 3:
         p[0] = Tree('statement list', p[2], children=[p[1], p[2]], lineno=p.lineno(1))
     elif p[1] == 'please':
         p[0] = Tree('statement list', p[3], children=[p[2], p[3]], lineno=p.lineno(1))
     else:
         if p[0] != '\n':
             p[0] = p[1]
         else:
             p[0] = Tree('NL', lineno=p.lineno(1))
Пример #9
0
 def p_comp(self, p):
     """comp : EQ expr
             | LT expr
             | GT expr
             | LTE expr
             | GTE expr"""
     p[0] = Tree('comparison', value=p[1], children=p[2], lineno=p.lineno(1))
Пример #10
0
 def p_var_list(self, p):
     """var_list : variable
                 | var_list COMMA var_list"""
     if len(p) == 2:
         p[0] = p[1]
     else:
         p[0] = Tree('var_list', children=[p[1], p[3]], lineno=p.lineno(2))
Пример #11
0
 def p_digits(self, p):
     """digits : digit
               | digits COMMA digits"""
     if len(p) == 2:
         p[0] = p[1]
     else:
         p[0] = Tree('matr', children=[p[1], p[3]], lineno=p.lineno)
Пример #12
0
 def p_indexing(self, p):
     """indexing : L_SQBRACKET VARIABLE R_SQBRACKET
                 | L_SQBRACKET digit R_SQBRACKET
                 | L_SQBRACKET digits R_SQBRACKET
                 | L_SQBRACKET var_list R_SQBRACKET"""
     # if len(p) == 3:
     p[0] = Tree('indexing', children=p[2], lineno=p.lineno(1))
Пример #13
0
    def p_comp_br(self, p):
        """comp_br : EQ L_SQBRACKET R_SQBRACKET expr
                   | LT L_SQBRACKET R_SQBRACKET expr
                   | GT L_SQBRACKET R_SQBRACKET expr
                   | LTE L_SQBRACKET R_SQBRACKET expr
                   | GTE L_SQBRACKET R_SQBRACKET expr"""

        p[0] = Tree('comparison', value=p[1], children=p[4], lineno=p.lineno(1))
Пример #14
0
def first_logic_expression(string,i, tree):
    mine = i.val()
    child = Tree(cargo=string[i.val()])
    tree.childs.append(child)
    if string[i.val()] in ['a', 'true', 'false']:
        print(f'({i.val()})fir log exp -> a true false')
        return True
    else:
        print(f'Position {i.val()} It`s not `a` or logic value')
Пример #15
0
 def check_string(self, inp):
     string = inp.split(' ')
     i = Integer(0)
     tree = Tree(cargo=self.start)
     res = operator(string, i, tree)
     if res and i.val() == len(string) - 1:
         print('Строка подходит')
         return True, tree
     else:
         print('Строка не подходит')
         return False, None
Пример #16
0
def expression(string, i, tree):
    mine = i.val()
    if len(string) <= i.val():
        return False
    child = Tree(cargo='logic expression')
    tree.childs.append(child)
    if logic_expression(string,i, child):
        print(f'({i.val()})exp -> log exp')
        return True
    else: print(f'Position {i.val()} It`s not logic_expression')
    return False
Пример #17
0
def operator(string, i, tree):
    if len(string) <= i.val():
        return False
    mine = i.val()
    child = Tree(cargo='a')
    tree.childs.append(child)
    if string[i.val()] == 'a':
        i.inc()
        child = Tree(cargo='=')
        tree.childs.append(child)
        if string[i.val()] == '=':
            i.inc()
            child = Tree(cargo='expression')
            tree.childs.append(child)
            if expression(string, i, child):
                print(f'({i.val()})op -> a = exp')
                return True
            else: print(f'Position {i.val()} It`s not expression')
        else: print(f'Position {i.val()} It`s not `=`')
    else: print(f'Position {i.val()} It`s not `a`')
    return False
Пример #18
0
def second_logic_expression(string,i, tree):
    mine = i.val()
    if len(string) <= i.val():
        return False
    child = Tree(cargo='~')
    tree.childs.append(child)
    if string[i.val()] == '~':
        i.inc()
        child = Tree(cargo='first logic expression')
        tree.childs.append(child)
        if first_logic_expression(string,i, child):
            print(f'({i.val()})sec log exp -> ~ fir log exp')
            return True
        else: print(f'Position {i.val()} It`s not first logic expression')
    else:
        tree.childs.remove(child)
        child = Tree(cargo='first logic expression')
        tree.childs.append(child)
        if first_logic_expression(string,i, child): # ###################################
            print(f'({i.val()})sec log exp -> fir log exp')
            return True
    print(f'Position {i.val()} It`s not first logic expression')
    return False
Пример #19
0
 def p_statement(self, p):
     """statement : declaration ENDSTR NL
                 | assignment ENDSTR NL
                 | size ENDSTR NL
                 | to ENDSTR NL
                 | resize ENDSTR NL
                 | comp_br ENDSTR NL
                 | for
                 | check
                 | routing
                 | perform ENDSTR NL
                 | command ENDSTR NL
                 | ENDSTR NL"""
     if len(p) == 4 or len(p) == 2:
         p[0] = p[1]
     else:
         p[0] = Tree('EOS', lineno=p.lineno(1))
Пример #20
0
 def p_perform(self, p):
     """perform : PERFORM VARIABLE var_list"""
     p[0] = Tree('perform', value=p[2], children=p[3], lineno=p.lineno(1))
Пример #21
0
 def check_string(self, inp):
     string = inp.split(' ')
     i = Integer(0)
     # while i.val() < len(string):
     tree = Tree(cargo=self.start)
Пример #22
0
 def p_for(self, p):
     """for : FOR VARIABLE STOP VARIABLE STEP VARIABLE NL stat_group"""
     p[0] = Tree('for', children={'start': Tree('variable', p[2]),
                                  'step': Tree('variable', p[6]),
                                  'stop': Tree('variable', p[4]),
                                  'body': p[8]}, lineno=p.lineno(1))
Пример #23
0
def add_child(tree, name):
    child = Tree(cargo=name)
    tree.childs.append(child)
Пример #24
0
 def p_check_error(self, p):
     """check : CHECK expr error"""
     p[0] = Tree('error', value='Wrong if declaration', lineno=p.lineno(1))
Пример #25
0
 def p_routing_error(self, p):
     """routing : ROUTING error"""
     p[0] = Tree('error', value='Wrong function declaration', children=p[1], lineno=p.lineno(1))
     sys.stderr.write(f'<<<<<Wrong function declaration\n')
Пример #26
0
 def p_ass_error(self, p):
     """assignment : variable EQQ error"""
     p[0] = Tree('error', value='Wrong assignment', children=p[1], lineno=p.lineno(2))
     sys.stderr.write(f'<<<<<Wrong assignment\n')
Пример #27
0
 def p_assignment(self, p):
     """assignment : variable EQQ expr"""
     p[0] = Tree('assignment', value=p[2], children=[p[1], p[3]], lineno=p.lineno(2))
Пример #28
0
 def p_command(self, p):
     """command : MOVE expr
                 | ROTATE expr
                 | SURROUNDINGS VARIABLE"""
     p[0] = Tree('command', value=p[1], children=p[2], lineno=p.lineno(1))
Пример #29
0
 def p_declaration(self, p):
     """declaration : type var_list EQQ expr"""
     if len(p) == 5:
         p[0] = Tree('declaration', children=[p[1], Tree('assign', children=[p[2], p[4]])], lineno=p.lineno(1))
Пример #30
0
 def p_program(self, p):
     """program : stat_list"""
     p[0] = Tree('program', children=p[1], lineno=p.lineno(1))