SlideShare a Scribd company logo
Akhil Kaushik
Asstt. Prof., CE Deptt.,
TIT Bhiwani
Context Free Grammar
Introduction
• Syntax:- the form or structure of the expressions,
statements, and program units.
• Semantics:- the meaning of the expressions,
statements, and program units.
• Syntax and semantics provide a language’s
definition.
Introduction
• A language is a set of sentences.
• A sentence is a string of characters over some
alphabets. Ex: a = b + c; or z = (a + b) * c;
• Syntax: <assign> →<id> = <expr> ;
• <id> →a | b | c
• <expr> →<id> + <expr> | <id> * <expr> | <id>
• Semantics : a = b + c;
Introduction
• Recognizers – A recognition device reads input
strings of the language and decides whether the
input strings belong to the language.
• Ex: Syntax analysis part of a compiler.
• Generators – A device that generates sentences of
a language.
• One can determine if the syntax of a particular
sentence is correct by comparing it to the structure
of the generator.
Introduction
The Chomsky Hierarchy
• Noam Chomsky, - American linguist, philosopher,
scientist and social activist gave Chomsky hierarchy
of grammars in 1956.
• Grammar Definition: It is defined by four tuples:
G = {V,T,P,S} where
– V = Non-terminals
– T = Terminals
– P = Production Rule
– S = Start Symbol
The Chomsky Hierarchy
The Chomsky Hierarchy
• Unrestricted grammar generates Recursively
enumerable languages (Type-0 Languages)
include all formal grammars.
• They generate exactly all languages that can be
recognized by a Turing machine.
• This grammar has rules of the form α → β (where α
contains non-terminals and β contains terminals or
non terminals).
The Chomsky Hierarchy
Examples:-
• AB → A
• AB → aB
• S → ε
• a → AB
• ε → a
The Chomsky Hierarchy
• Type-1 grammar generate the Context-sensitive
languages.
• These languages can be recognized by a linear
bounded automaton.
• These grammars have rules of the form α → β with
a restriction that length of | α | ≤ | β | .
The Chomsky Hierarchy
• Examples:-
• aAb → bbb
• aA → bbb
• aAb → bb
The Chomsky Hierarchy
• Type-2 grammar generate the Context-Free
languages.
• These languages are exactly all languages that can
be recognized by a non-deterministic pushdown
automaton.
• Context-free languages are the theoretical basis for
the syntax of most programming languages.
The Chomsky Hierarchy
• These are defined by rules of the form A → α where
A is a nonterminal and α is a string of terminals and
non-terminal (there will be no context on the left and
right of non-terminal ).
• Examples: A → BCD
• A →aBC
• a →AbC
The Chomsky Hierarchy
• Type-3 Languages generate the Regular languages.
• These languages are exactly all languages that can
be decided by a finite state automaton.
• Regular languages are commonly used to define
search patterns of programming languages.
The Chomsky Hierarchy
• It can be classified into two types:-
(1)Right Linear (2)Left Linear.
• If we have repetition of non terminals on right side
[ A → xB|x] then it is known as Right Linear.
• If we have repetition of non terminals on left side
[ A → Bx|x] then it is known as Left Linear.
(A,B є non terminals and x є Σ*)
The Chomsky Hierarchy
Examples:-
• S → aS|b or S → aS|c
• S → Sa|b or A → ba
• S → aS|b or S → Sa|c
The Chomsky Hierarchy
Context- Free Grammars
(CFG)
Introduction
• A context-free grammar is a notation for describing
languages.
• It is more powerful than finite automata or RE’s, but
still cannot define all possible languages.
• Useful for nested structures. Ex. parentheses in
programming languages.
Introduction
• Basic idea is to use “variables” to stand for sets of
strings (i.e., languages).
• These variables are defined recursively, in terms of
one another.
• Recursive rules (“productions”) involve only
concatenation.
• Alternative rules for a variable allow union.
Introduction
• CFGs are also known as:-
– Phrase Structure Grammars
– Backus-Naur Form (BNF)
• It consists of:-
– Rules (equations or productions)
– Terminals (tokens)
– Non-terminals (Language constituents like noun phrase,
verb phrase, sentence, etc.)
Formal Definition
• G (CFG) is defined by G = (V, Σ, P, S)
– V = variables a finite set (non-terminals)
– Σ = alphabet or terminals a finite set
– P = productions a finite set
– S = start variable S ϵ V
• Productions’ form, where A ϵ V, α ϵ (V υ S)*:
• A → a
Example
• Here is a formal CFG for { 0n1n | n >= 1}.
• Terminals = {0, 1}, Variables = {S}, Start symbol = S.
• Productions = S -> 01, S -> 0S1
• We derive strings in the language of a CFG by
starting with the start symbol, and repeatedly
replacing some variable A by the right side of one of
its productions.
RE to CFG
Derivation
• A derivation is a sequence of rules applied to a
string that accounts for that string.
– Covers all the elements in the string.
– Covers only the elements in the string.
• Derivations allow us to replace any of the variables
in a string.
• Leads to many different derivations of the same
string.
Derivation Tree
A parse tree of a derivation is a tree
in which:-
• Each internal node is labeled with a
non-terminal.
• If a rule A →A1A2…An occurs in the
derivation then is a parent node of
nodes labeled A1, A2, …, An
Derivation Tree
Leftmost vs Rightmost Derivation
A left-most derivation
of a sentential form is
one in which rules
transforming the left-
most non-terminal are
always applied.
A right-most derivation of
a sentential form is one in
which rules transforming
the right-most non-terminal
are always applied.
Leftmost vs Rightmost Derivation
Ambiguous Grammar
• A grammar G is ambiguous if there is a word
w ϵ L(G) having are least two different parse trees.
Ambiguous Grammar
Ambiguous Grammar
• Given an ambiguous grammar, would like an
equivalent unambiguous grammar.
– Allows you to know more about structure of a given
derivation.
– Can lead to more efficient parsing algorithms.
– In programming languages, want to impose a canonical
structure on derivations. E.g., for 1+2´3.
• Strategy: Force an ordering on all derivations.
Ambiguous Grammar
Ambiguous Grammar
Ambiguous Grammar
Ambiguous Grammar
Ambiguous Grammar
Ambiguous Grammar
Ambiguous Grammar
Ambiguous Grammar
Ambiguous Grammar
Ambiguous Grammar
Ambiguous Grammar
Ambiguous Grammar
Disambiguation
Disambiguation
Disambiguation
Disambiguation
By forcing the leftmost variable (or alternatively, the
rightmost variable) to be replaced, we avoid these
“distinctions without a difference.”
Akhil Kaushik
akhilkaushik05@gmail.com
9416910303
CONTACT ME AT:
Akhil Kaushik
akhilkaushik05@gmail.com
9416910303
THANK YOU !!!

More Related Content

What's hot

Context free languages
Context free languagesContext free languages
Context free languages
Jahurul Islam
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
Rabia Khalid
 
Chapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata TheoryChapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata Theory
Tsegazeab Asgedom
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free Grammars
Marina Santini
 
Chomsky Normal Form
Chomsky Normal FormChomsky Normal Form
Chomsky Normal Form
Dhrumil Panchal
 
Chomsky Normal Form
Chomsky Normal FormChomsky Normal Form
Chomsky Normal Form
Jasmine Peniel
 
AUTOMATA THEORY - SHORT NOTES
AUTOMATA THEORY - SHORT NOTESAUTOMATA THEORY - SHORT NOTES
AUTOMATA THEORY - SHORT NOTES
suthi
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
Mohammad Ilyas Malik
 
context free language
context free languagecontext free language
context free language
khush_boo31
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Shiraz316
 
Nondeterministic Finite Automata
Nondeterministic Finite AutomataNondeterministic Finite Automata
Nondeterministic Finite AutomataAdel Al-Ofairi
 
Ambiguous & Unambiguous Grammar
Ambiguous & Unambiguous GrammarAmbiguous & Unambiguous Grammar
Ambiguous & Unambiguous Grammar
MdImamHasan1
 
NLP_KASHK:Finite-State Morphological Parsing
NLP_KASHK:Finite-State Morphological ParsingNLP_KASHK:Finite-State Morphological Parsing
NLP_KASHK:Finite-State Morphological Parsing
Hemantha Kulathilake
 
Context free langauges
Context free langaugesContext free langauges
Context free langauges
sudhir sharma
 
Theory of computing
Theory of computingTheory of computing
Theory of computingRanjan Kumar
 
Pumping lemma Theory Of Automata
Pumping lemma Theory Of AutomataPumping lemma Theory Of Automata
Pumping lemma Theory Of Automata
hafizhamza0322
 
Finite automata(For college Seminars)
Finite automata(For college Seminars)Finite automata(For college Seminars)
Finite automata(For college Seminars)
Naman Joshi
 
Ch3 4 regular expression and grammar
Ch3 4 regular expression and grammarCh3 4 regular expression and grammar
Ch3 4 regular expression and grammar
meresie tesfay
 
Automata theory
Automata theoryAutomata theory
Automata theory
Pardeep Vats
 

What's hot (20)

Context free languages
Context free languagesContext free languages
Context free languages
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
Chapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata TheoryChapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata Theory
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free Grammars
 
Chomsky Normal Form
Chomsky Normal FormChomsky Normal Form
Chomsky Normal Form
 
Chomsky Normal Form
Chomsky Normal FormChomsky Normal Form
Chomsky Normal Form
 
AUTOMATA THEORY - SHORT NOTES
AUTOMATA THEORY - SHORT NOTESAUTOMATA THEORY - SHORT NOTES
AUTOMATA THEORY - SHORT NOTES
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 
context free language
context free languagecontext free language
context free language
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Nondeterministic Finite Automata
Nondeterministic Finite AutomataNondeterministic Finite Automata
Nondeterministic Finite Automata
 
Ambiguous & Unambiguous Grammar
Ambiguous & Unambiguous GrammarAmbiguous & Unambiguous Grammar
Ambiguous & Unambiguous Grammar
 
NLP_KASHK:Finite-State Morphological Parsing
NLP_KASHK:Finite-State Morphological ParsingNLP_KASHK:Finite-State Morphological Parsing
NLP_KASHK:Finite-State Morphological Parsing
 
Context free langauges
Context free langaugesContext free langauges
Context free langauges
 
Finite automata
Finite automataFinite automata
Finite automata
 
Theory of computing
Theory of computingTheory of computing
Theory of computing
 
Pumping lemma Theory Of Automata
Pumping lemma Theory Of AutomataPumping lemma Theory Of Automata
Pumping lemma Theory Of Automata
 
Finite automata(For college Seminars)
Finite automata(For college Seminars)Finite automata(For college Seminars)
Finite automata(For college Seminars)
 
Ch3 4 regular expression and grammar
Ch3 4 regular expression and grammarCh3 4 regular expression and grammar
Ch3 4 regular expression and grammar
 
Automata theory
Automata theoryAutomata theory
Automata theory
 

Similar to Context Free Grammar

Syntax Analyzer.pdf
Syntax Analyzer.pdfSyntax Analyzer.pdf
Syntax Analyzer.pdf
kenilpatel65
 
NLP_KASHK:Context-Free Grammar for English
NLP_KASHK:Context-Free Grammar for EnglishNLP_KASHK:Context-Free Grammar for English
NLP_KASHK:Context-Free Grammar for English
Hemantha Kulathilake
 
UNIT 1 part II.ppt
UNIT 1 part II.pptUNIT 1 part II.ppt
UNIT 1 part II.ppt
Ranjeet Reddy
 
8074448.ppt
8074448.ppt8074448.ppt
contextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdfcontextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdf
ry54321288
 
9781284077247_PPTx_CH01.pptx
9781284077247_PPTx_CH01.pptx9781284077247_PPTx_CH01.pptx
9781284077247_PPTx_CH01.pptx
mainakmail2585
 
Natural Language Processing
Natural Language ProcessingNatural Language Processing
Natural Language Processing
Varunjeet Singh Rekhi
 
Flat unit 3
Flat unit 3Flat unit 3
Flat unit 3
VenkataRaoS1
 
Unit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmm
Unit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmmUnit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmm
Unit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmm
DhruvKushwaha12
 
Control structure
Control structureControl structure
Control structure
baran19901990
 
Chomsky hierarchy
Chomsky hierarchyChomsky hierarchy
Chomsky hierarchy
SANUC2
 
sabesta3.ppt
sabesta3.pptsabesta3.ppt
sabesta3.ppt
NaveedAfzal34
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
Akila Krishnamoorthy
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
Kuppusamy P
 
