Hello all,

I am writing a program that implements a parser for the grammar. One of the methods I created is a boolean method which returns true or false depending on if a character is an expression. The compiler is saying "this method must return a result of type boolean" but i have four boolean returns! It is the last method in the following 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 [5];

        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: \"" + s + "\" .");
	    int j = 0;

	    
	    if (assign(s, j))
	       System.out.println("The string \"" + s + "\" is in the language.");
	    else
	       System.out.println("The string \"" + s + "\" 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 (j == '+' || s.charAt(j) == '-')
	          {
	             ++j;
	             if (expr(s, j))
	             {                
	                return true;
	             }
	             else
	            	 return false;
	            
	          }
	          	          
	     }
	     else
	    	 return false;
	   
	     
		
	}
}

Thanks in advance for everyone's help!

Recommended Answers

All 2 Replies

It's because the compiler doesn't think that your method is guaranteed to return either true or false. I.e., it thinks that it's possible to reach the end of your method without returning anything at all. If you're positive that your method will always return either true or false (based on your if statements), then you can go ahead and just put "return true;" at the very bottom of the method and the error will go away. I'd also put "throw new Exception("Shouldn't have gotten this far")" or something similar while you debug.

To drive my point home, consider this code:

public static boolean maybeReturns(int i){

if (i > 100) return true; 

}

The compiler would give the same error since it doesn't think that my method will *always* return something. Your case (in the eyes of the compiler) is the same, except that you code is a lot more complex.

thank you !!!

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.