...

/

Regular Grammars

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.

Press + to interact
NFA, which accepts strings of ones and zeros with a one in the third-to-last place
NFA, which accepts strings of ones and zeros with a one in the third-to-last place

To illustrate, the NFA given above, which accepts strings of ones and zeros with a one in the third-to-last place (assuming Σ={0,1}\Sigma = \{0,1\}), appears below with the states renamed for convenience.

Press + to interact
NFA accepting strings with 1 in the third-to-last place
NFA accepting strings with 1 in the third-to-last place

The state names serve as variable names in the grammar. From state SS we can loop on SS with either character, or we can move to state XX with a 11. The following rewrite rule expresses this in formal grammar terms:

S0S    1S    XS \rightarrow 0S \;|\; 1S \;|\; X

We can similarly express the transitions from XX and YY. Since ZZ 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:

S0S    1S    1XX0Y    1YY0Z    1ZZλ \begin{align*} S & \to 0S \;|\; 1S \; | \; 1X \\ X & \rightarrow 0Y \; | \; 1Y \\ Y & \rightarrow 0Z \; | \; 1Z \\ Z & \rightarrow \lambda \end{align*}

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:

S0S01X010Y0100Z0100S \Rightarrow 0S \Rightarrow 01X \Rightarrow 010Y \Rightarrow 0100Z \Rightarrow 0100

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 VsXV \to sX or VsV \to s, where VV and XX are variables and ss is any string in Σ\Sigma^* (i.e., ss contains no variables).

Examples of right-linear grammars

Suppose that we want to generate strings of ones and zeros representing binary numbers congruent to 1mod31 \bmod 3. We start with the automata shown in the following figure.

Press + to interact
 DFA whose states identify the residue mod 3 of a binary number
DFA whose states identify the residue mod 3 of a binary number

We begin by renaming the states and making the state where the remainder module 33 is 11 the accepting state (ZZ for 00 ...

Access this course and 1400+ top-rated courses and projects.