Types of Language in Theory of Computation
Types of Language in Theory of ComputationTypes of Language in Theory of Computation
Types of Language in Theory of Computation
Ankur Singh
 
3 describing syntax
3 describing syntax3 describing syntax
3 describing syntax
Munawar Ahmed
 
LECTURE 1 ALPHABET,STRINGS, LANGUAGE CHOMSKY TYPES OF GRAMMAR.pptx
LECTURE 1 ALPHABET,STRINGS, LANGUAGE CHOMSKY TYPES OF GRAMMAR.pptxLECTURE 1 ALPHABET,STRINGS, LANGUAGE CHOMSKY TYPES OF GRAMMAR.pptx
LECTURE 1 ALPHABET,STRINGS, LANGUAGE CHOMSKY TYPES OF GRAMMAR.pptx
AbhishekKumarPandit5
 
Syntax
SyntaxSyntax
Normal-forms-for-Context-Free-Grammars.ppt
Normal-forms-for-Context-Free-Grammars.pptNormal-forms-for-Context-Free-Grammars.ppt
Normal-forms-for-Context-Free-Grammars.ppt
Karthik Rohan
 

Similar to Context Free Grammar (20)

Syntax Analyzer.pdf
Syntax Analyzer.pdfSyntax Analyzer.pdf
Syntax Analyzer.pdf
 
NLP_KASHK:Context-Free Grammar for English
NLP_KASHK:Context-Free Grammar for EnglishNLP_KASHK:Context-Free Grammar for English
NLP_KASHK:Context-Free Grammar for English
 
UNIT 1 part II.ppt
UNIT 1 part II.pptUNIT 1 part II.ppt
UNIT 1 part II.ppt
 
intro.ppt
intro.pptintro.ppt
intro.ppt
 
8074448.ppt
8074448.ppt8074448.ppt
8074448.ppt
 
contextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdfcontextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdf
 
9781284077247_PPTx_CH01.pptx
9781284077247_PPTx_CH01.pptx9781284077247_PPTx_CH01.pptx
9781284077247_PPTx_CH01.pptx
 
Natural Language Processing
Natural Language ProcessingNatural Language Processing
Natural Language Processing
 
Flat unit 3
Flat unit 3Flat unit 3
Flat unit 3
 
Unit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmm
Unit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmmUnit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmm
Unit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmm
 
Control structure
Control structureControl structure
Control structure
 
Chomsky hierarchy
Chomsky hierarchyChomsky hierarchy
Chomsky hierarchy
 
sabesta3.ppt
sabesta3.pptsabesta3.ppt
sabesta3.ppt
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
Types of Language in Theory of Computation
Types of Language in Theory of ComputationTypes of Language in Theory of Computation
Types of Language in Theory of Computation
 
3 describing syntax
3 describing syntax3 describing syntax
3 describing syntax
 
LECTURE 1 ALPHABET,STRINGS, LANGUAGE CHOMSKY TYPES OF GRAMMAR.pptx
LECTURE 1 ALPHABET,STRINGS, LANGUAGE CHOMSKY TYPES OF GRAMMAR.pptxLECTURE 1 ALPHABET,STRINGS, LANGUAGE CHOMSKY TYPES OF GRAMMAR.pptx
LECTURE 1 ALPHABET,STRINGS, LANGUAGE CHOMSKY TYPES OF GRAMMAR.pptx
 
Syntax
SyntaxSyntax
Syntax
 
Normal-forms-for-Context-Free-Grammars.ppt
Normal-forms-for-Context-Free-Grammars.pptNormal-forms-for-Context-Free-Grammars.ppt
Normal-forms-for-Context-Free-Grammars.ppt
 

More from Akhil Kaushik

Symbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationSymbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code Generation
Akhil Kaushik
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
Akhil Kaushik
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
Akhil Kaushik
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & Recovery
Akhil Kaushik
 
Symbol Table
Symbol TableSymbol Table
Symbol Table
Akhil Kaushik
 
Lexical Analyzer Implementation
Lexical Analyzer ImplementationLexical Analyzer Implementation
Lexical Analyzer Implementation
Akhil Kaushik
 
Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - Compiler Design
Akhil Kaushik
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
Akhil Kaushik
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
Akhil Kaushik
 
Algorithms & Complexity Calculation
Algorithms & Complexity CalculationAlgorithms & Complexity Calculation
Algorithms & Complexity Calculation
Akhil Kaushik
 
Intro to Data Structure & Algorithms
Intro to Data Structure & AlgorithmsIntro to Data Structure & Algorithms
Intro to Data Structure & Algorithms
Akhil Kaushik
 
Decision Making & Loops
Decision Making & LoopsDecision Making & Loops
Decision Making & Loops
Akhil Kaushik
 
Basic programs in Python
Basic programs in PythonBasic programs in Python
Basic programs in Python
Akhil Kaushik
 
Python Data-Types
Python Data-TypesPython Data-Types
Python Data-Types
Akhil Kaushik
 
Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python Programming
Akhil Kaushik
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
Akhil Kaushik
 
Bootstrapping in Compiler
Bootstrapping in CompilerBootstrapping in Compiler
Bootstrapping in Compiler
Akhil Kaushik
 
Compiler construction tools
Compiler construction toolsCompiler construction tools
Compiler construction tools
Akhil Kaushik
 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
Akhil Kaushik
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to Compilers
Akhil Kaushik
 

More from Akhil Kaushik (20)

Symbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationSymbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code Generation
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & Recovery
 
Symbol Table
Symbol TableSymbol Table
Symbol Table
 
Lexical Analyzer Implementation
Lexical Analyzer ImplementationLexical Analyzer Implementation
Lexical Analyzer Implementation
 
Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - Compiler Design
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Algorithms & Complexity Calculation
Algorithms & Complexity CalculationAlgorithms & Complexity Calculation
Algorithms & Complexity Calculation
 
Intro to Data Structure & Algorithms
Intro to Data Structure & AlgorithmsIntro to Data Structure & Algorithms
Intro to Data Structure & Algorithms
 
Decision Making & Loops
Decision Making & LoopsDecision Making & Loops
Decision Making & Loops
 
Basic programs in Python
Basic programs in PythonBasic programs in Python
Basic programs in Python
 
Python Data-Types
Python Data-TypesPython Data-Types
Python Data-Types
 
Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python Programming
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
 
Bootstrapping in Compiler
Bootstrapping in CompilerBootstrapping in Compiler
Bootstrapping in Compiler
 
Compiler construction tools
Compiler construction toolsCompiler construction tools
Compiler construction tools
 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
 
Introduction to Compilers
Introduction to CompilersIntroduction to Compilers
Introduction to Compilers
 

Recently uploaded

678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 

Recently uploaded (20)

678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 

