$15
Assume we start with a simple "sentence" grammar as follows:
<S ::= <NP<V<NP
<NP ::= <A <N
<V ::= loves | hates | eats
<A ::= a | the
<N ::= dog | cat | rat
Part A:
Show the BNF above with the following additional grammar elements:
Adjectives: (0 or more Adjectives separated by commas may precede any Noun. A comma may or may not be preceded by a space.)
furry
fast
slow
delicious
Adverbs: (0 or 1 Adverb may precede any Verb)
quickly
secretly
Conjunctions: (0 or more may appear in any sentence
and
or
Sentence terminator (The terminator may or may not be preceded by a space.)
. (a single period)
! (a single period)
Part B:
Show/Draw the syntax diagrams for each of the grammar elements above. Hyperlink reference not valid.
Part C:
Show the parse trees (which can be generated in ANTLRWorks) for each of the following sentences:
Examples of SYNTACTICALLY VALID Input Strings:
a dog loves the cat.
the cat eats the slow rat and the slow , furry dog secretly hates the cat and a dog loves the rat !
Examples of SYNTACTICALLY INVALID Input Strings (where do they fail):
a dog barks at the cat.
the fast furry cat eats quickly
NOTE: You can generate the full parse trees from ANTLRWorks (as can be done with the attached sample for the base grammar) or simply draw out the cooresponding AST's (Abstract Syntax Trees) "by hand" on paper or with a simple drawing tool. The point of this is to have something that you can then verify against the parse trees generated by your own code (in the next part).
Part D:
Modify the (attached) sample code to accept valid sentences based upon the newly defined grammar above. The parser should also "reject" invalid sentences with a descriptive error message. Note that the program should still accept a filename from the "Command Line" as illustrated in the example. Please no HARD-CODED file paths in the source.