-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQwhileLexer.py
98 lines (87 loc) · 1.57 KB
/
QwhileLexer.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
import ply.lex as lex
### Reserved words
reserved = {
### Preamble
# classical integer variables, which are only used for counting
'count': 'COUNT',
# quantum qubit variables
'qubit': 'QUBIT',
# parameters
'para': 'PARA',
### Statement
'skip': 'SKIP',
'M': 'MEAS',
'if': 'IF',
'then': 'THEN',
'else': 'ELSE',
'fi': 'FI',
'while': 'WHILE',
'do': 'DO',
'od': 'OD',
### Reserved gates
# Fixed gates
'H': 'H',
'CNOT': 'CNOT',
'X': 'X',
'Y': 'Y',
'Z': 'Z',
# parameterized gates, Pauli rotations
'Rx': 'RX',
'Ry': 'RY',
'Rz': 'RZ',
### Controlled-gate
'C_': 'CONTROLLED',
### Logical connectives
'and': 'AND',
'or': 'OR',
}
### Tokens
tokens = [
'LEQ',
'LE',
'PLUS',
'COLON',
'LBC',
'RBC',
'LB',
'RB',
'ASS',
'EQS',
'KET0',
'CMA',
'SCN',
'ZERO',
'ONE',
'QID',
] + list(reserved.values())
### Rules for tokens
t_LEQ = r'\<='
t_LE = r'\<'
t_PLUS = r'\+\+'
t_COLON = r':'
t_LBC = r'\('
t_RBC = r'\)'
t_LB = r'\['
t_RB = r'\]'
t_ASS = r':='
t_EQS = r'='
t_KET0 = r'\|0>'
t_CMA = r','
t_SCN = r';'
t_ZERO = r'0'
t_ONE = r'1'
def t_QID(t):
r'C_|[a-zA-Z\-_][a-zA-Z0-9_]*'
t.type = reserved.get(t.value, 'QID')
return t
t_ignore = ' \t'
t_ignore_COMMENT = r'\#.*'
### Track line numbers
def t_newline(t):
r'\n'
t.lexer.lineno += len(t.value)
### Handle lexing errors for illegal characters
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
lexer = lex.lex()