Created
February 5, 2012 14:49
-
-
Save antsmartian/1745911 to your computer and use it in GitHub Desktop.
Delimiter Parsing In groovy
This file contains 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
/* | |
These two inputs input and input2 are passed via command line | |
*/ | |
input2 = args[0] | |
//input for the size of Stack! | |
input = input2.size() | |
top = -1 | |
stack = new String[input] | |
boolean full = false | |
def push(String j) | |
{ | |
stack.putAt(++top,j) | |
} | |
def pop() | |
{ | |
stack.getAt(top--) | |
} | |
boolean isFull() | |
{ | |
top == input -1 | |
} | |
def map = ['{':'}','(':')','[':']'] | |
flag = true | |
input2.each { elements -> | |
if(isFull()) | |
println "Sorry the stack is full" | |
else | |
{ | |
//flag is used to skip the part of the program if the error occurs! | |
if( flag && elements in ['{','(','['] ) | |
push elements | |
if ( flag && elements in ['}',')',']'] ) | |
{ | |
if(map.find{ it.key == stack[top] }?.value == elements) | |
pop() | |
else | |
{ | |
println "Program terminates!!" | |
flag = false | |
} | |
} | |
} | |
} | |
if(flag) | |
println "Moved on to next phase of compiler!" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment