1. Engineering
  2. Computer Science
  3. import javaio import javautil import javautilscanner import javalangstring public class...

Question: import javaio import javautil import javautilscanner import javalangstring public class...

Question details

import java.io.*;

import java.util.*;

import java.util.Scanner;

import java.lang.String;

public class Parser {

public static Scanner scanner;

public static String leng;

public static int count;

public static void main(String[] args) {

System.out.println("E -> P O P | P"); //

System.out.println("O -> + | - | * | / | **"); //

System.out.println("P -> I | L | UI | UL | (E)");//

System.out.println("U -> + | - | !"); //

System.out.println("I -> C | CI");

System.out.println("C -> a | b | ... | y | z ");

System.out.println("L -> D | DL");

System.out.println("D -> 0 | 1 | ... | 8 | 9 ");

// to read the file named 'input.txt'

String KL;

Parser document = new Parser();

KL = "input.txt";

if(document.open(KL)== 0)

{

while(scanner.hasNextLine())

{

count = 0;

document.reader();

}

scanner.close();

}

}

public int open(String Filer)

{

try

{

scanner = new Scanner(new File(Filer));

return 0;

}

catch(FileNotFoundException x)

{ System.out.println("No file has been found");

return 1;

}

}

public void reader()

{

leng = scanner.nextLine();

System.out.println("String read from file :" + leng);

if(E() && count == leng.length())

{

System.out.println("The given String= " + leng + " Is In The Language!");

}

else{ System.out.println("The String= " + leng + " Is Not In The Language!");

}

}

//**********************************************

public static boolean E()

{

if (P())

{

if (count < leng.length())

count++;

if(O())

{

return true;

}

else if(P())

{

return true;

}

}

return false;

}

//**********************************************

public static boolean O()

{

if(leng.charAt(count) == '+' || leng.charAt(count) == '-' || leng.charAt(count) == '*' || leng.charAt(count) == '/')

{

count++;

return true;

}

return false;

}

//**********************************************

public static boolean P()

{

if (I())

{

return true;

}

else if

(L())

{

return true;

}

else if(U())

{

count++;

if(I())

{

return true;

}

else if(L())

{

return true;

}

else{

return false;

}

}

else

{

if (count < leng.length() && leng.charAt(count) == '(')

{

count++;

if (E())

{

if (count < leng.length() && leng.charAt(count) == ')')

{

count++;

return true;

}

}

}

}

return false;

}

//*************************************

public static boolean U()

{

if (leng.charAt(count) == '+' || leng.charAt(count) == '-' || leng.charAt(count) == '!')

{

count++;

return true;

}

return false;

}

//*************************************

public static boolean I()

{

if (C()){return true;}

else if(C())

{

count++;

if(I())

{

return true;

}

return false;

}

return false;

}

//*************************************

public static boolean C()

{

if (count < leng.length() && (leng.charAt(count) >= 'a' && leng.charAt(count) <= 'z'))

{

count++;

return true;

}

return false;

}

//*************************************

public static boolean L()

{

if (D()){return true;

}

else if(D())

{

count++;

if(L())

{

return true;

}

return false;

}

return false;

}

//*************************************

public static boolean D()

{

if (count < leng.length() && (leng.charAt(count) >= '0' && leng.charAt(count) <= '9'))

{

count++;

return true;

}

return false;

}

}

when i read string a and 0 and a+ this gives me StringIndexOutOfBonds 1 and 2 i need to solve this error as soon as possible

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution