SlideShare a Scribd company logo
1 of 23
WELCOME TO A
JOURNEY TO
CS419

Dr. Hussien Sharaf
Computer Science Department

dr.sharaf@from-masr.com
Dr. Hussien M. Sharaf

PART ONE

2
PARSING




A parser gets a stream of
tokens from the scanner, and
determines if the syntax
(structure) of the program is
correct according to the
(context-free) grammar of the
source language.
Then, it produces a data
structure, called a parse tree or
an abstract syntax tree, which
describes the syntactic
structure of the program.

Dr. Hussien M. Sharaf

Stream of tokens

parser
Parse/syntax tree

3
CFG







A context-free grammar is a notation for
defining context free languages.
It is more powerful than finite automata or
RE’s, but still cannot define all possible
languages.
Useful for nested structures, e.g., parentheses
in programming languages.
Basic idea is to use “variables” to stand for
sets of strings.
These variables are defined recursively, in
terms of one another.

Dr. Hussien M. Sharaf

4
CFG FORMAL DEFINITION
C =(V, Σ, R, S)
 V: is a finite set of variables.
 Σ: symbols called terminals of the
alphabet of the language being defined.
 S V: a special start symbol.
 R: is a finite set of production rules of the
form A→ where A
V,  (V Σ)


Dr. Hussien M. Sharaf

5
CFG -1






Define the language { anbn | n > 1}.
Terminals = {a, b}.
Variables = {S}.
Start symbol = S.
Productions =




S → ab
S → aSb
Summary

S

→ ab
 S → aSb
Dr. Hussien M. Sharaf

6
DERIVATION





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.
Derivation example for “aabb”
Using S→ aSb
generates uncompleted string that still has a nonterminal S.



Then using S→ ab to replace the inner S
 Generates



“aabb”

S aSb aabb ……[Successful derivation of aabb]

Dr. Hussien M. Sharaf

7
CFG -1 : BALANCED-PARENTHESES
Prod1 S → (S)
Prod2 S → ()
 Derive the string ((())).
S → (S)
…..[by prod1]
→ ((S))
…..[by prod1]
→ ((()))
…..[by prod2]

Dr. Hussien M. Sharaf

8
CFG -2 : PALINDROME
Describe palindrome of a’s and b’s using
CFG
 1] S → aSa
2] S → bSb
 3] S → Λ


Derive “baab” from the above grammar.
 S → bSb
[by 2]
→ baSab
[by 1]
→ ba ab
[by 3]


Dr. Hussien M. Sharaf

9
CFG -3 : EVEN-PLAINDROME



i.e. {Λ, ab, abbaabba,… }
S → aSa| bSb| Λ
Derive abaaba
S
a

S

a

b

S

b

a

S

a

Λ

Dr. Hussien M. Sharaf

10
CFG – 4
Describe anything (a+b)* using CGF
1] S → Λ
2] S → Y
3] Y→ aY
4] Y → bY
5] Y →a
6] Y→ b


Derive “aab” from the above grammar.
 S → aY
[by 3]
Y → aaY
[by 3]
Y → aab
[by 6]


Dr. Hussien M. Sharaf

11
CFG – 5
1] S → Λ

2] S → aS

3] S→ bS

Derive “aa” from the above grammar.
 S → aS
[by 2]
→ aaS
[by 2]
→ aa
[by 1]


Dr. Hussien M. Sharaf

12
Dr. Hussien M. Sharaf

PART TWO

13
Parsing










CFG grammar is about categorizing the statements
of a language.
Parsing using CFG means categorizing a certain
statements into categories defined in the CFG.
Parsing can be expressed using a special type of
graph called Trees where no cycles exist.
A parse tree is the graph representation of a
derivation.
Programmatically; Parse tree can be represented as a
dynamic data structure using a single root node.

Dr. Hussien M. Sharaf

14
Parse tree

(1)A vertex with a label which is a
Non-terminal symbol is a parse tree.
(2) If A → y1 y2 … yn is a rule in R,
then the tree

A
y

1

y

2

...

y

n

is a parse tree.
Dr. Hussien M. Sharaf

15
Ambiguity

A grammar can generate the same string in
different ways.
 Ambiguity occurs when a string has two or
more leftmost derivations for the same CFG.
 There are ways to eliminate ambiguity such
as using Chomsky Normal Form (CNF)
which does n’t use Λ.
 Λ cause ambiguity.


Dr. Hussien M. Sharaf

16
Ex 1

Deduce CFG of addition and parse the
following expression 2+3+5
 1] S→S+S|N
 2] N→1|2|3|4|5|6|7|8|9|0
N1|N2|N3|N4|N5|N6|N7|N8|N9|N0


S
S+N
S
S

+

+
N

N

Can u make
another parsing
tree ?

5

N

3
2
Dr. Hussien M. Sharaf

17
Ex 2

Deduce CFG of a
addition/multiplication and parse the
following expression 2+3*5
 1] S→S+S|S S|N
*
 2] N→1|2|3|4|5|6|7|8|9|0|NN


S
S*S

S
S

+

*
N

N

Can u make
another parsing
tree ?

5

N
3
2

Dr. Hussien M. Sharaf

18
Ex 3 CFG without ambiguity


Deduce CFG of a addition/multiplication

and parse the following expression 2*3+5
1] S→ Term|Term + S
2] Term → N|N * Term
3] N→1|2|3|4|5|6|7|8|9|0
S
S+N
S
S

*

+
N

N

Can you make
another parsing
tree ?

5

N

3
2
Dr. Hussien M. Sharaf

19
Example 4 : AABB
S
A|AB
A
Λ| a | A b | A A
B
b|bc|Bc|bB
Sample derivations:
S AB AAB
aAB aaB aabB

aabb

S AB AbB
Aabb aabb

A

A

B
A

b

a

B

A

b
a

Dr. Hussien M. Sharaf

B

A
A

a

AAbb

S

S

A

Abb

a

b

b
20
Ex 5
S
A
B

A|AB
Λ|a|Ab|AA
b|bc|Bc|bB
S
A

S
B

A

A A b B
a a

S

b

A
a

A

B
A

A b

a

b

A
A A
a

A
A

b

A b
a

Dr. Hussien M. Sharaf

21
REMOVING AMBIGUITY
Eliminate “useless” variables.
Eliminate Λ-productions: A Λ.
Avoid left recursion by replacing it with
right-recursion.

But if a language is ambiguous, it can’t be
totally removed. We just need to the
parsing to continue without entering an
infinite loop.
Dr. Hussien M. Sharaf

22
THANK YOU

Dr. Hussien M. Sharaf

23

More Related Content

What's hot

Regular language and Regular expression
Regular language and Regular expressionRegular language and Regular expression
Regular language and Regular expressionAnimesh Chaturvedi
 
Chapter 2 2 1 1
Chapter 2 2 1 1Chapter 2 2 1 1
Chapter 2 2 1 1bolovv
 
Regular expression with DFA
Regular expression with DFARegular expression with DFA
Regular expression with DFAMaulik Togadiya
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Philip Schwarz
 
4.1 Inverse Functions
4.1 Inverse Functions4.1 Inverse Functions
4.1 Inverse Functionssmiller5
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computationBipul Roy Bpl
 
Processing Regex Python
Processing Regex PythonProcessing Regex Python
Processing Regex Pythonprimeteacher32
 
The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++Anjesh Tuladhar
 
Theory of computing
Theory of computingTheory of computing
Theory of computingRanjan Kumar
 
Python (regular expression)
Python (regular expression)Python (regular expression)
Python (regular expression)Chirag Shetty
 
Regular Languages
Regular LanguagesRegular Languages
Regular Languagesparmeet834
 
Top school in noida
Top school in noidaTop school in noida
Top school in noidaEdhole.com
 
Regular Expression (Regex) Fundamentals
Regular Expression (Regex) FundamentalsRegular Expression (Regex) Fundamentals
Regular Expression (Regex) FundamentalsMesut Günes
 

What's hot (20)

Minimizing DFA
Minimizing DFAMinimizing DFA
Minimizing DFA
 
