comparison ply-3.8/example/BASIC/basiclex.py @ 7267:343ff337a19b

<ais523> ` tar -xf ply-3.8.tar.gz
author HackBot
date Wed, 23 Mar 2016 02:40:16 +0000
parents
children
comparison
equal deleted inserted replaced
7266:61a39a120dee 7267:343ff337a19b
1 # An implementation of Dartmouth BASIC (1964)
2
3 from ply import *
4
5 keywords = (
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
10 tokens = 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
17 t_ignore = ' \t'
18
19 def t_REM(t):
20 r'REM .*'
21 return t
22
23 def 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
29 t_EQUALS = r'='
30 t_PLUS = r'\+'
31 t_MINUS = r'-'
32 t_TIMES = r'\*'
33 t_POWER = r'\^'
34 t_DIVIDE = r'/'
35 t_LPAREN = r'\('
36 t_RPAREN = r'\)'
37 t_LT = r'<'
38 t_LE = r'<='
39 t_GT = r'>'
40 t_GE = r'>='
41 t_NE = r'<>'
42 t_COMMA = r'\,'
43 t_SEMI = r';'
44 t_INTEGER = r'\d+'
45 t_FLOAT = r'((\d*\.\d+)(E[\+-]?\d+)?|([1-9]\d*E[\+-]?\d+))'
46 t_STRING = r'\".*?\"'
47
48 def t_NEWLINE(t):
49 r'\n'
50 t.lexer.lineno += 1
51 return t
52
53 def t_error(t):
54 print("Illegal character %s" % t.value[0])
55 t.lexer.skip(1)
56
57 lex.lex(debug=0)
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74