The Wayback Machine - https://web.archive.org/web/20240916174219/https://www.geeksforgeeks.org/classification-of-context-free-grammars/
Open In App

Classification of Context Free Grammars

Last Updated : 16 Mar, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

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.   

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.

Examples of Recursive and Non-Recursive Grammars 

Recursive Grammars

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. 

Note: A recursive context-free grammar that contains no useless rules necessarily produces an infinite language. 

Non-Recursive Grammars

   S->Aa
   A->b|c

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

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)

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. 


Previous Article
Next Article

Similar Reads

Difference between Thread Context Switch and Process Context Switch
A program is a set of instructions that is aimed at performing specific tasks.A program is therefore a passive entity. A program that is executing (under action) is called a process. Thus a process is an active entity. When a program runs then it may contain various threads run accordingly. In single threaded processes, the thread itself is the pro
2 min read
Parsing ambiguous grammars using LR parser
LR parser can be used to parse ambiguous grammars. LR parser resolves the conflicts (shift/reduce or reduce/reduce) in parsing table of ambiguous grammars based on certain rules (precedence and/or associativity of operators) of the grammar. Example: Lets take the following ambiguous grammar: E -> E+E E -> E*E E -> id Lets assume, the prece
3 min read
Right and Left linear Regular Grammars
Regular grammar is a type of grammar that describes a regular language. A regular grammar is a mathematical object, G, which consists of four components, G = (N, E, P, S), where N: non-empty, finite set of non-terminal symbols,E: a finite set of terminal symbols, or alphabet, symbols,P: a set of grammar rules, each of one having one of the formsA â‡Ē
2 min read
Regular grammar (Model regular grammars )
Prerequisites: Chomsky hierarchy Type-3 grammar/regular grammar: Regular grammar generates regular language. They have a single non-terminal on the left-hand side and a right-hand side consisting of a single terminal or single terminal followed by a non-terminal. The productions must be in the form: A â‡Ē xB A â‡Ē x A â‡Ē Bx where A, B ∈ Variable(V) and
5 min read
Converting Context Free Grammar to Chomsky Normal Form
Prerequisite - Simplifying Context Free Grammars A context free grammar (CFG) is in Chomsky Normal Form (CNF) if all production rules satisfy one of the following conditions: A non-terminal generating a terminal (e.g.; X->x) A non-terminal generating two non-terminals (e.g.; X->YZ) Start symbol generating Îĩ. (e.g.; S-> Îĩ) Consider the following gra
4 min read
Regular Expression Vs Context Free Grammar
Regular Expressions are capable of describing the syntax of Tokens. Any syntactic construct that can be described by Regular Expression can also be described by the Context free grammar. Regular Expression: (a|b)(a|b|01) Context-free grammar: S --> aA|bA A --> aA|bA|0A|1A|e *e denotes epsilon. The Context-free grammar form NFA for the Regular
2 min read
Various Properties of context free languages (CFL)
Context-Free Language (CFL) is a language which is generated by a context-free grammar or Type 2 grammar(according to Chomsky classification) and gets accepted by a Pushdown Automata. Some very much important properties of a context-free language is: Regularity- context-free languages are Non-Regular PDA language. Closure properties : The context-f
2 min read
Difference between Context Free Grammar and Regular Grammar
Noam Chomsky has divided grammar into four types : Type Name 0 Unrestricted Grammar1 Context Sensitive Grammar2 Context Free Grammar3 Regular Grammar 1. Context Free Grammar : Language generated by Context Free Grammar is accepted by Pushdown AutomataIt is a subset of Type 0 and Type 1 grammar and a superset of Type 3 grammar.Also called phase stru
2 min read
Converting Context Free Grammar to Greibach Normal Form
Context-free grammar (CFG) and Greibach Normal Form (GNF) are fundamental concepts in formal language theory, particularly in the field of compiler design and automata theory. This article delves into what CFG and GNF are, provides examples, and outlines the steps to convert a CFG into GNF. What is Context Free Grammar?Context-Free Grammar (CFG) is
6 min read
Context-sensitive Grammar (CSG) and Language (CSL)
Context-Sensitive Grammar - A Context-sensitive grammar is an Unrestricted grammar in which all the productions are of form - Where Îą and Îē are strings of non-terminals and terminals. Context-sensitive grammars are more powerful than context-free grammars because there are some languages that can be described by CSG but not by context-free grammars
2 min read
Measure the time spent in context switch?
A Context switch is a time spent between two processes (i.e., bringing a waiting process into execution and sending an executing process into a waiting-for state). This happens in multitasking. The operating system must bring the state information if waiting for the process into memory and save the state information of the currently running process
7 min read
Difference between Swapping and Context Switching
Programs are sets of instructions designed to accomplish specific tasks. Similarly, a process refers to a runtime instance of a computer program. During the execution of a program, several threads may be running in parallel. Single-threaded processes refer to the thread itself as the process.1. Context switching :An operating system uses this techn
3 min read
Difference between “Dispatch Latency” and “Context Switch” in operating systems
A process in a multitasking system is brought from the secondary memory to the main memory and sometimes switched back to the secondary memory. Thus, there are several states of a process throughout its lifecycle. Context Switching is an important part of that whole lifecycle. Dispatcher is a module that gives control of the CPU to the process sele
4 min read
Context-Insensitive Interprocedural Analysis
The context-insensitive interprocedural analysis is a method for predicting the behavior of programs at run-time. It has been used in compiler design to improve performance but has not yet been widely applied to other applications such as database query optimization. Effects of a Method invocation:The effects of a method invocation are the effects
5 min read
Classification of Sorting Algorithms
Sorting is an algorithm which arranges the elements of a given list in a particular order [ascending or descending]. Sorting algorithms are categorized on the following basis - By number of comparisons :Comparison-based sorting algorithms check the elements of the list by key comparison operation and need at least O(n log n) comparisons for most in
3 min read
Classification of Routing Algorithms
Pre-requisites: Difference between Static and Dynamic Routing Routing is the process of establishing the routes that data packets must follow to reach the destination. In this process, a routing table is created which contains information regarding routes that data packets follow. Various routing algorithms are used for the purpose of deciding whic
9 min read
Classification of Top Down Parsers
Parsing is classified into two categories, i.e. Top-Down Parsing, and Bottom-Up Parsing. Top-Down Parsing is based on Left Most Derivation whereas Bottom-Up Parsing is dependent on Reverse Right Most Derivation.  The process of constructing the parse tree which starts from the root and goes down to the leaf is Top-Down Parsing.  Top-Down Parsers co
5 min read
Classification of Number System
A number is a way to represent arithmetic value, count or measure of a particular quantity. A number system can be considered as a mathematical notation of numbers using a set of digits or symbols. In simpler words the number system is a method of representing numbers. Every number system is identified with the help of its base or radix. Base or Ra
2 min read
Classification and Programming of Read-Only Memory (ROM)
Read-Only Memory or ROM is another significant non Volatile storage control in computed systems which mainly stores data permanently whether the power is off or not. While RAM is a temporary storage type used in computers and printers, ROM contains firmware and other instructions which are burnt, into the hardware during its manufacturing. This art
15+ min read
Collision-Free Protocols in Computer Network
Almost all collisions can be avoided in CSMA/CD but they can still occur during the contention period. The collision during the contention period adversely affects the system performance, this happens when the cable is long and length of packet are short. This problem becomes serious as fiber optics network came into use. Here we shall discuss some
4 min read
FREE GATE CS 2023 All India Mock Tests
The GATE ( Graduate Aptitude Test in Engineering) Exam is one of the prominent exams in India which is conducted jointly by seven IITs (Delhi, Guwahati, Kanpur, Kharagpur, Roorkee, Madras, Bombay) and Indian Institute of Science (IISc) Bangalore. This online national-level examination is conducted for Master of Engineering (ME), Masters in Technolo
7 min read
GATE Mock Test Series Free - GATE CS/IT or GATE Data Science and Artificial Intelligence
Are you a GATE aspirant with CS/IT? If yes, you must be occupied by your GATE 2025 Exam preparations. As you know, the GATE 2025 Exam is set to occur in February 2025. You must want to analyze your preparation and performance for the GATE 2025 Exam. Boost your preparation with GeeksforGeeks's FREE GATE CS 2025 - All India Mock or GATE Data Science
5 min read
DBMS Tutorial – Learn Database Management System
Database Management System is a software or technology used to manage data from a database. Some popular databases are MySQL, Oracle, MongoDB, etc. DBMS provides many operations e.g. creating a database, Storing in the database, updating an existing database, delete from the database. DBMS is a system that enables you to store, modify, and retrieve
8 min read
Operating System Tutorial
An Operating System(OS) is software that manages and handles the hardware and software resources of a computer system. It provides interaction between users of computers and computer hardware. An operating system is responsible for managing and controlling all the activities and sharing of computer resources. An operating system is a low-level Soft
7 min read
Types of Operating Systems
An Operating System performs all the basic tasks like managing files, processes, and memory. Thus, the operating system acts as the manager of all the resources, i.e. resource manager. Thus, the operating system becomes an interface between the user and the machine. It is one of the most required software that is present in the device. Operating Sy
11 min read
TCP/IP Model
The TCP/IP model is a fundamental framework for computer networking. It stands for Transmission Control Protocol/Internet Protocol, which are the core protocols of the Internet. This model defines how data is transmitted over networks, ensuring reliable communication between devices. It consists of four layers: the Link Layer, the Internet Layer, t
13 min read
What is OSI Model? - Layers of OSI Model
OSI stands for Open Systems Interconnection , where open stands to say non-proprietary. It is a 7-layer architecture with each layer having specific functionality to perform. All these 7 layers work collaboratively to transmit the data from one person to another across the globe. The OSI reference model was developed by ISO - 'International Organiz
15+ min read
Types of Network Topology
Network topology refers to the arrangement of different elements like nodes, links, or devices in a computer network. It defines how these components are connected and interact with each other. Understanding various types of network topologies helps in designing efficient and robust networks. Common types include bus, star, ring, mesh, and tree top
12 min read
ACID Properties in DBMS
A transaction is a single logical unit of work that accesses and possibly modifies the contents of a database. Transactions access data using read and write operations. In order to maintain consistency in a database, before and after the transaction, certain properties are followed. These are called ACID properties. For those looking to master thes
6 min read
Introduction of ER Model
Peter Chen developed the ER diagram in 1976. The ER model was created to provide a simple and understandable model for representing the structure and logic of databases. It has since evolved into variations such as the Enhanced ER Model and the Object Relationship Model The Entity Relational Model is a model for identifying entities to be represent
10 min read
Article Tags :