***IN JAVA CODE creating the basic language parsing mechanism to recognize valid
ID: 3887410 • Letter: #
Question
***IN JAVA CODE
creating the basic language parsing mechanism to recognize valid (and invalid) phrases in the language.
Below you will find a complete sampling of the valid statements in the language. Note that some of the statements can be nested together. You should only print the following:
You must implement functionality for both the execute command as well as the exit command.
If it is a valid statement is should print “VALID select Statement” (where select is whatever the type is)
If it is not a valid statement it should print “INVALID select Syntax” (where select is whatever the type is)
If it doesn’t have a type (complete garbage) then print “INVALID SYNTAX”
**IN JAVA CODE
Explanation / Answer
Comp.java
package Core;
import java.io.IOException;
public class Comp {
/* Comp-
*
* Purpose: Object oriented representation of Comp part of Core programming language.
* Used for parsing, printing, and executing. */
private Op op1;
private Op op2;
private CompOp cop;
public Comp() {
op1 = null;
op2 = null;
cop = null;
}
void ParseComp() throws IOException {
Interpreter.tokenizer.skipToken(); //skip left parentheses
op1 = new Op();
op1.ParseOp();
cop = new CompOp();
cop.ParseCompOp();
op2 = new Op();
op2.ParseOp();
Interpreter.tokenizer.skipToken(); //skip right parentheses
}
void PrintComp() {
System.out.print("(");
op1.PrintOp();
cop.PrintCompOp();
op2.PrintOp();
System.out.print(")");
}
public boolean EvalComp() {
Boolean result = false;
if (cop.getOp() == "==") {
result = op1.getVal() == op2.getVal();
} else if (cop.getOp() == "!=") {
result = op1.getVal() != op2.getVal();
} else if (cop.getOp() == "<") {
result = op1.getVal() < op2.getVal();
} else if (cop.getOp() == ">") {
result = op1.getVal() > op2.getVal();
} else if (cop.getOp() == "<=") {
result = op1.getVal() <= op2.getVal();
} else if (cop.getOp() == ">=") {
result = op1.getVal() >= op2.getVal();
}
return result;
}
}
Comp.Op.java
package Core;
import java.io.IOException;
public class CompOp {
/* CompOp-
*
* Purpose: Object oriented representation of CompOp part of Core programming language.
* Used for parsing, printing, and executing. */
private String symbol;
void ParseCompOp() throws IOException {
String token = Interpreter.tokenizer.getToken();
if (token.equals("!=")) {
symbol = "!=";
}
if (token.equals("==")) {
symbol = "==";
}
if (token.equals(">")) {
symbol = ">";
}
if (token.equals("<")) {
symbol = "<";
}
if (token.equals(">=")) {
symbol = ">=";
}
if (token.equals("<=")) {
symbol = "<=";
}
Interpreter.tokenizer.skipToken(); // comparison op
}
void PrintCompOp() {
System.out.print(symbol);
}
public String getOp() {
return symbol;
}
}
Cond.java
package Core;
import java.io.IOException;
public class Cond {
/* Cond-
*
* Purpose: Object oriented representation of Cond part of Core programming language.
* Used for parsing, printing, and executing. */
private Comp comp;
private Cond c1;
private Cond c2;
private Boolean and;
private Boolean or;
private Boolean negated;
public Cond() {
comp = null;
c1 = null;
c2 = null;
and = false;
or = false;
negated = false;
}
void ParseCond() throws IOException {
String token = Interpreter.tokenizer.getToken();
if (token.equals("(")) {
comp = new Comp();
comp.ParseComp();
} else if (token.equals("!")) {
Interpreter.tokenizer.skipToken(); //skip !
negated = true;
c1 = new Cond();
c1.ParseCond();
} else if (token.equals("[")) {
Interpreter.tokenizer.skipToken(); //skip [
c1 = new Cond();
c1.ParseCond();
token = Interpreter.tokenizer.getToken();
if (token.equals("&&")) {
Interpreter.tokenizer.skipToken(); //skip &&
and = true;
c2 = new Cond();
c2.ParseCond();
Interpreter.tokenizer.skipToken(); //skip ]
} else if (token.equals("||")) {
Interpreter.tokenizer.skipToken(); //skip ||
or = true;
c2 = new Cond();
c2.ParseCond();
Interpreter.tokenizer.skipToken(); //skip ]
}
}
}
void PrintCond() {
if (negated) {
System.out.print("!");
}
if (and) {
System.out.print("[");
c1.PrintCond();
System.out.print("&&");
c2.PrintCond();
System.out.print("]");
} else if (or) {
System.out.print("[");
c1.PrintCond();
System.out.print("||");
c2.PrintCond();
System.out.print("]");
} else {
if (c1 != null) {
c1.PrintCond();
} else {
comp.PrintComp();
}
}
}
public boolean EvalCond() {
if (and) {
return (c1.EvalCond() && c2.EvalCond());
} else if (or) {
return (c1.EvalCond() || c2.EvalCond());
} else {
if (c1 != null) {
return c1.EvalCond();
} else {
return comp.EvalComp();
}
}
}
}
Decl.java
package Core;
import java.io.IOException;
public class Decl {
/* Decl-
*
* Purpose: Object oriented representation of Decl part of Core programming language.
* Used for parsing, printing, and executing. */
private IdList il;
public Decl() {
il = null;
}
void ParseDecl() throws IOException {
if(Interpreter.tokenizer.getToken().equals("int"))
Interpreter.tokenizer.skipToken(); //skip int
il = new IdList();
il.ParseIdList();
}
void PrintDecl() {
System.out.print("int ");
il.PrintIdList();
}
void ExecDecl() {}
}
DeclSeq.java
package Core;
import java.io.IOException;
public class DeclSeq {
/* DeclSeq-
*
* Purpose: Object oriented representation of DeclSeq part of Core programming language.
* Used for parsing, printing, and executing. */
private Decl d;
private DeclSeq ds;
public DeclSeq() {
d = null;
ds = null;
}
void ParseDeclSeq() throws IOException {
d = new Decl();
d.ParseDecl();
Interpreter.tokenizer.skipToken();
if (!Interpreter.tokenizer.getToken().equals("begin")) {
ds = new DeclSeq();
ds.ParseDeclSeq();
}
}
void PrintDeclSeq() {
d.PrintDecl();
if (ds != null) {
ds.PrintDeclSeq();
}
}
void ExecDeclSeq() {}
}
Exp.java
package Core;
import java.io.IOException;
public class Exp {
/* Exp-
*
* Purpose: Object oriented representation of Exp part of Core programming language.
* Used for parsing, printing, and executing. */
private Fac fac;
private Exp exp;
private Boolean plus;
private Boolean minus;
public Exp() {
fac = null;
exp = null;
plus = false;
minus = false;
}
void ParseExp() throws IOException {
fac = new Fac();
fac.ParseFac();
String token = Interpreter.tokenizer.getToken();
if (token.equals("+")) {
plus = true;
Interpreter.tokenizer.skipToken(); //skip +
exp = new Exp();
exp.ParseExp();
} else if (token.equals("-")) {
minus = true;
Interpreter.tokenizer.skipToken(); //skip -
exp = new Exp();
exp.ParseExp();
}
}
void PrintExp() {
if (fac != null) {
fac.PrintFac();
}
if (plus) {
System.out.print("+");
}
if (minus) {
System.out.print("-");
}
if (exp != null) {
exp.PrintExp();
}
}
public int getVal() {
if (exp == null) {
return fac.getVal();
} else {
if (plus) {
return fac.getVal() + exp.getVal();
} else if (minus) {
return fac.getVal() - exp.getVal();
}
}
return 0;
}
}
Fac.java
package Core;
import java.io.IOException;
public class Fac {
/* Fac-
*
* Purpose: Object oriented representation of Fac part of Core programming language.
* Used for parsing, printing, and executing. */
private Op op;
private Fac fac;
private Boolean mult;
public Fac() {
op = null;
fac = null;
mult = false;
}
void ParseFac() throws IOException {
op = new Op();
op.ParseOp();
if (Interpreter.tokenizer.getToken().equals("*")) {
mult = true;
Interpreter.tokenizer.skipToken(); //skip *
fac = new Fac();
fac.ParseFac();
}
}
void PrintFac() {
if (op != null) {
op.PrintOp();
}
if (mult) {
System.out.print("*");
}
if (fac != null) {
fac.PrintFac();
}
}
public int getVal() {
if (!mult) {
return op.getVal();
} else {
return op.getVal()*fac.getVal();
}
}
}
Id.java
package Core;
import java.util.HashMap;
public class Id {
/* Id-
*
* Purpose: Object oriented representation of Id part of Core programming language.
* Used for parsing, printing, and executing. */
private static HashMap<String, Id> idSet = new HashMap<String, Id>();
private String name;
private Integer val;
private Id(String n) {
name = n;
val = null;
}
public static Id ParseId() {
String token = Interpreter.tokenizer.getToken();
if (idSet.containsKey(token)) {
return idSet.get(token);
} else {
Id id = new Id(token);
idSet.put(token, id);
return id;
}
}
void PrintId() {
System.out.print(name);
}
void OutputId() {
System.out.println(name + " = " + val);
}
public int getVal() {
return val;
}
public void setVal(int val) {
this.val = val;
}
}
IdList.java
package Core;
import java.io.IOException;
public class IdList {
/* IdList-
*
* Purpose: Object oriented representation of IdList part of Core programming language.
* Used for parsing, printing, and executing. */
private Id id;
private IdList il;
public IdList() {
id = null;
il = null;
}
void ParseIdList() throws IOException {
id = Id.ParseId();
Interpreter.tokenizer.skipToken(); // skip id
if (!Interpreter.tokenizer.getToken().equals(";")) {
Interpreter.tokenizer.skipToken(); // skip ,
il = new IdList();
il.ParseIdList();
}
}
void PrintIdList() {
id.PrintId();
if (il != null) {
System.out.print(",");
il.PrintIdList();
} else {
System.out.println(";");
}
}
void WriteIdList() {
id.OutputId();
if (il != null) {
il.WriteIdList();
}
}
public int ReadIdList(int pos) {
id.setVal(Input.getVal(pos));
if (il != null) {
il.ReadIdList(pos + 1);
}
return pos + 1;
}
}
If.java
package Core;
import java.io.IOException;
public class If {
/* If-
*
* Purpose: Object oriented representation of If part of Core programming language.
* Used for parsing, printing, and executing. */
private Cond c;
private StmtSeq ss1;
private StmtSeq ss2;
public If() {
c = null;
ss1 = null;
ss2 = null;
}
void ParseIf() throws IOException {
Interpreter.tokenizer.skipToken(); //skip if
c = new Cond();
c.ParseCond();
Interpreter.tokenizer.skipToken(); //skip then
ss1 = new StmtSeq();
ss1.ParseStmtSeq();
if (!Interpreter.tokenizer.getToken().equals("end")) {
Interpreter.tokenizer.skipToken(); //skip else
ss2 = new StmtSeq();
ss2.ParseStmtSeq();
Interpreter.tokenizer.skipToken(); //skip end and semicolon
Interpreter.tokenizer.skipToken();
} else {
Interpreter.tokenizer.skipToken(); //skip end and semicolon
Interpreter.tokenizer.skipToken();
}
}
void PrintIf() {
System.out.print("if ");
c.PrintCond();
System.out.println(" then");
TabControl.increaseTabCount();
ss1.PrintStmtSeq();
if (ss2 != null) {
TabControl.printTabs();
System.out.println("else");
ss2.PrintStmtSeq();
}
TabControl.decreaseTabCount();
TabControl.printTabs();
System.out.println("end;");
}
void ExecIf() {
if (c.EvalCond()) {
ss1.ExecStmtSeq();
} else {
ss2.ExecStmtSeq();
}
}
}
Input.java
package Core;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Input {
/* Input-
*
* Purpose: Object oriented representation of Input part of Core programming language.
* Used for parsing, printing, and executing. */
private static int pos = 0;
private static List<String> input = new ArrayList<String>();
static {
/*First place all ints from input into arrayList*/
input = Arrays.asList(Interpreter.dataString.split("\s+"));
}
private IdList idl;
public Input() {
idl = null;
}
void ParseInput() throws IOException {
Interpreter.tokenizer.skipToken(); //skip read
idl = new IdList();
idl.ParseIdList();
Interpreter.tokenizer.skipToken(); //skip ;
}
void PrintInput() {
System.out.print("read ");
idl.PrintIdList();
}
void ExecInput() {
pos = idl.ReadIdList(pos);
}
public static int getVal(int p) {
if (p >= input.size()) {
System.out.println("Error in data file.");
}
return Integer.valueOf(input.get(p));
}
}
Interpreter.java
package Core;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class Interpreter {
/* Interpreter-
*
* Purpose: Main class of interpreter. Will ask user for input, read input, and initialize
* tokenizer. Will also call Prog to parse, print and execute the program*/
public static int tabCount;
public static String dataString;
public static Tokenizer tokenizer = null;
public static void main(String[] args) {
String inputString = "";
String fileName;
String dataName;
Scanner userInput = new Scanner(System.in);
Scanner programScanner = null;
Scanner dataScanner = null;
//Get name of program inputted from user
System.out.println("Please enter program file name: ");
fileName = userInput.nextLine();
File programFile = new File(fileName);
//Get name of data file inputted from user
System.out.println("Please enter data file name: ");
dataName = userInput.nextLine();
File dataFile = new File(dataName);
//Try to read file and put into String
try {
programScanner = new Scanner(programFile);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println("Input File Not Found");
}
while(programScanner.hasNext())
{
inputString += programScanner.nextLine();
}
//Put Data File into String
try {
dataScanner = new Scanner(dataFile);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println("Data File Not Found");
}
while(dataScanner.hasNext())
{
dataString = "";
dataString += dataScanner.nextLine();
}
//initialize Tokenizer with input string
try {
tokenizer = new Tokenizer(inputString);
} catch (IOException e1) { //if first token is invalid
e1.printStackTrace();
System.out.println("Invalid Token Structure");
}
//Parse, Print, and Execute the program
Prog program = new Prog();
try {
program.ParseProg();
} catch (IOException e) {
e.printStackTrace();
System.out.println("Invalid Token Structure");
}
program.PrintProg();
program.ExecProg();
}
}
Loop.java
package Core;
import java.io.IOException;
public class Loop {
/* Loop-
*
* Purpose: Object oriented representation of Loop part of Core programming language.
* Used for parsing, printing, and executing */
private Cond c;
private StmtSeq ss;
public Loop() {
c = null;
ss = null;
}
void ParseLoop() throws IOException {
Interpreter.tokenizer.skipToken(); //skip while
c = new Cond();
c.ParseCond();
Interpreter.tokenizer.skipToken(); //skip loop
ss = new StmtSeq();
ss.ParseStmtSeq();
Interpreter.tokenizer.skipToken(); //skip end and semicolon
Interpreter.tokenizer.skipToken();
}
void PrintLoop() {
System.out.print("while ");
c.PrintCond();
System.out.println(" loop");
TabControl.increaseTabCount();
ss.PrintStmtSeq();
TabControl.decreaseTabCount();
TabControl.printTabs();
System.out.println("end;");
}
void ExecLoop() {
while (c.EvalCond()) {
ss.ExecStmtSeq();
}
}
}
Op.java
package Core;
import java.io.IOException;
public class Op {
/* Op-
*
* Purpose: Object oriented representation of Op part of Core programming language.
* Used for parsing, printing, and executing */
private Integer i;
private Id id;
private Exp exp;
public Op() {
i = null;
id = null;
exp = null;
}
void ParseOp() throws IOException{
String token = Interpreter.tokenizer.getToken();
if(token.equals("(")){
Interpreter.tokenizer.skipToken(); //skip left parentheses
exp = new Exp();
exp.ParseExp();
Interpreter.tokenizer.skipToken(); //skip right parentheses
}
else if (token.matches("\d*")) {
i = Integer.valueOf(token);
Interpreter.tokenizer.skipToken();
}
else {
id = Id.ParseId();
Interpreter.tokenizer.skipToken(); //skip id
}
}
void PrintOp(){
if(i != null) System.out.print(i);
if(id != null) id.PrintId();
if(exp != null) {
System.out.print("(");
exp.PrintExp();
System.out.print(")");
}
}
public int getVal() {
if (i != null) {
return i;
} else if(id != null){
return id.getVal();
} else if (exp != null) {
return exp.getVal();
}
return 0;
}
}
Output.java
package Core;
import java.io.IOException;
public class Output {
/* Output-
*
* Purpose: Object oriented representation of Output part of Core programming language.
* Used for parsing, printing, and executing */
private IdList idl;
public Output() {
idl = null;
}
void ParseOutput() throws IOException{
Interpreter.tokenizer.skipToken(); //skip write
idl = new IdList();
idl.ParseIdList();
Interpreter.tokenizer.skipToken(); //skip ;
}
void PrintOutput(){
System.out.print("write ");
idl.PrintIdList();
}
void ExecOutput(){
idl.WriteIdList();
}
}
Prog.java
package Core;
import java.io.IOException;
public class Prog {
/* StmtSeq-
*
* Purpose: Object oriented representation of Prog - the main part of Core programming language.
* Used for parsing, printing, and executing */
private DeclSeq ds;
private StmtSeq ss;
public Prog(){
ds = null;
ss = null;
}
void ParseProg() throws IOException{
Interpreter.tokenizer.skipToken(); //skip program
ds = new DeclSeq();
ds.ParseDeclSeq();
Interpreter.tokenizer.skipToken(); //skip begin
ss = new StmtSeq();
ss.ParseStmtSeq();
}
void PrintProg(){
System.out.println("program");
TabControl.increaseTabCount();
TabControl.printTabs();
ds.PrintDeclSeq();
System.out.println("begin");
ss.PrintStmtSeq();
TabControl.decreaseTabCount();
System.out.println("end");
}
void ExecProg(){
ds.ExecDeclSeq();
ss.ExecStmtSeq();
}
}
Stmt.java
package Core;
import java.io.IOException;
public class Stmt {
/* StmtSeq-
*
* Purpose: Object oriented representation of Stmt part of Core programming language.
* Used for parsing, printing, and executing */
private Assign a;
private If i;
private Loop loop;
private Input in;
private Output out;
public Stmt() {
a = null;
i = null;
loop = null;
in = null;
out = null;
}
void ParseStmt() throws IOException {
String token = Interpreter.tokenizer.getToken();
if (token.equals("if")) {
i = new If();
i.ParseIf();
} else if (token.equals("while")) {
loop = new Loop();
loop.ParseLoop();
} else if (token.equals("read")) {
in = new Input();
in.ParseInput();
} else if (token.equals("write")) {
out = new Output();
out.ParseOutput();
} else {
a = new Assign();
a.ParseAssign();
}
}
void PrintStmt() {
if (a != null) {
a.PrintAssign();
}
if (i != null) {
i.PrintIf();
}
if (loop != null) {
loop.PrintLoop();
}
if (in != null) {
in.PrintInput();
}
if (out != null) {
out.PrintOutput();
}
}
void ExecStmt() {
if (a != null) {
a.ExecAssign();
}
if (i != null) {
i.ExecIf();
}
if (loop != null) {
loop.ExecLoop();
}
if (in != null) {
in.ExecInput();
}
if (out != null) {
out.ExecOutput();
}
}
}
StmtSeq.java
package Core;
import java.io.IOException;
public class StmtSeq {
/* StmtSeq-
*
* Purpose: Object oriented representation of StmtSeq part of Core programming language.
* Used for parsing, printing, and executing */
private Stmt st;
private StmtSeq sq;
public StmtSeq() {
st = null;
sq = null;
}
void ParseStmtSeq() throws IOException {
st = new Stmt();
st.ParseStmt();
String token = Interpreter.tokenizer.getToken();
if (!token.equals("end") &&!token.equals("else")) {
sq = new StmtSeq();
sq.ParseStmtSeq();
}
}
void PrintStmtSeq() {
TabControl.printTabs();
st.PrintStmt();
if (sq != null) {
sq.PrintStmtSeq();
}
}
void ExecStmtSeq() {
st.ExecStmt();
if (sq != null) {
sq.ExecStmtSeq();
}
}
}
TabControl.java
package Core;
public class TabControl {
/* TabControl-
*
* Purpose: Static class to keep track of the amount of tabs that should be printed
* in order to make prettyprint possible */
private static int tabCount = 0;
public static int getTabCount() {
return tabCount;
}
public static void increaseTabCount() {
TabControl.tabCount += 1;
}
public static void decreaseTabCount() {
TabControl.tabCount -= 1;
}
public static void printTabs() {
int i = tabCount;
while(i>0)
{
System.out.print(" ");
i--;
}
}
}
Tokenizer.java
package Core;
import java.io.IOException;
import java.util.StringTokenizer;
public class Tokenizer {
/* Tokenizer-
*
* Purpose: Class to tokenize an input string in Core programming language into the correct tokens. It contains several methods,
* the public ones being constructor, getToken, getTokenInt, tokensLeft, and skipToken */
private String tokenString; //full tokenString that has not yet been tokenized
private String currentToken; //currentToken string value
private int currentTokenInt; //integer value of currentToken
//private String currentTokenId; //possible implementation later
private StringTokenizer st; //used to get rid of whitespace
private String nextToken = ""; //only used when verifyToken() splits a token
private boolean tokensLeft = true;; //will be false when no tokens are left
/*Tokenizer constructor simply takes input, places it in the StringTokenizer, and calls skipToken to
* populate currentToken and currentTokenInt */
public Tokenizer(String input) throws IOException
{
tokenString = input;
st = new StringTokenizer(tokenString);
skipToken(); //throws IOException if first token is invalid
}
/* endOfFile changes the Tokenizer state to reflect that the end of file is reached */
private void endOfFile() {
currentToken = "";
currentTokenInt = 33; //eof token
tokensLeft = false;
}
public String getToken()
{
return currentToken;
}
public int getTokenInt()
{
return currentTokenInt;
}
public boolean tokensLeft()
{
return tokensLeft;
}
/* skipToken will get replace the current token with the next token in line */
public void skipToken() throws IOException
{
//if nextToken exists, it needs handled first. Put it in currentToken, verify it, and update the int value
if (nextToken != "")
{
currentToken = nextToken;
nextToken = "";
verifyToken();
setTokenValues();
if(currentTokenInt == -1) //if token is invalid
{
throw new IOException();
}
}
//else if eof
else if(!st.hasMoreTokens()) endOfFile();
//else if tokens remain, call stringTokenizer to give next token, verify it, and update integer values
else
{
currentToken = st.nextToken();
verifyToken();
setTokenValues();
if(currentTokenInt == -1) //if token is invalid
{
throw new IOException();
}
}
}
/*The StringTokenizer used only seperates tokens by whitespace. verifyToken will split up tokens by other
* characters if necessary. For example, in the case A=3, there is no space, but should be three tokens.
* nextToken will be used to hold the extra characters if a token is split */
private void verifyToken() {
//if it is an identifier, find the end of UpperCase characters, then the end of integers, and split the token
if(Character.isUpperCase(currentToken.charAt(0)))
{
int i = 1;
while(i < currentToken.length() && Character.isUpperCase(currentToken.charAt(i)))
{
i++;
}
while(i < currentToken.length() && Character.isDigit(currentToken.charAt(i)))
{
i++;
}
if(i != currentToken.length())
{
nextToken = currentToken.substring(i, currentToken.length());
currentToken = currentToken.substring(0, i);
}
}
//if a reserved word, find end of lowercase characters and then split
else if(Character.isLowerCase(currentToken.charAt(0)))
{
int i = 1;
while(i < currentToken.length() && Character.isLowerCase(currentToken.charAt(i)))
{
i++;
}
if(i != currentToken.length())
{
nextToken = currentToken.substring(i, currentToken.length());
currentToken = currentToken.substring(0, i);
}
}
//if an integer, find end of digits and then split
else if(Character.isDigit(currentToken.charAt(0)))
{
int i = 1;
while(i < currentToken.length() && Character.isDigit(currentToken.charAt(i)))
{
i++;
}
if(i != currentToken.length())
{
nextToken = currentToken.substring(i, currentToken.length());
currentToken = currentToken.substring(0, i);
}
}
//else if it is a symbol, call verifySymbolToken
else if(isSymbolToken(currentToken.charAt(0)))
{
verifySymbolToken();
}
}
/*verifySymbolToken validates symbol tokens similarly to verifyToken does for identifiers, integers, and reserved words */
private void verifySymbolToken() {
//First handle symbols that start with = ! > or <
if(currentToken.charAt(0) == '=' || currentToken.charAt(0) == '!' || currentToken.charAt(0) == '>' || currentToken.charAt(0) == '<')
{
if(currentToken.length() > 1)
{
if(currentToken.charAt(1) == '=') //if '==' '!=' '>=' or '<='
{
if(currentToken.length() > 2)
{
nextToken = currentToken.substring(2, currentToken.length());
}
currentToken = currentToken.substring(0,2);
}
else // if '=' '!' '<' '>'
{
nextToken = currentToken.substring(1, currentToken.length());
currentToken = currentToken.substring(0,1);
}
}
}
//Handle ||
else if(currentToken.charAt(0) == '|')
{
if(currentToken.length() > 2 && currentToken.charAt(1) == '|') //if '||'
{
nextToken = currentToken.substring(2, currentToken.length());
currentToken = currentToken.substring(0,2);
}
}
//Handle &&
else if(currentToken.charAt(0) == '&')
{
if(currentToken.length() > 2 && currentToken.charAt(1) == '&') //if '&&'
{
nextToken = currentToken.substring(2, currentToken.length());
currentToken = currentToken.substring(0,2);
}
}
//any other single length symbol
else
{
if(currentToken.length() > 1)
{
nextToken = currentToken.substring(1, currentToken.length());
currentToken = currentToken.substring(0,1);
}
}
}
/*isSymbolToken returns true if the character passed is the first character of a valid symbol token */
private boolean isSymbolToken(char c) {
if(c == ';' || c == ',' || c == '=' || c == '!' || c == ']' || c == '[' || c == '&' || c =='|' || c == '(' || c == ')' || c == '+' || c == '-' || c == '*' || c =='<' || c == '>')
{ return true;
}
else return false;
}
private boolean isInt(String s)
{
try {
Integer.parseInt(s);
} catch(NumberFormatException e) {
return false;
}
return true;
}
private boolean isID(String s) {
boolean upperCase = true;
if(!Character.isUpperCase(s.charAt(0))) return false;
for(int i = 0; i < s.length(); i++)
{
if(upperCase == false)
{
if(!Character.isDigit(s.charAt(i)))
{
return false;
}
}
else if(!Character.isUpperCase(s.charAt(i)))
{
upperCase = false;
if(!Character.isDigit(s.charAt(i))) return false;
}
}
return true;
}
/*setTokenValues sets the currentTokenInt property to the correct integer value */
private void setTokenValues()
{
if (isInt(currentToken))
{
currentTokenInt = 31;
}
else if (isID(currentToken))
{
currentTokenInt = 32;
}
else switch (currentToken) {
case "program": currentTokenInt = 1; break;
case "begin": currentTokenInt = 2; break;
case "end": currentTokenInt = 3; break;
case "int": currentTokenInt = 4; break;
case "if": currentTokenInt = 5; break;
case "then": currentTokenInt = 6; break;
case "else": currentTokenInt = 7; break;
case "while": currentTokenInt = 8; break;
case "loop": currentTokenInt = 9; break;
case "read": currentTokenInt = 10; break;
case "write": currentTokenInt = 11; break;
case ";": currentTokenInt = 12; break;
case ",": currentTokenInt = 13; break;
case "=": currentTokenInt = 14; break;
case "!": currentTokenInt = 15; break;
case "[": currentTokenInt = 16; break;
case "]": currentTokenInt = 17; break;
case "&&": currentTokenInt = 18; break;
case "||": currentTokenInt = 19; break;
case "(": currentTokenInt = 20; break;
case ")": currentTokenInt = 21; break;
case "+": currentTokenInt = 22; break;
case "-": currentTokenInt = 23; break;
case "*": currentTokenInt = 24; break;
case "!=": currentTokenInt = 25; break;
case "==": currentTokenInt = 26; break;
case "<": currentTokenInt = 27; break;
case ">": currentTokenInt = 28; break;
case "<=": currentTokenInt = 29; break;
case ">=": currentTokenInt = 30; break;
default: currentTokenInt = -1; //ERROR
break;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.