The Wayback Machine - https://web.archive.org/web/20240927190318/https://www.geeksforgeeks.org/context-sensitive-grammar-csg-and-language-csl/
Open In App

Context-sensitive Grammar (CSG) and Language (CSL)

Last Updated : 20 Nov, 2019
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 and CSL are less powerful than Unrestricted grammar. That’s why context-sensitive grammars are positioned between context-free and unrestricted grammars in the Chomsky hierarchy.

Context-sensitive grammar has 4-tuples. G = {N, ÎĢ, P, S}, Where
N = Set of non-terminal symbols
ÎĢ = Set of terminal symbols
S = Start symbol of the production
P = Finite set of productions
All rules in P are of the form Îą1 A Îą2 –> Îą1 Îē Îą2

Context-sensitive Language: The language that can be defined by context-sensitive grammar is called CSL. Properties of CSL are :

  • Union, intersection and concatenation of two context-sensitive languages is context-sensitive.
  • Complement of a context-sensitive language is context-sensitive.

Example –

Consider the following CSG.
S → abc/aAbc
Ab → bA
Ac → Bbcc
bB → Bb
aB → aa/aaA
What is the language generated by this grammar?

Solution:
S → aAbc
→ abAc
→ abBbcc
→ aBbbcc
→ aaAbbcc
→ aabAbcc
→ aabbAcc
→ aabbBbccc
→ aabBbbccc
→ aaBbbbccc
→ aaabbbccc
The language generated by this grammar is {anbncn | nâ‰Ĩ1}.

GATE CS Corner Questions

Practicing the following questions will help you test your knowledge. All questions have been asked in GATE in previous years or in GATE Mock Tests. It is highly recommended that you practice them.

  1. GATE CS 2005, Question 55
  2. GATE CS 2004, Question 87

References –
CSG – Wikipedia
CSG – csa.iisc.ernet.in


Previous Article
Next Article

