Context-Free Grammars Programming Languages
Context-Free Grammars Programming Languages
Context-Free Grammars
A context-free grammar basically consists of a finite set of grammar rules. In order to define grammar
rules, we assume that we have two kinds of symbols: the terminals, which are the symbols of the
alphabet underlying the languages under consideration, and the non-terminals, which behave like
variables ranging over strings of terminals. A rule is of the form A, where A is a single nonterminal, and
the right-hand side x is a string of terminal and/or nonterminal symbols. As usual, first we need to define
what the object is (a context free grammar), and then we need to explain how it is used. Unlike
automata, grammars are used to generate strings, rather than recognize things.
1. V is the (finite) set of variables (or non-terminals or syntactic categories). Each variable
represents a language i.e., a set of strings
2. T is a finite set of terminals, i.e., the symbols that form the strings of the language being defined.
3. P is a set of production rules that represent the recursive definition of the language.
4. S is the start symbol that represents the language being defined. Other variables represent
auxiliary classes of strings that are used to help define the language of the start symbol.
The productions of a grammar are used to derive strings. In this process, the productions are used a
rewrite rules.
1. A variable that is being (partially) defined by the production. This variable is often called the
head of the production.
2. The production symbol --->.
3. A string zero or more terminals and variables. This string called the body of the prodcution,
represents one way to form strings in the language of the variable of the head.
In doing so, we leave terminals unchanged and substitute for each variable of the body any string that is
known to be in the language of that variable.
Formally, we define the derivation relation associated with a context-free grammar. First, let us review
the concepts of transitive closure and reflexive and transitive closure of a binary relation.