-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathf4lex.py
136 lines (111 loc) · 1.79 KB
/
f4lex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from f4error import error
__author__ = 'hyst329'
from ply import *
# Lexing file for F4 programming language
tokens = [
# Some literals
'intlit',
'reallit',
'chrlit',
'strlit',
'ident',
# 2 char operators
'equals',
'leq',
'geq',
'aplus',
'aminus',
'larrow',
'rarrow',
# 1 char operators
'plus',
'minus',
'mult',
'divide',
'less',
'greater',
'assign',
'lparen',
'rparen',
'colon',
'semi',
'comma',
'point',
# New line
'newline'
]
keywords = (
'if',
'else',
'endif',
'fun',
'endfun',
'declare',
'loop',
'endloop',
'return',
'use',
'resize',
'size',
# Basic types
'int',
'real',
'logical',
'char',
'string',
# Basic operations
'in',
'out',
'debugvar'
)
tokens += keywords
t_ignore = ' \t'
t_equals = r'=='
t_leq = r'\<='
t_geq = r'\>='
t_less = r'\<'
t_greater = r'\>'
t_aplus = r'\+='
t_aminus = r'\-='
t_larrow = r'\<\-'
t_rarrow = r'\-\>'
t_plus = r'\+'
t_minus = r'\-'
t_mult = r'\*'
t_divide = r'\/'
t_assign = r'='
t_lparen = r'\('
t_rparen = r'\)'
t_colon = r'\:'
t_semi = r'\;'
t_comma = r'\,'
t_point = r'\.'
def t_ident(t):
r"""[a-zA-Z][a-zA-Z0-9]*"""
if t.value in keywords:
t.type = t.value
return t
def t_newline(t):
r"""\n"""
t.lexer.lineno += 1
return t
def t_reallit(t):
r"""\d+[\.\,]\d+"""
t.value = float(t.value)
return t
def t_intlit(t):
r"""\d+"""
t.value = int(t.value)
return t
def t_chrlit(t):
r"""'[^(')]'"""
t.value = t.value[1:-1]
return t
def t_strlit(t):
r""" "[^(")]+" """
t.value = t.value[1:-1]
return t
def t_error(t):
error('INVTOK', t.value[0])
t.lexer.skip(1)
lexer = lex.lex()