Created
January 7, 2016 12:49
-
-
Save huseyin/cb1e1e5ca0056c30c383 to your computer and use it in GitHub Desktop.
Detaylandırılmamış Veri Yapıları çalışması
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import sys | |
SYNTASTIC_ELEMENTS = { | |
'(': ')', | |
'[': ']', | |
'{': '}', | |
} | |
class Stack: | |
def __init__(self): | |
self.list = [] | |
def push(self, item): | |
self.list.append(item) | |
def pop(self): | |
return self.list.pop() | |
def peek(self): | |
return self.list[-1] | |
def size(self): | |
return len(self.list) | |
def isEmpty(self): | |
return self.list == [] | |
def controlParans(strLine): | |
stack=Stack() | |
for token in strLine: | |
if token in SYNTASTIC_ELEMENTS.keys(): | |
stack.push(token) | |
else: | |
if stack.isEmpty() and token in SYNTASTIC_ELEMENTS.values(): | |
idx = strLine.index(token) | |
sys.stdout.write("Hiç açılmamış blok. Kritik durum!\n") | |
sys.stdout.write("%s\n%*s\033[0;31m^\n\033[0m\n" %(strLine, idx, '')) | |
if not stack.isEmpty(): | |
if token == SYNTASTIC_ELEMENTS[stack.peek()]: | |
stack.pop() | |
if not stack.isEmpty(): | |
idx = strLine.index(stack.peek()) | |
sys.stdout.write("Kapanmayan bloklar mevcut!\n") | |
sys.stdout.write("%s\n%*s\033[0;31m^\n\033[0m\n" %(strLine, idx, '')) | |
if __name__ == '__main__': | |
controlParans("()") | |
controlParans("(") | |
controlParans(")") | |
controlParans("if ( []") | |
controlParans("if (control == true) { printf(\"Doğru\"); }") | |
controlParans("if (control == true) { printf(\"Doğru\"); ") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment