Introduction To Grammar in Theory of Computation
Last Updated :
28 Dec, 2024
In the context of the Theory of Computation, grammar refers to a formal system that defines how strings in a language are structured. It plays a crucial role in determining the syntactic correctness of languages and serves as a foundation for parsing and interpreting programming languages, natural languages, and various formal systems. This article provides an in-depth look at the types of grammars, their components, and the derivation of strings from grammars.
What is Grammar in Computation?
Grammar is a formal system that defines a set of rules for generating valid strings within a language. It serves as a blueprint for constructing syntactically correct sentences or meaningful sequences in a formal language.
Grammar is basically composed of two basic elements:
- Terminal Symbols: Terminal symbols are those that are the components of the sentences generated using grammar and are represented using small case letters like a, b, c, etc.
- Non-Terminal Symbols: Non-terminal symbols are those symbols that take part in the generation of the sentence but are not the component of the sentence. Non-Terminal Symbols are also called Auxiliary Symbols and Variables. These symbols are represented using a capital letters like A, B, C, etc.
Representation of Grammar
Any Grammar can be represented by 4 tuples – <N, T, P, S>
- N – Finite Non-Empty Set of Non-Terminal Symbols.
- T – Finite Set of Terminal Symbols.
- P – Finite Non-Empty Set of Production Rules.
- S – Start Symbol (Symbol from where we start producing our sentences or strings).
Production Rules
A production or production rule in computer science is a rewrite rule specifying a symbol substitution that can be recursively performed to generate new symbol sequences. It is of the form α-> β where α is a Non-Terminal Symbol which can be replaced by β which is a string of Terminal Symbols or Non-Terminal Symbols.
Example-1: Consider Grammar G1 = <N, T, P, S>
T = {a,b} #Set of terminal symbols
P = {A->Aa,A->Ab,A->a,A->b,A-> [Tex]\epsilon[/Tex]} #Set of all production rules
S = {A} #Start Symbol
As the start symbol is S then we can produce Aa, Ab, a,b,[Tex]\epsilon [/Tex]which can further produce strings where A can be replaced by the Strings mentioned in the production rules and hence this grammar can be used to produce strings of the form (a+b)*.
Derivation of Strings
A->a #using production rule 3
OR
A->Aa #using production rule 1
Aa->ba #using production rule 4
OR
A->Aa #using production rule 1
Aa->AAa #using production rule 1
AAa->bAa #using production rule 4
bAa->ba #using production rule 5
Example-2: Consider Grammar G2 = <N, T, P, S>
N = {A} #Set of non-terminals Symbols
T = {a} #Set of terminal symbols
P = {A->Aa, A->AAa, A->a, A->[Tex]\epsilon[/Tex]} #Set of all production rules
S = {A} #Start Symbol
As the start symbol is S then we can produce Aa, AAa, a,[Tex]\epsilon [/Tex]which can further produce strings where A can be replaced by the Strings mentioned in the production rules and hence this grammar can be used to produce strings of form (a)*.
Derivation of Strings
A->a #using production rule 3
OR
A->Aa #using production rule 1
Aa->aa #using production rule 3
OR
A->Aa #using production rule 1
Aa->AAa #using production rule 1
AAa->Aa #using production rule 4
Aa->aa #using production rule 3
Equivalent Grammars
Two grammars are said to be equivalent if they generate the same language. For instance, if Grammar 1 and Grammar 2 both generate strings of the form (𝑎+𝑏)∗, they are considered equivalent.
Types of Grammars
There are several types of Grammar. We classify them on the basis mentioned below.
- Type of Production Rules: The form and complexity of the production rules define the grammar type, such as context-free, context-sensitive, regular, or unrestricted grammars.
- Number of Derivation Trees: The number of ways a string can be derived from the grammar. Ambiguous grammars have multiple derivation trees for the same string.
- Number of Strings: The size and nature of the language generated by the grammar.
Conclusion
Grammar is the foundation of understanding languages in computation. It provides a structured way of generating strings that belongs to a specific language. From programming languages to natural language processing, formal grammars are critical in ensuring that the sentences (or strings) formed are syntactically correct.
Introduction To Grammar in Theory of Computation – FAQs
What is the role of grammar in compiler design?
Grammar helps define the syntax of programming languages, making it crucial for the parsing stage in compiler design, where the source code is analyzed for syntactical correctness.
What is the difference between terminal and non-terminal symbols?
Terminal symbols form the actual strings in the language, while non-terminal symbols are placeholders used during the derivation process but do not appear in the final string.
What is an ambiguous grammar?
An ambiguous grammar allows for more than one parse tree for the same string, meaning the string can be derived in multiple ways.
What are the types of grammars in the Chomsky hierarchy?
The Chomsky hierarchy classifies grammars into four types: regular grammars, context-free grammars, context-sensitive grammars, and unrestricted grammars.
Similar Reads
Introduction To Grammar in Theory of Computation
In the context of the Theory of Computation, grammar refers to a formal system that defines how strings in a language are structured. It plays a crucial role in determining the syntactic correctness of languages and serves as a foundation for parsing and interpreting programming languages, natural l
4 min read
Introduction To Grammar in Theory of Computation
In the context of the Theory of Computation, grammar refers to a formal system that defines how strings in a language are structured. It plays a crucial role in determining the syntactic correctness of languages and serves as a foundation for parsing and interpreting programming languages, natural l
4 min read
Introduction of Theory of Computation
Automata theory (also referred to as the Theory Of Computation) is a branch of Computer Science and Mathematics that studies how machines compute functions and solve problems. This field is mainly focused on mathematical structures called automata and is crucial for the purpose of studying processes
4 min read
Introduction of Theory of Computation
Automata theory (also referred to as the Theory Of Computation) is a branch of Computer Science and Mathematics that studies how machines compute functions and solve problems. This field is mainly focused on mathematical structures called automata and is crucial for the purpose of studying processes
4 min read
Introduction to Computation Complex Theory
Broad Overview : Complexity theory, in a nutshell, a complexity word is a quite fancy word, literally, it sounds complex, but it is not an intimidating topic. What it really means is analyzing the program or we can say analyzing the efficiency of the program, figuring out whether the program is corr
4 min read
Last Minute Notes - Theory of Computation
See Last Minute Notes on all subjects here. We will discuss the important key points useful for GATE exams in summarized form. For details you may refer this. Finite Automata: It is used to recognize patterns of specific type input. It is the most restricted type of automata which can accept only re
5 min read
Theory of Computation (TOC) for GATE
Theory of Computation (TOC) is a key subject in the GATE CSE exam. Here's a complete tutorial on the Theory of Computation for the GATE CSE exam. Whether you're revising or starting fresh, this tutorial will help you prepare effectively. If you have less time to study topic-wise in detail, you may r
4 min read
Theory of Computation (TOC) for GATE
Theory of Computation (TOC) is a key subject in the GATE CSE exam. Here's a complete tutorial on the Theory of Computation for the GATE CSE exam. Whether you're revising or starting fresh, this tutorial will help you prepare effectively. If you have less time to study topic-wise in detail, you may r
4 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
4 min read
Introduction of Compiler Design
Compiler design is design of a software that translates the source code written in a high-level programming language (Like C, C++, Java or Python) into the low level language like Machine code, Assembly code or Byte code. It comprises a number of phases where the code is pre-processed, and parsed, i
12 min read
Introduction of Compiler Design
Compiler design is design of a software that translates the source code written in a high-level programming language (Like C, C++, Java or Python) into the low level language like Machine code, Assembly code or Byte code. It comprises a number of phases where the code is pre-processed, and parsed, i
12 min read
Chomsky Hierarchy in Theory of Computation
According to Chomsky hierarchy, grammar is divided into 4 types as follows: Type 0 is known as unrestricted grammar.Type 1 is known as context-sensitive grammar.Type 2 is known as a context-free grammar.Type 3 Regular Grammar.Type 0: Unrestricted Grammar: Type-0 grammars include all formal grammar.
2 min read
Decidability Table in Theory of Computation
Prerequisite - Undecidability, Decidable and undecidable problems Identifying languages (or problems*) as decidable, undecidable or partially decidable is a very common question in GATE. With correct knowledge and ample experience, this question becomes very easy to solve. A language is undecidable
2 min read
Theory of Computation - GATE CSE Previous Year Questions
In this article, we are mainly focusing on the Theory of Computation GATE Questions that have been asked in Previous Years, with their solutions. And, where an explanation is required, we have also provided the reason. Topic-Wise Quizzes to Practice Previous Year's QuestionsFinite Automata and Regul
2 min read
Theory of Computation - GATE CSE Previous Year Questions
In this article, we are mainly focusing on the Theory of Computation GATE Questions that have been asked in Previous Years, with their solutions. And, where an explanation is required, we have also provided the reason. Topic-Wise Quizzes to Practice Previous Year's QuestionsFinite Automata and Regul
2 min read
Introduction of Finite Automata
Finite automata are abstract machines used to recognize patterns in input sequences, forming the basis for understanding regular languages in computer science. They consist of states, transitions, and input symbols, processing each symbol step-by-step. If the machine ends in an accepting state after
4 min read
Introduction of Pushdown Automata
We have already discussed finite automata. But finite automata can be used to accept only regular languages. Pushdown Automata is a finite automata with extra memory called stack which helps Pushdown automata to recognize Context Free Languages. This article describes pushdown automata in detail. Pu
5 min read
Introduction to Queue Automata
Queue Automata (QA) is an extended computational model of Finite Automata which is capable of performing several tasks more complex than those of Finite Automata due to its incorporation of a queue data type. This queue helps a Queue Automata (QA) to remember more about the inputs hence making it ea
4 min read
Introduction to Syntax Analysis in Compiler Design
Syntax analysis, often known as parsing, is an important step in the compilation process. Following lexical analysis (which divides the input into tokens), syntax analysis ensures that these tokens are arranged according to with the programming language's grammar. This process helps in detecting and
8 min read