Open In App

Classification of Context Free Grammars

Last Updated : 28 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A Context-Free Grammar (CFG) is a formal rule system used to describe the syntax of programming languages in compiler design. It provides a set of production rules that specify how symbols (terminals and non-terminals) can be combined to form valid sentences in the language. CFGs are important in the parsing phase of a compiler, where the source code is checked to make sure it follows the rules of the programming language. By using CFGs, the compiler can confirm the code's structure is correct and then organize it into a format that makes it easier to work with for the next steps, like optimization and code generation.

Key components of a CFG:

  1. Non-terminals: These are symbols that can be replaced by other symbols in the production rules. They represent syntactical categories (e.g., statements, expressions). Example: S,A,B.
  2. Terminals: These are the basic symbols of the language that cannot be replaced further. They are the actual symbols in the language (e.g., keywords, operators). Example: a, b, +, if.
  3. Start symbol: This is the non-terminal from which the derivation begins. It represents the entire language. Example: S.
  4. Production rules: These describe how non-terminals can be expanded into sequences of non-terminals and/or terminals. Example: S→A B , A→a.

Example of a CFG:

Let’s take a simple CFG for arithmetic expressions:

  1. E→E+T ∣ T
  2. T→T×F ∣ F
  3. F→(E) ∣ id

Here, E is an expression, T is a term, F is a factor, +,× are operators, id is an identifier (terminal).

Read more about Context Free Grammar.

Classification of CFG

Context Free Grammars (CFG) can be classified on the basis of following two properties: 

1) Based on number of strings it generates:

  • If CFG is generating finite number of strings, then CFG is Non-Recursive (or the grammar is said to be Non-recursive grammar)
  • If CFG can generate infinite number of strings then the grammar is said to be Recursive grammar.

Examples of Recursive Grammar

1) S->SaS    
S->b

The language(set of strings) generated by the above grammar is :{b, bab, babab,...}, which is infinite.

2) S-> Aa
A->Ab|c

The language generated by the above grammar is :{ca, cba, cbba ...}, which is infinite. 

Types of Recursive Grammars 
Based on the nature of the recursion in a recursive grammar, a recursive CFG can be again divided into the following:

  • Left Recursive Grammar (having left Recursion)
  • Right Recursive Grammar (having right Recursion)
  • General Recursive Grammar(having general Recursion)

Example of Non-Recursive Grammar

   S->Aa
A->b|c

The language generated by the above grammar is :{ba, ca}, which is finite.

2) Based on number of derivation trees:

  • If there is only 1 derivation tree then the CFG is unambiguous.
  • If there are more than 1 left most derivation tree or right most derivation  or parse tree , then the CFG is ambiguous .

During Compilation, the parser uses the grammar of the language to make a parse tree(or derivation tree) out of the source code. The grammar used must be unambiguous. An ambiguous grammar must not be used for parsing.

Example of an Unambiguous CFG:

Consider the CFG for a simple arithmetic expression involving addition and multiplication:

E→E+T ∣ T
T→T×F ∣ F
F→(E) ∣ id

This grammar ensures that:

  • The addition operation is done after multiplication.
  • There is only one valid way to parse an expression.

For example, for the expression a+b×c , the only valid parse is that multiplication happens first due to the structure of the grammar. This is an unambiguous grammar because there is only one way to derive the string.

Example of an Ambiguous CFG:

Consider the following CFG for arithmetic expressions:

E→E+E ∣ E×E ∣ id

This grammar does not specify operator precedence. For the string a+b×c , there are two possible derivation trees:

  1. a+(b×c)
  2. (a+b)×c

Both derivations are valid according to the grammar, but they represent different interpretations of the same string, leading to ambiguity.

Note: A linear grammar is a context-free grammar that has at most one non-terminal in the right hand side of each of its productions.


Next Article
Article Tags :

Similar Reads