1# An implementation of Dartmouth BASIC (1964)
2
3from ply import *
4
5keywords = (
6    'LET','READ','DATA','PRINT','GOTO','IF','THEN','FOR','NEXT','TO','STEP',
7    'END','STOP','DEF','GOSUB','DIM','REM','RETURN','RUN','LIST','NEW',
8)
9
10tokens = keywords + (
11     'EQUALS','PLUS','MINUS','TIMES','DIVIDE','POWER',
12     'LPAREN','RPAREN','LT','LE','GT','GE','NE',
13     'COMMA','SEMI', 'INTEGER','FLOAT', 'STRING',
14     'ID','NEWLINE'
15)
16
17t_ignore = ' \t'
18
19def t_REM(t):
20    r'REM .*'
21    return t
22
23def t_ID(t):
24    r'[A-Z][A-Z0-9]*'
25    if t.value in keywords:
26        t.type = t.value
27    return t
28
29t_EQUALS  = r'='
30t_PLUS    = r'\+'
31t_MINUS   = r'-'
32t_TIMES   = r'\*'
33t_POWER   = r'\^'
34t_DIVIDE  = r'/'
35t_LPAREN  = r'\('
36t_RPAREN  = r'\)'
37t_LT      = r'<'
38t_LE      = r'<='
39t_GT      = r'>'
40t_GE      = r'>='
41t_NE      = r'<>'
42t_COMMA   = r'\,'
43t_SEMI    = r';'
44t_INTEGER = r'\d+'
45t_FLOAT   = r'((\d*\.\d+)(E[\+-]?\d+)?|([1-9]\d*E[\+-]?\d+))'
46t_STRING  = r'\".*?\"'
47
48def t_NEWLINE(t):
49    r'\n'
50    t.lexer.lineno += 1
51    return t
52
53def t_error(t):
54    print("Illegal character %s" % t.value[0])
55    t.lexer.skip(1)
56
57lex.lex(debug=0)
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75