Results 1 to 3 of 3
- 03-21-2014, 02:39 AM #1
Member
- Join Date
- Mar 2014
- Posts
- 2
- Rep Power
- 0
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."); } } }
Java Code:The string read from file: a=a+b-c*d The string "" is not in the language.
Java Code:s = args.length == 1 ? args[0] : "";
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.
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.
- 03-21-2014, 04:27 AM #2
Member
- Join Date
- Mar 2014
- Posts
- 2
- Rep Power
- 0
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); } }
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.
The next two strings should be in the language though.
Thoughts?
- 03-21-2014, 08:16 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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.
Build a wall around Donald Trump; I'll pay for it.
Similar Threads
-
HashMap with Objects, reading from file, writing to file. (note system)
By Java Dude in forum New To JavaReplies: 0Last Post: 12-15-2012, 02:37 AM -
help with BNF Grammar program using recursive descent parsing
By carolain79@hotmail.com in forum New To JavaReplies: 1Last Post: 10-21-2009, 09:00 PM -
BNF grammar using recursive descent parsing
By carolain79@hotmail.com in forum New To JavaReplies: 2Last Post: 10-19-2009, 03:40 PM -
Grammar Checker
By zaz_rin in forum New To JavaReplies: 1Last Post: 07-19-2009, 03:01 PM -
Output correct grammar
By JordashTalon in forum New To JavaReplies: 2Last Post: 03-06-2009, 01:22 AM
Bookmarks