Created
February 18, 2015 21:54
-
-
Save Bjornhall/96dadd7acdbd1fa8aabd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package p1; | |
import java.util.ArrayList; | |
public class Parser { | |
private static int index = 0; | |
private static Token token; | |
private static ArrayList<Token> tokens = testTokens(); | |
public static ArrayList<Token> testTokens(){ | |
ArrayList<Token> token = new ArrayList<Token>(); | |
Token a1 = new Token(Token.TokenCode.ID, "var"); | |
Token a2 = new Token(Token.TokenCode.ASSIGN, "="); | |
Token a3 = new Token(Token.TokenCode.INT, "3"); | |
Token a4 = new Token(Token.TokenCode.SEMICOL, ";"); | |
Token a5 = new Token(Token.TokenCode.ID, "b"); | |
Token a6 = new Token(Token.TokenCode.ASSIGN, "="); | |
Token a7 = new Token(Token.TokenCode.INT, "4"); | |
Token a8 = new Token(Token.TokenCode.MULT, "*"); | |
Token a9 = new Token(Token.TokenCode.LPAREN, "("); | |
Token a10 = new Token(Token.TokenCode.INT, "7"); | |
Token a11 = new Token(Token.TokenCode.MINUS, "-"); | |
Token a12 = new Token(Token.TokenCode.ID, "var"); | |
Token a13 = new Token(Token.TokenCode.RPAREN, ")"); | |
Token a14 = new Token(Token.TokenCode.SEMICOL, ";"); | |
Token a15 = new Token(Token.TokenCode.PRINT, "print"); | |
Token a16 = new Token(Token.TokenCode.SEMICOL, ";"); | |
Token a17 = new Token(Token.TokenCode.END, "end"); | |
// Token a1 = new Token(Token.TokenCode.INT, "3"); | |
// Token a2 = new Token(Token.TokenCode.ASSIGN, "="); | |
// Token a3 = new Token(Token.TokenCode.ID, "var"); | |
// Token a4 = new Token(Token.TokenCode.INT, "4"); | |
// Token a5 = new Token(Token.TokenCode.SEMICOL, ";"); | |
// Token a17 = new Token(Token.TokenCode.END, "end"); | |
token.add(a1); | |
token.add(a2); | |
token.add(a3); | |
token.add(a4); | |
token.add(a5); | |
token.add(a6); | |
token.add(a7); | |
token.add(a8); | |
token.add(a10); | |
token.add(a11); | |
token.add(a12); | |
token.add(a13); | |
token.add(a14); | |
token.add(a15); | |
token.add(a16); | |
token.add(a17); | |
return token; | |
} | |
private static void ParserError() { | |
System.out.println ("Syntax error!"); | |
System.exit(0); | |
} | |
private static Token nextToken() { | |
ArrayList<Token> myToken = tokens; | |
Token t = myToken.get(index); | |
if(index < myToken.size() - 1) { | |
index++; | |
} | |
return t; | |
} | |
public static void parse() { | |
token = nextToken(); | |
System.out.println(" THIS IS A NEW GUY --> "+ token.code + " " + token.data); | |
// if the nextToken has TokenCode ERROR | |
if(token.code == Token.TokenCode.ERROR) { | |
ParserError(); | |
} | |
if(token.code == Token.TokenCode.END){ | |
//the end | |
System.out.println("THE END"); | |
} | |
if(token.code == Token.TokenCode.SEMICOL) { | |
token = nextToken(); | |
System.out.println("printing semicol"); | |
parse(); | |
} | |
else { | |
Statement(); | |
parse(); | |
} | |
} | |
public static void Statement(){ | |
// if the nextToken has TokenCode ERROR | |
if(token.code == Token.TokenCode.ERROR) { | |
ParserError(); | |
} | |
if(token.code == Token.TokenCode.PRINT){ | |
token = nextToken(); | |
// if erroor... | |
System.out.println("PRINT " + token.data); | |
} | |
if(token.code == Token.TokenCode.ID) { | |
//Factor(); | |
Expr(); | |
token = nextToken(); | |
if(token.code == Token.TokenCode.ASSIGN) { | |
token = nextToken(); | |
Expr(); | |
System.out.println("ASSIGN"); | |
System.out.println(token.code + token.data); | |
// token = nextToken(); | |
} | |
else { | |
// handle var INT .... | |
ParserError(); | |
} | |
} | |
if(token.code == Token.TokenCode.SEMICOL) { | |
//do nothing | |
} | |
else if(token.code != Token.TokenCode.ID || token.code != Token.TokenCode.PRINT){ | |
ParserError(); | |
} | |
} | |
// Expr -> Term | Term + Expr | Term - Expr | |
public static void Expr() { | |
// if the nextToken has TokenCode ERROR | |
if(token.code == Token.TokenCode.ERROR) { | |
ParserError(); | |
} | |
Term(); | |
if(token.code == Token.TokenCode.PLUS) { | |
// do stuff with plus | |
token = nextToken(); | |
Expr(); | |
System.out.println("PLUS" + " " + token.data); | |
} | |
if(token.code == Token.TokenCode.MINUS) { | |
// System.out.println("before sub" + " " + token.data); ************ | |
token = nextToken(); | |
Expr(); | |
System.out.println("SUB" + " " + token.data); | |
} | |
} | |
public static void Term() { | |
// if the nextToken has TokenCode ERROR | |
if(token.code == Token.TokenCode.ERROR) { | |
ParserError(); | |
} | |
// System.out.println("im inside before FActor...." + token.data); | |
Factor(); | |
//token = nextToken(); | |
//System.out.println("This inside Term and my T is: " + token.data); | |
if(token.code == Token.TokenCode.MULT){ | |
// System.out.println("before mult " + token.data); | |
token = nextToken(); | |
// System.out.println("after mult " + token.data); | |
Term(); | |
System.out.println("MULT"); | |
} | |
} | |
public static void Factor() { | |
// if the nextToken has TokenCode ERROR | |
if(token.code == Token.TokenCode.ERROR) { | |
ParserError(); | |
} | |
if(token.code == Token.TokenCode.INT) { | |
//do stuff with INT | |
//System.out.println("This inside Factor and my INT is: " + token.data); | |
System.out.println("PUSH " + token.data); | |
token = nextToken(); | |
} | |
if(token.code == Token.TokenCode.ID) { | |
//do stuff with ID | |
//System.out.println("PUSH " + token.data); ********************************+ | |
System.out.println("PUSH " + token.data); | |
} | |
if(token.code == Token.TokenCode.LPAREN) { | |
// switch to next token | |
//System.out.println("This inside " + token.code + " and my is: " + token.data); | |
//System.out.println("This inside Lparen and my is: " + token.data); | |
token = nextToken(); | |
Expr(); | |
if(token.code == Token.TokenCode.RPAREN) { | |
//System.out.println("This is inside " + token.code + " and my is: " + token.data); | |
// token = nextToken(); | |
} | |
else { | |
//return error | |
ParserError(); | |
} | |
} | |
} | |
public static void main(String[] args){ | |
parse(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment