-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBashParser.py
More file actions
203 lines (164 loc) · 5.96 KB
/
BashParser.py
File metadata and controls
203 lines (164 loc) · 5.96 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import ply.yacc as yacc
from Lexer import Lexer
import sys
import AST
import TreePrinter
class BashParser(object):
def __init__(self):
self.lexer = Lexer()
self.lexer.build()
tokens = Lexer.tokens
precedence = (
("left", 'PLUS', 'MINUS'),
("left", 'TIMES', 'DIVIDE'),
)
def p_program(self, p):
"""program : instructions"""
p[0] = AST.Program(p[1])
p[0].printTree(0)
def p_instructions(self, p):
""" instructions : instruction instructions
| instruction """
if len(p) == 2:
p[0] = AST.Instructions(p[1], None)
if len(p) == 3:
p[0] = AST.Instructions(p[1], p[2])
def p_instruction_assignment(self, p):
"""instruction : identifier ASSIGNMENT expression
| identifier ASSIGNMENT double_paren_dollar_prefix_expression
"""
p[0] = AST.Assignment(p[1], p[3])
def p_instruction_if(self, p):
"""instruction : IF condition SEMICOLON THEN instructions SEMICOLON FI
| IF condition SEMICOLON THEN instructions SEMICOLON ELSE instructions SEMICOLON FI
"""
if len(p) == 8:
p[0] = AST.If(p[2], p[5], None)
elif len(p) == 11:
p[0] = AST.If(p[2], p[5], p[8])
def p_instruction_while(self, p):
'''instruction : WHILE condition SEMICOLON DO instructions DONE
'''
p[0] = AST.While(p[2], p[5])
def p_instruction_echo(self, p):
"""instruction : ECHO expression
"""
p[0] = AST.Echo(p[2])
def p_instruction_break(self, p):
""" instruction : BREAK """
p[0] = AST.Break()
def p_instruction_continue(self, p):
""" instruction : CONTINUE """
p[0] = AST.Continue()
def p_double_paren_dollar_prefix_expression(self, p):
""" double_paren_dollar_prefix_expression : DOLLAR DOUBLE_LPAREN expression DOUBLE_RPAREN
"""
if len(p) == 5:
p[0] = AST.DoubleParenArithmetic(p[3])
def p_double_paren_expression(self, p):
""" instruction : DOUBLE_LPAREN expression DOUBLE_RPAREN
"""
if len(p) == 4:
p[0] = AST.DoubleParenArithmetic(p[2])
def p_identifier(self, p):
"""identifier : IDENTIFIER"""
p[0] = AST.Identifier(p[1])
def p_comparators(self, p):
"""comparators : TEST_EQ
| TEST_NEQ
| TEST_LT
| TEST_GT
| EQUALS
| GT
| GTE
| LT
| LTE
"""
p[0] = p[1]
def p_condition(self, p):
""" condition : expression
| DOUBLE_LBRACKET expression DOUBLE_RBRACKET
| LBRACKET expression RBRACKET
"""
if len(p) == 4:
p[0] = p[2]
else:
p[0] = p[1]
def p_expression(self, p):
"""expression : identifier
| identifier DECREMENT
| identifier INCREMENT
| const
| expression PLUS expression
| expression MINUS expression
| expression TIMES expression
| expression DIVIDE expression
| expression comparators expression
| LPAREN expression RPAREN"""
if len(p) == 2:
p[0] = p[1]
elif len(p) == 4:
if p[1] == '(' and p[3] == ')':
p[0] = p[2]
else:
print("p2 {0}, p1 {1}, p3: {2}".format(p[2],p[1],p[3]))
p[0] = AST.BinExpr(p[2], p[1], p[3])
elif len(p) == 3:
if p[2] == '--':
p[0] = AST.BinExpr('-', p[1], AST.Number(1))
elif p[2] == '++':
p[0] = AST.BinExpr('+', p[1], AST.Number(1))
def p_const_number(self, p):
"""const : NUMBER"""
p[0] = AST.Number(p[1])
def p_const_string(self, p):
"""const : STRING"""
p[0] = AST.String(p[1])
def p_variable(self, p):
"""const : VARIABLE"""
p[0] = AST.Variable(p[1])
def p_const_boolean(self, p):
"""const : BOOLEAN"""
p[0] = AST.Boolean(p[1])
###
# Command Call
###
def p_instruction_command_call(self, p):
""" instruction : command switch_list argument_list
| command argument_list
| command switch_list
| command """
if len(p) == 2:
p[0] = AST.CommandCall(p[1], None, None)
if len(p) == 3:
if isinstance(p[2], AST.ArgumentList):
p[0] = AST.CommandCall(p[1], None, p[2])
elif isinstance(p[2], AST.SwitchList):
p[0] = AST.CommandCall(p[1], p[2], None)
else:
p[0] = AST.CommandCall(p[1], None, None)
if len(p) == 4:
p[0] = AST.CommandCall(p[1], p[2], p[3])
def p_command(self, p):
""" command : identifier """
p[0] = AST.Identifier(p[1])
def p_command_switch(self, p):
""" switch : MINUS identifier """
p[0] = AST.CommandSwitch(p[2])
def p_command_switch_list(self, p):
""" switch_list : switch switch_list"""
p[0] = AST.SwitchList([p[1]] + p[2].switch_list)
def p_command_switch_list_first(self, p):
""" switch_list : switch """
p[0] = AST.SwitchList([p[1]])
def p_command_argument(self, p):
""" argument : const """
p[0] = p[1]
def p_command_argument_list(self, p):
""" argument_list : argument argument_list"""
p[0] = AST.ArgumentList([p[1]] + p[2].argument_list)
def p_command_argument_list_first(self, p):
""" argument_list : argument """
p[0] = AST.ArgumentList([p[1]])
def p_error(self, p):
print("Syntax error in input! Token: {0}".format(p))