<s> import sys <EOL> tokens = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def t_PIPE ( t ) : <EOL> r'<STR_LIT>' <EOL> return t <EOL> def t_COLON ( t ) : <EOL> r'<STR_LIT:;>' <EOL> return t <EOL> def t_RREDIRECT ( t ) : <EOL> r'<STR_LIT:>>' <EOL> return t <EOL> t_LREDIRECT = r'<STR_LIT:<>' <EOL> t_ignore = "<STR_LIT>" <EOL> def t_newline ( t ) : <EOL> r'<STR_LIT>' <EOL> t . lexer . lineno += t . value . count ( "<STR_LIT:\n>" ) <EOL> def t_error ( t ) : <EOL> print ( "<STR_LIT>" % t . value [ <NUM_LIT:0> ] ) <EOL> t . lexer . skip ( <NUM_LIT:1> ) <EOL> def t_COMMAND ( t ) : <EOL> r'<STR_LIT>' <EOL> return t <EOL> import ply . lex as lex <EOL> lexer = lex . lex ( ) <EOL> import os <EOL> import utilities <EOL> cmds = dir ( utilities ) <EOL> def placeholder ( ) : <EOL> '<STR_LIT>' <EOL> pass <EOL> class expr : <EOL> def __init__ ( self , cmd , params ) : <EOL> self . cmd = cmd <EOL> self . params = params <EOL> def __str__ ( self ) : <EOL> return self . cmd + "<STR_LIT:U+0020>" + str ( self . params ) <EOL> def execute ( self ) : <EOL> if self . cmd in cmds : <EOL> try : <EOL> result = getattr ( utilities , self . cmd ) ( * self . params [ <NUM_LIT:0> : ] ) <EOL> return result <EOL> except Exception , e : <EOL> print e <EOL> else : <EOL> print "<STR_LIT>" + self . cmd <EOL> precedence = ( <EOL> ( '<STR_LIT:left>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:left>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> ) <EOL> def p_error ( p ) : <EOL> print p <EOL> def p_statement ( p ) : <EOL> '''<STR_LIT>''' <EOL> result = p [ <NUM_LIT:1> ] . execute ( ) <EOL> if result : <EOL> print result <EOL> def p_expression_pipe ( p ) : <EOL> '<STR_LIT>' <EOL> params = [ ] <EOL> params . extend ( p [ <NUM_LIT:3> ] . params ) <EOL> params . append ( p [ <NUM_LIT:1> ] . execute ( ) ) <EOL> p [ <NUM_LIT:0> ] = expr ( p [ <NUM_LIT:3> ] . cmd , params ) <EOL> def p_expression_colon ( p ) : <EOL> '<STR_LIT>' <EOL> result = p [ <NUM_LIT:1> ] . execute ( ) <EOL> if result : <EOL> print result <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:3> ] <EOL> def p_expression_lredirect ( p ) : <EOL> '<STR_LIT>' <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def p_expression_rredirect ( p ) : <EOL> '<STR_LIT>' <EOL> result = p [ <NUM_LIT:1> ] . execute ( ) <EOL> utilities . redirect ( result , p [ <NUM_LIT:3> ] ) <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> p [ <NUM_LIT:0> ] . execute = placeholder <EOL> def p_expression ( p ) : <EOL> '''<STR_LIT>''' <EOL> if len ( p ) == <NUM_LIT:2> : <EOL> p [ <NUM_LIT:0> ] = expr ( p [ <NUM_LIT:1> ] , [ ] ) <EOL> else : <EOL> p [ <NUM_LIT:1> ] . params . append ( p [ <NUM_LIT:2> ] ) <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> import ply . yacc as yacc <EOL> yacc . yacc ( ) <EOL> def parse ( s ) : <EOL> yacc . parse ( s ) </s>
