/******************************************************************************
* Name: COS 126 Staff
* NetID: cos126
* Precept: P00
*
* Fall 15 Programming Exam 2, part 2
* Description: Client class that simulates a stack machine to create
* languages.
*
* Started with code copied from Postfix.java in Lecture 12 slide 18:
* http://www.cs.princeton.edu/courses/archive/fall15/cos126/lectures
* /CS.12.StacksQueues.pdf
*
*****************************************************************************/
public class PostfixL {
public static void main(String[] args)
{
Stack<Language> stack = new Stack<Language>();
while (!StdIn.isEmpty())
{
String token = StdIn.readString();
if (token.equals("UNION"))
stack.push(stack.pop().union(stack.pop()));
else if (token.equals("CONCATENATE")) {
Language top = stack.pop();
Language nextToTop = stack.pop();
stack.push(nextToTop.concatenate(top));
}
else if (token.equals("CLOSURE")) {
int n = StdIn.readInt();
stack.push(stack.pop().closure(n));
}
else if (token.equals("PRINT")) {
StdOut.println(stack.pop());
}
else {
// new language formed from string
Language newLang = new Language(token);
stack.push(newLang);
}
}
}
}