Context Free Grammar

  • 1. Akhil Kaushik Asstt. Prof., CE Deptt., TIT Bhiwani Context Free Grammar
  • 2. Introduction • Syntax:- the form or structure of the expressions, statements, and program units. • Semantics:- the meaning of the expressions, statements, and program units. • Syntax and semantics provide a language’s definition.
  • 3. Introduction • A language is a set of sentences. • A sentence is a string of characters over some alphabets. Ex: a = b + c; or z = (a + b) * c; • Syntax: <assign> →<id> = <expr> ; • <id> →a | b | c • <expr> →<id> + <expr> | <id> * <expr> | <id> • Semantics : a = b + c;
  • 4. Introduction • Recognizers – A recognition device reads input strings of the language and decides whether the input strings belong to the language. • Ex: Syntax analysis part of a compiler. • Generators – A device that generates sentences of a language. • One can determine if the syntax of a particular sentence is correct by comparing it to the structure of the generator.
  • 6. The Chomsky Hierarchy • Noam Chomsky, - American linguist, philosopher, scientist and social activist gave Chomsky hierarchy of grammars in 1956. • Grammar Definition: It is defined by four tuples: G = {V,T,P,S} where – V = Non-terminals – T = Terminals – P = Production Rule – S = Start Symbol
  • 8. The Chomsky Hierarchy • Unrestricted grammar generates Recursively enumerable languages (Type-0 Languages) include all formal grammars. • They generate exactly all languages that can be recognized by a Turing machine. • This grammar has rules of the form α → β (where α contains non-terminals and β contains terminals or non terminals).
  • 9. The Chomsky Hierarchy Examples:- • AB → A • AB → aB • S → ε • a → AB • ε → a
  • 10. The Chomsky Hierarchy • Type-1 grammar generate the Context-sensitive languages. • These languages can be recognized by a linear bounded automaton. • These grammars have rules of the form α → β with a restriction that length of | α | ≤ | β | .
  • 11. The Chomsky Hierarchy • Examples:- • aAb → bbb • aA → bbb • aAb → bb
  • 12. The Chomsky Hierarchy • Type-2 grammar generate the Context-Free languages. • These languages are exactly all languages that can be recognized by a non-deterministic pushdown automaton. • Context-free languages are the theoretical basis for the syntax of most programming languages.
  • 13. The Chomsky Hierarchy • These are defined by rules of the form A → α where A is a nonterminal and α is a string of terminals and non-terminal (there will be no context on the left and right of non-terminal ). • Examples: A → BCD • A →aBC • a →AbC
  • 14. The Chomsky Hierarchy • Type-3 Languages generate the Regular languages. • These languages are exactly all languages that can be decided by a finite state automaton. • Regular languages are commonly used to define search patterns of programming languages.
  • 15. The Chomsky Hierarchy • It can be classified into two types:- (1)Right Linear (2)Left Linear. • If we have repetition of non terminals on right side [ A → xB|x] then it is known as Right Linear. • If we have repetition of non terminals on left side [ A → Bx|x] then it is known as Left Linear. (A,B є non terminals and x є Σ*)
  • 16. The Chomsky Hierarchy Examples:- • S → aS|b or S → aS|c • S → Sa|b or A → ba • S → aS|b or S → Sa|c
  • 19. Introduction • A context-free grammar is a notation for describing languages. • It is more powerful than finite automata or RE’s, but still cannot define all possible languages. • Useful for nested structures. Ex. parentheses in programming languages.
  • 20. Introduction • Basic idea is to use “variables” to stand for sets of strings (i.e., languages). • These variables are defined recursively, in terms of one another. • Recursive rules (“productions”) involve only concatenation. • Alternative rules for a variable allow union.
  • 21. Introduction • CFGs are also known as:- – Phrase Structure Grammars – Backus-Naur Form (BNF) • It consists of:- – Rules (equations or productions) – Terminals (tokens) – Non-terminals (Language constituents like noun phrase, verb phrase, sentence, etc.)
  • 22. Formal Definition • G (CFG) is defined by G = (V, Σ, P, S) – V = variables a finite set (non-terminals) – Σ = alphabet or terminals a finite set – P = productions a finite set – S = start variable S ϵ V • Productions’ form, where A ϵ V, α ϵ (V υ S)*: • A → a
  • 23. Example • Here is a formal CFG for { 0n1n | n >= 1}. • Terminals = {0, 1}, Variables = {S}, Start symbol = S. • Productions = S -> 01, S -> 0S1 • We derive strings in the language of a CFG by starting with the start symbol, and repeatedly replacing some variable A by the right side of one of its productions.
  • 25. Derivation • A derivation is a sequence of rules applied to a string that accounts for that string. – Covers all the elements in the string. – Covers only the elements in the string. • Derivations allow us to replace any of the variables in a string. • Leads to many different derivations of the same string.
  • 26. Derivation Tree A parse tree of a derivation is a tree in which:- • Each internal node is labeled with a non-terminal. • If a rule A →A1A2…An occurs in the derivation then is a parent node of nodes labeled A1, A2, …, An
  • 28. Leftmost vs Rightmost Derivation A left-most derivation of a sentential form is one in which rules transforming the left- most non-terminal are always applied. A right-most derivation of a sentential form is one in which rules transforming the right-most non-terminal are always applied.
  • 29. Leftmost vs Rightmost Derivation
  • 30. Ambiguous Grammar • A grammar G is ambiguous if there is a word w ϵ L(G) having are least two different parse trees.
  • 32. Ambiguous Grammar • Given an ambiguous grammar, would like an equivalent unambiguous grammar. – Allows you to know more about structure of a given derivation. – Can lead to more efficient parsing algorithms. – In programming languages, want to impose a canonical structure on derivations. E.g., for 1+2´3. • Strategy: Force an ordering on all derivations.
  • 48. Disambiguation By forcing the leftmost variable (or alternatively, the rightmost variable) to be replaced, we avoid these “distinctions without a difference.”
  • 49. Akhil Kaushik akhilkaushik05@gmail.com 9416910303 CONTACT ME AT: Akhil Kaushik akhilkaushik05@gmail.com 9416910303 THANK YOU !!!