Grammar
A grammar defines the legal sentence structure of a language via production rules; common forms include BNF, EBNF, and PEG.
Purpose
Precisely define language syntax, driving parser generation and syntax validation.
Mechanism
BNF: nonterminal → sequence of terminals/nonterminals. EBNF adds repetition/optional/grouping syntactic sugar. Context-free grammars (CFG) describe most programming languages. The LL/LR grammar subsets can be parsed efficiently.
Use cases
- Language design
- Parser generation
- Syntax validation