-2
 public class leftrec {

 static int isleft(String[] left,String[] right)

    {
       int f=0;
       for(int i=0;i<left.length;i++)
       {
           for(int j=0;j<right.length;j++)

           {
               if(left[i].charAt(0)==right[j].charAt(0))
               {
                   System.out.println("Grammar is left recursive");
                   f=1;
               }

           }
       }
          return f;   
    }
    public static void main(String[] args) {
        // TODO code application logic here
        String[] left=new String[10];
        String[] right=new String[10];
        Scanner sc=new Scanner(System.in);
        System.out.println("enter no of prod");
        int n=sc.nextInt();
        for(int i=0;i<n;i++)
        {
            System.out.println("enter left prod");
            left[i]=sc.next();
            System.out.println("enter right prod");
            right[i]=sc.next();
        }

        System.out.println("the productions are");
        for(int i=0;i<n;i++)
        {
            System.out.println(left[i]+"->"+right[i]);
        }
        int flag=0;
       flag=isleft(left,right);
           if(flag==1)
           {
               System.out.println("Removing left recursion");
           }
           else
           {
               System.out.println("No left recursion");
           }       
    }
}

I've written this code to find out if the given grammar is left recursive or not. When i compile the program it gives me NullPointerException in lines

if(left[i].charAt(0)==right[j].charAt(0))

and

isleft(left,right);

How do i remove the exception ?

hivert
  • 10,579
  • 3
  • 31
  • 56

3 Answers3

2

I Guess the problem with your inputs , You are just taking the String Array lengths as 10.

String[] left=new String[10];
String[] right=new String[10];

Dont HardCode the String Array length

int n=sc.nextInt();
String[] left=new String[n];
String[] right=new String[n];
for(int i=0;i<n;i++){
    System.out.println("enter left prod");
    left[i]=sc.next();
    System.out.println("enter right prod");
    right[i]=sc.next();
}

Might ,this would be the problem

Jagadesh Seeram
  • 2,630
  • 1
  • 16
  • 29
0

You need to change the code as follows::

package com.cgi.ie2.common;

import java.util.Scanner;

public class LeftRecursive {

static int isleft(String[] left, String[] right)
{
    int f = 0;
    for (int i = 0; i < left.length; i++) {
        for (int j = 0; j < right.length; j++)
        {
            if (left[i].charAt(0) == right[j].charAt(0)) {
                System.out.println("Grammar is left recursive");
                f = 1;
            }
        }
    }
    return f;
}

public static void main(String[] args) {
    // TODO code application logic here
    Scanner sc = new Scanner(System.in);
    System.out.println("enter no of prod");
    int n = sc.nextInt();
    //Changes done here::::
    String[] left = new String[n];
    String[] right = new String[n];
    for (int i = 0; i < n; i++) {
        System.out.println("enter left prod");
        left[i] = sc.next();
        System.out.println("enter right prod");
        right[i] = sc.next();
    }

    System.out.println("the productions are");
    for (int i = 0; i < n; i++) {
        System.out.println(left[i] + "->" + right[i]);
    }
    int flag = 0;
    flag = isleft(left, right);
    if (flag == 1) {
        System.out.println("Removing left recursion");
    } else {
        System.out.println("No left recursion");
    }
}
  }

This code will eliminate the NullpointerExceptions

If you getting the no. of prod from the console, the String arrays need to set accordingly,for that the changes i have done are::

    System.out.println("enter no of prod");
    int n = sc.nextInt();
    //Changes done here::::
    String[] left = new String[n];
    String[] right = new String[n];

And for better codes what i can suggest you is you need to follow basic coding conventions,which makes your codes readable,codes are not perfect only if it runs corectly,codes are perfect if a coding conventions are follow,so please go through the following links to undestand basic idea of coding conventions::

http://www.javacodegeeks.com/2012/10/java-coding-conventions-considered-harmful.html http://java.about.com/od/javasyntax/a/nameconventions.htm

Charles Stevens
  • 1,568
  • 15
  • 30
0

you can`t initialize an array without size. You have already given the array sizes as 10 and if you enter products which bigger than 10 or smaller than 10 you will get errors. There fore if you want to use dynamic size , you should use a java collection. best approach for this is array list

static int isLeft(ArrayList left, ArrayList right)

    {
        int f = 0;
        for (int i = 0; i < left.size(); i++) {
            for (int j = 0; j < right.size(); j++)

            {
                if (left.get(i).charAt(0) == right.get(j).charAt(0)) {
                    System.out.println("Grammar is left recursive");
                    f = 1;
                }

            }
        }
        return f;

    }

    public static void main(String[] args) {
        // TODO code application logic here
        ArrayList<String> left = new ArrayList<String>();
        ArrayList<String> right = new ArrayList<String>();


        Scanner sc = new Scanner(System.in);
        System.out.println("enter no of prod");
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            System.out.println("enter left prod");
            String leftText = sc.next();
            left.add(leftText);
            System.out.println("enter right prod");
            String rightText = sc.next();
            right.add(rightText);
        }

        System.out.println("the productions are");
        for (int i = 0; i < n; i++) {
            System.out.println(left.get(i) + "->" + right.get(i));
        }
        int flag;
        flag = isLeft(left, right);
        if (flag == 1) {
            System.out.println("Removing left recursion");
        } else {
            System.out.println("No left recursion");
        }

    }
User123456
  • 2,492
  • 3
  • 30
  • 44