Sample: An English Sentence Parser

This example implements a simple English sentence parser. The user types in a simple sentence in English, using words from a very tiny vocabulary, then clicks the Parse button. The parser reads the sentence, and identifies the sentence structure. Finally it reports on the number of adjectives in the sentence, whether it was in the present or past tense, and whether the subject of the sentence was singular or plural. The rules for sentence construction are very simple. The sentence can begin with an optional adverb. The subject can be preceded by the definite or indefinite article if singular, and can be decorated by zero or more adjectives. The verb can be preceded by an adverb, and is either present tense, or the past (perfect) tense. The object of the sentence can be preceded by the definite or indefinite article, and can be decorated by zero or more adjectives. Finally an optional adverb can appear at the end of the sentence.

The vocabulary is also extremely limited in this demonstration, with words taken from the following sets:

Nouns Verbs Adjectives Adverbs
Cat Lick Hairy Noisily
Dog Defend Pink Morosely
Rabbit Like Little Lovingly
Human Reveal Cold Quickly

This sample demonstrates the use of an inline parser, with the grammar being read from a data file SentenceParser.g at run-time. The parser is built by calling the parser library method ParserEngine.CreateInlineParser<SentenceParser>(...), and this parser is then used as a prototype so that the parsers actually used for parsing successive blocks of input text are cloned from it. The sample also demonstrates the use of guard functions on input tokens within the grammar. These are used to make sure that a plural verb matches a plural subject, for example.

The structure of the parser tables that drive the parser state machine can be viewed by clicking the Parser Tables button. This is interesting as it demonstrates how the parser generator algorithm rewrites the grammar rules to deal with guard conditions on non-terminal tokens in the grammar.

To download a zipped copy of the source files, solution and project files for this example, click here.