module5-print

module5-print - Module 5 Context-free grammars and...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Module 5 Context-free grammars and languages Making a computer that can’t think, but has infinite memory CS 360: Introduction to the Theory of Computing Daniel G. Brown, University of Waterloo 5.1 Topics for this module Context-free grammars and languages Parsing words in CFGs Ambiguity in CFGs CFGs and CFLs are the second major topic of this course. They have applications to parsing, primar- ily, but actually pop up in bioinformatics quite a bit. They also have had applications in (essentially) political theory. Yes, really. 5.2 1 Context-free grammars and languages Context-free grammars: preliminaries Context-free grammars: More complicated structure than regular expressions Can encode more interesting languages Still constrained in how one part of the input can relate to another part (essentially, it’s “context- free”: here, the name of language class means something important) We will have an automaton for CFGs, called pushdown automata , which we’ll talk about in Module 6. 5.3 Context-free grammars: the idea Basic idea: Construct words in language by applying rules to symbols. Finish when all of the symbols in the word are fleshed out. Hard to explain without an example. Example: S subject verb object S subject verb subject “billy” or “john” or “bob” verb “wants” or “broke” object “the toy” or “a dog” Here, sentences have forms like: “billy wants a dog” or “john broke” 5.4 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
A more typical example Another example (with the form we’ll use): S AB A 0 A | ε B B 1 | ε Here, S generates AB , and then we produce a word with A , and follow with a word generated by B . The word from A is from 0 * , and the word generated from B is from 1 * . Words in this CFG’s language: ε , 0000001111 ,... . In fact, its language is 0 * 1 * . 5.5 Formal definition of a CFG Let’s give a fully formal definition. A context-free grammar (CFG) is a 4-tuple: G = ( V , T , P , S ) : V = finite set of variables Usually capital letters. T = finite alphabet, called terminals . Disjoint from V . T is the alphabet for the CFG’s language . P = finite set of productions : definitions for the variables. Productions are of the form: A α Here, A is a variable, and α is a string of symbols from V and T . Notation : If we have A α and A β , we will write this as A α | β S = Start variable : the symbol from V where the derivation of a word begins. 5.6 Formalizing the previous example Our example from before, which generated 0 * 1 * : G = ( V , T , P , S ) , where V = { A , B , S } T = { 0 , 1 } P = { S AB , A 0 A | ε , B B 1 | ε } S = S Now, need to define what a word being in the CFG’s language is. Derivations : produce a string in T * by applying rules in P to strings we get from S .
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/14/2012 for the course CS 360 taught by Professor Johnwatrous during the Winter '08 term at Waterloo.

Page1 / 16

module5-print - Module 5 Context-free grammars and...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online