Hello all,

I am writing a program for a class that implements a parser for a given grammar. I have already successfully written it in c++ and now I have to also write it in java. The string to be parsed is a string array in my program and when I try to run it, I get a string index out of bounds exception: 5
Any adivce/help is greatly appreciated ! Here is my code:

package parser;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Parser {



	/**
	 * @param args
	 */
	public static void main(String[] args) {



		/*
		A -> I = E
		E -> T + E | T - E | T
		T -> P * T | P / T | P
		P -> I | L | (E)
		I -> a | b | ... | y | z
		L -> 0 | 1 | ... | 8 | 9
		*/
		

        File in = new File("data.txt");
        Scanner scan = null;
        
        String [] line = new String [20];

        String s;
        int i = 0;

        try {
            scan = new Scanner(in);
        }
        catch (FileNotFoundException e) {
            System.exit(1);
        }

        while (scan.hasNext()) {
            line[i] = scan.next();
            ++i;
        }

        scan.close();
        
        s = line[0];


	    
	    System.out.println("String read from file: " + line);
	    int j = 0;

	    
	    if (assign(s, j))
	       System.out.println("The string: '" + line + "' is in the language");
	    else
	       System.out.println("The string: '" + line + "' is not in the language"); 
	

	}


	public static boolean assign (String s, int j)
	{
	    if (integer(s, j))
	    {        
	         
	         ++j;  
	         if (j < s.length() && s.charAt(j) == '=')
	         {             
	             ++j;              
	             if (expr(s, j))
	             {                
	                ++j; 
	                return true;       
	             }
	             else
	                 return false;
	         }
	         else
	             return false;
	     }
	     else
	         return false;
	}

	public static boolean literal(String s, int j)
	{
	     if( j < s.length() && s.charAt(j) >= '0' && s.charAt(j) <= '9')
	     {   
	        
	        return true;
	     }
	     else
	         return false;
	}

	public static boolean integer (String s, int j)
	{
	     if( j <s.length() && s.charAt(j) >= 'a' && s.charAt(j) <= 'z' ) 
	     {
	        return true;
	     }
	     else
	         return false;
	}

	public static boolean primary (String s, int j)
	{
	     if(integer(s, j))
	        return true;
	     else if (literal(s, j))
	          return true;
	     else if (j < s.length() && s.charAt(j) == '(')
	     {
	          ++j;
	          if (expr(s, j))
	          {
	             ++j;
	             if (j < s.length() && s.charAt(j) == ')')
	             {
	                ++j;
	                return true;
	             }
	             else
	                 return false;
	          }
	          else
	              return false;
	     }
	     else
	         return false;
	}

	public static boolean term (String s, int j)
	{
	     if (primary(s, j))
	     {
	          ++j;
	          if (s.charAt(j) == '*' || s.charAt(j) == '/')
	          {
	             ++j;
	             if (term(s, j))
	             {
	                return true;
	             }
	             else
	                 return false;
	          }
	          return true;
	     }
	     else
	         return false;
	}

	public static boolean expr (String s, int j)
	{
	     if (term(s, j))
	     {
	          ++j;          
	          if (s.charAt(j) == '+' || s.charAt(j) == '-')
	          {
	             ++j;
	             if (expr(s, j))
	             {                
	                return true;
	             }
	          }
	          return true;
	     }
	     else
	         return false;
	}
}

Which line is causing the error?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.