def p_pop(self, p): '''pop : empty | LPOP VALUE RPOP | LPOP empty RPOP | POP''' p[0] = None if len(p) < 3 else Element(type='pop', value=p[2])
def p_push(self, p): '''push : empty | LPUSH VALUE RPUSH | LPUSH empty RPUSH | PUSH''' p[0] = None if len(p) < 3 else Element(type='push', value=p[2])
def parse(self, ast=None): if ast is None: first_token = self.token_stream.get_token() ast = Element(first_token.token) operator = self.token_stream.get_token() if operator.empty: return ast if operator.operator: return self.parse_operator(ast, operator)
def p_right(self, p): '''right : LRIGHT VALUE RRIGHT | LRIGHT empty RRIGHT | RIGHT empty''' p[0] = Element(type='right', value=p[2])
def p_axiom(self, p): '''axiom : empty | LAXIOM VALUE RAXIOM''' p[0] = None if len(p) < 3 else Element(type='axiom', value=p[2])
def parse_operator(self, left_ast, operator): right_token = self.token_stream.get_token() return self.parse( Operation(left_ast, Element(right_token.token), Operator(operator.token)))
def p_final(self, p): '''final : empty | FINAL''' p[0] = None if p[1] is None else Element(type='final')
def p_block(self, p): '''block : LBLOCK tag x y special RBLOCK''' p[0] = Element(type='block', children=[p[2], p[3], p[4]])
def p_customblock(self, p): '''customblock : LCUSTOMBLOCK inside RCUSTOMBLOCK''' p[0] = Element(type='customblock', value=p[1], children=[p[2]])
def p_value(self, p): '''value : LVALUE VALUE RVALUE''' p[0] = Element(type='left', value=p[2])
def p_expression(self, p): '''expression : LEXPRESSION VALUE REXPRESSION''' p[0] = Element(type='expression', value=p[2])
def p_output(self, p): '''output : empty | LOUTPUT VALUE ROUTPUT | LOUTPUT empty ROUTPUT''' p[0] = None if len(p) < 3 else Element(type='output', value=p[2])
def p_parameter(self, p): '''parameter : LPARAMETER name value RPARAMETER''' p[0] = Element(type='parameter', children=[p[2], p[3]])
def p_read(self, p): '''read : READ | LREAD VALUE RREAD | LREAD empty RREAD''' p[0] = Element(type='read') if len(p) < 3 else Element(type='read', value=p[2])
def p_to(self, p): '''to : LTO VALUE RTO ''' p[0] = Element(type='to', value=p[2])
def p_from(self, p): '''from : LFROM VALUE RFROM''' p[0] = Element(type='from', value=p[2])
def p_transition(self, p): '''transition : LTRANSITION from to action pop push transout RTRANSITION''' p[0] = Element(type='transition', children=[p[2], p[3], p[4], p[5], p[6], p[7]])
def p_structure(self, p): '''structure : LSTRUCTURE type axiom tapes body RSTRUCTURE''' p[0] = Element(type='structure', children=[p[2], p[3], p[4], p[5]])
def p_name(self, p): '''name : LNAME VALUE RNAME''' p[0] = Element(type='left', value=p[2])
def p_type(self, p): '''type : LTYPE VALUE RTYPE''' p[0] = Element(type='type', value=p[2])
def p_tapes(self, p): '''tapes : empty | LTAPES VALUE RTAPES''' p[0] = None if len(p) < 3 else Element(type='tapes', value=p[2])
def p_transout(self, p): '''transout : empty | LTRANSOUT VALUE RTRANSOUT | LTRANSOUT empty RTRANSOUT''' p[0] = None if len(p) < 3 else Element(type='transout', value=p[2])
def p_automaton(self, p): '''automaton : LAUTOMATON inside RAUTOMATON''' p[0] = Element(type='automaton', children=[p[2]])
def p_write(self, p): '''write : empty | WRITE | LWRITE VALUE RWRITE''' p[0] = None if len(p) < 3 else Element(type='write', value=p[2])
def p_tag(self, p): '''tag : LTAG VALUE RTAG''' p[0] = Element(type='tag', value=p[2])
def p_move(self, p): '''move : empty | LMOVE VALUE RMOVE''' p[0] = None if len(p) < 3 else Element(type='move', value=p[2])
def p_production(self, p): '''production : LPRODUCTION left right RPRODUCTION''' p[0] = Element(type='production', children=[p[2], p[3]])
def p_left(self, p): '''left : LLEFT VALUE RLEFT''' p[0] = Element(type='left', value=p[2])
def test_ast(): calcul_visitor = Interpreter() op = Operation(Element(7), Element(3), Operator('+')) Operation(op, op, Operator('+')).accept(calcul_visitor) calcul_visitor.print_result()
def p_initial(self, p): '''initial : empty | INITIAL''' p[0] = None if p[1] is None else Element(type='initial')