Using Java, write 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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Using Java, write 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.

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Sorting
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education