Using Java, a program that reads as input an arbitrary context-free grammar from a text file and then derives/enumerates five strings from the grammar. The file cfg.txt can be used to test your program. The grammar represented in cfg.txt is the following grammar for non-palindromes (from input alphabet {a,b}): S -> aSa | bSb | X X -> aYb | bYa Y -> aY | bY | lambda This is formatted in cfg.txt as follows: S aSa S bSb S X X aYb X bYa Y aY Y bY Y * An asterisk is used for lambda. Assume that the first state listed in the input file is the start state. Read the grammar into a map (aka hash or dictionary) where the key is a variable and the value is a list of right-hand sides. If you are using Java, you could use Hashtable by declaring a Hashtable of the following type: Hashtable> Do not write the program specific to this grammar in cfg.txt. Further, assume that the test file will have more than three variables, and productions on the right hand side can contain more than three terminals or variables. You should modify cfg.txt to ensure that it will works with a larger grammar. In general, the loop which reads in the grammar should look up the variable in the hash table. If the table returns null, it means that the variable is not present yet, so an array list should be created, the right hand side will be added to it, and stored into table. If the variable already exists, simply add the right hand side to the end of the array list (returned from the hash table). After reading the grammar, simulate the derivation of a string in the language of a grammar by pushing the start variable onto a stack. In Java, the Stack would be of this type: Stack Next, follow this procedure: while stack not empty: pop a character if it's a terminal, print it otherwise push one of its right hand sides, chosen randomly Generate five strings randomly for this grammar.     public class CFG { public static void main(String[] args) throws Exception { Hashtable> hash = new Hashtable<>(); File theFile = new File("cfg.txt"); Scanner keyboard = new Scanner(theFile); while (keyboard.hasNext()) { String left = keyboard.next(); String right = keyboard.next(); ArrayList prods = hash.get(left); if (prods == null) { prods = new ArrayList<>(); } prods.add(right); hash.put(left, prods); }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Using Java, a program that reads as input an arbitrary context-free grammar from a text file and then derives/enumerates five strings from the grammar. The file cfg.txt can be used to test your program. The grammar represented in cfg.txt is the following grammar for non-palindromes (from input alphabet {a,b}):

S -> aSa | bSb | X
X -> aYb | bYa
Y -> aY | bY | lambda
This is formatted in cfg.txt as follows:

S aSa
S bSb
S X
X aYb
X bYa
Y aY
Y bY
Y *
An asterisk is used for lambda. Assume that the first state listed in the input file is the start state. Read the grammar into a map (aka hash or dictionary) where the key is a variable and the value is a list of right-hand sides. If you are using Java, you could use Hashtable by declaring a Hashtable of the following type:
Hashtable<String, ArrayList<String>>
Do not write the program specific to this grammar in cfg.txt. Further, assume that the test file will have more than three variables, and productions on the right hand side can contain more than three terminals or variables. You should modify cfg.txt to ensure that it will works with a larger grammar. In general, the loop which reads in the grammar should look up the variable in the hash table. If the table returns null, it means that the variable is not present yet, so an array list should be created, the right hand side will be added to it, and stored into table. If the variable already exists, simply add the right hand side to the end of the array list (returned from the hash table).
After reading the grammar, simulate the derivation of a string in the language of a grammar by pushing the start variable onto a stack. In Java, the Stack would be of this type:

Stack<String>
Next, follow this procedure:
while stack not empty:
pop a character
if it's a terminal, print it
otherwise push one of its right hand sides, chosen randomly
Generate five strings randomly for this grammar.

 

 

public class CFG {

public static void main(String[] args) throws Exception {
Hashtable<String, ArrayList<String>> hash = new Hashtable<>();
File theFile = new File("cfg.txt");
Scanner keyboard = new Scanner(theFile);

while (keyboard.hasNext()) {
String left = keyboard.next();
String right = keyboard.next();
ArrayList<String> prods = hash.get(left);

if (prods == null) {
prods = new ArrayList<>();
}
prods.add(right);
hash.put(left, prods);
}

Expert Solution
steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY