Regular Grammars
Learn about regular grammars and types of linear grammars.
We know that formal languages generate strings in a language. Grammars that generate regular languages have a special form, which naturally corresponds to a finite automaton.
To illustrate, the NFA given above, which accepts strings of ones and zeros with a one in the third-to-last place (assuming ), appears below with the states renamed for convenience.
The state names serve as variable names in the grammar. From state we can loop on with either character, or we can move to state with a . The following rewrite rule expresses this in formal grammar terms:
We can similarly express the transitions from and . Since is a final state, we allow it to be replaced by the empty string, meaning that the generation of symbols may terminate. The complete grammar appears below:
Observe the clear correspondence to the NFA. The from-state appears alone on the left, and the to-states appear on the right, preceded by the symbol from the appropriate edge. This is an example of a right-linear grammar. It is “linear” because only one variable appears on each right-hand side. It is “right-linear” because the variable appears last (i.e., on the right end) of each replacement rule. We can derive a sample string as follows:
Right linear grammar: Formal definition
A right-linear grammar is a formal grammar where there is at most one variable on the right-hand side of any rule, and if present, it is the rightmost symbol of the rule.
More formally, rules in a right-linear grammar are of form or , where and are variables and is any string in (i.e., contains no variables).
Examples of right-linear grammars
Suppose that we want to generate strings of ones and zeros representing binary numbers congruent to . We start with the automata shown in the following figure.
We begin by renaming the states and making the state where the remainder module is the accepting state ( for ...