Change the below java code to Python with the correct output (Check the sample output below the code)
HERE IS THE JAVA CODE (Sorry for not having indentation the reason for not having that is becuase for some reason I could not able to post the whole code)
Test.java public class Test { /** * Tests various inputs * * @param args */ public static void main(String args[]) { // testing the parser Parser parser = new Parser("SELECT C1,C2 FROM T1 WHERE C1=5.23"); parser.run(); parser = new Parser("SELECT col1, c99 FROM tab1, t2, t9"); parser.run(); parser = new Parser("SELECT c1, c2 FROM t1, t2 WHERE c3 = 7"); parser.run(); parser = new Parser("SELECT c1, c2 FROM t1, t2 WHERE c3c123"); parser.run(); parser = new Parser("SELECT c1 FROM t1,t2 WHERE t1a=t2a"); parser.run(); parser = new Parser("SELECT c1 FROM t1 WHERE c2>2.78 AND c22.78 AND c2Parser.java public class Parser { Token token; Lexer lexer; /** * Constructor to init Lexer and Token objects checks for multiple instances * of Query or QUERY in invalid strings * * @param input */ public Parser(String input) { for (int i = 0; i FROM [WHERE ] */ public void query() { System.out.println(""); if (token.getTokenType() == Token.TokenType.KEYWORD && (token.getTokenValue().equals("SELECT"))) { System.out.println("\t" + token.getTokenValue() + ""); next(); idList(); } else { error(Token.TokenType.KEYWORD); } if (token.getTokenType() == Token.TokenType.KEYWORD && (token.getTokenValue().equals("FROM"))) { System.out.println("\t" + token.getTokenValue() + ""); next(); idList(); } else { error(Token.TokenType.KEYWORD); } if (token.getTokenType() == Token.TokenType.KEYWORD && (token.getTokenValue().equals("WHERE"))) { System.out.println("\t" + token.getTokenValue() + ""); next(); condList(); } if (token.getTokenType() == Token.TokenType.EOI) { System.out.println(""); } if (token.getTokenType() == Token.TokenType.INVALID) { error(token.getTokenType()); next(); } } /** * idList() parses grammar {, } */ public void idList() { System.out.println("\t"); if (token.getTokenType() == Token.TokenType.ID) { System.out.println("\t\t" + token.getTokenValue() + ""); next(); } while (token.getTokenType() == Token.TokenType.COMMA) { System.out.println("\t\t" + "," + ""); next(); if (token.getTokenType() == Token.TokenType.ID) { System.out.println("\t\t" + token.getTokenValue() + ""); next(); } else { error(Token.TokenType.ID); } } System.out.println("\t"); /ext(); } /** * condList(), parses grammar {AND } */ public void condList() { System.out.println("\t"); if (token.getTokenType() == Token.TokenType.ID) { /ext(); cond(); } else { error(token.getTokenType()); } while (token.getTokenType() == Token.TokenType.KEYWORD && token.getTokenValue().equals("AND")) { System.out.println("\t" + token.getTokenValue() + ""); next(); if (token.getTokenType() == Token.TokenType.ID) { /ext(); cond(); } else { error(Token.TokenType.CONDLIST); } } System.out.println("\t"); } /** * cond(), parses grammar */ public void cond() { System.out.println("\t "); System.out.println("\t\t" + token.getTokenValue() + ""); next(); if (token.getTokenType() == Token.TokenType.OPERATOR) { System.out.println("\t\t" + token.getTokenValue() + ""); next(); term(); } else { error(Token.TokenType.COND); } System.out.println("\t "); } /** * term() parses grammar, | | */ public void term() { System.out.println("\t\t"); if (token.getTokenType() == Token.TokenType.ID) { System.out.println("\t\t\t" + token.getTokenValue() + ""); next(); } else if (token.getTokenType() == Token.TokenType.INT) { System.out.println("\t\t\t" + token.getTokenValue() + ""); next(); } else if (token.getTokenType() == Token.TokenType.FLOAT) { System.out.println("\t\t\t" + token.getTokenValue() + ""); next(); } else { error(Token.TokenType.TERM); } System.out.println("\t\t"); /ext(); } /** * output error message when unexpected input is found. * * @param type */ private void error(Token.TokenType type) { System.err.println("Syntax error: Expecting: " + Token.typeToString(type) + "; saw: " + Token.typeToString(token.getTokenType())); System.exit(1); } } Token.java public class Token { public enum TokenType { DIGIT, LETTER, INT, FLOAT, ID, KEYWORD, OPERATOR, COMMA, QUERY, IDLIST, CONDLIST, COND, TERM, INVALID, EOI } private TokenType type; private String val; /** * Constructor inits type and val * * @param t * @param s */ public Token(TokenType t, String s) { this.type = t; this.val = s; } /** * return type of the token * * @return */ public TokenType getTokenType() { return this.type; } /** * return token value. * * @return */ public String getTokenValue() { return this.val; } /** * Converts terminals to strings for output. * * @param tp * @return */ public static String typeToString(TokenType tp) { String s = ""; switch (tp) { case DIGIT: { s = "Digit"; break; } case LETTER: { s = "Letter"; break; } case INT: { s = "Int"; break; } case FLOAT: { s = "Float"; break; } case ID: { s = "ID"; break; } case KEYWORD: { s = "KEYWORD"; break; } case OPERATOR: { s = "Operator"; break; } case COMMA: { s = "COMMA"; break; } case QUERY: { s = "QUERY"; break; } case IDLIST: { s = "IDLIST"; break; } case CONDLIST: { s = "CONDLIST"; break; } case COND: { s = "COND"; break; } case TERM: { s = "TERM"; break; } case EOI: { s = "EOI"; break; } case INVALID: { s = "INVALID"; break; } } return s; } } Lexer.java public class Lexer { private static final String letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; private static final String digits = "0123456789"; String in = ""; int index = 0; char ch; /** * Lexer constructor * * @param input */ public Lexer(String input) { this.in = input; this.index = 0; this.ch = nextChar(); } /** * reads next available character from stream. * * @return */ private char nextChar() { ch = in.charAt(index); index += 1; return ch; } /** * reads in seres of chars until a space, then concatenates them into a * string for better readability * * @param st * @return */ private String concat(String st) { StringBuffer read = new StringBuffer(""); do { read.append(ch); ch = nextChar(); } while (st.indexOf(ch) >= 0); return read.toString(); } /** * Output error mesasge. * * @param msg */ public void error(String msg) { System.err.println(" Error: location " + index + " " + msg); System.exit(1); } /** * Reads in chars to form new tokens returns tokens to be parsed by Parser * * @return */ public Token nextToken() { do { if (Character.isLetter(ch)) { String id = concat(letters + digits); if (id.equals("SELECT") || id.equals("FROM") || id.equals("WHERE") || id.equals("AND")) { return new Token(Token.TokenType.KEYWORD, id); } else { return new Token(Token.TokenType.ID, id); } } else if (Character.isDigit(ch)) { String num = concat(digits); if (ch != '.') { return new Token(Token.TokenType.INT, num); } num += ch; ch = nextChar(); if (Character.isDigit(ch)) { num += concat(digits); return new Token(Token.TokenType.FLOAT, num); } else { return new Token(Token.TokenType.INVALID, num); } } else { switch (ch) { case ' ': ch = nextChar(); break; case ',': char comma = ch; ch = nextChar(); return new Token(Token.TokenType.COMMA, Character.toString(comma)); case '=': char temp = ch; ch = nextChar(); return new Token(Token.TokenType.OPERATOR, Character.toString(temp)); case '': char temp2 = ch; ch = nextChar(); return new Token(Token.TokenType.OPERATOR, Character.toString(temp2)); case '$': return new Token(Token.TokenType.EOI, "EndOfInput"); default: ch = nextChar(); error("Invalid token type"); return new Token(Token.TokenType.INVALID, Character.toString(ch)); } } } while (true); } }
HERE IS THE SAMPLE CODE
HERE IS THE JAVA CODE (SCREENSHOT)
SELECT C1 , C2 FROM 11 WHERE C1 = 5.23 Figure 1: Sample output. Testjava public class Test -Tests various inputs Boaram args public static void main(String args[]) { // testing the parser Parser parser = new Parser("SELECT C1.C2 FROM TI WHERE C1=5.23"); parser.runo: parser = new Parser("SELECT coli, c99 FROM tabi, t2, t9): parser.runo: parser = new Parser("SELECT C1, C2 FROM t1, t2 WHERE 3 = 7): parser.runo: parser = new Parser"SELECT C1, C2 FROM t1, t2 WHERE 123): parser.rund: parser = new Parser("SELECT 1 FROM t1,72 WHERE tla=t2a): parser.run: Darser = new Parser("SELECT 1 FROM 1 WHERE >>278 AND >278 AND FROM
[WHERE] */ public void query0 { System.out.println("'); if (token.getTokenType() == Token.TokenType.KEYWORD && (token.getTokenValue().equals("SELECT"))) { System.out.println("\t" + token.getTokenValue() + ""); next(); idListo; } else { * query(). parses grammar SELECT FROM [WHERE ] public void query0 { System.out.println("'); if (token.getTokenType() == Token.TokenType.KEYWORD && (token.getTokenValue().equals("SELECT"))) System.out.println("\t" + token.getTokenValue() + ""); next(); id List(); } else { error(Token.TokenType.KEYWORD); if (token.getTokenType() == Token.TokenType.KEYWORD && (token.getTokenValue().equals("FROM"))) { System.out.println("\t" + token.getTokenValue() + ""); next(); idListo; } else { error(Token.TokenType.KEYWORD): if (token.getTokenType() == Token.TokenType.KEYWORD && (token.getTokenValue().equals("WHERE")) { System.out.println("\t" + token.getTokenValue() + ""); next(); condListo; if (token.getTokenType() == Token.TokenType.EOI) { System.out.println(""); if (token.getTokenType() == Token.TokenType.INVALID) { error(token.getTokenType()); next0; *id List( parses grammar { public void idList() { System.out.println("\t"); if (token.getTokenType() == Token.TokenType.ID) { System.out.println("\t\t" + token.getTokenValue() + ""); next(); while (token.getTokenType() == Token.TokenType.COMMA) { System.out.println("\t\t" +""+"'); next0; if (token.getTokenType() == Token.TokenType.ID) { System.out.println("\t\t" + token.getTokenValue() + ""); * idList() parses grammar {} */ public void idList() { System.out.println("\t"); if (token.getTokenType() == Token.TokenType.ID) { System.out.println("\t\t" + token.getTokenValue() + ""); next(); while (token.getTokenType() == Token.TokenType.COMMA) { System.out.println("\t\t" +""+""); next0; if (token.getTokenType() == Token.TokenType.ID) { System.out.println("\t\t" + token.getTokenValue() + ""); next(); } else { error(Token.TokenType.ID); System.out.println("\tList>"); /ext(); * condList(), parses grammar {AND * public void condList() { System.out.println("\t"); if (token.getTokenType() == Token.TokenType.ID) { /ext; cond(); } else { error(token.getTokenType()); while (token.getTokenType() == Token.TokenType.KEYWORD && token.getTokenValue().equals("AND")) { System.out.println("\t" + token.getTokenValue() + ""); next(); if (token.getTokenType() == Token.TokenType.ID) { /ext(); cond(); } else { error(Token.TokenType.CONDLIST); System.out.println("\t"); System.out.println("\t"); * cond(), parses grammar public void cond() { System.out.println("\t '); System.out.println("\t\t" + token.getTokenValue() + ""); next(); if (token.getTokenType() == Token.TokenType.OPERATOR) { System.out.println("\t\t' + token.getTokenValue() + ""); next0; term0; } else { error(Token.TokenType.COND); System.out.println("\t "); * term( parses grammar, | public void termo System.out.println("\t\t"); if (token.getTokenType() == Token.TokenType.ID) { System.out.println("\t\t\t" + token.getTokenValue() + ""); next(): } else if (token.getTokenType() == Token.TokenType.INT) { System.out.println("\t\t\t" + token.getTokenValue() + ""); next(); } else if (token.getTokenType() == Token.TokenType.FLOAT) { System.out.println("\t\t\t" + token.getTokenValue() + ""); next0; } else { error(Token.TokenType.TERM); System.out.println("\t\t"); Iext0; * output error message when unexpected input is found. * @param type * output error message when unexpected input is found. * @param type private void error(Token.TokenType type) { System.err.println("Syntax error: Expecting: " + Token.typeToString(type) + "; saw: " + Token.typeToString(token.getTokenType()); System.exit(1); Token.java public class Token public enum TokenType { DIGIT, LETTER, INT, FLOAT, ID, KEYWORD, OPERATOR, COMMA, QUERY, IDLIST, CONDLIST, COND, TERM, INVALID, EOI private TokenType type; private String val; * Constructor inits type and val * @paramt * @param s public Token(TokenType t, String s) { this.type = t; this.val = S; * return type of the token * @return public TokenType getTokenType() { return this.type; * return token value. * return token value. * @return public String getTokenValue() { return this.val; * Converts terminals to strings for output. * @param tp * @return public static String typeToString(TokenType tp) { String s=""; switch (tp) { case DIGIT: s = "Digit"; break; case LETTER: S = "Letter"; break; case INT: S = "Int"; break; case FLOAT: S = "Float"; break; case ID: S = "ID"; break; case KEYWORD: S ="KEYWORD"; break; case OPERATOR: S = "Operator": break; UI , case OPERATOR: S = "Operator"; break; case COMMA: S = "COMMA; break; case QUERY: { S="QUERY"; break; case IDLIST: S = "IDLIST": break; case CONDLIST: S = "CONDLIST": break; case COND: S = "COND"; break; case TERM: { S="TERM"; break; case EOI: { S = "EOI"; break; case INVALID: S="INVALID"; break; return s; Lexer.java public class Lexer Lexer.java public class Lexer private static final String letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; private static final String digits = "0123456789"; String in = ""; int index = 0; char ch; * Lexer constructor * @param input public Lexer(String input) { this.in = input; this.index = 0; this.ch = nextChar(; *reads next available character from stream. * @return */ private char nextChar() { ch = in.charAt(index); index += 1; return ch; *reads in seres of chars until a space, then concatenates them into a * string for better readability * @param st * @return private String concat(String st) { StringBuffer read = new StringBuffer(""); do read.append(ch); ch = nextCharo; } while (st.indexOf(ch) >= 0); return read.toString(; private String concat(String st) { StringBuffer read = new StringBuffer(""); do { read.append(ch); ch = nextChart; } while (st.indexOf(ch) >= 0); return read.toString(; * Output error mesasge. * @param msg public void error(String msg) { System.err.println(" Error: location" + index +""+ msg); System.exit(1); * Reads in chars to form new tokens returns tokens to be parsed by Parser * @return public Token nextToken() { do { if (Character.isLetter(ch)) { String id = concat(letters + digits); if (d.equals("SELECT") || id.equals("FROM") || id.equals("WHERE") || id.equals("AND")) { return new Token(Token.TokenType.KEYWORD, id); } else { return new Token(Token.TokenType.ID, id); } else if (Character.is Digit(ch)) { String num = concat(digits); if (ch!=) { return new Token(Token.TokenType.INT, num); num += ch; ch = nextChar(); if (Character.isDigit(ch)) { num += concat(digits); return new Token(Token.TokenType.FLOAT, num); } else { return new Token(Token.TokenType.INVALID, num); } else { return new Token(Token.TokenType.FLOAT, num): } else { return new Token(Token.TokenType.INVALID, num); } else { switch (ch) { case' ch = nextCharo; break; case: char comma = ch; ch = nextCharo; return new Token(Token.TokenType.COMMA, Character.toString(comma)); case '=; char temp = ch; ch = nextChar(); return new Token(Token.TokenType.OPERATOR, Character.toString(temp)); case'<: char templ="ch;" ch="nextChar(;" return new token character.tostring case>: char temp2 = ch; ch = nextChar(); return new Token(Token.TokenType.OPERATOR, Character.toString(temp2)); case 'S': return new Token(Token.TokenType.EOI, "EndOflnput"); default: ch = nextChar(); error("Invalid token type"); return new Token(Token.TokenType.INVALID, Character.toString(ch)); } while (true); SELECT C1 , C2 FROM 11 WHERE C1 = 5.23 Figure 1: Sample output. Testjava public class Test -Tests various inputs Boaram args public static void main(String args[]) { // testing the parser Parser parser = new Parser("SELECT C1.C2 FROM TI WHERE C1=5.23"); parser.runo: parser = new Parser("SELECT coli, c99 FROM tabi, t2, t9): parser.runo: parser = new Parser("SELECT C1, C2 FROM t1, t2 WHERE 3 = 7): parser.runo: parser = new Parser"SELECT C1, C2 FROM t1, t2 WHERE 123): parser.rund: parser = new Parser("SELECT 1 FROM t1,72 WHERE tla=t2a): parser.run: Darser = new Parser("SELECT 1 FROM 1 WHERE >>278 AND >278 AND FROM [WHERE] */ public void query0 { System.out.println("'); if (token.getTokenType() == Token.TokenType.KEYWORD && (token.getTokenValue().equals("SELECT"))) { System.out.println("\t" + token.getTokenValue() + ""); next(); idListo; } else { * query(). parses grammar SELECT FROM [WHERE ] public void query0 { System.out.println("'); if (token.getTokenType() == Token.TokenType.KEYWORD && (token.getTokenValue().equals("SELECT"))) System.out.println("\t" + token.getTokenValue() + ""); next(); id List(); } else { error(Token.TokenType.KEYWORD); if (token.getTokenType() == Token.TokenType.KEYWORD && (token.getTokenValue().equals("FROM"))) { System.out.println("\t" + token.getTokenValue() + ""); next(); idListo; } else { error(Token.TokenType.KEYWORD): if (token.getTokenType() == Token.TokenType.KEYWORD && (token.getTokenValue().equals("WHERE")) { System.out.println("\t" + token.getTokenValue() + ""); next(); condListo; if (token.getTokenType() == Token.TokenType.EOI) { System.out.println(""); if (token.getTokenType() == Token.TokenType.INVALID) { error(token.getTokenType()); next0; *id List( parses grammar { public void idList() { System.out.println("\t"); if (token.getTokenType() == Token.TokenType.ID) { System.out.println("\t\t" + token.getTokenValue() + ""); next(); while (token.getTokenType() == Token.TokenType.COMMA) { System.out.println("\t\t" +""+"'); next0; if (token.getTokenType() == Token.TokenType.ID) { System.out.println("\t\t" + token.getTokenValue() + ""); * idList() parses grammar {} */ public void idList() { System.out.println("\t"); if (token.getTokenType() == Token.TokenType.ID) { System.out.println("\t\t" + token.getTokenValue() + ""); next(); while (token.getTokenType() == Token.TokenType.COMMA) { System.out.println("\t\t" +""+""); next0; if (token.getTokenType() == Token.TokenType.ID) { System.out.println("\t\t" + token.getTokenValue() + ""); next(); } else { error(Token.TokenType.ID); System.out.println("\tList>"); /ext(); * condList(), parses grammar {AND * public void condList() { System.out.println("\t"); if (token.getTokenType() == Token.TokenType.ID) { /ext; cond(); } else { error(token.getTokenType()); while (token.getTokenType() == Token.TokenType.KEYWORD && token.getTokenValue().equals("AND")) { System.out.println("\t" + token.getTokenValue() + ""); next(); if (token.getTokenType() == Token.TokenType.ID) { /ext(); cond(); } else { error(Token.TokenType.CONDLIST); System.out.println("\t"); System.out.println("\t"); * cond(), parses grammar public void cond() { System.out.println("\t '); System.out.println("\t\t" + token.getTokenValue() + ""); next(); if (token.getTokenType() == Token.TokenType.OPERATOR) { System.out.println("\t\t' + token.getTokenValue() + ""); next0; term0; } else { error(Token.TokenType.COND); System.out.println("\t "); * term( parses grammar, | public void termo System.out.println("\t\t"); if (token.getTokenType() == Token.TokenType.ID) { System.out.println("\t\t\t" + token.getTokenValue() + ""); next(): } else if (token.getTokenType() == Token.TokenType.INT) { System.out.println("\t\t\t" + token.getTokenValue() + ""); next(); } else if (token.getTokenType() == Token.TokenType.FLOAT) { System.out.println("\t\t\t" + token.getTokenValue() + ""); next0; } else { error(Token.TokenType.TERM); System.out.println("\t\t"); Iext0; * output error message when unexpected input is found. * @param type * output error message when unexpected input is found. * @param type private void error(Token.TokenType type) { System.err.println("Syntax error: Expecting: " + Token.typeToString(type) + "; saw: " + Token.typeToString(token.getTokenType()); System.exit(1); Token.java public class Token public enum TokenType { DIGIT, LETTER, INT, FLOAT, ID, KEYWORD, OPERATOR, COMMA, QUERY, IDLIST, CONDLIST, COND, TERM, INVALID, EOI private TokenType type; private String val; * Constructor inits type and val * @paramt * @param s public Token(TokenType t, String s) { this.type = t; this.val = S; * return type of the token * @return public TokenType getTokenType() { return this.type; * return token value. * return token value. * @return public String getTokenValue() { return this.val; * Converts terminals to strings for output. * @param tp * @return public static String typeToString(TokenType tp) { String s=""; switch (tp) { case DIGIT: s = "Digit"; break; case LETTER: S = "Letter"; break; case INT: S = "Int"; break; case FLOAT: S = "Float"; break; case ID: S = "ID"; break; case KEYWORD: S ="KEYWORD"; break; case OPERATOR: S = "Operator": break; UI , case OPERATOR: S = "Operator"; break; case COMMA: S = "COMMA; break; case QUERY: { S="QUERY"; break; case IDLIST: S = "IDLIST": break; case CONDLIST: S = "CONDLIST": break; case COND: S = "COND"; break; case TERM: { S="TERM"; break; case EOI: { S = "EOI"; break; case INVALID: S="INVALID"; break; return s; Lexer.java public class Lexer Lexer.java public class Lexer private static final String letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; private static final String digits = "0123456789"; String in = ""; int index = 0; char ch; * Lexer constructor * @param input public Lexer(String input) { this.in = input; this.index = 0; this.ch = nextChar(; *reads next available character from stream. * @return */ private char nextChar() { ch = in.charAt(index); index += 1; return ch; *reads in seres of chars until a space, then concatenates them into a * string for better readability * @param st * @return private String concat(String st) { StringBuffer read = new StringBuffer(""); do read.append(ch); ch = nextCharo; } while (st.indexOf(ch) >= 0); return read.toString(; private String concat(String st) { StringBuffer read = new StringBuffer(""); do { read.append(ch); ch = nextChart; } while (st.indexOf(ch) >= 0); return read.toString(; * Output error mesasge. * @param msg public void error(String msg) { System.err.println(" Error: location" + index +""+ msg); System.exit(1); * Reads in chars to form new tokens returns tokens to be parsed by Parser * @return public Token nextToken() { do { if (Character.isLetter(ch)) { String id = concat(letters + digits); if (d.equals("SELECT") || id.equals("FROM") || id.equals("WHERE") || id.equals("AND")) { return new Token(Token.TokenType.KEYWORD, id); } else { return new Token(Token.TokenType.ID, id); } else if (Character.is Digit(ch)) { String num = concat(digits); if (ch!=) { return new Token(Token.TokenType.INT, num); num += ch; ch = nextChar(); if (Character.isDigit(ch)) { num += concat(digits); return new Token(Token.TokenType.FLOAT, num); } else { return new Token(Token.TokenType.INVALID, num); } else { return new Token(Token.TokenType.FLOAT, num): } else { return new Token(Token.TokenType.INVALID, num); } else { switch (ch) { case' ch = nextCharo; break; case: char comma = ch; ch = nextCharo; return new Token(Token.TokenType.COMMA, Character.toString(comma)); case '=; char temp = ch; ch = nextChar(); return new Token(Token.TokenType.OPERATOR, Character.toString(temp)); case'<: char templ="ch;" ch="nextChar(;" return new token character.tostring case>: char temp2 = ch; ch = nextChar(); return new Token(Token.TokenType.OPERATOR, Character.toString(temp2)); case 'S': return new Token(Token.TokenType.EOI, "EndOflnput"); default: ch = nextChar(); error("Invalid token type"); return new Token(Token.TokenType.INVALID, Character.toString(ch)); } while (true)