Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
CS 133 : Automata Theory and Computability Context-Free Grammars and Languages Henry N. Adorna Nestine Hope S. Hernandez Algorithms and Complexity Laboratory Department of Computer Science University of the Philippines, Diliman {hnadorna,nshernandez}@dcs.upd.edu.ph
LEC 4
Context-Free Grammars
Introduction
Designing CFGs
Context-Free Grammars and Languages
Introduction Context-Free Grammars Formal Definition Derivations using a Grammar Context-Free Language Designing CFGs Ambiguous CFGs
Ambiguous CFGs
Context-Free Grammars
Introduction
Designing CFGs
Context-Free Grammars and Languages
Introduction Context-Free Grammars Formal Definition Derivations using a Grammar Context-Free Language Designing CFGs Ambiguous CFGs
Ambiguous CFGs
Introduction
Context-Free Grammars
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
Introduction
Context-Free Grammars
Designing CFGs
Context-Free Grammars • a more powerful method of describing languages
Ambiguous CFGs
Introduction
Context-Free Grammars
Designing CFGs
Context-Free Grammars • a more powerful method of describing languages • describe certain features that have a recursive structure
Ambiguous CFGs
Introduction
Context-Free Grammars
Designing CFGs
Context-Free Grammars • a more powerful method of describing languages • describe certain features that have a recursive structure • first used in the study of human languages (noun, verb,
preposition)
Ambiguous CFGs
Context-Free Grammars
Introduction
Designing CFGs
Ambiguous CFGs
Context-Free Grammars • a more powerful method of describing languages • describe certain features that have a recursive structure • first used in the study of human languages (noun, verb,
preposition) • application: specification and compilation of programming
languages
Context-Free Grammars
Introduction
Designing CFGs
Ambiguous CFGs
Context-Free Grammars • a more powerful method of describing languages • describe certain features that have a recursive structure • first used in the study of human languages (noun, verb,
preposition) • application: specification and compilation of programming
languages • parser - extracts the meaning of a program prior to generating
the compiled code or performing the interpreted action
Context-Free Grammars
Introduction
Designing CFGs
Ambiguous CFGs
Context-Free Grammars • a more powerful method of describing languages • describe certain features that have a recursive structure • first used in the study of human languages (noun, verb,
preposition) • application: specification and compilation of programming
languages • parser - extracts the meaning of a program prior to generating
the compiled code or performing the interpreted action • context free languages - collection of languages associated with
CFGs
Context-Free Grammars
Introduction
Designing CFGs
Ambiguous CFGs
Context-Free Grammars • a more powerful method of describing languages • describe certain features that have a recursive structure • first used in the study of human languages (noun, verb,
preposition) • application: specification and compilation of programming
languages • parser - extracts the meaning of a program prior to generating
the compiled code or performing the interpreted action • context free languages - collection of languages associated with
CFGs • pushdown automata - a class of machines recognizing the CFLs
Introduction
Example A → 0A1 A→B B→#
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
Context-Free Grammars
Introduction
Designing CFGs
Ambiguous CFGs
Example A → 0A1 A→B B→#
Definition • A grammar consists of a collection of substitution rules, also
called productions
Context-Free Grammars
Introduction
Designing CFGs
Ambiguous CFGs
Example A → 0A1 A→B B→#
Definition • A grammar consists of a collection of substitution rules, also
called productions (symbol → string)
Context-Free Grammars
Introduction
Designing CFGs
Ambiguous CFGs
Example A → 0A1 A→B B→#
Definition • A grammar consists of a collection of substitution rules, also
called productions (symbol → string) • The symbol is called a variable (represented by capital letters)
Context-Free Grammars
Introduction
Designing CFGs
Ambiguous CFGs
Example A → 0A1 A→B B→#
Definition • A grammar consists of a collection of substitution rules, also
called productions (symbol → string) • The symbol is called a variable (represented by capital letters) • The string consists of variables and other symbols called
terminals (input alphabet)
Context-Free Grammars
Introduction
Designing CFGs
Ambiguous CFGs
Example A → 0A1 A→B B→#
Definition • A grammar consists of a collection of substitution rules, also
called productions (symbol → string) • The symbol is called a variable (represented by capital letters) • The string consists of variables and other symbols called
terminals (input alphabet) • One variable is designated as the start variable (usually, the
left-hand side of topmost rule)
Context-Free Grammars
Introduction
Designing CFGs
Ambiguous CFGs
Example A → 0A1 A→B B→#
Definition • A grammar consists of a collection of substitution rules, also
called productions (symbol → string) • The symbol is called a variable (represented by capital letters) • The string consists of variables and other symbols called
terminals (input alphabet) • One variable is designated as the start variable (usually, the
left-hand side of topmost rule)
Introduction
Context-Free Grammars
Grammar −→ Language
Designing CFGs
Ambiguous CFGs
Introduction
Context-Free Grammars
Grammar −→ Language 1. Write down the start variable.
Designing CFGs
Ambiguous CFGs
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
Grammar −→ Language 1. Write down the start variable. 2. Find a variable that is written down and a rule that starts with that variable. Replace the written down variable with the right-hand side of that rule.
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
Grammar −→ Language 1. Write down the start variable. 2. Find a variable that is written down and a rule that starts with that variable. Replace the written down variable with the right-hand side of that rule. 3. Repeat step2 until no variables remain.
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
Grammar −→ Language 1. Write down the start variable. 2. Find a variable that is written down and a rule that starts with that variable. Replace the written down variable with the right-hand side of that rule. 3. Repeat step2 until no variables remain. The sequence of substitutions to obtain a string is called a derivation.
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
Grammar −→ Language 1. Write down the start variable. 2. Find a variable that is written down and a rule that starts with that variable. Replace the written down variable with the right-hand side of that rule. 3. Repeat step2 until no variables remain. The sequence of substitutions to obtain a string is called a derivation.
Example (G1) A → 0A1 A→B B→#
Context-Free Grammars
Introduction
Designing CFGs
Ambiguous CFGs
Grammar −→ Language 1. Write down the start variable. 2. Find a variable that is written down and a rule that starts with that variable. Replace the written down variable with the right-hand side of that rule. 3. Repeat step2 until no variables remain. The sequence of substitutions to obtain a string is called a derivation.
Example (G1) A → 0A1 A→B B→# A ⇒ 0A1 ⇒ 00A11 ⇒ 000A111 ⇒ 000B111 ⇒ 000#111
Context-Free Grammars
Introduction
Designing CFGs
Ambiguous CFGs
Grammar −→ Language 1. Write down the start variable. 2. Find a variable that is written down and a rule that starts with that variable. Replace the written down variable with the right-hand side of that rule. 3. Repeat step2 until no variables remain. The sequence of substitutions to obtain a string is called a derivation.
Example (G1) A → 0A1 A→B B→# A ⇒ 0A1 ⇒ 00A11 ⇒ 000A111 ⇒ 000B111 ⇒ 000#111
May be represented by a parse tree
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
Context-Free Language Any language that can be generated by some context-free grammar is called a context-free language.
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
Context-Free Language Any language that can be generated by some context-free grammar is called a context-free language.
Example (G2) <sentence> <nounphrase> <verbphrase> <prepphrase> <cmplxnoun> <cmplxverb> <article> <noun> <verb> <prep>
−→ −→ −→ −→ −→ −→ −→ −→ −→ −→
<nounphrase> <verbphrase> <cmplxnoun> | <cmplxnoun> <prepphrase> <cmplxverb> | <cmplxverb> <prepphrase> <prep> <cmplxnoun> <article> <noun> <verb> | <verb> <nounphrase> a | the boy | girl | flower touches | likes | sees with
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
Context-Free Language Any language that can be generated by some context-free grammar is called a context-free language.
Example (G2) <sentence> <nounphrase> <verbphrase> <prepphrase> <cmplxnoun> <cmplxverb> <article> <noun> <verb> <prep>
−→ −→ −→ −→ −→ −→ −→ −→ −→ −→
the girl likes the flower
<nounphrase> <verbphrase> <cmplxnoun> | <cmplxnoun> <prepphrase> <cmplxverb> | <cmplxverb> <prepphrase> <prep> <cmplxnoun> <article> <noun> <verb> | <verb> <nounphrase> a | the boy | girl | flower touches | likes | sees with
Context-Free Grammars
Introduction
Designing CFGs
Context-Free Grammars and Languages
Introduction Context-Free Grammars Formal Definition Derivations using a Grammar Context-Free Language Designing CFGs Ambiguous CFGs
Ambiguous CFGs
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
Formal Definition
Definition A context-free grammar is a 4-tuple (V, Σ, R, S), where 1. V is a finite set called the variables, 2. Σ is a finite set, disjoint from V , called the terminals, 3. R is a finite set of rules, with each rule being a variable and a string of variables and terminals, and 4. S ∈ V is the start variable.
Example (G3) G3 = ({S}, {(, )}, R, S) R: S → (S)|SS|ε
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
Formal Definition
Definition A context-free grammar is a 4-tuple (V, Σ, R, S), where 1. V is a finite set called the variables, 2. Σ is a finite set, disjoint from V , called the terminals, 3. R is a finite set of rules, with each rule being a variable and a string of variables and terminals, and 4. S ∈ V is the start variable.
Example (G3) G3 = ({S}, {(, )}, R, S) R: S → (S)|SS|ε
Example (Gpal ) Gpal = ({P }, {0, 1}, R, P ) R: P → ε | 0 | 1 | 0P 0 | 1P 1
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
Formal Definition
Definition A context-free grammar is a 4-tuple (V, Σ, R, S), where 1. V is a finite set called the variables, 2. Σ is a finite set, disjoint from V , called the terminals, 3. R is a finite set of rules, with each rule being a variable and a string of variables and terminals, and 4. S ∈ V is the start variable.
Example (G3) G3 = ({S}, {(, )}, R, S) R: S → (S)|SS|ε
Example (Gpal ) Gpal = ({P }, {0, 1}, R, P ) R: P → ε | 0 | 1 | 0P 0 | 1P 1 A CFG for palindromes
Introduction
Derivations:
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
Introduction
Context-Free Grammars
Derivations: B Sentential Form
Designing CFGs
Ambiguous CFGs
Introduction
Context-Free Grammars
Derivations: B Sentential Form B Parse Tree
Designing CFGs
Ambiguous CFGs
Introduction
Context-Free Grammars
Derivations: B Sentential Form B Parse Tree
Example (G) S → A1B A → 0A | ε B → 0B | 1B | ε
Designing CFGs
Ambiguous CFGs
Introduction
Context-Free Grammars
Designing CFGs
Derivations: B Sentential Form B Parse Tree
Example (G) S → A1B A → 0A | ε B → 0B | 1B | ε Give leftmost derivations of the following strings: 1. 00101 2. 1001 3. 00011
Ambiguous CFGs
Introduction
Context-Free Grammars
Designing CFGs
Derivations: B Sentential Form B Parse Tree
Example (G) S → A1B A → 0A | ε B → 0B | 1B | ε Give leftmost derivations of the following strings: 1. 00101 2. 1001 3. 00011 Give parse trees for each of the strings above.
Ambiguous CFGs
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
Definition If G(V, Σ, R, S) is a CFG, the language of G, denoted by L(G), is the set of terminal strings that have derivations from the start symbols. That is, L(G) = {w ∈ Σ∗ |S ⇒∗ w}
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
Definition If G(V, Σ, R, S) is a CFG, the language of G, denoted by L(G), is the set of terminal strings that have derivations from the start symbols. That is, L(G) = {w ∈ Σ∗ |S ⇒∗ w} Any language that can be generated by some context-free grammar is called a context-free language.
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
Definition If G(V, ÎŁ, R, S) is a CFG, the language of G, denoted by L(G), is the set of terminal strings that have derivations from the start symbols. That is, L(G) = {w â&#x2C6;&#x2C6; ÎŁâ&#x2C6;&#x2014; |S â&#x2021;&#x2019;â&#x2C6;&#x2014; w} Any language that can be generated by some context-free grammar is called a context-free language.
Example (Gpal ) Gpal = ({P }, {0, 1}, R, P ) R: P â&#x2020;&#x2019; | 0 | 1 | 0P 0 | 1P 1
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
Definition If G(V, ÎŁ, R, S) is a CFG, the language of G, denoted by L(G), is the set of terminal strings that have derivations from the start symbols. That is, L(G) = {w â&#x2C6;&#x2C6; ÎŁâ&#x2C6;&#x2014; |S â&#x2021;&#x2019;â&#x2C6;&#x2014; w} Any language that can be generated by some context-free grammar is called a context-free language.
Example (Gpal ) Gpal = ({P }, {0, 1}, R, P ) R: P â&#x2020;&#x2019; | 0 | 1 | 0P 0 | 1P 1 L(Gpal ) is the set of palindromes over {0, 1}.
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
Definition If G(V, ÎŁ, R, S) is a CFG, the language of G, denoted by L(G), is the set of terminal strings that have derivations from the start symbols. That is, L(G) = {w â&#x2C6;&#x2C6; ÎŁâ&#x2C6;&#x2014; |S â&#x2021;&#x2019;â&#x2C6;&#x2014; w} Any language that can be generated by some context-free grammar is called a context-free language.
Example (G) G = ({S, A, B}, {0, 1}, R, S) R: S â&#x2020;&#x2019; A1B A â&#x2020;&#x2019; 0A | B â&#x2020;&#x2019; 0B | 1B |
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
Definition If G(V, ÎŁ, R, S) is a CFG, the language of G, denoted by L(G), is the set of terminal strings that have derivations from the start symbols. That is, L(G) = {w â&#x2C6;&#x2C6; ÎŁâ&#x2C6;&#x2014; |S â&#x2021;&#x2019;â&#x2C6;&#x2014; w} Any language that can be generated by some context-free grammar is called a context-free language.
Example (G) G = ({S, A, B}, {0, 1}, R, S) R: S â&#x2020;&#x2019; A1B A â&#x2020;&#x2019; 0A | B â&#x2020;&#x2019; 0B | 1B | L(G) is the language described by the regular expression 0â&#x2C6;&#x2014; 1(0 â&#x2C6;Ş 1)â&#x2C6;&#x2014; .
Context-Free Grammars
Introduction
Designing CFGs
Context-Free Grammars and Languages
Introduction Context-Free Grammars Formal Definition Derivations using a Grammar Context-Free Language Designing CFGs Ambiguous CFGs
Ambiguous CFGs
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
1. Certain CFLs contain strings with two substrings that are linked in the sense that a machine for such a language need to remember an unbounded amount of info about one of the substrings to verify that it corresponds properly to the other substring.
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
1. Certain CFLs contain strings with two substrings that are linked in the sense that a machine for such a language need to remember an unbounded amount of info about one of the substrings to verify that it corresponds properly to the other substring. {0n 1n | n â&#x2030;Ľ 0}
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
1. Certain CFLs contain strings with two substrings that are linked in the sense that a machine for such a language need to remember an unbounded amount of info about one of the substrings to verify that it corresponds properly to the other substring. {0n 1n | n â&#x2030;Ľ 0} 2. If you must construct a CFG for a CFL that you can break into simpler pieces, do so and then construct individual grammars for each piece.
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
1. Certain CFLs contain strings with two substrings that are linked in the sense that a machine for such a language need to remember an unbounded amount of info about one of the substrings to verify that it corresponds properly to the other substring. {0n 1n | n ≥ 0} 2. If you must construct a CFG for a CFL that you can break into simpler pieces, do so and then construct individual grammars for each piece. {0n 1n | n ≥ 0} ∪ {1n 0n | n ≥ 0}
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
1. Certain CFLs contain strings with two substrings that are linked in the sense that a machine for such a language need to remember an unbounded amount of info about one of the substrings to verify that it corresponds properly to the other substring. {0n 1n | n ≥ 0} 2. If you must construct a CFG for a CFL that you can break into simpler pieces, do so and then construct individual grammars for each piece. {0n 1n | n ≥ 0} ∪ {1n 0n | n ≥ 0} 3. To construct a CFG for a regular language, convert a DFA for that language into an equivalent CFG.
Introduction
Context-Free Grammars
Designing CFGs
â&#x20AC;˘ First construct a DFA for that language.
Ambiguous CFGs
Introduction
Context-Free Grammars
Designing CFGs
• First construct a DFA for that language. • Make a variable Ri for each state qi of the DFA.
Ambiguous CFGs
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
• First construct a DFA for that language. • Make a variable Ri for each state qi of the DFA. • Add the rule Ri → aRj to the CFG if δ(qi , a) = qj is a transition
in the DFA. Add the rule Ri → ε if qi is an accept state of the DFA.
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
• First construct a DFA for that language. • Make a variable Ri for each state qi of the DFA. • Add the rule Ri → aRj to the CFG if δ(qi , a) = qj is a transition
in the DFA. Add the rule Ri → ε if qi is an accept state of the DFA. • Make R0 the start variable of the grammar, where q0 is the start
state of the machine.
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
• First construct a DFA for that language. • Make a variable Ri for each state qi of the DFA. • Add the rule Ri → aRj to the CFG if δ(qi , a) = qj is a transition
in the DFA. Add the rule Ri → ε if qi is an accept state of the DFA. • Make R0 the start variable of the grammar, where q0 is the start
state of the machine. Design CFGs for the following languages: 1. {ai bj ck |i, j, k ≥ 0} 2. (0 ∪ 1)∗ 111
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
1. Certain CFLs contain strings with two substrings that are linked in the sense that a machine for such a language need to remember an unbounded amount of info about one of the substrings to verify that it corresponds properly to the other substring. {0n 1n | n ≥ 0} 2. If you must construct a CFG for a CFL that you can break into simpler pieces, do so and then construct individual grammars for each piece. {0n 1n | n ≥ 0} ∪ {1n 0n | n ≥ 0} 3. To construct a CFG for a regular language, convert a DFA for that language into an equivalent CFG. 4. The strings may contain certain structures that appear recursively as part of other (or the same) structures. To achieve this effect, place the variable symbol generating the structure in the location of the rules corresponding to where that structure may recursively appear.
Context-Free Grammars
Introduction
Designing CFGs
Context-Free Grammars and Languages
Introduction Context-Free Grammars Formal Definition Derivations using a Grammar Context-Free Language Designing CFGs Ambiguous CFGs
Ambiguous CFGs
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
Definition A string w is derived ambiguously in CFG G if it has two or more different leftmost derivations. Grammar G is ambiguous if it generates some string ambiguously.
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
Definition A string w is derived ambiguously in CFG G if it has two or more different leftmost derivations. Grammar G is ambiguous if it generates some string ambiguously. Note: B Sometimes when we have an ambiguous grammar we can find an unambiguous grammar that generates the same language.
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
Definition A string w is derived ambiguously in CFG G if it has two or more different leftmost derivations. Grammar G is ambiguous if it generates some string ambiguously. Note: B Sometimes when we have an ambiguous grammar we can find an unambiguous grammar that generates the same language. B Some CFLs, however, can be generated only by ambiguous grammars (inherently ambiguous).
Introduction
Context-Free Grammars
Designing CFGs
Example (A grammar for expressions) GE = ({E}, {+, ∗, (, ), a}, R, E) R: E → a | E + E | E ∗ E | (E)
Ambiguous CFGs
Introduction
Context-Free Grammars
Designing CFGs
Example (A grammar for expressions) GE = ({E}, {+, ∗, (, ), a}, R, E) R: E → a | E + E | E ∗ E | (E) Consider a + a ∗ a.
Ambiguous CFGs
Introduction
Context-Free Grammars
Designing CFGs
Example (A grammar for expressions) GE = ({E}, {+, ∗, (, ), a}, R, E) R: E → a | E + E | E ∗ E | (E) Consider a + a ∗ a. derivation?
Ambiguous CFGs
Introduction
Context-Free Grammars
Designing CFGs
Example (A grammar for expressions) GE = ({E}, {+, ∗, (, ), a}, R, E) R: E → a | E + E | E ∗ E | (E) Consider a + a ∗ a. derivation? parse tree?
Ambiguous CFGs
Introduction
Context-Free Grammars
Designing CFGs
Example (An unambiguous grammar for expressions) GEU = ({E, F, T }, {+, ∗, (, ), a}, R, E) R: E → T | E+T T → F | T ∗F F → a | (E)
Ambiguous CFGs
Introduction
Context-Free Grammars
Designing CFGs
Example (An unambiguous grammar for expressions) GEU = ({E, F, T }, {+, ∗, (, ), a}, R, E) R: E → T | E+T T → F | T ∗F F → a | (E) Parse tree of a + a ∗ a?
Ambiguous CFGs
Introduction
Context-Free Grammars
Designing CFGs
Ambiguous CFGs
Inherent Ambiguity
Example L = {an bn cm dm | n ≥ 1, m ≥ 1} ∪ {an bm cm dn | n ≥ 1, m ≥ 1}.
Context-Free Grammars
Introduction
Designing CFGs
Ambiguous CFGs
Inherent Ambiguity
Example L = {an bn cm dm | n ≥ 1, m ≥ 1} ∪ {an bm cm dn | n ≥ 1, m ≥ 1}. S→ A→ B→ C→ D→
AB | C aAb | ab cBd | cd aCd | aDd bDc | bc
Context-Free Grammars
Introduction
Designing CFGs
Ambiguous CFGs
Inherent Ambiguity
Example L = {an bn cm dm | n ≥ 1, m ≥ 1} ∪ {an bm cm dn | n ≥ 1, m ≥ 1}. S→ A→ B→ C→ D→
AB | C aAb | ab cBd | cd aCd | aDd bDc | bc
Consider aabbccdd.
Introduction
Context-Free Grammars
Designing CFGs
Questions? See you next meeting!
Ambiguous CFGs