Regular language and Regular expression
Regular language and Regular expressionRegular language and Regular expression
Regular language and Regular expression
 
Chapter 2 2 1 1
Chapter 2 2 1 1Chapter 2 2 1 1
Chapter 2 2 1 1
 
Regular expression with DFA
Regular expression with DFARegular expression with DFA
Regular expression with DFA
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
 
Theory of computation Lec1
Theory of computation Lec1Theory of computation Lec1
Theory of computation Lec1
 
4.1 Inverse Functions
4.1 Inverse Functions4.1 Inverse Functions
4.1 Inverse Functions
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computation
 
Processing Regex Python
Processing Regex PythonProcessing Regex Python
Processing Regex Python
 
The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++
 
Ch02
Ch02Ch02
Ch02
 
Theory of computing
Theory of computingTheory of computing
Theory of computing
 
Ch04
Ch04Ch04
Ch04
 
Python (regular expression)
Python (regular expression)Python (regular expression)
Python (regular expression)
 
Regular Languages
Regular LanguagesRegular Languages
Regular Languages
 
Matlab
MatlabMatlab
Matlab
 
Top school in noida
Top school in noidaTop school in noida
Top school in noida
 
Regular Expression (Regex) Fundamentals
Regular Expression (Regex) FundamentalsRegular Expression (Regex) Fundamentals
Regular Expression (Regex) Fundamentals
 
Ultra-efficient algorithms for testing well-parenthesised expressions by Tati...
Ultra-efficient algorithms for testing well-parenthesised expressions by Tati...Ultra-efficient algorithms for testing well-parenthesised expressions by Tati...
Ultra-efficient algorithms for testing well-parenthesised expressions by Tati...
 
Ch03
Ch03Ch03
Ch03
 

Similar to Cs419 lec7 cfg

CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR Zahid Parvez
 
05 8640 (update email) multiset cs closure propertie (edit lafi)2
05 8640 (update email) multiset cs closure propertie (edit lafi)205 8640 (update email) multiset cs closure propertie (edit lafi)2
05 8640 (update email) multiset cs closure propertie (edit lafi)2IAESIJEECS
 
Arabic Phoneme Recognition using Hierarchical Neural Fuzzy Petri Net and LPC ...
Arabic Phoneme Recognition using Hierarchical Neural Fuzzy Petri Net and LPC ...Arabic Phoneme Recognition using Hierarchical Neural Fuzzy Petri Net and LPC ...
Arabic Phoneme Recognition using Hierarchical Neural Fuzzy Petri Net and LPC ...CSCJournals
 
A taxonomy of suffix array construction algorithms
A taxonomy of suffix array construction algorithmsA taxonomy of suffix array construction algorithms
A taxonomy of suffix array construction algorithmsunyil96
 
Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Bryan O'Sullivan
 
Conteext-free Grammer
Conteext-free GrammerConteext-free Grammer
Conteext-free GrammerHASHIR RAZA
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free GrammarniveditJain
 
Context free grammars
Context free grammarsContext free grammars
Context free grammarsRonak Thakkar
 
Intuitionistic Fuzzy Semipre Generalized Connected Spaces
Intuitionistic Fuzzy Semipre Generalized Connected SpacesIntuitionistic Fuzzy Semipre Generalized Connected Spaces
Intuitionistic Fuzzy Semipre Generalized Connected SpacesEditor IJCATR
 
Cs6660 compiler design november december 2016 Answer key
Cs6660 compiler design november december 2016 Answer keyCs6660 compiler design november december 2016 Answer key
Cs6660 compiler design november december 2016 Answer keyappasami
 
Programmable PN Sequence Generators
Programmable PN Sequence GeneratorsProgrammable PN Sequence Generators
Programmable PN Sequence GeneratorsRajesh Singh
 
Formal methods 3 - languages and machines
Formal methods   3 - languages and machinesFormal methods   3 - languages and machines
Formal methods 3 - languages and machinesVlad Patryshev
 
CH 2.pptx
CH 2.pptxCH 2.pptx
CH 2.pptxObsa2
 

Similar to Cs419 lec7 cfg (20)

CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR
 
05 8640 (update email) multiset cs closure propertie (edit lafi)2
05 8640 (update email) multiset cs closure propertie (edit lafi)205 8640 (update email) multiset cs closure propertie (edit lafi)2
05 8640 (update email) multiset cs closure propertie (edit lafi)2
 
Arabic Phoneme Recognition using Hierarchical Neural Fuzzy Petri Net and LPC ...
Arabic Phoneme Recognition using Hierarchical Neural Fuzzy Petri Net and LPC ...Arabic Phoneme Recognition using Hierarchical Neural Fuzzy Petri Net and LPC ...
Arabic Phoneme Recognition using Hierarchical Neural Fuzzy Petri Net and LPC ...
 
A taxonomy of suffix array construction algorithms
A taxonomy of suffix array construction algorithmsA taxonomy of suffix array construction algorithms
A taxonomy of suffix array construction algorithms
 
Theory of computation Lec3 dfa
Theory of computation Lec3 dfaTheory of computation Lec3 dfa
Theory of computation Lec3 dfa
 
Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Real World Haskell: Lecture 2
Real World Haskell: Lecture 2
 
Theory of computation Lec2
Theory of computation Lec2Theory of computation Lec2
Theory of computation Lec2
 
Conteext-free Grammer
Conteext-free GrammerConteext-free Grammer
Conteext-free Grammer
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free Grammar
 
Context free grammer.ppt
Context free grammer.pptContext free grammer.ppt
Context free grammer.ppt
 
Ambiguous grammar, dfa and slr grammar.
Ambiguous grammar, dfa and slr  grammar.Ambiguous grammar, dfa and slr  grammar.
Ambiguous grammar, dfa and slr grammar.
 
Bound
BoundBound
Bound
 
RegexCat
RegexCatRegexCat
RegexCat
 
Lec1.pptx
Lec1.pptxLec1.pptx
Lec1.pptx
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
Intuitionistic Fuzzy Semipre Generalized Connected Spaces
Intuitionistic Fuzzy Semipre Generalized Connected SpacesIntuitionistic Fuzzy Semipre Generalized Connected Spaces
Intuitionistic Fuzzy Semipre Generalized Connected Spaces
 
Cs6660 compiler design november december 2016 Answer key
Cs6660 compiler design november december 2016 Answer keyCs6660 compiler design november december 2016 Answer key
Cs6660 compiler design november december 2016 Answer key
 
Programmable PN Sequence Generators
Programmable PN Sequence GeneratorsProgrammable PN Sequence Generators
Programmable PN Sequence Generators
 
Formal methods 3 - languages and machines
Formal methods   3 - languages and machinesFormal methods   3 - languages and machines
Formal methods 3 - languages and machines
 
CH 2.pptx
CH 2.pptxCH 2.pptx
CH 2.pptx
 

More from Arab Open University and Cairo University

More from Arab Open University and Cairo University (20)

Infos2014
Infos2014Infos2014
Infos2014
 
File Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswerFile Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswer
 
Model answer of compilers june spring 2013
Model answer of compilers june spring 2013Model answer of compilers june spring 2013
Model answer of compilers june spring 2013
 
Model answer of exam TC_spring 2013
Model answer of exam TC_spring 2013Model answer of exam TC_spring 2013
Model answer of exam TC_spring 2013
 
Theory of computation Lec7 pda
Theory of computation Lec7 pdaTheory of computation Lec7 pda
Theory of computation Lec7 pda
 
Setup python with eclipse
Setup python with eclipseSetup python with eclipse
Setup python with eclipse
 
Cs419 lec8 top-down parsing
Cs419 lec8    top-down parsingCs419 lec8    top-down parsing
Cs419 lec8 top-down parsing
 
Cs419 lec11 bottom-up parsing
Cs419 lec11   bottom-up parsingCs419 lec11   bottom-up parsing
Cs419 lec11 bottom-up parsing
 
Cs419 lec12 semantic analyzer
Cs419 lec12  semantic analyzerCs419 lec12  semantic analyzer
Cs419 lec12 semantic analyzer
 
Cs419 lec9 constructing parsing table ll1
Cs419 lec9   constructing parsing table ll1Cs419 lec9   constructing parsing table ll1
Cs419 lec9 constructing parsing table ll1
 
Cs419 lec10 left recursion and left factoring
Cs419 lec10   left recursion and left factoringCs419 lec10   left recursion and left factoring
Cs419 lec10 left recursion and left factoring
 
Cs419 lec6 lexical analysis using nfa
Cs419 lec6   lexical analysis using nfaCs419 lec6   lexical analysis using nfa
Cs419 lec6 lexical analysis using nfa
 
Compilers Final spring 2013 model answer
 Compilers Final spring 2013 model answer Compilers Final spring 2013 model answer
Compilers Final spring 2013 model answer
 
Compilers midterm spring 2013 model answer
Compilers midterm spring 2013   model answerCompilers midterm spring 2013   model answer
Compilers midterm spring 2013 model answer
 
Final Exam OS fall 2012-2013 with answers
Final Exam OS fall 2012-2013 with answersFinal Exam OS fall 2012-2013 with answers
Final Exam OS fall 2012-2013 with answers
 
CS215 - Lec 8 searching records
CS215 - Lec 8  searching recordsCS215 - Lec 8  searching records
CS215 - Lec 8 searching records
 
CS215 - Lec 7 managing records collection
CS215 - Lec 7  managing records collectionCS215 - Lec 7  managing records collection
CS215 - Lec 7 managing records collection
 
CS215 - Lec 6 record index
CS215 - Lec 6  record indexCS215 - Lec 6  record index
CS215 - Lec 6 record index
 
CS215 - Lec 5 record organization
CS215 - Lec 5  record organizationCS215 - Lec 5  record organization
CS215 - Lec 5 record organization
 
CS215 - Lec 4 single record organization
CS215 - Lec 4  single record organizationCS215 - Lec 4  single record organization
CS215 - Lec 4 single record organization
 

Recently uploaded

Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 

Recently uploaded (20)

Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 

Cs419 lec7 cfg

  • 1. WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Computer Science Department dr.sharaf@from-masr.com
  • 2. Dr. Hussien M. Sharaf PART ONE 2
  • 3. PARSING   A parser gets a stream of tokens from the scanner, and determines if the syntax (structure) of the program is correct according to the (context-free) grammar of the source language. Then, it produces a data structure, called a parse tree or an abstract syntax tree, which describes the syntactic structure of the program. Dr. Hussien M. Sharaf Stream of tokens parser Parse/syntax tree 3
  • 4. CFG      A context-free grammar is a notation for defining context free languages. It is more powerful than finite automata or RE’s, but still cannot define all possible languages. Useful for nested structures, e.g., parentheses in programming languages. Basic idea is to use “variables” to stand for sets of strings. These variables are defined recursively, in terms of one another. Dr. Hussien M. Sharaf 4
  • 5. CFG FORMAL DEFINITION C =(V, Σ, R, S)  V: is a finite set of variables.  Σ: symbols called terminals of the alphabet of the language being defined.  S V: a special start symbol.  R: is a finite set of production rules of the form A→ where A V,  (V Σ)  Dr. Hussien M. Sharaf 5
  • 6. CFG -1      Define the language { anbn | n > 1}. Terminals = {a, b}. Variables = {S}. Start symbol = S. Productions =    S → ab S → aSb Summary S → ab  S → aSb Dr. Hussien M. Sharaf 6
  • 7. DERIVATION    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. Derivation example for “aabb” Using S→ aSb generates uncompleted string that still has a nonterminal S.  Then using S→ ab to replace the inner S  Generates  “aabb” S aSb aabb ……[Successful derivation of aabb] Dr. Hussien M. Sharaf 7
  • 8. CFG -1 : BALANCED-PARENTHESES Prod1 S → (S) Prod2 S → ()  Derive the string ((())). S → (S) …..[by prod1] → ((S)) …..[by prod1] → ((())) …..[by prod2] Dr. Hussien M. Sharaf 8
  • 9. CFG -2 : PALINDROME Describe palindrome of a’s and b’s using CFG  1] S → aSa 2] S → bSb  3] S → Λ  Derive “baab” from the above grammar.  S → bSb [by 2] → baSab [by 1] → ba ab [by 3]  Dr. Hussien M. Sharaf 9
  • 10. CFG -3 : EVEN-PLAINDROME   i.e. {Λ, ab, abbaabba,… } S → aSa| bSb| Λ Derive abaaba S a S a b S b a S a Λ Dr. Hussien M. Sharaf 10
  • 11. CFG – 4 Describe anything (a+b)* using CGF 1] S → Λ 2] S → Y 3] Y→ aY 4] Y → bY 5] Y →a 6] Y→ b  Derive “aab” from the above grammar.  S → aY [by 3] Y → aaY [by 3] Y → aab [by 6]  Dr. Hussien M. Sharaf 11
  • 12. CFG – 5 1] S → Λ 2] S → aS 3] S→ bS Derive “aa” from the above grammar.  S → aS [by 2] → aaS [by 2] → aa [by 1]  Dr. Hussien M. Sharaf 12
  • 13. Dr. Hussien M. Sharaf PART TWO 13
  • 14. Parsing      CFG grammar is about categorizing the statements of a language. Parsing using CFG means categorizing a certain statements into categories defined in the CFG. Parsing can be expressed using a special type of graph called Trees where no cycles exist. A parse tree is the graph representation of a derivation. Programmatically; Parse tree can be represented as a dynamic data structure using a single root node. Dr. Hussien M. Sharaf 14
  • 15. Parse tree (1)A vertex with a label which is a Non-terminal symbol is a parse tree. (2) If A → y1 y2 … yn is a rule in R, then the tree A y 1 y 2 ... y n is a parse tree. Dr. Hussien M. Sharaf 15
  • 16. Ambiguity A grammar can generate the same string in different ways.  Ambiguity occurs when a string has two or more leftmost derivations for the same CFG.  There are ways to eliminate ambiguity such as using Chomsky Normal Form (CNF) which does n’t use Λ.  Λ cause ambiguity.  Dr. Hussien M. Sharaf 16
  • 17. Ex 1 Deduce CFG of addition and parse the following expression 2+3+5  1] S→S+S|N  2] N→1|2|3|4|5|6|7|8|9|0 N1|N2|N3|N4|N5|N6|N7|N8|N9|N0  S S+N S S + + N N Can u make another parsing tree ? 5 N 3 2 Dr. Hussien M. Sharaf 17
  • 18. Ex 2 Deduce CFG of a addition/multiplication and parse the following expression 2+3*5  1] S→S+S|S S|N *  2] N→1|2|3|4|5|6|7|8|9|0|NN  S S*S S S + * N N Can u make another parsing tree ? 5 N 3 2 Dr. Hussien M. Sharaf 18
  • 19. Ex 3 CFG without ambiguity  Deduce CFG of a addition/multiplication and parse the following expression 2*3+5 1] S→ Term|Term + S 2] Term → N|N * Term 3] N→1|2|3|4|5|6|7|8|9|0 S S+N S S * + N N Can you make another parsing tree ? 5 N 3 2 Dr. Hussien M. Sharaf 19
  • 20. Example 4 : AABB S A|AB A Λ| a | A b | A A B b|bc|Bc|bB Sample derivations: S AB AAB aAB aaB aabB aabb S AB AbB Aabb aabb A A B A b a B A b a Dr. Hussien M. Sharaf B A A a AAbb S S A Abb a b b 20
  • 21. Ex 5 S A B A|AB Λ|a|Ab|AA b|bc|Bc|bB S A S B A A A b B a a S b A a A B A A b a b A A A a A A b A b a Dr. Hussien M. Sharaf 21
  • 22. REMOVING AMBIGUITY Eliminate “useless” variables. Eliminate Λ-productions: A Λ. Avoid left recursion by replacing it with right-recursion. But if a language is ambiguous, it can’t be totally removed. We just need to the parsing to continue without entering an infinite loop. Dr. Hussien M. Sharaf 22
  • 23. THANK YOU Dr. Hussien M. Sharaf 23