Build a program to take a BNF grammar and generate sentences in the language specified by the grammar. The program will read the grammar and then it will allow interactively building a sentence in the language specified by the grammar. Assume the BNF is correct. All items in a rule are separated by white space. (You can require exactly one space between items). Each rule is exactly one line.
The BNF rules (referred to as a grammar) are stored in a file. Your program is to read and echo the grammar file, and then walk the user through the process of building a sentence.
Once the grammar is read, the program will prompt the user to build a sentence in the specified grammar. For example assume the grammar is:
Code:<s> -> let <var> = <val> <val> -> <var> | <num> <var> -> a | b <num> -> 1 | 2 | 3
The program and the user could have the following interaction (user answers after the ?):
Code:deriving <s> deriving <var> specify number of choice: 1 a, 2 b ?2 deriving <val> specify number of choice: 1 <var>, 2 <num> ?2 deriving <num> specify number of choice: 1 1, 2 2, 3 3 ?1 The derived sentence is: let b = 1
Assume the left hand side (LHS) of the first rule specifies the start symbol. Demonstrate the program with at least two grammars and for each grammar at least two examples. You need to use recursion to run through a rule after you start. Otherwise, you have to keep a stack of states of where you are.
I am looking to do this in java and I have no idea where to begin. I'm guessing I need to use tokens for sure, and file input, but i'm not sure how to do the recursive part.
I figured I would ask for help on here, because it's been a helpful place in the past and I know there are some fellow CSers on here.
Any help/tips are greatly appreciated,
Nick