
















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
This course is about: Formal Languages, Automata and Complexity. Context Free Languages, Dfa, Regular Expression, Basis, Recursion, Grammars, Nonterminal Symbol, Terminal Symbols, Start Variable, Parse Trees, Structural Ambiguity
Typology: Slides
1 / 24
This page cannot be seen from the preview
Don't miss anything!

















CONTEXT FREE LANGUAGES
Carnegie Mellon University in Qatar
Consider L = {anbn^ | n ≥ 0 } again. How can we generate such strings? Remember DFAs did recognition, not generation. Consider the following inductive way to generate elements of L Basis: is in the language Recursion: If the string w is in the language, then so is the string awb. → ab → aabb · · · → a^55 b^55 · · · Looks like we have simple and finite length process to generate all the strings in L How can we generalize this kind of description?
Consider L = {w | na(w) = nb(w)}. Now consider the following inductive way to generate elements of L Basis: is in the language Recursion 1: If the string w is in the language, then so are awb and bwa Recursion 2: If the strings w and v are in the language, so is wv. The first recursion rules makes sure that the a’s and b’s are generated in the same number (regardless of order) The second recursion takes any two strings each with equal number of a’s and b’s and generates a
(CARNEGIE new such string by concatenating them.MELLON UNIVERSITY IN QATAR) SLIDES FOR 15-453 LECTURE 7 SPRING 2011 5 / 24
Consider once again L = {anbn^ | n ≥ 0 } Basis: is in the language Production: S → Recursion: If w is in the language, then so is the string awb. Production: S → aSb S is called a variable or a nonterminal symbol a, b etc., are called terminal symbols One variable is designated as the start variable or start symbol.
Consider the set of rules R = {S → , S → aSb} Start with the start variable S Apply the following until all remaining symbols are terminal. Choose a production in R whose left-hand sides matches one of the variables. Replace the variable with the rule’s right hand side. S ⇒ aSb ⇒ aaSbb ⇒ aaaSbbb ⇒ aaaaSbbbb ⇒ aaaabbbb The string aaaabbbb is in the language L The sequence of rule applications above is called a derivation.
All strings generated this way starting with the start variable constitute the language of the grammar. We write L(G) for the language of the grammar G.
DT → a | the N → boy | girl | flower | telescope V → touches | likes | sees | gives P → with | to
Nomenclature: S: Sentence NP: Noun Phrase CN: Complex Noun PP: Prepositional Phrase VP: Verb Phrase CV: Complex Verb P: Preposition DT: Determiner
S NP CN DT a
N boy
PP P with
NP CN DT a
N flower
VP CV V sees
NP CN DT a
N girl
PP P with
NP CN DT a
N
telescope
This structure is for the interpretation where the boy is seeing with the telescope!
ALTERNATE STRUCTURE
S NP CN DT a
N boy
PP P with
NP CN DT a
N flower
VP CV V sees
NP CN DT a
N girl
PP P with
NP CN DT a
N
telescope This is for the interpretation where the girl is carrying a telescope.
Programming Languages Users need to how to “generate” correct programs. Compilers need to know how to “check” and “translate” programs. XML Documents Documents need to have a structure defined by a DTD grammar. Natural Language Processing, Machine Translation
A Grammar is a 4-tuple G = (V , Σ, R, S) where V is a finite set of variables Σ is a finite set of terminals, disjoint from V. R is a set of rules of the X → Y S ∈ V is the start variable In general X ∈ (V ∪ Σ)+^ and Y ∈ (V ∪ Σ)∗ A context-free grammar is a grammar where all rules have X ∈ V (remember V ⊂ (V ∪ Σ)+) The substitution is independent of the context V appears in. The right hand side of the rules can be any combination of variables and terminals, including (hence Y ∈ (V ∪ Σ)∗).
Consider once again the language L = {w | na(w) = nb(w)}. The grammar for this language is G = ({S}, {a, b}, R, S) with R as follows: (^1) S → aSb (^2) S → bSa (^3) S → SS (^4) S → From now we will only list the productions, the others will be implicit. We will also combine productions with the same left-hand side using | symbol. S → aSb | bSa | SS |
L = {w | na(w) = nb(w)}. S → aSb | bSa | SS |
Clearly the strings generated by G have equal number of a’s and b’s. (Obvious from the rules!) We also have to show that all strings in L can be generated with this grammar.