<s> import sys <EOL> sys . path . insert ( <NUM_LIT:0> , "<STR_LIT>" ) <EOL> if sys . version_info [ <NUM_LIT:0> ] >= <NUM_LIT:3> : <EOL> raw_input = input <EOL> import ply . lex as lex <EOL> import ply . yacc as yacc <EOL> import os <EOL> class Parser ( object ) : <EOL> """<STR_LIT>""" <EOL> tokens = ( ) <EOL> precedence = ( ) <EOL> def __init__ ( self , ** kw ) : <EOL> self . debug = kw . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . names = { } <EOL> try : <EOL> modname = os . path . split ( os . path . splitext ( __file__ ) [ <NUM_LIT:0> ] ) [ <NUM_LIT:1> ] + "<STR_LIT:_>" + self . __class__ . __name__ <EOL> except : <EOL> modname = "<STR_LIT>" + "<STR_LIT:_>" + self . __class__ . __name__ <EOL> self . debugfile = modname + "<STR_LIT>" <EOL> self . tabmodule = modname + "<STR_LIT:_>" + "<STR_LIT>" <EOL> lex . lex ( module = self , debug = self . debug ) <EOL> yacc . yacc ( module = self , <EOL> debug = self . debug , <EOL> debugfile = self . debugfile , <EOL> tabmodule = self . tabmodule ) <EOL> def run ( self ) : <EOL> while <NUM_LIT:1> : <EOL> try : <EOL> s = raw_input ( '<STR_LIT>' ) <EOL> except EOFError : <EOL> break <EOL> if not s : continue <EOL> yacc . parse ( s ) <EOL> class Calc ( Parser ) : <EOL> tokens = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> ) <EOL> t_PLUS = r'<STR_LIT>' <EOL> t_MINUS = r'<STR_LIT:->' <EOL> t_EXP = r'<STR_LIT>' <EOL> t_TIMES = r'<STR_LIT>' <EOL> t_DIVIDE = r'<STR_LIT:/>' <EOL> t_EQUALS = r'<STR_LIT:=>' <EOL> t_LPAREN = r'<STR_LIT>' <EOL> t_RPAREN = r'<STR_LIT>' <EOL> t_NAME = r'<STR_LIT>' <EOL> def t_NUMBER ( self , t ) : <EOL> r'<STR_LIT>' <EOL> try : <EOL> t . value = int ( t . value ) <EOL> except ValueError : <EOL> print ( "<STR_LIT>" % t . value ) <EOL> t . value = <NUM_LIT:0> <EOL> return t <EOL> t_ignore = "<STR_LIT>" <EOL> def t_newline ( self , t ) : <EOL> r'<STR_LIT>' <EOL> t . lexer . lineno += t . value . count ( "<STR_LIT:\n>" ) <EOL> def t_error ( self , t ) : <EOL> print ( "<STR_LIT>" % t . value [ <NUM_LIT:0> ] ) <EOL> t . lexer . skip ( <NUM_LIT:1> ) <EOL> precedence = ( <EOL> ( '<STR_LIT:left>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:left>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:left>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:right>' , '<STR_LIT>' ) , <EOL> ) <EOL> def p_statement_assign ( self , p ) : <EOL> '<STR_LIT>' <EOL> self . names [ p [ <NUM_LIT:1> ] ] = p [ <NUM_LIT:3> ] <EOL> def p_statement_expr ( self , p ) : <EOL> '<STR_LIT>' <EOL> print ( p [ <NUM_LIT:1> ] ) <EOL> def p_expression_binop ( self , p ) : <EOL> """<STR_LIT>""" <EOL> if p [ <NUM_LIT:2> ] == '<STR_LIT:+>' : p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] + p [ <NUM_LIT:3> ] <EOL> elif p [ <NUM_LIT:2> ] == '<STR_LIT:->' : p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] - p [ <NUM_LIT:3> ] <EOL> elif p [ <NUM_LIT:2> ] == '<STR_LIT:*>' : p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] * p [ <NUM_LIT:3> ] <EOL> elif p [ <NUM_LIT:2> ] == '<STR_LIT:/>' : p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] / p [ <NUM_LIT:3> ] <EOL> elif p [ <NUM_LIT:2> ] == '<STR_LIT>' : p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] ** p [ <NUM_LIT:3> ] <EOL> def p_expression_uminus ( self , p ) : <EOL> '<STR_LIT>' <EOL> p [ <NUM_LIT:0> ] = - p [ <NUM_LIT:2> ] <EOL> def p_expression_group ( self , p ) : <EOL> '<STR_LIT>' <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:2> ] <EOL> def p_expression_number ( self , p ) : <EOL> '<STR_LIT>' <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> def p_expression_name ( self , p ) : <EOL> '<STR_LIT>' <EOL> try : <EOL> p [ <NUM_LIT:0> ] = self . names [ p [ <NUM_LIT:1> ] ] <EOL> except LookupError : <EOL> print ( "<STR_LIT>" % p [ <NUM_LIT:1> ] ) <EOL> p [ <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> def p_error ( self , p ) : <EOL> if p : <EOL> print ( "<STR_LIT>" % p . value ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> calc = Calc ( ) <EOL> calc . run ( ) </s>
