Results 1 to 3 of 3
  1. #1
    mr_zebes is offline Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Default BNF Grammar and reading from a txt file

    Hi everyone. I am still getting the hang of Java and I believe I have coded 90% of this task. Just a few things that I can't seem to grasp.

    I am implementing a recursive descent parser that recognizes strings in the language below. The input should be from a file "input.txt" and output should be to the console.

    The grammar:
    A -> I = E | E
    E -> T + E | T - E | T
    T -> F * T | F / T | F
    F -> P ^ F | P
    P -> I | L | UI | UL | (A)
    U -> + | - | !
    I -> C | CI
    C -> a | b | ... | y | z
    L -> D | DL
    D -> 0 | 1 | ... | 8 | 9

    An example session might look like this:

    String read from file: a=a+b-c*d
    The string "a=a+b-c*d" is in the language.
    String read from file: a=a**b++c
    The string "a=a**b++c" is not in the language.

    Java Code:
    /** 
      * The Grammar
      * A -> I = E | E 
      *	E -> T + E | T - E | T
      *	T -> F * T | F / T | F
      *	F -> P ^ F | P
      *	P -> I | L | UI | UL | (A)
      *	U -> + | - | !
      *	I -> C | CI
      *	C -> a | b | ... | y | z
      *	L -> D | DL
      *	D -> 0 | 1 | ... | 8 | 9
    **/
    
    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class Main {
    	
    private static String s;
    private static int i;
    
    public static boolean A() 
    {
        if (I()) 
        {
            if (i < s.length() && s.charAt(i) == '=') 
            {
                ++i;
                if (E()) 
                {
                    return true;
                }
            }
                else
                {
                	return true;
                }      
        }
        return false;
    }
    
    public static boolean E() 
    {
        if (T()) 
        {
            if (i < s.length() && (s.charAt(i) == '+' || s.charAt(i) == '-')) 
            {
            	++i;
                if (E()) 
                {
                    return true;
                }
            }
            else 
            {
                return true;
            }
        }
        return false;
    }
    
    public static boolean T() 
    {
        if (F()) 
        {
            if (i < s.length() && (s.charAt(i) == '*' || s.charAt(i) == '/')) 
            {
                ++i;
                if (T()) {
                    return true;
                }
            }
            else 
            {
                return true;
            }
        }
        return false;
    }
    
    public static boolean F()
    {	
    	if(P())
    	{
    		if (i < s.length() && s.charAt(i) == '^')
    		{
    			i++;
    			if (T())
    			{
    				return true;
    			}
    		}
    		else
    		{
    			return true;
    		}
    	}
    	return false;
    }
    
    public static boolean P()
    {
    	if(I())
    	{
    		return true;
    	}
    	else if(L())
    	{
    		return true;
    	}
    	else if(U() && I())
    	{
    		return true;
    		
    	}
    	else if(U() && L())
    	{
    		return true;
    	}
    	else if (i < s.length() && s.charAt(i) == '(')
    	{
    		i++;
    		if(A())
    		{
    			if(i < s.length() && s.charAt(i) ==')')
    				i++;
    			return true;
    		}
    	}
    	return false;
    }
    
    public static boolean U()
    {
    	if (i < s.length() && (s.charAt(i) == '+' | s.charAt(i) == '-' | s.charAt(i) == '!'))
    	{
    		i++;
    		return true;
    	}
    	return false;
    }
    
    public static boolean I() 
    {
    	if(C())
    	{
    		return true;
    	}
    	else if(C() && I())
    	{
    		return true;
    	}
    	return false;
    }
    
    public static boolean C()
    {
    	if(i < s.length() && 'a' <= s.charAt(i) && s.charAt(i) <='z')
    	{
    		i++;
    		return true;
    	}
    	return false;
    }
    
    public static boolean L()
    {
    	if(D())
    	{
    		return true;
    	}
    	else if(D() && L())
    	{
    		return true;
    	}
    	return false;
    }
    
    public static boolean D() 
    {
        if (i < s.length() && '0' <= s.charAt(i) && s.charAt(i) <= '9') 
        {
            ++i;
            return true;
        }
        return false;
    }
        
    public static void main(String[] args)
    {	
        File in = new File("input.txt");
    
        try 
        {
        	Scanner scan = new Scanner(in);
      
            while (scan.hasNext())
            {
            	System.out.printf("The string read from file: ");
            	System.out.println(scan.next());
            } 
        }
      
        catch (IOException e) 
        {
            e.printStackTrace();
            System.exit(0);
        }
       
    s = args.length == 1 ? args[0] : "";
    
    if (A() && i == s.length()) 
    {
        System.out.println("The string \"" + s + "\" is in the language.");
    }
    else 
    {
        System.out.println("The string \"" + s + "\" is not in the language.");
    }
    
    }
    
    }
    My current output looks like this:

    Java Code:
    The string read from file: a=a+b-c*d
    The string "" is not in the language.
    So it seems to be reading the input file correctly. My error seems to be on this part
    Java Code:
    s = args.length == 1 ? args[0] : "";
    When I put the string in that line such as: s = args.length == 1 ? args[0] : "a=a+b-c*d";

    My output is:
    Java Code:
    The string read from file: a=a+b-c*d
    The string "a=a+b-c*d" is in the language.
    How can I go about it reading the string from the input file though?

    Also, I am not too sure my boolean functions for P, I and, L are correct. Mainly when two terms are together (UI, UL, CI, DL).

    Thank you.

  2. #2
    mr_zebes is offline Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Default Re: BNF Grammar and reading from a txt file

    So further analyzing I think I am getting on the right track. Here is my revision on the main function.

    Java Code:
    public static void main(String[] args)
    {	
    	s = args.length == 1 ? args[0] : "" ;
        
    	File in = new File("input.txt");
    
        try 
        {
        	Scanner scan = new Scanner(in);
      
            while (scan.hasNext())
            {
            	s = scan.next();
            	System.out.printf("The string read from file: ");
            	System.out.println(s);
            	if (A() && i == s.length())
            	{
            	    System.out.println("The string \"" + s + "\" is in the language.");
            	}
            	else 
            	{
            	    System.out.println("The string \"" + s + "\" is not in the language.");
            	}
            } 
        }
     
        catch (IOException e) 
        {
            e.printStackTrace();
            System.exit(0);
        }
    }
    My input text file looks like this:
    a=a**b++c
    a=a+b-c*d
    a=b

    Output in console:
    Java Code:
    The string read from file: a=a**b++c
    The string "a=a**b++c" is not in the language.
    The string read from file: a=a+b-c*d
    The string "a=a+b-c*d" is not in the language.
    The string read from file: a=b
    The string "a=b" is not in the language.
    So the first string is correctly analyzed as not being in the language.

    The next two strings should be in the language though.

    Thoughts?

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: BNF Grammar and reading from a txt file

    You should reset the current character index 'i' to zero before you start parsing.

    kind regards,

    Jos

    ps. the few rules you where uncertain about, are correct although highly inefficient.

    edit: I take that back; e.g. your method that tries to parse an integer number, return true after having parsed a single digit so it can't even recursively parse the entire number; change it into a loop ...
    Last edited by JosAH; 03-21-2014 at 08:35 AM.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 0
    Last Post: 12-15-2012, 02:37 AM
  2. help with BNF Grammar program using recursive descent parsing
    By carolain79@hotmail.com in forum New To Java
    Replies: 1
    Last Post: 10-21-2009, 09:00 PM
  3. BNF grammar using recursive descent parsing
    By carolain79@hotmail.com in forum New To Java
    Replies: 2
    Last Post: 10-19-2009, 03:40 PM
  4. Grammar Checker
    By zaz_rin in forum New To Java
    Replies: 1
    Last Post: 07-19-2009, 03:01 PM
  5. Output correct grammar
    By JordashTalon in forum New To Java
    Replies: 2
    Last Post: 03-06-2009, 01:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •