One Grammar with Two Alternative Forms
A grammar production specifies replacements for a category of strings. Alternatives can describe different valid forms under one name.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Trace a finite example
Let S produce ab or ba. The grammar generates exactly those two words because each alternative contains only terminal symbols. The generated language is a set of words.
Add a recursive rule deliberately
Adding a choice that wraps S with a and b permits arbitrarily long words. Reading recursive grammars explains how a base alternative still ends the derivation.[1]
Show which production determines the outer shape
Let start symbol S expand to a parenthesized or bracketed copy of T, and let T expand to x or y. The grammar can derive (x) and [y] while ruling out (x]. The paired delimiters come from the same production. See recursive grammar examples.
S → (T) or [T]
T → x or y
Full outputs: (x),(y),[x],[y]
Preserve dependencies between alternatives
A different grammar with independent opening and closing choices would allow mismatched delimiters. Test both outer alternatives and one hybrid. Add recursion only with a defined condition that can eventually produce terminals.