-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifelse.py
180 lines (146 loc) · 4.09 KB
/
ifelse.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
tokens = ('NAME', 'INT', 'DOUBLE', 'GREATER', 'LESS', 'PLUS', 'MINUS', 'TIMES', 'DIVIDE', 'EQUALS', 'LPAREN', 'RPAREN', 'CHAR', 'TRUE', 'FALSE', 'GREATEQ', 'LESSEQ', 'EQEQ', 'NOTEQ', 'AND', 'OR', 'COLON', 'IF', 'ELSE')
# Reserved words
reserved = {'if' : 'IF', 'else' : 'ELSE'}
# Tokens
t_PLUS = r'\+'
t_MINUS = r'-'
t_TIMES = r'\*'
t_DIVIDE = r'/'
t_EQUALS = r'='
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'
t_CHAR = r'\'[a-zA-Z0-9_+\*\- :,\s]*\''
t_TRUE = r'\t'
t_FALSE = r'\f'
t_GREATER = r'>'
t_LESS = r'<'
t_GREATEQ = r'>='
t_LESSEQ = r'<='
t_EQEQ = r'=='
t_NOTEQ = r'!='
t_AND = r'&'
t_OR = r'\|'
t_COLON = r':'
def t_DOUBLE(t):
r'[0-9]+\.[0-9]+'
try:
t.value = float(t.value)
except ValueError:
print("Integer value too large %d", t.value)
t.value = 0
return t
def t_INT(t):
r'[0-9]+'
try:
t.value = int(t.value)
except ValueError:
print("Double value too large %d", t.value)
t.value = 0
return t
# Ignored characters
t_ignore = " \t"
def t_IF(t):
r'if'
return t
def t_ELSE(t):
r'else'
return t
def t_newline(t):
r'\n+'
t.lexer.lineno += t.value.count("\n")
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
# Build the lexer
import ply.lex as lex
lexer = lex.lex()
# Parsing rules
precedence = (
('left','AND','OR'),
('left','GREATER','LESS', 'GREATEQ', 'LESSEQ', 'EQEQ', 'NOTEQ'),
('left','PLUS','MINUS'),
('left','TIMES','DIVIDE'),
('right','UMINUS')
)
# dictionary of names
names = { }
def p_statement_assign(t):
'statement : NAME EQUALS expression'
names[t[1]] = t[3]
def p_statement_expr(t):
'statement : expression'
print(t[1])
def p_statement_if(t):
'statement : IF LPAREN expression RPAREN COLON statement'
pass
def p_statement_else(t):
'statement : IF LPAREN expression RPAREN COLON statement ELSE COLON statement '
pass
def p_expression_ariop(t):
'''expression : expression PLUS expression
| expression MINUS expression
| expression TIMES expression
| expression DIVIDE expression'''
if t[2] == '+' : t[0] = t[1] + t[3]
elif t[2] == '-': t[0] = t[1] - t[3]
elif t[2] == '*': t[0] = t[1] * t[3]
elif t[2] == '/': t[0] = t[1] / t[3]
def p_expression_logop(t):
'''expression : expression GREATER expression
| expression LESS expression
| expression GREATEQ expression
| expression LESSEQ expression
| expression EQEQ expression
| expression NOTEQ expression
| expression AND expression
| expression OR expression'''
if t[2] == '>' : t[0] = t[1] > t[3]
elif t[2] == '<': t[0] = t[1] < t[3]
elif t[2] == '>=': t[0] = t[1] >= t[3]
elif t[2] == '<=': t[0] = t[1] <= t[3]
elif t[2] == '==': t[0] = t[1] == t[3]
elif t[2] == '!=': t[0] = t[1] != t[3]
elif t[2] == '&': t[0] = t[1] and t[3]
elif t[2] == '|': t[0] = t[1] or t[3]
def p_expression_uminus(t):
'expression : MINUS expression %prec UMINUS'
t[0] = -t[2]
def p_expression_group(t):
'expression : LPAREN expression RPAREN'
t[0] = t[2]
def p_expression_int(t):
'expression : INT'
t[0] = t[1]
def p_expression_double(t):
'expression : DOUBLE'
t[0] = t[1]
def p_expression_char(t):
'expression : CHAR'
t[0] = t[1]
def p_expression_name(t):
'expression : NAME'
try:
t[0] = names[t[1]]
except LookupError:
t[0] = 0
def p_expression_bool(t):
'expression : bool'
t[0] = t[1]
def p_true(t):
'bool : TRUE'
t[0] = True
def p_false(t):
'bool : FALSE'
t[0] = False
def p_error(t):
print("Syntax error at '%s'" % t.value)
import ply.yacc as yacc
parser = yacc.yacc()
# input file containing the Python if else code
print("Reading input from text file... \n")
myFile = open("input.txt","r+")
inputFile = myFile.read()
print(inputFile)
parser.parse(inputFile)
print("Grammar has been verified...")