-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.py
155 lines (135 loc) · 2.83 KB
/
1.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import ply.lex as lex
import ply.yacc as yacc
# Kotlin Lexer
tokens = (
'ID',
'INT',
'FLOAT',
'STRING',
'PLUS',
'MINUS',
'TIMES',
'DIVIDE',
'ASSIGN',
'SEMICOLON',
'LPAREN',
'RPAREN',
'LBRACE',
'RBRACE',
'IF',
'ELSE',
'WHILE',
'FUN',
'VAR',
'LESSTHAN', # Add LESSTHAN token for '<'
'GREATERTHAN', # Add GREATERTHAN token for '>'
'EQUALS', # Add EQUALS token for '=='
)
t_PLUS = r'\+'
t_MINUS = r'-'
t_TIMES = r'\*'
t_DIVIDE = r'/'
t_ASSIGN = r'='
t_SEMICOLON = r';'
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_LBRACE = r'\{'
t_RBRACE = r'\}'
t_IF = r'if'
t_ELSE = r'else'
t_WHILE = r'while'
t_LESSTHAN = r'<'
t_GREATERTHAN = r'>'
t_EQUALS = r'=='
def t_ID(t):
r'[a-zA-Z_][a-zA-Z0-9_]*'
t.type = reserved.get(t.value, 'ID')
return t
def t_FLOAT(t):
r'\d+\.\d+'
t.value = float(t.value)
return t
def t_INT(t):
r'\d+'
t.value = int(t.value)
return t
def t_STRING(t):
r'"[^"]*"'
t.value = t.value[1:-1] # Remove double quotes
return t
t_ignore = ' \t\n'
def t_error(t):
print(f"Illegal character '{t.value[0]}'")
t.lexer.skip(1)
reserved = {
'if': 'IF',
'else': 'ELSE',
'while': 'WHILE',
'fun': 'FUN',
'var': 'VAR',
}
lexer = lex.lex()
# Kotlin Parser
def p_program(p):
'''
program : statement
| program statement
'''
pass
def p_statement(p):
'''
statement : var_declaration
| expression SEMICOLON
| if_statement
| while_loop
| function_definition
'''
pass
def p_var_declaration(p):
'''
var_declaration : VAR ID ASSIGN expression
'''
pass
def p_expression_binop(p):
'''
expression : expression PLUS expression
| expression MINUS expression
| expression TIMES expression
| expression DIVIDE expression
| expression LESSTHAN expression
| expression GREATERTHAN expression
| expression EQUALS expression
| LPAREN expression RPAREN
| INT
| FLOAT
| STRING
| ID
'''
pass
def p_if_statement(p):
'''
if_statement : IF LPAREN expression RPAREN LBRACE program RBRACE
| IF LPAREN expression RPAREN LBRACE program RBRACE ELSE LBRACE program RBRACE
'''
pass
def p_while_loop(p):
'''
while_loop : WHILE LPAREN expression RPAREN LBRACE program RBRACE
'''
pass
def p_function_definition(p):
'''
function_definition : FUN ID LPAREN RPAREN LBRACE program RBRACE
'''
pass
def p_error(p):
print("Syntax error")
parser = yacc.yacc()
while True:
try:
s = input('Kotlin >>> ')
except EOFError:
break
if not s:
continue
result = parser.parse(s)