Home | Site Map |
 
Anoca.org  


Context free grammar

(contextfreegrammar,contextfree grammar,context freegrammar)





In linguistics and computer science , a context-free grammar (CFG) is a formal grammar in which every production rule is of the form

V → w

where V is a non-terminal symbol and w is a string consisting of terminals and/or non-terminals. The term"context-free" comes from the fact that the non-terminal V can always be replaced by w, regardless of in what context itoccurs. A formal language is context-free if thereis a context-free grammar that generates it.

Context-free grammars are powerful enough to describe the syntax of programming languages ; in fact, the syntax of most programming languages are specified with the helpof context-free grammars. On the other hand, context-free grammars are simple enough to allow the construction of efficient parsing algorithms which, for a given string, determine whether and how it can be generated from the grammar. See Earley parser for an example of such an algorithm. LRparser and LL parser are further methods to parse more restrictive subsets ofcontext-free grammars. See also parsing expressiongrammar for another type of formal grammar particularly suited to programming languages.

BNF ( Backus-Naur Form ) is the most common notation used toexpress context-free grammars.

Contents

Examples

Example 1

A simple context-free grammar is

S → aSb | ε

where | is used to separate different options for the same non-terminal and ε stands for the empty string. This grammargenerates the language \{ a^n b^n : n \ge 0 \}which is not regular .

Example 2

Here is a context-free grammar for syntactically correct infix algebraic expressions in the variables x, y and z:

S → x | y | z | S + S | S - S | S * S | S/S | (S)

This grammar can for example generate the string "( x + y ) * x - z * y / ( x + x )".

Example 3

A context-free grammar for the language consisting of all strings over {a,b} which contain a different number of a's than b'sis

S → U | V
U → TaU | TaT
V → TbV | TbT
T → aTbT | bTaT | ε

Here, T can generate all strings with the same number of a's as b's, U generates all strings with more a's than b's and Vgenerates all strings with fewer a's than b's.

Other examples

Context-free grammars are not limited in application to mathematical ("formal") languages. The ancient Indian linguist Panini described Sanskrit using acontext-free grammar. Recently, it has been suggested that a class of Tamil poetry called Venpa is governed by a context-free grammar.

Derivations and Syntax trees

There are basically two ways to describe how in a certain grammar a string can be derived from the start symbol. The simplestway is to list the consecutive strings of symbols, beginning with the start symbol and ending with the string, and the rules thathave been applied. If we introduce a strategy such as "always replace the left-most nonterminal first" then for context-freegrammars the list of applied grammar rules is by itself sufficient. This is called the leftmost derivation of a string.For example, if we take the following grammar:

(1) S → S + S
(2) S → 1

and the string "1 + 1 + 1" then the left derivation of this string is the list [ (1), (1), (2), (2), (2) ]. Analogously therightmost derivation is defined as the list that we get if we always replace the rightmost nonterminal first. In thiscase this would be the list [ (1), (2), (1), (2), (2)].

The distinction between leftmost derivation and rightmost derivation is important because in most parsers the transformation of the input is defined by giving a piece of code for every grammar rule that isexecuted whenever the rule is applied. Therefore it is important to know whether the parser determines a leftmost or a rightmostderivation because this determines the order in which the pieces of code will be executed. See for an example LL parsers and LR parsers .

A derivation also imposes in some sense a hierarchical structure on the string that is derived. For example the structure ofthe string "1 + 1 + 1" would, according to the leftmost derivation, be:

S→S+S (1)
S→S+S+S (1)
S→1+S+S (2)
S→1+1+S (2)
S→1+1+1 (2)
{ { { 1 }S + { 1 }S }S + { 1 }S }S

where { ... }S indicates a substring recognized as belonging to S. This hierarchy can also be seen as a tree:

            S           /|\          / | \         /  |  \        S  '+'  S       /|\      |      / | \     |     S '+' S   '1'     |     |    '1'   '1'


This tree is called a concrete syntax tree (see also abstract syntax tree ) of the string. In this case the presented leftmost and the rightmost derivationdefine the same syntax tree, however there is another (leftmost) derivation of the same string possible

S→ S + S (1)
S→ 1 + S (2)
S→ 1 + S + S (1)
S→ 1 + 1 + S (2)
S→ 1 + 1 + 1 (2)

and this defines the following syntax tree:

            S            /|\          / | \         /  |  \        S  '+'  S        |      /|\        |     / | \       '1'   S '+' S             |     |            '1'   '1'

If for certain strings in the language of the grammar there are more than one parsing tree then the grammar is said to be anambiguous grammar. Such grammars are usually hard to parse because the parser cannot always decide which grammar rule ithas to apply.

Normal forms

Every context-free grammar which does not generate the empty string can be transformed into an equivalent one in Chomsky normal form or Greibach normal form . "Equivalent" here means that the two grammars generate the same language.

Because of the especially simple form of production rules in Chomsky Normal Form grammars, this normal form has boththeoretical and practical implications. For instance, given a context-free grammar, one can use the Chomsky Normal Form toconstruct a polynomial-time algorithm which decides whether a given string is in the language represented by that grammar or not(the CYK algorithm ).

Properties of context-free languages

  • An alternative and equivalent definition of context-free languages employs non-deterministic push-down automata : a language is context-free if and only if it canbe accepted by such an automaton.
  • The union and concatenation of two context-free languages is context-free; the intersection need not be.
  • The reverse of a context-free language is context-free, but the complement need not be.
  • Every regular language is context-free because it can bedescribed by a regular grammar .
  • The intersection of a context-free language and a regular language is always context-free.
  • There exist context-sensitive languages which are not context-free.
  • To prove that a given language is not context-free, one employs the pumping lemma for context-free languages.

See also




context free garmmar, language, context free grammra, languages, cntext free grammar, form, context free gammar, derivation, contetx free grammar, tree, context free grammar, leftmost, , formal, conext free grammar, every, context free rammar, parsing, contxet free grammar, examples, ocntext free grammar, one, context freegrammar, rightmost, conetxt free grammar, whether, context fre egrammar, parsers, context free grammr, equivalent, cnotext free grammar, rules, context fre grammar, chomsky, contet free grammar, terminal, contextf ree grammar, structure, cotnext free grammar, determines, co...


This article is completely or partly from Wikipedia - The Free Online Encyclopedia. Original Article. The text on this site is made available under the terms of the GNU Free Documentation Licence. We take no responsibility for the content, accuracy and use of this article.

Anoca.org Encyclopedia
0.01s