Question:

Which type of grammar is mainly used in programming languages?

Show Hint

While the syntax of a language is Context-Free, the semantics (like checking if a variable was declared before use) often require Context-Sensitive analysis.
Updated On: Jul 4, 2026
  • Regular
  • Context free
  • Unrestricted
  • Linear
Show Solution
collegedunia
Verified By Collegedunia

The Correct Option is B

Solution and Explanation

Concept: Chomsky hierarchy classifies grammars into four types: Type 0 (Unrestricted), Type 1 (Context-Sensitive), Type 2 (Context-Free), and Type 3 (Regular).
Context-Free Grammar (CFG): Capable of describing recursive structures.
Programming Languages: Require nesting, such as balanced parentheses and nested loops.

Step 1:
Understanding the limitations of Regular Grammars.
Regular grammars cannot handle "nesting" or "matching" structures. They cannot verify if every opening brace has a corresponding closing brace.

Step 2:
Matching CFG to language syntax.
Most syntax in C, Java, or Python is defined by "Context-Free" rules. Backus-Naur Form (BNF), used to specify language standards, is a notation for CFG.

Step 3:
Identifying the role in compilers.
The "Parser" stage of a compiler uses a CFG to build a Parse Tree. This tree represents the logical structure of the source code.
Was this answer helpful?
0
0