Grammar Guide
 

Grammar Guide


Notes on grammar rule style.

While the format of a grammar file may vary depending both on the phrases to be recognized and the preferences of the grammar writer, we would like to illustrate two common approaches to grammar writing.

The string of terminals approach

In this approach, the right hand sides of rules consist almost exclusively of terminals. For example,
<commands> = show smart icons                    |
             hide smart icons                    |
             show ruler                          |
             hide ruler                          |
             show headers                        |
             hide headers                        |
             span margin to margin               |
             go to the beginning of the document .

The lexicon approach

In this approach grammar writers follow the additional constraint that the right hand sides of rules can consist of a sequence of nonterminals or a single terminal. (Thus, the grammar above would not be allowed.) In this scheme the terminal rules are typically grouped in a "lexicon" section of the grammar file. For example:
<show-hide-command> = <show-hide> <show-object>  .

<show-hide> = <show> | <hide> .

<show-object> = <smart> <icons>       |
                <ruler>               |
                <headers>             |
                <outline> <tools>     |
                <misspelled> <words>  .

// lexicon

<headers> = headers .
<hide> = hide .
<icons> = icons .
<misspelled> = misspelled .
<outline> = outline .
<ruler> = ruler .
<show> = show .
<smart> = smart .
<tools> = tools .
<words> = words .


[ Top of Page | Previous Page | Back to Grammar Guide ]