------------------------------ dog ------------------------------ dog |
public interface INode { public void parse(Context context); }
// <program> ::= PROGRAM <command list> public class ProgramNode implements INode { private INode commandListNode; public void parse(Context context) { context.skipToken("PROGRAM"); commandListNode = new CommandListNode(); commandListNode.parse(context); } public String toString() { return "[PROGRAM " + commandListNode + "]"; } }
import java.util.Vector; // <command list> ::= <command>* END public class CommandListNode implements INode { private Vector list = new Vector(); public void parse(Context context) { while (true) { if (context.currentToken() == null) { System.err.println("Missing 'END'"); break; } else if ( context.currentToken().equals("END")) { context.skipToken("END"); break; } else { INode commandNode = new CommandNode(); commandNode.parse(context); list.add(commandNode); } } } public String toString() { return "" + list; } }
// <command> ::= <repeat command> | <primitive command> public class CommandNode implements INode { private INode node; public void parse(Context context) { if (context.currentToken().equals("REPEAT")) { node = new RepeatCommandNode(); node.parse(context); } else { node = new PrimitiveCommandNode(); node.parse(context); } } public String toString() { return node.toString(); } }
public class RepeatCommandNode implements INode { private int number; private INode commandListNode; public void parse(Context context) { context.skipToken("REPEAT"); number = context.currentNumber(); context.nextToken(); commandListNode = new CommandListNode(); commandListNode.parse(context); } public String toString() { return "[REPEAT " + number + " " + commandListNode + "]"; } }
// <primitive command> ::= PRINT <string> // | SPACE | BREAK | LINEBREAK public class PrimitiveCommandNode implements INode { private String name; private String text; public void parse(Context context) { name = context.currentToken(); context.skipToken(name); if (!name.equals("PRINT") && !name.equals("BREAK") && !name.equals("LINEBREAK") && !name.equals("SPACE")) { System.err.println("Undefined Command"); } if (name.equals("PRINT")) { text = context.currentToken(); name += text; context.nextToken(); } } public String toString() { return name; } }
import java.util.*; public class Context { private StringTokenizer tokenizer; private String currentToken; public Context(String text) { tokenizer = new StringTokenizer(text); nextToken(); } public String nextToken() { if (tokenizer.hasMoreTokens()) { currentToken = tokenizer.nextToken(); } else { currentToken = null; } return currentToken; } public String currentToken() { return currentToken; } public void skipToken(String token) { if (!token.equals(currentToken)) { System.err.println("Warning: " + token + " is expected, but " + currentToken + " is found."); } nextToken(); } public int currentNumber() { int number = 0; try { number = Integer.parseInt(currentToken); } catch (NumberFormatException e) { System.err.println("Warning: " + e); } return number; } }
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { try { BufferedReader reader = new BufferedReader(new FileReader(args[0])); String text; while ((text = reader.readLine()) != null) { System.out.println("text = \"" + text + "\""); INode node = new ProgramNode(); node.parse(new Context(text)); System.out.println("node = " + node); } } catch (ArrayIndexOutOfBoundsException e) { System.err.println( "Usage: java Main yourprogram.txt"); } catch (Exception e) { e.printStackTrace(); } } }
PROGRAM PRINT xxx END PROGRAM REPEAT 4 PRINT xxx END END PROGRAM REPEAT 4 PRINT xxx PRINT "yyy" END END
$ java Main program.txt text = "PROGRAM PRINT xxx END" node = [PROGRAM [PRINTxxx]] text = "PROGRAM REPEAT 4 PRINT xxx END END" node = [PROGRAM [[REPEAT 4 [PRINTxxx]]]] text = "PROGRAM REPEAT 4 PRINT xxx PRINT "yyy" END END" node = [PROGRAM [[REPEAT 4 [PRINTxxx, PRINT"yyy"]]]] |
public interface INode { public void parse(Context context); public void execute(); }
// <program> ::= PROGRAM <command list> public class ProgramNode implements INode { private INode commandListNode; public void parse(Context context) { context.skipToken("PROGRAM"); commandListNode = new CommandListNode(); commandListNode.parse(context); } public void execute() { commandListNode.execute(); } public String toString() { return "[PROGRAM " + commandListNode + "]"; } }
import java.util.*; // <command list> ::= <command>* END public class CommandListNode implements INode { private Vector list = new Vector(); private INode commandNode; public void parse(Context context) { while (true) { if (context.currentToken() == null) { System.err.println("Missing 'END'"); break; } else if(context.currentToken().equals("END")) { context.skipToken("END"); break; } else { commandNode = new CommandNode(); commandNode.parse(context); list.add(commandNode); } } } public void execute() { Iterator it = list.iterator(); while (it.hasNext()) { ((CommandNode)it.next()).execute(); } } public String toString() { return "" + list; } }
// <command> ::= <repeat command> | <primitive command> public class CommandNode implements INode { private INode node; public void parse(Context context) { if (context.currentToken().equals("REPEAT")) { node = new RepeatCommandNode(); node.parse(context); } else { node = new PrimitiveCommandNode(); node.parse(context); } } public void execute() { node.execute(); } public String toString() { return node.toString(); } }
// <primitive command> ::= PRINT <string> // | SPACE | BREAK | LINEBREAK public class PrimitiveCommandNode implements INode { private String name; private String text; public void parse(Context context) { name = context.currentToken(); context.skipToken(name); if (!name.equals("PRINT") && !name.equals("BREAK") && !name.equals("LINEBREAK") && !name.equals("SPACE")) { System.err.println("Undefined Command"); } if (name.equals("PRINT")) { text = context.currentToken(); context.nextToken(); } } public void execute() { if(name.equals("PRINT")) System.out.print(text); else if(name.equals("SPACE")) System.out.print(" "); else if(name.equals("BREAK")) System.out.println(); else if(name.equals("LINEBREAK")) System.out.println( "\n------------------------------"); } public String toString() { return name; } }
public class RepeatCommandNode implements INode { private int number; private INode commandListNode; public void parse(Context context) { context.skipToken("REPEAT"); number = context.currentNumber(); context.nextToken(); commandListNode = new CommandListNode(); commandListNode.parse(context); } public void execute() { for(int i = 0; i < number; i++) commandListNode.execute(); } public String toString() { return "[REPEAT " + number + " " + commandListNode + "]"; } }
import java.util.*; public class Context { private StringTokenizer tokenizer; private String currentToken; public Context(String text) { tokenizer = new StringTokenizer(text); nextToken(); } public String nextToken() { if (tokenizer.hasMoreTokens()) { currentToken = tokenizer.nextToken(); } else { currentToken = null; } return currentToken; } public String currentToken() { return currentToken; } public void skipToken(String token) { if (!token.equals(currentToken)) { System.err.println("Warning: " + token + " is expected, but " + currentToken + " is found."); } nextToken(); } public int currentNumber() { int number = 0; try { number = Integer.parseInt(currentToken); } catch (NumberFormatException e) { System.err.println("Warning: " + e); } return number; } }
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { try { BufferedReader reader = new BufferedReader( new FileReader(args[0])); String text; while ((text = reader.readLine()) != null) { System.out.println("text = \"" + text + "\""); INode node = new ProgramNode(); node.parse(new Context(text)); node.execute(); } } catch (ArrayIndexOutOfBoundsException e) { System.err.println( "Useage: java Main yourprogram.txt"); } catch (Exception e) { e.printStackTrace(); } } }
PROGRAM REPEAT 4 LINEBREAK PRINT justin SPACE PRINT momor LINEBREAK END END
$ java Main program.txt text = "PROGRAM REPEAT 4 LINEBREAK PRINT justin SPACE PRINT momor LINEBREAK END END" ------------------------------ justin momor ------------------------------ ------------------------------ justin momor ------------------------------ ------------------------------ justin momor ------------------------------ ------------------------------ justin momor ------------------------------ |