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. It then 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 ("the" or "a"), and can be decorated by zero or more adjectives. Finally, an optional adverb can appear to the left of the verb.
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 |
An example of a valid (grammar conformant) sentence could be: "The hairy dogs lovingly licked the little pink rabbit."
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
ParserFactory<SentenceParser>.CreateInstance()
,
after initialising the parser factory using the InitializeFromGrammar
method. This method reads the input grammar at runtime,
and converts it into a loaded dynamic assembly for the parser defined by the
grammar.
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, in this 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.
The other thing of note in this example is that it shows how to bind rule
reduction actions to named methods in the SentenceParser
class, as well as
how to write and use inline action code in the grammar itself. Mixing these two
approaches is probably undesirable, but for the purposes of demonstrating
both techniques in one example it has been done here.
The source code and grammar files for the projects in this
example are included in the parser toolkit download file ParseLR.zip
,
and when unzipped will be found in the SentenceParserExample
folder.