Skip to content

Commit

Permalink
Refactor RegExp (#7)
Browse files Browse the repository at this point in the history
* optimize RegExp
* add output ref
  • Loading branch information
Hagar-Usama authored Oct 15, 2022
1 parent 5993c69 commit b48cd87
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 110 deletions.
57 changes: 57 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

.DS_Store
.huskyrc.json
out
log.log
**/node_modules
*.pyc
*.vsix
**/.vscode/.ropeproject/**
**/testFiles/**/.cache/**
*.noseids
.nyc_output
.vscode-test
__pycache__
npm-debug.log
**/.mypy_cache/**
!yarn.lock
coverage/
cucumber-report.json
**/.vscode-test/**
**/.vscode test/**
**/.vscode-smoke/**
**/.venv*/
port.txt
precommit.hook
pythonFiles/lib/**
debug_coverage*/**
languageServer/**
languageServer.*/**
bin/**
obj/**
.pytest_cache
tmp/**
.python-version
.vs/
test-results*.xml
xunit-test-results.xml
build/ci/performance/performance-results.json
!build/
debug*.log
debugpy*.log
pydevd*.log
nodeLanguageServer/**
nodeLanguageServer.*/**
dist/**
# translation files
*.xlf
*.nls.*.json
*.i18n.json
132 changes: 24 additions & 108 deletions modules/RegExp.py
Original file line number Diff line number Diff line change
@@ -1,72 +1,50 @@
import ast
from modules.color_print import print_blue, print_green, print_purple, print_red, print_yellow, ANSI_RED, ANSI_RESET


STAR = 'STAR'
CONCAT = 'CONCAT'
OR = 'OR'
QSNMRK = 'QSNMRK'
QSNMRK = 'QSNMRK'
PLUS = 'PLUS'


class RegExp:

def __init__(self, exp_list, operators, star= STAR):
def __init__(self, exp_list, operators, star=STAR):

#self.exp = exp_str
self.exp_list = exp_list
self.cat_list = []
self.star = star
self.operators = operators
self.post_list = []


def handle_exp_2(self):

exp_list = []

exp = self.exp_list
op = self.operators

last = exp[-1]

while len(exp) > 1:

if len(exp) > 1:
x = exp[0]
y = exp[1]


#print(x,y)
#print(x in op)
#print(y in op)


x, y = exp[0], exp[1]
if y not in op:
#print("y is NOT operator")
if (x not in op) or (x == STAR) or (x == PLUS) or (x == QSNMRK) or (x == ")"):
exp_list.append(exp.pop(0))
exp_list.append("CONCAT")
#print("CONCAT")
else:
exp_list.append(exp.pop(0))

else:
# y is operator
#print("y is operator")

if (x == STAR) or (x == PLUS) or (x == QSNMRK):
exp_list.append(exp.pop(0))
exp_list.append("CONCAT")
#print("CONCAT")


elif (x not in op) and y == '(':
exp_list.append(exp.pop(0))
exp_list.append("CONCAT")
#print("CONCAT")

else:
exp_list.append(exp.pop(0))
exp_list.append(exp.pop(0))

exp_list.append(exp.pop(0))
self.cat_list = exp_list
self.operators.add('CONCAT')
Expand All @@ -75,7 +53,7 @@ def handle_exp_2(self):
def handle_exp(self):

exp_list = []

exp = self.exp_list
op = self.operators

Expand All @@ -86,90 +64,60 @@ def handle_exp(self):
QSNMRK = "QSNMRK"
LBRKT = "("
RBRKT = ")"

while len(exp) > 1:

if len(exp) > 1:
x = exp[0]
y = exp[1]

#print(x,y)

if (x == STAR) or (x == PLUS) or (x == QSNMRK):
if (y == STAR) or (y == PLUS) or (y == QSNMRK) or (y == LBRKT):
#print_red("case 1")
exp_list.append(exp.pop(0))
exp_list.append("CONCAT")

#print(CONCAT)

elif y not in op:
#print_red("case 2")
exp_list.append(exp.pop(0))
exp_list.append("CONCAT")

#print(CONCAT)
else:
#print_red("case 3")
exp_list.append(exp.pop(0))

elif x == OR:
if y == LBRKT:
#print_red("case 4")
exp_list.append(exp.pop(0))
#exp_list.append("CONCAT")
elif (y == STAR) or (y == PLUS) or (y == QSNMRK) or (y == OR) or (y == RBRKT):
#print_red("case 5")
print("Error!")
else:
#print_red("case 6")
exp_list.append(exp.pop(0))

elif x == LBRKT:

if (y == STAR) or (y == PLUS) or (y == QSNMRK) or (y == OR):
#print_red("case 7")
print("Error!")

else:
#print_red("case 8")
exp_list.append(exp.pop(0))

elif x == RBRKT:
#if (y == LBRKT) or (y not in op):
if (y == LBRKT) or (y not in op):
#print_red("case 9")

exp_list.append(exp.pop(0))
exp_list.append("CONCAT")

#print(CONCAT)
else:
#print_red("case 10")
exp_list.append(exp.pop(0))

elif x not in op:
# x is character
if (y == LBRKT) or (y not in op):
#print_red("case 11")
exp_list.append(exp.pop(0))
exp_list.append("CONCAT")

#print(CONCAT)

else:
#print_red("case 12")
exp_list.append(exp.pop(0))



exp_list.append(exp.pop(0))
self.cat_list = exp_list
self.operators.add('CONCAT')
return exp_list




def get_postfix(self):
"""
opstack = []
Expand Down Expand Up @@ -198,21 +146,17 @@ def get_postfix(self):
y = compare(x,i)
opstack.push(i)
"""
opstack = []
output = []
operators = self.operators

exp = self.cat_list
#print(f"catlist = {self.cat_list}")

for i in exp:
if i == "(":
opstack.append(i)
elif i == ')':
# pop stack til pop = (
# pop stack til pop = (
while opstack:
p = opstack.pop(-1)
if p == '(':
Expand All @@ -226,35 +170,24 @@ def get_postfix(self):
while opstack:
p_stack = self.get_precedence(opstack[-1])
p_current = self.get_precedence(i)
#print(f"current: {i},{p_current} , stack {opstack[-1]},{p_stack}")

if p_current >= p_stack:
output.append(opstack.pop(-1))

else:
if p_current < p_stack:
break

output.append(opstack.pop(-1))
opstack.append(i)
#print(f"opstack : {opstack}")


while opstack:
output.append(opstack.pop(-1))

return output
while opstack:
output.append(opstack.pop(-1))

return output

def get_precedence(self, op):

#print(f"operand entered = {op}")
STAR = "STAR"
PLUS = "PLUS"
QSTMRK = "QSTMRK"
CONCAT = "CONCAT"
OR = "OR"



if (op == STAR) or (op == PLUS) or (op == QSNMRK):
return 2
elif (op == CONCAT):
Expand All @@ -264,21 +197,16 @@ def get_precedence(self, op):
else:
return 50

def compare(self, i ,opstack):
def compare(self, i, opstack):
if not opstack:
return True

return self.get_precedence(i) < self.get_precedence(opstack[-1])



def postfix_me(exp_dict, operators):

"""
consider removing operators (redundant)
"""

new_dict = {}
tuple_list = []
"""
for k, v in ((k, exp_dict[k]) for k in reversed(exp_dict)):
Expand All @@ -289,24 +217,12 @@ def postfix_me(exp_dict, operators):
new_dict[k] = post
"""


for key, value in exp_dict.items():

r = RegExp(value, operators, STAR)
exp2 = r.handle_exp()
#print(exp2)
r.handle_exp()
post = r.get_postfix()
#new_dict[key] = post
tuple_list.insert(0,(key,post))
tuple_list.insert(0, (key, post))


post_dict = dict(tuple_list)

#print_blue(tuple_list)
#print_green(post_dict)
return post_dict





1 change: 0 additions & 1 deletion outputs/lexemes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,4 @@ mnt
end
write
count
)
end
1 change: 0 additions & 1 deletion outputs/tokens.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,4 @@
'end'
'write'
'id'
')'
'end'
Loading

0 comments on commit b48cd87

Please sign in to comment.