Similar Reads

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
Ambiguity in Context free Grammar and Context free Languages
Before reading this article, we recommend you to first read about Pushdown Automata and Context Free Languages. Suppose we have a context free grammar G with production rules : S -> aSb | bSa | SS | e Left Most Derivation (LMD) and Derivation Tree : Leftmost derivation of a string from starting symbol S is done by replacing leftmost non-terminal
5 min read
Ambiguity in Context free Grammar and Context free Languages
Prerequisite - Pushdown Automata and Context Free Languages . Suppose we have a context free grammar G with production rules: S->aSb|bSa|SS|? Left most derivation (LMD) and Derivation Tree: Leftmost derivation of a string from starting symbol S is done by replacing leftmost non-terminal symbol by RHS of corresponding production rule. For example
4 min read
Removal of Ambiguity (Converting an Ambiguous Grammar into Unambiguous Grammar)
Ambiguous grammar, in computer languages and compilers, refers to when certain elements from source code may be parsed according to more than one possible interpretation since there exist ambiguous or conflicting grammar production rules. This may lead to uncertainty about how the code will be processed and what will be executed. Think of it this w
9 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
CYK Algorithm for Context Free Grammar
Prerequisite - Converting Context Free Grammar to Chomsky Normal Form CYK algorithm is a parsing algorithm for context free grammar. In order to apply CYK algorithm to a grammar, it must be in Chomsky Normal Form. It uses a dynamic programming algorithm to tell whether a string is in the language of a grammar. Algorithm : Let w be the n length stri
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
What is Context-Free Grammar?
Context Free Grammar is formal grammar, the syntax or structure of a formal language can be described using context-free grammar (CFG), a type of formal grammar. The grammar has four tuples: (V,T,P,S). V - It is the collection of variables or non-terminal symbols.T - It is a set of terminals. P - It is the production rules that consist of both term
3 min read
Difference between Thread Context Switch and Process Context Switch
A context switch is the process where the operating system switches the CPU's attention from one task to another, ensuring multitasking. This allows multiple processes or threads to share the CPU efficiently. In a thread context switch, the CPU switches between threads within the same process. Since threads share the same memory space. In contrast,
5 min read
Relationship between grammar and language in Theory of Computation
A grammar is a set of production rules which are used to generate strings of a language. In this article, we have discussed how to find the language generated by a grammar and vice versa as well. Language generated by a grammar - Given a grammar G, its corresponding language L(G) represents the set of all strings generated from G. Consider the foll
4 min read
Relationship between grammar and Language
Prerequisite - Regular Grammar, Regular Expressions, Chomsky hierarchy Overview :In this article, we will discuss the overview of Regular Grammar can be either Right Linear or Left Linear, and mainly focus will be on the relationship between grammar and Language. Let's discuss it one by one. Types :There are two types of grammar as follows. Right L
2 min read
Check if the language is Context Free or Not
Prerequisite - Pumping Lemma, How to identify if a language is regular or not We typically face questions to identify which of the given languages are context free. In case of regular languages, it is comparatively easy to answer this, but for Context Free languages, it is tricky sometimes. Pumping Lemma provides us with ability to perform negative
4 min read
Operator grammar and precedence parser in TOC
A grammar that is used to define mathematical operators is called an operator grammar or operator precedence grammar. Such grammars have the restriction that no production has either an empty right-hand side (null productions) or two adjacent non-terminals in its right-hand side. Examples - This is an example of operator grammar: E->E+E/E*E/id H
6 min read
Removing Direct and Indirect Left Recursion in a Grammar
Prerequisite - Classification of Context Free Grammars, Ambiguity and Parsers Introduction: Left recursion is a common problem that occurs in grammar during parsing in the syntax analysis part of compilation. It is important to remove left recursion from grammar because it can create an infinite loop, leading to errors and a significant decrease in
10 min read
Difference between Ambiguous and Unambiguous Grammar
Prerequisite - Context Free Grammars 1. Ambiguous Grammar : A context-free grammar is called ambiguous grammar if there exists more than one derivation tree or parse tree. Example - S -> S + S / S * S / S / a 2. Unambiguous Grammar : A context-free grammar is called unambiguous grammar if there exists one and only one derivation tree or parse tr
2 min read
Regular Expressions, Regular Grammar and Regular Languages
As discussed in Chomsky Hierarchy, Regular Languages are the most restricted types of languages and are accepted by finite automata. Regular Expressions Regular Expressions are used to denote regular languages. An expression is regular if: ? is a regular expression for regular language ?.? is a regular expression for regular language {?}.If a ? ? (
6 min read
Introduction To Grammar in Theory of Computation
Prerequisite - Theory of ComputationGrammar :It is a finite set of formal rules for generating syntactically correct sentences or meaningful correct sentences.Constitute Of Grammar :Grammar is basically composed of two basic elements - Terminal Symbols -Terminal symbols are those which are the components of the sentences generated using a grammar a
3 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
Ambiguous Grammar
You can also read our previously discussed article on the Classification of Context-Free Grammar. Context Free Grammars(CFGs) are classified based on: Number of Derivation treesNumber of strings Depending on the Number of Derivation trees, CFGs are sub-divided into 2 types: Ambiguous grammarsUnambiguous grammars Ambiguous grammar: A CFG is said to
4 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
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
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
Classification of Context Free Grammars
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 gramm
2 min read
Closure Properties of Context Free Languages
Context Free Languages (CFLs) are accepted by pushdown automata. Context free languages can be generated by context free grammars, which have productions (substitution rules) of the form : A -> ? (where A ? N and ? ? (T ? N)* and N is a non-terminal and T is a terminal) Properties of Context Free Languages Union : If L1 and L2 are two context fr
9 min read
Simplifying Context Free Grammars
The definition of context free grammars (CFGs) allows us to develop a wide variety of grammars. Most of the time, some of the productions of CFGs are not useful and are redundant. This happens because the definition of CFGs does not restrict us from making these redundant productions. By simplifying CFGs we remove all these redundant productions fr
6 min read
Star Height of Regular Expression and Regular Language
The star height relates to the field of theoretical of computation (TOC). It is used to indicate the structural complexity of regular expressions and regular languages. Here complexity, relates to the maximum nesting depth of Kleene stars present in a regular expression. It may be noted here that a regular language may be represented by regular exp
6 min read
NPDA for the language L ={w∈ {a,b}*| w contains equal no. of a's and b's}
Prerequisite - Pushdown automata, Pushdown automata acceptance by final state Problem - Design a non deterministic PDA for accepting the language L ={w?{a,b}* | w contains equal no. of a's and b's}, i.e., L = {ab, aabb, abba, aababb, bbabaa, baaababb, .......} The number of a's and b's are same in all the strings. Explanation - Here, we need not to
3 min read
Hypothesis (language regularity) and algorithm (L-graph to NFA) in TOC
Prerequisite - Finite automata, L-graphs and what they represent L-graphs can generate context sensitive languages, but it’s much harder to program a context sensitive language over programming a regular one. This is why I’ve came up with a hypothesis about what kind of L-graphs can generate a regular language. But first, I need to introduce you to
7 min read
Practice Tags :