1 / 17

Introduction to CS Theory

Introduction to CS Theory. Lecture 11 – Context-Free Grammars Piotr Faliszewski pf@cs.rit.edu. Definition A context-free grammar (CFG) is a quadruple G = (V, Σ , S, P), where V is a finite set of variables (non-terminal symbols) Σ is a finite set of terminal symbols

driskellj
Télécharger la présentation

Introduction to CS Theory

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to CS Theory Lecture 11 – Context-Free Grammars Piotr Faliszewski pf@cs.rit.edu

  2. Definition A context-free grammar (CFG) is a quadruple G = (V, Σ, S, P), where V is a finite set of variables (non-terminal symbols) Σ is a finite set of terminal symbols S  V is the start symbol P is a finite set of productions Each production is of the form A  α, whereA  V, α  {V  Σ}* V and Σ are disjoint Example G = (V, Σ, S, P) V = {S} Σ = {a,b} P = { S  aSb, S  ε} Context-Free Grammars

  3. Grammar G: S  ε S  aSb Intuitively, aaabbb  L(G) because we can transform S into aaabbb in a finite number of steps: S  aSb  aaSbb  aaaSbbb  aaabbb Derivations Notation. If x = α1Aα2 and grammar G containsa rule A  β then x Gα1βα2 Def. Let G be a context-free grammar. L(G) is the language {xΣ* | S*Gx } Notation. By x *Gy we mean that y can be derived from x by 0, 1, or more steps. Def. A language L is context-free if there is a CFG G s.t. L(G) = L.

  4. Consider the following languages (Σ = {a,b}) {x | x is a palindrome} {x | x has at least two b’s} {x | |x| is even} {x | x has as many a’s as b’s} Give context-free grammars for each of them. How to argue that your grammar is correct? Examples

  5. CFL = the set of context-free languages Closure properties of CFLs? Union? Concatenation? Kleene’s star? Complementation? Intersection? Closure properties Union S  S1 | S2 Concatenation S  S1S2 Kleene’s star S  S1S | ε What about intersection and union? Closure Properties of CFLs

  6. Simplified grammars of expressions S  S + S S  S * S S  (S) S  x Can you derive string:x + x * x Now… give all derivations! Derivation trees We can represent a derivation in a graphical form! Two derivations are the same if they yield the same derivation tree. We need to “normalize” our derivations Leftmost derivations: In each step we replace the leftmost variable 1-1 correspondence between leftmost derivations and derivation trees Derivation Trees

  7. Def. A CFG G is ambiguous if and only if there is a string x in L(G) s.t. x has two or more distinct leftmost derivations Ambiguity Usually inconvenient Sometimes inherent in the language Sometimes we can find an unambiguous version of the grammar Example Finding an unambiguous grammar for the language of expressions What should we get forx + x * x What should we get forx + x + x Ambiguity Disabled Fly to See Carter

  8. Dangling else problem Many programming languages have the following construct in their grammar: <statement>  if ( <expr> ) <statement> |if ( <expr> ) <statement> else <statement> This grammar is amiguous! How do programming languages deal with this issue? Can we fix the grammar? Ambiguity: Dangling Else

  9. Not every CFL is regular aibi But, every regular language is context free. How to show this? Convert an FA to a grammar? Convert a regular expression to a grammar? Somehow else? Example: (011+1)*(01)* CFLs Versus Regular Languages

  10. Can we define an automaton (a variant of NFA-ε) for CFLs? What extra properties do we need? Extra memory! A counter? Ability to read the input back and forth? Machines for CFLs?

  11. A machine to recognize context-free languages An NFA-ε + a stack Called: pushdown automaton (PDA) A move of a PDA depends on: Current state The next input symbol The symbol on the top of the stack A move consists of: Changing the state Replacing the symbol on the top of the stack by 0 or more symbols What if the stack is empty? The machine is stuck! Start condition: Special start symbol on the stack (stack bottom) Acceptance condition A PDA accepts a string x if there is a way to process the whole string without getting stuck, and finishing in an accepting state Pushdown Automata

  12. Consider the followign languages: L(G), where G: S  (S) | SS | ε {x | x is a palindrome} A move of a PDA depends on: Current state The next input symbol The symbol on the top of the stack A move consists of: Changing the state Replacing the symbol on the top of the stack by 0 or more symbols Examples

  13. A PDA is a septuple (Q, Σ, Γ, q0, Z0, A, δ): Q – is a finite set of states Σ – is the input alphabet Γ – is the stack alphabet q0 – is the start state Z0 – is the initial stack symbol, Z0 Γ A – set of accepting states, A  Q δ – is the transition function How to define the transition function: δ: Q(Σ {ε})Γ QΓ* Input: Current state Viewed symbol (or ε) Top symbol on the stack Output: New state Symbols to replace the current stack top (if ε then we pop a single element from the stack) Formal Definition

  14. Goal: PDA for the language{aibi | i ≥ 0 } But before… How to describe a configuration of a PDA? (so we can trace a PDA’s execution) (q, x, α) – a configuration q – current state x – unprocessed input α – contents of the stack (written right to left) A single step of a PDA (p, x, α) (q, y, β) We write (p, x, α)  (q, y, β) if the PDA can go from the left one to the right one in one step How to formally define acceptance by a PDA? Give a PDA for our goal language! Example

  15. Deterministic PDAs • Determinism in PDAs • By default: Nondeterministic • We can get deterministic machines! • What does it mean for a PDA to be deterministic? • Give a DPDA (deterministic PDA) for our goal language • Show that L(G), for G: • S  SS | (S) | ε is in DCFL (class of CFL languages accepted by DPDAs)

  16. Equivalence of PDAs and CFLs • Theorem • Let G be a context-free grammar. • Then there is a PDA M such that L(M) = L(G) • Theorem • Let M be a PDA. Then there is a context-free grammar G such that L(G) = L(M) • Comments: • PDA  CFG – seems hard • CFG  PDA – quite intuitive! Try it!

  17. Example • Convert G: • S  S + T | T • T  T + F | F • F  (S) | a to a PDA!

More Related