From: Eric S. Raymond Date: Wed, 14 Jan 2004 04:23:36 +0000 (+0000) Subject: All the MAD rules about expressions. X-Git-Tag: 1.0~204 X-Git-Url: http://thyrsus.com/gitweb/?a=commitdiff_plain;h=b40873d69791da2be7aff0cb62169c8f857bc0f6;p=mad.git All the MAD rules about expressions. --- diff --git a/mad b/mad index f32c76c..446b8c7 100755 --- a/mad +++ b/mad @@ -5,15 +5,25 @@ tokens = ( 'LPAREN','RPAREN', 'PLUS','MINUS','TIMES','DIVIDE','EQUALS', 'NOT', 'OR', 'AND', 'THEN', 'EQV', + 'EQ', 'NE', 'LT', 'LE', 'GT', 'GE', + 'ABS', 'EXP', ) # Tokens -t_NOT = r'\.NOT\.' +t_ABS = r'\.ABS\.' +t_EXP = r'\.P\.' +t_NOT = r'\.NOT\.' t_OR = r'\.OR\.' t_AND = r'\.AND\.' t_THEN = r'\.OR\.' t_EQV = r'\.EQV\.' +t_LT = r'\.L\.' +t_LE = r'\.LE\.' +t_GT = r'\.G\.' +t_GE = r'\.GE\.' +t_EQ = r'\.E\.' +t_NE = r'.\NE\.' t_PLUS = r'\+' t_MINUS = r'-' t_TIMES = r'\*' @@ -47,9 +57,12 @@ precedence = ( ('left','OR'), ('left','AND'), ('right','NOT'), + ('left', 'EQ', 'NE', 'LT', 'LE', 'GT', 'GE'), ('left','PLUS','MINUS'), ('left','TIMES','DIVIDE'), ('right','UMINUS'), + ('left','EXP'), + ('right', 'ABS', 'UPLUS') ) def p_statement_assign(t): @@ -73,7 +86,15 @@ def p_expression_binop2(t): '''expression : expression OR expression | expression AND expression | expression THEN expression - | expression EQV expression''' + | expression EQV expression + | expression LT expression + | expression LE expression + | expression GT expression + | expression GE expression + | expression EQ expression + | expression NE expression + | expression EXP expression + ''' t[0] = (t[2][1:-1].lower(), t[1], t[3]) def p_expression_not(t): @@ -84,6 +105,14 @@ def p_expression_uminus(t): 'expression : MINUS expression %prec UMINUS' t[0] = ('-', t[2]) +def p_expression_uplus(t): + 'expression : PLUS expression %prec UPLUS' + t[0] = ('+', t[2]) + +def p_expression_abs(t): + 'expression : ABS expression %prec ABS' + t[0] = ('abs', t[2]) + def p_expression_group(t): 'expression : LPAREN expression RPAREN' t[0] = t[2] @@ -106,6 +135,8 @@ def p_error(t): import yacc yacc.yacc() +import readline + while 1: try: s = raw_input('calc > ')