9 - Context-Free Grammars and Context-Free Languages

In any language such as English, Hindi or Sanskrit, words can be combined in several ways. Naturally, some combinations form valid sentences, while others do not. The validity of a sentence is determined by the grammar of a language, which comprises a set of rules . For instance, ‘The boy prepares tea quickly’, although meaningless, is a perfectly legal sentence. In other words, the sentences in a language may be nonsensical, but they must obey the rules of grammar. The discussion in this chapter deals with only the syntax of sentence (the way the words are combined) and not with the semantics of sentences (meaning).

In the previous chapters, two different (though equivalent) methods: finite automata and regular expressions were introduced for describing languages. These methods have their own limitations in the sense that some simple languages, such as , cannot be described by these methods. Formal languages and grammars are widely used in connection with programming languages. During programming, we proceed with an intuitive knowledge of the languages, which leads to errors. Therefore, a precise description of the language is needed, at almost every step, which helps to understand the syntax diagrams found in programming texts. Among the ways in which programming languages can be defined precisely, grammars or context-free grammars are most widely used. This method happens to be a very powerful method and such grammars can describe certain features which have a recursive structure. Context-free-grammars (CFG) were first used in the study of human languages.