Skip to content

Instantly share code, notes, and snippets.

@antsmartian
Created February 5, 2012 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antsmartian/1745911 to your computer and use it in GitHub Desktop.
Save antsmartian/1745911 to your computer and use it in GitHub Desktop.
Delimiter Parsing In groovy
/*
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