CONTEXTFREE GRAMMARS COP 4620 Programming Language Translators Dr

  • Slides: 20
Download presentation
CONTEXT-FREE GRAMMARS COP 4620 – Programming Language Translators Dr. Manuel E. Bermudez

CONTEXT-FREE GRAMMARS COP 4620 – Programming Language Translators Dr. Manuel E. Bermudez

TOPICS Context-free Grammars Derivation Trees Transduction Grammars Ambiguity Grammar Reduction.

TOPICS Context-free Grammars Derivation Trees Transduction Grammars Ambiguity Grammar Reduction.

CONTEXT-FREE GRAMMARS • Definition: A context-free grammar (CFG) is a quadruple G = (

CONTEXT-FREE GRAMMARS • Definition: A context-free grammar (CFG) is a quadruple G = ( , , P, S), where all productions are of the form A → , for A ∊ and ∊ ( ∪ )*. • Re-writing using grammar rules: – βAγ ⇒ β γ if A → (derivation). • Left-most derivation: At each step, the left-most nonterminal is re-written. • Right-most derivation: At each step, the right-most nonterminal is re-written.

SAMPLE GRAMMAR AND DERIVATIONS

SAMPLE GRAMMAR AND DERIVATIONS

DERIVATION TREES • Describe re-writes, independently of the order (left -most or right-most). •

DERIVATION TREES • Describe re-writes, independently of the order (left -most or right-most). • Each tree branch matches a production rule in the grammar. • Leaves are terminals. • Bottom contour is the sentence. • Left recursion causes left branching. • Right recursion causes right branching.

TRANSDUCTION GRAMMARS Definition: A transduction grammar (a. k. a. syntax-directed translation scheme) is like

TRANSDUCTION GRAMMARS Definition: A transduction grammar (a. k. a. syntax-directed translation scheme) is like a CFG, except for the following generalization: Each production is a triple (A, β, ω) Ф x V*, called a translation rule, denoted A → β => ω, where A is the left part, β is the right part, and ω is the translation part.

TRANSDUCTION GRAMMARS Translation of infix to postfix expressions. E→E+T →T T→P*T => E T

TRANSDUCTION GRAMMARS Translation of infix to postfix expressions. E→E+T →T T→P*T => E T + => T => P T * →P => P P → (E) => E →i => i The translation part describes how the output is generated, as the input is derived. Derivation: ( E, E) => ( E + T, E T + ) => ( T + T, TT+) => ( P + T, PT+) => ( i + T, i. T+) => ( i + P * T, i P T * + ) => ( i + i * T, i i T * + ) => ( i + i * i, i i i * + )

TRANSDUCTION GRAMMARS Transduction to Abstract Syntax Trees N Notation: < N t 1 …

TRANSDUCTION GRAMMARS Transduction to Abstract Syntax Trees N Notation: < N t 1 … tn > denotes t … t 1 n String-to-tree transduction grammar: E→E+T →T T→P*T => < + E T > => T Derivation: (E, E) => (E+T, => (T+T, => (P+T, => (i+P*T, => (i+i*P, => (i+i*i, => < * P T > →P => P P → (E) => E →i => i <+E T>) <+T T>) <+P T>) <+i<*P T>>) <+i<*i P>>) <+i<*i i>>) + i * i i

TRANSDUCTION GRAMMARS Definition: A transduction grammar is simple if for every rule A →

TRANSDUCTION GRAMMARS Definition: A transduction grammar is simple if for every rule A → => β, the sequence of nonterminals in is identical to the sequence in β. E → E + T => < + E T > →T T→P*T => < * P T > →P => P P → (E) => E →i => i Notation: dispense with nonterminals and tree notation: in the translation parts, leaving: E → E + T => + →T T → P * T => * →P P → (E) →i => i Look familiar ? AST !!

GRAMMAR AMBIGUITY • Goal of parsing: – Examine input string, determine whether it's legal.

GRAMMAR AMBIGUITY • Goal of parsing: – Examine input string, determine whether it's legal. – Same as: try to build derivation tree. – Therefore, tree should be unique. • Definition: A CFG is ambiguous if there exist two different right-most (or left-most, but not both) derivations for some sentence z. • (Equivalent) Definition: A CFG is ambiguous if there exist two different derivation trees for some sentence z.

CLASSIC AMBIGUITIES • Simultaneous left/right recursion: E→ E+E → i • Dangling else problem:

CLASSIC AMBIGUITIES • Simultaneous left/right recursion: E→ E+E → i • Dangling else problem: S → if E then S else S → … Ambiguity is undecidable: no algorithm exists.

GRAMMAR REDUCTION What language does this grammar generate? S→a D → EDBC A →

GRAMMAR REDUCTION What language does this grammar generate? S→a D → EDBC A → BCDEF E → CBA B → ASDFA F→S C → DDCF L(G) = {a} Problem: Many nonterminals (and productions) cannot be used in the generation of any sentence.

GRAMMAR REDUCTION Definition: A CFG is reduced iff for all A Ф, a) b)

