2.1 Context-Free Grammars What's a Context-Free Grammar? S -> aSb S -> SS S -> X X -> c What'a a more compact notation for a grammar? S -> aSb | SS | X X -> c Grammar Terminology What's a Rule or Production? What's a Variable or Nonterminal? What's a Terminal? What's the Start Symbol or Start Variable or Start Nonterminal? Derivation How do you use a grammar to produce strings of symbols? What's a Derivation? 1. write down the start nonterminal 2. repeatedly apply productions until only terminals remain How do you apply a single production? What symbol is used to show a single production application? find a variable that matches left of production replace variable with right of production Derive aabaa using the grammar. Derive bbabbbb using the grammar. Derive ab using the grammar. S -> X | Y X -> b | a X a Y -> a | b Y bb Classwork (You may work with a partner.) Derive aabbb using the grammar. S -> a A A -> a A | b B B -> b B | b What's the Language of a Grammar? What's a Context-Free Language? How do Context-Free Languages compare to Regular Languages? How do Context-Free Grammars compare to DFAs, NFAs, and Regular Expressions? CFLs are a larger class than RLs CFGs are more powerful than DFA, NFA, regular expression What are some applications of Context-Free Grammars? specify/parse programming languages study human languages (noun, verb, preposition) Designing Context-Free Grammars How do you design a Context-Free Grammar? there is no cookbook algorithm use union of simpler CFGs use recursive productions Design a grammar that generates the language. (w is a string of 0's and 1's) { w | the number of 0s in w is equal to the number of 1s } Classwork (You may work with a partner.) Design a grammar that generates the language. (w is a string of 0's and 1's) { w | the length of w is odd and the first symbol is the same as the middle symbol } Formal Definition What's a Formal Definition of a Context-Free Grammar? a context-free grammar is a 4-tuple (V, Z, R, S) 1. V is a finite set of Variables 2. Z is a finite set of Terminals (Z should be Sigma) 3. R is a finite set of Rules rule is a variable and a string of variables and terminals 4. S is a member of V and is the start symbol Write the grammar in formal notation. S -> aSb S -> SS S -> X X -> c How do you write a grammar informally? How do you know which symbols are variables? How do you know which symbols are terminals? How do you know which symbol is the start symbol?