-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCellChainLex.py
52 lines (42 loc) · 992 Bytes
/
CellChainLex.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
from ply import lex
__author__ = 'mfansler'
# Tokens
tokens = (
'PARTIAL', 'DELTA',
'GROUP', 'IDENTIFIER',
'PLUS', 'EQUALS', 'OTIMES',
'LBRACE', 'RBRACE',
'LPAREN', 'RPAREN',
'COMMA'
)
# Token definitions
t_PARTIAL = r'\\partial'
t_DELTA = r'\\Delta'
t_OTIMES = r'\\otimes'
t_IDENTIFIER = r'[a-zA-Z0-9]+(_{[^}]+})?(\^{[^}]+})?'
t_PLUS = r'\+'
t_EQUALS = r'='
t_LBRACE = r'\\{'
t_RBRACE = r'\\}'
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_COMMA = r','
# Ignored characters
t_ignore = " \t&"
def t_GROUP(t):
r'C_{([0-9]+)}\([a-zA-Z0-9]*\)'
t.value = int(t.lexer.lexmatch.group(2))
return t
def t_newline(t):
r'\n+'
t.lexer.lineno += t.value.count("\n")
# Ignore comment lines
def t_comment(t):
r'%.*'
def t_error(t):
print "Illegal character '%s' on line %d" % (t.value[0], t.lexer.lineno)
t.lexer.skip(1)
# Build the lexer
lexer = lex.lex()
if __name__ == "__main__":
lex.runmain(lexer)