Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.InputStream;
- import java.util.*;
- /**
- * Created by tangmaolei on 9/26/16.
- */
- //all terminal symbols: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +, -, (, ), $, ++, --}
- enum TokenType {
- DIGIT,
- PLUS,
- MINUS,
- LEFTPAREN,
- RIGHTPAREN,
- REFERENCE,
- INCREMENT,
- DECREMENT
- }
- class Token {
- private TokenType mType;
- private int mValue;
- private int mLineNum;
- public Token(TokenType type, int line) {
- this.mType = type;
- this.mLineNum = line;
- }
- public Token(TokenType type, int value, int line) {
- this.mType = type;
- this.mValue = value;
- this.mLineNum = line;
- }
- public TokenType getType() {
- return mType;
- }
- public int getValue() {
- return mValue;
- }
- @Override
- public String toString() {
- return mType.name();
- }
- }
- public class Parse {
- // class Token {
- // private TokenType mType;
- // private int mValue;
- // private int mLineNum;
- //
- // public Token(TokenType type, int line) {
- // this.mType = type;
- // this.mLineNum = line;
- // }
- //
- // public Token(TokenType type, int value, int line) {
- // this.mType = type;
- // this.mValue = value;
- // this.mLineNum = line;
- // }
- //
- // public TokenType getType() {
- // return mType;
- // }
- //
- // public int getValue() {
- // return mValue;
- // }
- // }
- // convert a file stream into a string
- public static String convertStreamToString(InputStream is) {
- Scanner myScanner = new Scanner(is);
- myScanner.useDelimiter("\\A");
- if (myScanner.hasNext()) {
- return myScanner.next();
- }
- else
- return "";
- }
- public static void printErrorInfo(int lineNum) {
- System.out.printf("Parse error in line <%d>\n", lineNum);
- }
- //all terminal symbols: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +, -, (, ), $, ++, --}
- public static List<Token> scanner(String input) {
- List<Token> tokenList = new ArrayList<Token>();
- int index = 0;
- int range = input.length();
- int line = 1;
- while (index < range) {
- char pattern = input.charAt(index);
- switch (pattern) {
- case '+':
- // INCREMENT
- if (input.charAt(index+1) == '+') {
- tokenList.add(new Token(TokenType.INCREMENT, line));
- index = index + 2;
- }
- // PLUS
- else {
- tokenList.add(new Token(TokenType.PLUS, line));
- index++;
- }
- break;
- case '-':
- // DECREMENT
- if (input.charAt(index+1) == '-') {
- tokenList.add(new Token(TokenType.DECREMENT, line));
- index = index + 2;
- }
- // MINUS
- else {
- tokenList.add(new Token(TokenType.MINUS, line));
- index++;
- }
- break;
- // LEFT-PARENTHESES
- case '(':
- tokenList.add(new Token(TokenType.LEFTPAREN, line));
- index++;
- break;
- // RIGHT-PARENTHESES
- case ')':
- tokenList.add(new Token(TokenType.RIGHTPAREN, line));
- index++;
- break;
- // DIGIT
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- tokenList.add(new Token(TokenType.DIGIT, Character.getNumericValue(pattern), line));
- index++;
- break;
- // REFERENCE SIGN
- case '$':
- tokenList.add(new Token(TokenType.REFERENCE, line));
- index++;
- break;
- // COMMENTS
- case '#':
- while (pattern != '\n') {
- index++;
- pattern = input.charAt(index);
- }
- break;
- case '\t':
- case ' ':
- index++;
- break;
- case '\r':
- case '\n':
- index++;
- line++;
- break;
- default:
- printErrorInfo(line);
- // FOR TEST
- for ( Token token: tokenList.toArray(new Token[0])) {
- System.out.println(token);
- }
- System.exit(1);
- }
- }
- // FOR TEST
- // for ( Token token: tokenList.toArray(new Token[0])) {
- // System.out.println(token);
- // }
- return tokenList;
- }
- //TODO: paser
- public static String paser(List<Token> tokenList) {
- return "";
- }
- public static void main(String[] args) {
- // convert file stream into a string
- String inputString = convertStreamToString(System.in);
- // Test: input
- // System.out.println(inputString);
- List<Token> list = scanner(inputString);
- // FOR TEST
- // for ( Token token: list.toArray(new Token[0])) {
- // System.out.println(token);
- // }
- String result = paser(list);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement