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!