GRAMMAR REDUCTION Definition: A CFG is reduced iff for all A Ф, a) b) S =>* αAβ, for some α, β V*, (we say A is generable), A =>* z, for some z Σ* (we say A is terminable). G is reduced iff every nonterminal A is both generable and terminable. Example: S → BB B → b. B A → a. A →a B is not terminable, since B =>* z, for any z Σ*. A is not generable, since S =>* αAβ, for any α, β V*.

GRAMMAR REDUCTION To find out which nonterminals are generable: 1. Build the graph (Ф,

GRAMMAR REDUCTION To find out which nonterminals are generable: 1. Build the graph (Ф, δ), where (A, B) δ iff A → αBβ is a production. 2. Check that all nodes are reachable from S. Example: S → BB B → b. B A → a. A →a A is not reachable from S, so A is not generable. S B A

GRAMMAR REDUCTION Algorithmically, Generable : = {S} while(Generable changes) do for each A →

GRAMMAR REDUCTION Algorithmically, Generable : = {S} while(Generable changes) do for each A → Bβ do if A Generable then Generable : = Generable U {B} od { Now, Generable contains the nonterminals that are generable }

GRAMMAR REDUCTION To find out which nonterminals are terminable: 1. Build the graph (2

GRAMMAR REDUCTION To find out which nonterminals are terminable: 1. Build the graph (2 Ф, δ), where (N, N U {A}) δ iff A → X 1 … Xn is a production, and for all i, Xi Σ or Xi N. 2. Check that the node Ф (set of all nonterminals) is reachable from node ø (empty set). ø {B} {A, B} Example: S → BB A → a. A B → b. B →a {A} {A, S, B} not reachable from ø ! Only {A} is reachable from ø. Thus S and B are not terminable. {A, S} {A, S, B} {B, S}

GRAMMAR REDUCTION Algorithmically, Terminable : = { }; while (Terminable changes) do for each

GRAMMAR REDUCTION Algorithmically, Terminable : = { }; while (Terminable changes) do for each A → X 1…Xn do if every nonterminal among the X’s is in Terminable then Terminable : = Terminable U {A} od { Now, Terminable contains the nonterminals that are terminable. }

GRAMMAR REDUCTION Reducing a grammar: 1. Find all terminable nonterminals. 2. Remove any production

GRAMMAR REDUCTION Reducing a grammar: 1. Find all terminable nonterminals. 2. Remove any production A→X 1…Xn if any Xi is not terminable. 3. Find all generable nonterminals. 4. Remove any production A → X 1 … Xn if A is not generable.

GRAMMAR REDUCTION Example: E→E+T →T T→F*T →P F → not F Q→P/Q P →

GRAMMAR REDUCTION Example: E→E+T →T T→F*T →P F → not F Q→P/Q P → (E) →i Terminable: {P, T, E}, not Terminable: {F, Q}. So, eliminate every production whose right-part contains either F or Q. E→E+T →T T→P P → (E) →i Generable: {E, T, P}, not Generable: { }. Grammar is reduced.

SUMMARY Context-free Grammars Derivation Trees Transduction Grammars Ambiguity Grammar Reduction.

SUMMARY Context-free Grammars Derivation Trees Transduction Grammars Ambiguity Grammar Reduction.