




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 122
This page cannot be seen from the preview
Don't miss anything!
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:
C++ program:
Computer program compilation
C++ program:
C++ program:
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,
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)}