Grammas are the language of languages. A grammar defines a language. Behind every language, there is a grammar that determines its structure.
In computer science, the most common type of grammar is the context-free grammar. Context-free grammars have sufficent richness to describle the recursive syntactic structure of many languages.
A set of rules is the core components of a grammar. Each rule has two parts: (1)a name and (2) an expression of the name.
For instance, if we were creating a grammar to handle english text, we might add a rule like:
noun-phras may expand into article noun
from which we could ultimately deduce the that 'the dog' is a noun-phrase. Or, if we were describing a programming language, we could add a rule like:
expression may expand into expression + expression
If we're looking with grammars as mathematical objects, thne instead of writing 'may expand into', we'd simplt write ->:
noun-phrase -> article noun
expression -> expression + expression
As an example, consider the classic unambiguouse expression grammar:
expr -> term + expr
expr -> term
term -> term * factor
term -> factor
factor -> ( expr )
factor -> const
const -> integer