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

PPTX
Cs419 lec3 lexical analysis using re
PPTX
Cs419 lec5 lexical analysis using dfa
PPTX
Cs419 lec4 lexical analysis using re
PPTX
Cs419 Compiler lec1&2 introduction
PPTX
Theory of computation Lec6
PPTX
theory of computation lecture 02
PDF
Regular expression
Cs419 lec3 lexical analysis using re
Cs419 lec5 lexical analysis using dfa
Cs419 lec4 lexical analysis using re
Cs419 Compiler lec1&2 introduction
Theory of computation Lec6
theory of computation lecture 02
Regular expression

What's hot (20)

PDF
Minimizing DFA
PDF
Regular language and Regular expression
DOC
Chapter 2 2 1 1
PPT
Regular expression with DFA
PDF
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
PPTX
Theory of computation Lec1
PDF
4.1 Inverse Functions
PPT
Regular expressions-Theory of computation
PPTX
Processing Regex Python
PPT
The Power of Regular Expression: use in notepad++
PDF
Ch02
PPT
Theory of computing
PDF
Ch04
PDF
Python (regular expression)
PPT
Regular Languages
PPTX
PPT
Top school in noida
PPTX
Regular Expression (Regex) Fundamentals
PDF
Ultra-efficient algorithms for testing well-parenthesised expressions by Tati...
PDF
Ch03
Minimizing DFA
Regular language and Regular expression
Chapter 2 2 1 1
Regular expression with DFA
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Theory of computation Lec1
4.1 Inverse Functions
Regular expressions-Theory of computation
Processing Regex Python
The Power of Regular Expression: use in notepad++
Ch02
Theory of computing
Ch04
Python (regular expression)
Regular Languages
Top school in noida
Regular Expression (Regex) Fundamentals
Ultra-efficient algorithms for testing well-parenthesised expressions by Tati...
Ch03
Ad

Similar to Cs419 lec7 cfg (20)

PPTX
CONTEXT FREE GRAMMAR
PDF
05 8640 (update email) multiset cs closure propertie (edit lafi)2
PDF
Arabic Phoneme Recognition using Hierarchical Neural Fuzzy Petri Net and LPC ...
PDF
A taxonomy of suffix array construction algorithms
PPTX
Theory of computation Lec3 dfa
PDF
Real World Haskell: Lecture 2
PPTX
Theory of computation Lec2
PPTX
Conteext-free Grammer
PDF
Context Free Grammar
PPT
Context free grammer.ppt
PPTX
PDF
Natural language Processing: Word Level Analysis
PDF
RegexCat
PPT
Compiler design lessons notes from Semester
PPTX
Lec1.pptx
PPTX
1. Introduction automata throry and intoduction
PPTX
1. Introduction to machine learning and AI
PPT
Context free grammars
PDF
Intuitionistic Fuzzy Semipre Generalized Connected Spaces
CONTEXT FREE GRAMMAR
05 8640 (update email) multiset cs closure propertie (edit lafi)2
Arabic Phoneme Recognition using Hierarchical Neural Fuzzy Petri Net and LPC ...
A taxonomy of suffix array construction algorithms
Theory of computation Lec3 dfa
Real World Haskell: Lecture 2
Theory of computation Lec2
Conteext-free Grammer
Context Free Grammar
Context free grammer.ppt
Natural language Processing: Word Level Analysis
RegexCat
Compiler design lessons notes from Semester
Lec1.pptx
1. Introduction automata throry and intoduction
1. Introduction to machine learning and AI
Context free grammars
Intuitionistic Fuzzy Semipre Generalized Connected Spaces
Ad

More from Arab Open University and Cairo University (20)

PDF
File Organization & processing Mid term summer 2014 - modelanswer
PDF
Model answer of compilers june spring 2013
PDF
Model answer of exam TC_spring 2013
PPTX
Theory of computation Lec7 pda
PPTX
PPTX
Cs419 lec8 top-down parsing
PPTX
Cs419 lec11 bottom-up parsing
PPTX
Cs419 lec12 semantic analyzer
PPTX
Cs419 lec9 constructing parsing table ll1
PPTX
Cs419 lec10 left recursion and left factoring
PPTX
Cs419 lec6 lexical analysis using nfa
DOCX
Compilers Final spring 2013 model answer
PDF
Compilers midterm spring 2013 model answer
PDF
Final Exam OS fall 2012-2013 with answers
PDF
CS215 - Lec 8 searching records
PDF
CS215 - Lec 7 managing records collection
PDF
CS215 - Lec 6 record index
PDF
CS215 - Lec 5 record organization
PDF
CS215 - Lec 4 single record organization
File Organization & processing Mid term summer 2014 - modelanswer
Model answer of compilers june spring 2013
Model answer of exam TC_spring 2013
Theory of computation Lec7 pda
Cs419 lec8 top-down parsing
Cs419 lec11 bottom-up parsing
Cs419 lec12 semantic analyzer
Cs419 lec9 constructing parsing table ll1
Cs419 lec10 left recursion and left factoring
Cs419 lec6 lexical analysis using nfa
Compilers Final spring 2013 model answer
Compilers midterm spring 2013 model answer
Final Exam OS fall 2012-2013 with answers
CS215 - Lec 8 searching records
CS215 - Lec 7 managing records collection
CS215 - Lec 6 record index
CS215 - Lec 5 record organization
CS215 - Lec 4 single record organization

Recently uploaded (20)

DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
Empowerment Technology for Senior High School Guide
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PPTX
TNA_Presentation-1-Final(SAVE)) (1).pptx
PPTX
History, Philosophy and sociology of education (1).pptx
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PPTX
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
My India Quiz Book_20210205121199924.pdf
PDF
International_Financial_Reporting_Standa.pdf
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
Trump Administration's workforce development strategy
PDF
advance database management system book.pdf
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
Environmental Education MCQ BD2EE - Share Source.pdf
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Empowerment Technology for Senior High School Guide
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
TNA_Presentation-1-Final(SAVE)) (1).pptx
History, Philosophy and sociology of education (1).pptx
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
What if we spent less time fighting change, and more time building what’s rig...
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
Share_Module_2_Power_conflict_and_negotiation.pptx
LDMMIA Reiki Yoga Finals Review Spring Summer
My India Quiz Book_20210205121199924.pdf
International_Financial_Reporting_Standa.pdf
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
Weekly quiz Compilation Jan -July 25.pdf
Trump Administration's workforce development strategy
advance database management system book.pdf
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
Environmental Education MCQ BD2EE - Share Source.pdf

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