-
Notifications
You must be signed in to change notification settings - Fork 1
/
smsk.py
36 lines (30 loc) · 1.34 KB
/
smsk.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
from sys import stdin
from lark import Lark, Transformer
g = ''' start: t*
t: T_ID | T_INTLITERAL | T_DOUBLELITERAL | T_STRINGLITERAL | T_BOOLEANLITERAL | RESERVED
T_ID : /[a-z][a-z0-9_]{,30}/i
T_INTLITERAL : /[0-9]+/i | /0x[a-f0-9]+/i
T_DOUBLELITERAL: /(\\d)+\\.(\\d)*/ | /(\\d)*\\.(\\d)+/ | /(\\d)+\.(\\d)*E[+-]?(\\d)+/
T_STRINGLITERAL : /"[^\\n"]*"/
T_BOOLEANLITERAL.2 : "true" | "false"
RESERVED.2 : "{" | "}" | "+" | "-" | "*" | "/" | "%" | "<" | "<=" | ">" | ">=" | "==" | "=" | "!=" | "&&" | "||"
| "!" | ";" | "," | "." | "[]" | "[" | "]" | "(" | ")" | "void" | "interface" | "double" | "bool" | "string"
| "class" | "int" | "null" | "this" | "extend" | "implement" | "for" | "while" | "if" | "else" | "return"
| "break" | "new" | "NewArray" | "Print" | "ReadInteger" | "ReadLine"
SL_COMMENT: "//" /[^\\n]*/ "\\n"
ML_COMMENT: "/*" /(\\*(?!\\/)|[^*])*/ "*/"
%ignore SL_COMMENT
%ignore ML_COMMENT
%import common.WS
%ignore WS
'''
class CodeGen(Transformer):
def t(self, args):
token = args[0]
if token.type == 'RESERVED':
print(token.value)
else:
print(token.type + ' ' + token.value)
l = Lark(g, transformer=CodeGen(), parser='lalr')
for line in stdin:
l.parse(line)