From: Eric S. Raymond Date: Fri, 23 Jan 2004 08:28:11 +0000 (+0000) Subject: Parses all known examples. X-Git-Tag: 1.0~173 X-Git-Url: http://thyrsus.com/gitweb/?a=commitdiff_plain;h=3b39fa612593d8a8cd95b8f117095847b2fb41ab;p=mad.git Parses all known examples. --- diff --git a/mad b/mad index 2c9cb21..d9a8594 100755 --- a/mad +++ b/mad @@ -2,7 +2,7 @@ reserved = ('CONDITIONAL', 'DIMENSION', 'END', 'FOR', 'FORMAT', 'FUNCTION', 'INTEGER', 'INTERNAL', 'OF', 'PRINT', 'PROGRAM', - 'READ', 'REMARK', 'THROUGH', 'TO', 'TRANSFER', 'VALUES', 'VECTOR', + 'READ', 'COMMENT', 'THROUGH', 'TO', 'TRANSFER', 'VALUES', 'VECTOR', 'WHENEVER') tokens = reserved + ( @@ -158,27 +158,30 @@ def p_internal(t): ## READ FORMAT def p_read_format(t): - 'statement : READ FORMAT varlist' - t[0] = ('read',) + tuple(t[3]) + 'statement : READ FORMAT NAME COMMA varlist' + t[0] = ('read', t[3]) + tuple(t[5]) ## PRINT FORMAT def p_print_format(t): - 'statement : PRINT FORMAT exprlist' - t[0] = ('print',) + tuple(t[3]) + 'statement : PRINT FORMAT NAME COMMA exprlist' + t[0] = ('print', t[3]) + tuple(t[5]) def p_exprlist(t): 'exprlist : expression' t[0] = [t[1]] def p_exprlist_continue(t): 'exprlist : exprlist COMMA expression' t[0] = t[1] + [t[3]] +def p_print_format0(t): + 'statement : PRINT FORMAT NAME' + t[0] = ('print', t[3]) -## REMARK +## R def p_remark1(t): - 'statement : REMARK STRING' + 'statement : COMMENT STRING' t[0] = ('remark', t[2]) def p_remark2(t): - 'statement : REMARK' + 'statement : COMMENT' t[0] = ('remark',) ## THROUGH @@ -449,7 +452,7 @@ def fmt_convert(fmt, write=True): str = str[i:] elif str[0] == 'H': if write: - res += str[1:count] + res += str[1:count+1] str = str[count+1:] elif str[0] == 'I': i = 1 @@ -492,7 +495,7 @@ def print_statement(statement): if len(statement) < 3: return 'printf("' + fmt + '");' else: - return 'printf("' + fmt + '", ' + ', '.join(statement[2:]) + ');' + return 'printf("' + fmt + '", ' + ', '.join(map(print_expr, statement[2:])) + ');' elif statement[0] == 'substitution': return statement[1] + ' = ' + print_expr(statement[2]) + ';' else: @@ -560,8 +563,8 @@ if __name__ == '__main__': # Grotty hack to remove continuation digits source = re.sub("\n [0-9] ", "\n ", source) # A grotty hack to transform remarks - source = re.sub(r'\tR\n', '\tREMARK\n', source) - source = re.sub(r'\tR (.*)', '\tREMARK $\\1$', source) + source = re.sub(r'\tR\n', '\tCOMMENT\n', source) + source = re.sub(r'\tR (.*)', '\tCOMMENT $\\1$', source) # Equally grotty hack to add end-of-statement delimiters source = re.sub('\n([A-Z]|\t)', ' ;\n\\1', source).rstrip() + ' ;\n' # Parse