Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Anureet Kaur
- * CSC 135: Hw5 Interpreter
- */
- import java.util.Scanner;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- public class Hw5Interpreter {
- private static LexerSingleCharTokens lex;
- static int index = 0;
- static byte[] array = new byte[30000];
- public static void main(String[] args) {
- String prog = "";
- try {
- Scanner file = new Scanner(new File("helloworld.txt"));
- while (file.hasNextLine())
- prog = prog + file.nextLine();
- prog = prog.replaceAll("[^-+<>,\\.\\[\\]]", ""); // Deletes all non-bf chars
- } catch (FileNotFoundException e) {
- System.out.println("File bf.txt could not be read");
- return;
- }
- lex = new LexerSingleCharTokens(prog);
- try {
- ParseTreeNode tree = parseS();
- treeWalk(tree);
- } catch (IllegalArgumentException e) {
- System.out.println("Error");
- }
- }
- private static ParseTreeNode parseS() throws IllegalArgumentException {
- if (!lex.hasNext()) {
- ParseTreeNode c = new ParseTreeNode("", null, null, null);
- return new ParseTreeNode("S", c, null, null);
- }
- switch (lex.next()) {
- case ">":
- case "<":
- case "+":
- case "-":
- case ",":
- case ".":
- return new ParseTreeNode("S", parseC(), parseS(), null);
- case "[":
- return new ParseTreeNode("S", parseL(), parseS(), null);
- case "]":
- ParseTreeNode c = new ParseTreeNode("", null, null, null);
- return new ParseTreeNode("S", c, null, null);
- default:
- System.out.println("Parse Error: S " + lex.next());
- return null;
- }
- }
- private static ParseTreeNode parseC() throws IllegalArgumentException {
- if (!lex.hasNext())
- throw new IllegalArgumentException();
- switch (lex.next()) {
- case "+":
- lex.match("+");
- return new ParseTreeNode("C", new ParseTreeNode("+", null, null, null), null, null);
- case "-":
- lex.match("-");
- return new ParseTreeNode("C", new ParseTreeNode("-", null, null, null), null, null);
- case "<":
- lex.match("<");
- return new ParseTreeNode("C", new ParseTreeNode("<", null, null, null), null, null);
- case ">":
- lex.match(">");
- return new ParseTreeNode("C", new ParseTreeNode(">", null, null, null), null, null);
- case ",":
- lex.match(",");
- return new ParseTreeNode("C", new ParseTreeNode(",", null, null, null), null, null);
- case ".":
- lex.match(".");
- return new ParseTreeNode("C", new ParseTreeNode(".", null, null, null), null, null);
- default:
- throw new IllegalArgumentException();
- }
- }
- private static ParseTreeNode parseL() throws IllegalArgumentException {
- if (!lex.hasNext())
- throw new IllegalArgumentException();
- switch (lex.next()) {
- case "[":
- lex.match("[");
- ParseTreeNode returnNode = new ParseTreeNode("L", new ParseTreeNode("[", null, null, null), parseS(),
- new ParseTreeNode("]", null, null, null));
- if (!lex.match("]")) {
- System.out.println("Error" + lex.next());
- return null;
- }
- return returnNode;
- default:
- throw new IllegalArgumentException();
- }
- }
- private static void printTree(ParseTreeNode t) {
- if (t.firstChild == null)
- System.out.print(t.type);
- else {
- printTree(t.firstChild);
- if (t.secondChild != null)
- printTree(t.secondChild);
- if (t.thirdChild != null)
- printTree(t.thirdChild);
- }
- }
- public static void treeWalk(ParseTreeNode node) {
- if (node == null)
- return;
- switch (node.type) {
- case "+":
- array[index]++;
- break;
- case "-":
- array[index]--;
- break;
- case "<":
- index--;
- break;
- case ">":
- index++;
- break;
- case ".":
- System.out.print((char) array[index]);
- break;
- case ",":
- try {
- array[index] = ((byte) System.in.read());
- } catch (Exception e) {
- System.out.println("Failure: Unable to read byte.");
- }
- break;
- case "L":
- while (array[index] != 0) {
- treeWalk(node.firstChild);
- treeWalk(node.secondChild);
- treeWalk(node.thirdChild);
- }
- break;
- // the following accounts for the case where if the node is an S or a C, then we just continue
- default:
- treeWalk(node.firstChild);
- treeWalk(node.secondChild);
- treeWalk(node.thirdChild);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement