Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Theory of Computation: Context-Free Grammars, Lecture notes of Theory of Computation

Context-free grammars (CFG) and their use in checking the syntax of computer programs. It includes examples of C++ programs and their compilation errors, as well as solutions to constructing CFGs for various languages, including palindromes and non-palindromes over {a,b}. The author, Pramod Ganapathi, is affiliated with the Department of Computer Science at the State University of New York at Stony Brook.

Typology: Lecture notes

2021/2022

Uploaded on 05/11/2023

shachi_984a
shachi_984a 🇺🇸

4.6

(15)

222 documents

1 / 122

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Theory of Computation
(Context-Free Grammars)
Pramod Ganapathi
Department of Computer Science
State University of New York at Stony Brook
January 24, 2021
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Theory of Computation: Context-Free Grammars and more Lecture notes Theory of Computation in PDF only on Docsity!

Theory of Computation

(Context-Free Grammars)

Pramod Ganapathi

Department of Computer Science State University of New York at Stony Brook

January 24, 2021

Contents

Contents Context-Free Grammars (CFG) Context-Free Languages Pushdown Automata (PDA) Transformations Pumping Lemma

Computer program compilation

C++ program:

  1. #include
  2. using namespace std;
  3. int main()
  4. {
  5. if (true)
  6. {
  7. cout << "Hi 1";
  8. else
  9. cout << "Hi 2";
  10. }
  11. return 0;
  12. }

C++ program:

  1. #include
  2. using namespace std;
  3. int main()
  4. {
  5. if (true)
  6. cout << "Hi 1";
  7. else
  8. cout << "Hi 2";
  9. return 0;
  10. }

Computer program compilation

C++ program:

  1. #include
  2. using namespace std;
  3. int main()
  4. {
  5. if (true)
  6. {
  7. cout << "Hi 1";
  8. else
  9. cout << "Hi 2";
  10. }
  11. return 0;
  12. }

C++ program:

  1. #include
  2. using namespace std;
  3. int main()
  4. {
  5. if (true)
  6. cout << "Hi 1";
  7. else
  8. cout << "Hi 2";
  9. return 0;
  10. }

Output: error: expected ‘}’ before ‘else’

Output: Hi 1

Construct CFG for L = { a n b n^ | n ≥ 0 }

Problem Construct a CFG that accepts all strings from the language L = {a n b n^ | n ≥ 0 }

Construct CFG for L = { a n b n^ | n ≥ 0 }

Problem Construct a CFG that accepts all strings from the language L = {a n b n^ | n ≥ 0 } Solution Language L = {, ab, aabb, aaabbb, aaaabbbb,.. .} CFG G. S → aSb S → 

Construct CFGs

Problems Construct CFGs to accept all strings from the following languages: R = a∗ R = a+ R = a∗b∗ R = a+b+ R = a∗^ ∪ b∗ R = (a ∪ b)∗ R = a∗b∗c∗

Construct CFG for palindromes over { a, b }

Problem Construct a CFG that accepts all strings from the language L = {w | w = w R^ and Σ = {a, b}}

Construct CFG for palindromes over { a, b }

Solution (continued) CFG G. S → aSa | bSb | a | b |  Accepting . S ⇒  B 1 step Accepting a. S ⇒ a Accepting b. S ⇒ b Accepting aa. S ⇒ aSa ⇒ aa B 2 steps Accepting bb. S ⇒ bSb ⇒ bb Accepting aaa. S ⇒ aSa ⇒ aaa B 2 steps Accepting aba. S ⇒ aSa ⇒ aba Accepting bab. S ⇒ bSb ⇒ bab Accepting bbb. S ⇒ bSb ⇒ bbb Accepting aaaa. S ⇒ aSa ⇒ aaSaa ⇒ aaaa B 3 steps Accepting abba. S ⇒ aSa ⇒ abSba ⇒ abba Accepting baab. S ⇒ bSb ⇒ baSab ⇒ baab Accepting bbbb. S ⇒ bSb ⇒ bbSbb ⇒ bbbb

Construct CFG for non-palindromes over { a, b }

Problem Construct a CFG that accepts all strings from the language L = {w | w 6 = w R^ and Σ = {a, b}}

Construct CFG for non-palindromes over { a, b }

Solution (continued) CFG G. S → aSa | bSb | aAb | bAa A → Aa | Ab |  Accepting abbbbaaba. B 7-step derivation S ⇒ aSa ⇒ abSba ⇒ abbAaba ⇒ abbAaaba ⇒ abbAbaaba ⇒ abbAbbaaba ⇒ abbbbaaba

What is a context-free grammar (CFG)?

Grammar = A set of rules for a language Context-free = LHS of productions have only 1 nonterminal

Definition A context-free grammar (CFG) G is a 4-tuple G = (N, Σ, S, P ), where,

  1. N : A finite set (set of nonterminals/variables).
  2. Σ: A finite set (set of terminals).
  3. P : A finite set of productions/rules of the form A → α, A ∈ N, α ∈ (N ∪ Σ)∗. B Time (computation) B Space (computer memory)
  4. S: The start nonterminal (belongs to N ).

What is a context-free language (CFL)?

Definition If G = (N, Σ, S, P ) is a CFG, the language generated by G is L(G) = {w ∈ Σ∗^ | S ⇒∗ G w} A language L is a context-free language (CFL) if there is a CFG G with L = L(G).

Construct CFG for L = { w | n a( w ) = n b( w )}

Problem Construct a CFG that accepts all strings from the language L = {w | n a (w) = n b (w)}