Question: I am trying to write an lexical analyzer from an input file, but i am getting error. may some one help me? import java.io.*; import

I am trying to write an lexical analyzer from an input file, but i am getting error. may some one help me?

import java.io.*; import java.util.*; import java.util.ArrayList; import java.util.regex.Pattern; import java.util.regex.Matcher; import java.util.Scanner;

public class LexicalAnalyzer { static final int MAX_LEXEME_LENGTH = 100; static final int MAX_TOKEN_LENGTH = 100; static int charClass; static char[] lexeme = new char[MAX_LEXEME_LENGTH]; static char nextChar; static int lexLen; static int token; static String thisLine; static int nextToken; static int read; static char endChar; static char[] newChar = new char[100]; static String nextLine; /*Character classes*/ static final int LETTER = 0; static final int DIGIT = 1; static final int IDENTIFER = 99;

/*Token codes*/ static String[] token_dict = new String[MAX_TOKEN_LENGTH]; static final int INT_LIT = 10; static final int IDENT = 11; static final int ASSIGN_OP = 20; static final int ADD_OP = 21; static final int SUB_OP = 22; static final int MULT_OP = 23; static final int DIV_OP = 24; static final int LEFT_PAREN = 25; static final int RIGHT_PAREN = 26; static final int END_OF_FILE = 98; public static void main(String[] args) { token_dict[INT_LIT] = "INT_LIT\t"; token_dict[IDENT] = "IDENT\t"; token_dict[ASSIGN_OP] = "ASSIGN_OP"; token_dict[ADD_OP] = "ADD_OP\t"; token_dict[SUB_OP] = "SUB_OP\t"; token_dict[MULT_OP] = "MULT_OP\t"; token_dict[DIV_OP] = "DIV_OP\t"; token_dict[LEFT_PAREN] = "LEFT_PAREN"; token_dict[RIGHT_PAREN] = "RIGHT_PAREN"; token_dict[END_OF_FILE] = "END_OF_FILE";

} /** Starts the program, and the definitons of the program */ public void getStarted() throws FileNotFoundException, IOException { openFile(); readData(); search(); addcharacter(); getNonBlank(); lexical(); determineCharacter(); display(); infile.close(); } // method getStarted //open the file when the input file name is typed in the keyboard. public void openFile() throws FileNotFoundException { Scanner input; String infile_name; File file;

input = new Scanner(System.in); do { System.out.print("Enter the input file name: "); infile_name = input.next();

// open the input data file file = new File(infile_name); if (file.exists()) infile = new Scanner(new File(infile_name)); else System.out.println(infile_name + " does not exist"); } while (!file.exists()); } // method openFile public void readData() {

System.out.println("*********************************************************************"); System.out.println("Parsing the statement: " + nextLine + " "); getChar(); do { lex(); read++; } while(nextToken != END_OF_FILE); System.out.println("");

System.out.println("Next token is: END_OF_FILE Next lexeme is EOF");

} public void search() { switch (ch) { case '(': addChar(); nextToken = LEFT_PAREN; break;

case ')': addChar(); nextToken = RIGHT_PAREN; break;

case '+': addChar(); nextToken = ADD_OP; break;

case '-': addChar(); nextToken = SUB_OP; break;

case '*': addChar(); nextToken = MULT_OP; break;

case '/': addChar(); nextToken = DIV_OP; break;

case '=': addChar(); System.out.println("Enter "); nextToken = ASSIGN_OP; break;

default: lexeme[0] = 'E'; lexeme[1] = 'O'; lexeme[2] = 'F'; lexeme[3] = 0; lexLen = 4; nextToken = END_OF_FILE; break; } return nextToken; }

public void addCharacter() {

if(lexLen <= (MAX_LEXEME_LENGTH-2)) { lexeme[lexLen++] = nextChar; lexeme[lexLen] = 0; }else { System.out.println("ERROR - lexeme is too long "); } }

/*The following gets the next char of an input and determines its character class*/

public void getChar() {

char c = 0; try { c = (char)reader.read(); } catch (IOException e) { e.printStackTrace(); } nextChar = c; if((int)nextChar != 0) { if(isalpha(nextChar)) { charClass = LETTER; }else if(isdigit(nextChar)) { charClass = DIGIT; } else { charClass = IDENTIFER; } } else { charClass = END_OF_FILE; } }

/* The following calls the getChar function until it returns a non-whitespace character */

public void getNonBlank() {

while(isspace(nextChar)) { getChar(); }

} /* The following is a lexical analyzer method for arithmetic expressions */

static int lex() {

lexLen = 0; getNonBlank(); switch (charClass) {

/* Determines parse identifiers */ case LETTER: addChar(); getChar(); while(charClass == LETTER || charClass == DIGIT) { addChar(); getChar(); } nextToken = IDENTIFER; break;

/* Determines parse integer literals */ case DIGIT: addChar(); getChar(); while(charClass == DIGIT)

addChar(); getChar(); } nextToken = INT_LIT; break;

/* Determines parentheses and operators */ case IDENTIFER: lookup(nextChar); getChar(); break;

/* Determines the end of the file*/ case END_OF_FILE: nextToken = END_OF_FILE;; lexeme[0] = 'E'; lexeme[1] = 'O'; lexeme[2] = 'F'; lexeme[3] = 0; lexLen = 4; break; } public void determineCharacter() { /* The following returns true if the char is a letter */

public boolean isalpha(char c) {

int ascii = (int) c; if((ascii > 64 && ascii < 91) || (ascii > 96 && ascii < 123)) { return true; } else { return false; } }

/* The following returns true if the char is a digit */

public boolean isdigit(char c) {

int ascii = (int) c; if(ascii > 47 && ascii < 58) { return true; }else { return false; } }

/* The following returns true if the char is a space*/

public boolean isspace(char c) {

int ascii = (int) c; if(ascii == 32) { return true; } else { return false; } }

} public void display() { System.out.println("Lexical analyzer"); System.out.println("Letter"+ LETTER); System.out.println("digit"+DIGIT); System.out.println("Identifier" + IDENTIFER); } }

here are the errors

LexicalAnalyzer.java:264: error: orphaned case case IDENTIFER: ^ LexicalAnalyzer.java:283: error: illegal start of expression public boolean isalpha(char c) ^ LexicalAnalyzer.java:283: error: ';' expected public boolean isalpha(char c) ^ LexicalAnalyzer.java:283: error: ';' expected public boolean isalpha(char c) ^ LexicalAnalyzer.java:299: error: illegal start of expression public boolean isdigit(char c) ^ LexicalAnalyzer.java:299: error: ';' expected public boolean isdigit(char c) ^ LexicalAnalyzer.java:299: error: ';' expected public boolean isdigit(char c) ^ LexicalAnalyzer.java:314: error: illegal start of expression public boolean isspace(char c) ^ LexicalAnalyzer.java:314: error: ';' expected public boolean isspace(char c) ^ LexicalAnalyzer.java:314: error: ';' expected public boolean isspace(char c)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!