Bnf Ebnf Homework For Kids

This action might not be possible to undo. Are you sure you want to continue?

class BnfTokenizerTest

Thoroughly test (with JUnit 4) all your tokenizer methods.

Part 2

class BNF

An instance of the class represents a complete BNF grammar, typically consisting of many rules. Each rule is represented as a tree.

Your data representation should be a . The keys are nonterminals; the values are the definitions of those nonterminals, represented as trees. In your tree representation, use the token types , ,, , , and , but not .

  • and tokens are the leaves of your tree.
  • When one thing follows another in sequence, make them children of a token.
    • Example: would be entered into the HashMap with the key and the value as a tree with the root and the three children ,(a terminal), and .
    • Every node should have at least two children.
  • When there are alternatives, make them children of an token.
  • When there is an option, make the optional part the child or children of an token.
  • When something may be repeated zero or more times, make that thing the child or children of an token.

Required methods of the BNF class

Uses the to read in any number of grammar rules into this grammar.

Note: is an abstract class. Concrete subclasses include , which is good for reading from a file, and , which is good for reading from a String. You need the former in your class, and you should use the latter for unit testing.
Writes out the entire grammar, in a way that it could be read in again by . Note: See . Do at least a small amount of unit testing of this method; the class makes this possible. You don't have to do a lot, just enough to show that you can.
Look up the nonterminal in the HashMap and return the Tree. This method should check whether the argument is enclosed in angle brackets, and add them if they are not already present; so and should both work.

A Program Grammar

Write a grammar for a really, really simple programming language. (You will never need to implement this language; just define the syntax for it.)

Keep your language really simple, but do try to make it something that it would be possible to use to write programs. Use all of the features of EBNF: Sequencing, alternatives, optional elements, and repeated elements.

Use as your "top-level" nonterminal.

class ProgramGenerator

Write a main class that does the following:

  • Uses a to prompt the user for a file containing the BNF grammar rules.
  • Reads in the BNF grammar and stores it in a data structure as described above.
  • Prints out the grammar.
  • Generates and prints three or four random "programs."

should have (at least) the following methods:

    • Given a terminal or a nonterminal, expands it into a of terminals.
      • Base case: If is a terminal, returns a containing it.
      • Recursive case: If is a nonterminal, randomly choose a definition for it; expand each nonterminal in the definition into a list of terminals. Write this method as a recursive method.
    • Print out the strings (which should all be terminals), separated by spaces.
    • Don't print out the list all on one extremely long line! Break it up into lines somehow. If you use semicolons or newlines to terminate statements in your little programming language, you can use them here.
    • If an argument is given, that will be the path to the BNF file you should use (there will not be more than one). If no argument is given, uses a to prompt the user for a file containing the BNF grammar rules.
    • Reads in the BNF grammar and stores it in a data structure as described above.
    • Uses the instance method in the class to print out the grammar.
    • Generates and prints (using ) three or four random nonsense "programs

JUnit and Javadoc

JUnit test all your methods except those that do I/O. For the BNF class, also test .

Document all non-private methods. JUnit test methods don't have to be documented unless they do something non-obvious. Generate the Javadoc and turn it in as part of your assignment submission; but look it over before you turn it in, because you are likely to discover some problems that way

Due date

Zip up and turn in Part 1, including all test files and the generated Javadoc, via Canvas, before 6 AM Monday, February 18. Part 2 will be due a week later.


0 thoughts on “Bnf Ebnf Homework For Kids

Leave a Reply

Your email address will not be published. Required fields are marked *