Context-Free Grammars

Relaterede dokumenter
Skriftlig Eksamen Beregnelighed (DM517)

Skriftlig Eksamen Beregnelighed (DM517)

Strings and Sets: set complement, union, intersection, etc. set concatenation AB, power of set A n, A, A +

The X Factor. Målgruppe. Læringsmål. Introduktion til læreren klasse & ungdomsuddannelser Engelskundervisningen

Skriftlig Eksamen Automatteori og Beregnelighed (DM17)

Vina Nguyen HSSP July 13, 2008

Project Step 7. Behavioral modeling of a dual ported register set. 1/8/ L11 Project Step 5 Copyright Joanne DeGroat, ECE, OSU 1

Engelsk. Niveau D. De Merkantile Erhvervsuddannelser September Casebaseret eksamen. og

Basic statistics for experimental medical researchers

Trolling Master Bornholm 2015

Skriftlig Eksamen Kombinatorik, Sandsynlighed og Randomiserede Algoritmer (DM528)

Tema: Pets Fag: Engelsk Målgruppe: 4. klasse Titel: Me and my pet Vejledning Lærer

Observation Processes:

DET KONGELIGE BIBLIOTEK NATIONALBIBLIOTEK OG KØBENHAVNS UNIVERSITETS- BIBLIOTEK. Index

Vores mange brugere på musskema.dk er rigtig gode til at komme med kvalificerede ønsker og behov.

Engelsk. Niveau C. De Merkantile Erhvervsuddannelser September Casebaseret eksamen. og

Linear Programming ١ C H A P T E R 2

Brug sømbrættet til at lave sjove figurer. Lav fx: Få de andre til at gætte, hvad du har lavet. Use the nail board to make funny shapes.

Generelt om faget: - Hvordan vurderer du dit samlede udbytte af dette fag?

Titel: Barry s Bespoke Bakery

GUIDE TIL BREVSKRIVNING

IBM Network Station Manager. esuite 1.5 / NSM Integration. IBM Network Computer Division. tdc - 02/08/99 lotusnsm.prz Page 1

Portal Registration. Check Junk Mail for activation . 1 Click the hyperlink to take you back to the portal to confirm your registration

Bilag. Resume. Side 1 af 12

Trolling Master Bornholm 2013

Det er muligt at chekce følgende opg. i CodeJudge: og

Trolling Master Bornholm 2014

how to save excel as pdf

LESSON NOTES Extensive Reading in Danish for Intermediate Learners #8 How to Interview

Titel: Hungry - Fedtbjerget

Danish Language Course for International University Students Copenhagen, 12 July 1 August Application form

Subject to terms and conditions. WEEK Type Price EUR WEEK Type Price EUR WEEK Type Price EUR WEEK Type Price EUR

Trolling Master Bornholm 2016 Nyhedsbrev nr. 5

1 What is the connection between Lee Harvey Oswald and Russia? Write down three facts from his file.

Kurver og flader Aktivitet 15 Geodætiske kurver, Isometri, Mainardi-Codazzi, Teorema Egregium

University of Copenhagen Faculty of Science Written Exam - 3. April Algebra 3

Privat-, statslig- eller regional institution m.v. Andet Added Bekaempelsesudfoerende: string No Label: Bekæmpelsesudførende

Barnets navn: Børnehave: Kommune: Barnets modersmål (kan være mere end et)

How Long Is an Hour? Family Note HOME LINK 8 2

Travel General. General - Essentials. General - Conversation. Asking for help. Asking if a person speaks English

Travel General. General - Essentials. General - Conversation. Asking for help. Asking if a person speaks English

Nyhedsmail, december 2013 (scroll down for English version)

Trolling Master Bornholm 2016 Nyhedsbrev nr. 8

Aktivering af Survey funktionalitet

The River Underground, Additional Work

Trolling Master Bornholm 2013

Danish Language Course for Foreign University Students Copenhagen, 13 July 2 August 2016 Advanced, medium and beginner s level.

Trolling Master Bornholm 2016 Nyhedsbrev nr. 7

Financial Literacy among 5-7 years old children

Læs vejledningen godt igennem, før du begynder at samle vuggen. Please read the instruction carefully before you start.

Boligsøgning / Search for accommodation!

Åbenrå Orienteringsklub

United Nations Secretariat Procurement Division

Trolling Master Bornholm 2014?

On the complexity of drawing trees nicely: corrigendum

Black Jack --- Review. Spring 2012

PARALLELIZATION OF ATTILA SIMULATOR WITH OPENMP MIGUEL ÁNGEL MARTÍNEZ DEL AMOR MINIPROJECT OF TDT24 NTNU

Trolling Master Bornholm 2016 Nyhedsbrev nr. 3

CHAPTER 8: USING OBJECTS

Trolling Master Bornholm 2015

Asking whether there are commission fees when you withdraw money in a certain country

Asking whether there are commission fees when you withdraw money in a certain country

Trolling Master Bornholm 2014

Skriftlig Eksamen Diskret matematik med anvendelser (DM72)

Trolling Master Bornholm 2014

Name: Week of April 22 MathWorksheets.com

Generalized Probit Model in Design of Dose Finding Experiments. Yuehui Wu Valerii V. Fedorov RSU, GlaxoSmithKline, US

To the reader: Information regarding this document

Trolling Master Bornholm 2014

Shooting tethered med Canon EOS-D i Capture One Pro. Shooting tethered i Capture One Pro 6.4 & 7.0 på MAC OS-X & 10.8

Help / Hjælp

November hilsner fra NORDJYSKE Medier, Distributionen

Appendix 1: Interview guide Maria og Kristian Lundgaard-Karlshøj, Ausumgaard

About the course: (Please comment to your right) - To which degree has the course facilitated learning the skills outlined in the academic aims?

Trolling Master Bornholm 2016 Nyhedsbrev nr. 6

DK - Quick Text Translation. HEYYER Net Promoter System Magento extension

Trolling Master Bornholm 2013

Sign variation, the Grassmannian, and total positivity

Appendix A. correspondence with Fyrvaerkeri.dk:

Userguide. NN Markedsdata. for. Microsoft Dynamics CRM v. 1.0

University of Copenhagen Faculty of Science Written Exam April Algebra 3

Curve Modeling B-Spline Curves. Dr. S.M. Malaek. Assistant: M. Younesi

Bookingmuligheder for professionelle brugere i Dansehallerne

Trolling Master Bornholm 2012

VEDLIGEHOLDELSE AF SENGE

1 s01 - Jeg har generelt været tilfreds med praktikopholdet

Our activities. Dry sales market. The assortment

Statistik for MPH: 7

The complete construction for copying a segment, AB, is shown above. Describe each stage of the process.

Status på det trådløse netværk

Nyhedsmail, november 2013 (scroll down for English version)

Kalkulation: Hvordan fungerer tal? Jan Mouritsen, professor Institut for Produktion og Erhvervsøkonomi

Terese B. Thomsen 1.semester Formidling, projektarbejde og webdesign ITU DMD d. 02/

Timetable will be aviable after sep. 5. when the sing up ends. Provicius timetable on the next sites.

Rejse Logi. Logi - Resultat. Logi - Booking. At spørge efter vej til et logi. ... et værelse som man kan leje?... a room to rent?

Hvor er mine runde hjørner?

Nyhedsmail, april 2014 (scroll down for English version)

INSTALLATION INSTRUCTIONS STILLEN FRONT BRAKE COOLING DUCTS NISSAN 370Z P/N /308960!

Richter 2013 Presentation Mentor: Professor Evans Philosophy Department Taylor Henderson May 31, 2013

Choosing a Medicare prescription drug plan.

Transkript:

Context-Free Grammars

Describing Languages We've seen two models for the regular languages: Automata accept precisely the strings in the language. Regular expressions describe precisely the strings in the language. Finite automata recognize strings in the language. Perform a computation to determine whether a specific string is in the language. Regular expressions match strings in the language. Describe the general shape of all strings in the language.

Context-Free Grammars A context-free grammar (or CFG) is an entirely different formalism for defining a class of languages. Goal: Give a procedure for listing off all strings in the language. CFGs are best explained by example...

Arithmetic Expressions Suppose we want to describe all legal arithmetic expressions using addition, subtraction, multiplication, and division. Here is one possible CFG: E int E E Op E E (E) Op + Op - Op * Op / E E Op E E Op (E) E Op (E Op E) E * (E Op E) int * (E Op E) int * (int Op E) int * (int Op int) int * (int + int)

Arithmetic Expressions Suppose we want to describe all legal arithmetic expressions using addition, subtraction, multiplication, and division. Here is one possible CFG: E int E E Op E E (E) Op + Op - Op * Op / E E Op E E Op int int Op int int / int

Context-Free Grammars Formally, a context-free grammar is a collection of four objects: A set of nonterminal symbols (also called variables), A set of terminal symbols (the alphabet of the CFG) A set of production rules saying how each nonterminal can be converted by a string of terminals and nonterminals, and A start symbol (which must be a nonterminal) that begins the derivation. E int E E Op E E (E) Op + Op - Op * Op /

Some CFG Notation Capital letters in Bold Red Uppercase will represent nonterminals. i.e. A, B, C, D Lowercase letters in blue monospace will represent terminals. i.e. t, u, v, w Lowercase Greek letters in gray italics will represent arbitrary strings of terminals and nonterminals. i.e. α, γ, ω

A Notational Shorthand E int E E Op E E (E) Op + Op - Op * Op /

A Notational Shorthand E int E Op E (E) Op + - * /

Derivations E E Op E int (E) Op + * - / E E Op E E Op (E) E Op (E Op E) E * (E Op E) int * (E Op E) int * (int Op E) int * (int Op int) int * (int + int) A sequence of steps where nonterminals are replaced by the right-hand side of a production is called a derivation. If string α derives string ω, we write α * ω. In the example on the left, we see E * int * (int + int).

The Language of a Grammar If G is a CFG with alphabet Σ and start symbol S, then the language of G is the set L(G) = { ω Σ* S * ω } That is, L( G) is the set of strings derivable from the start symbol. Note: ω must be in Σ*, the set of strings made from terminals. Strings involving nonterminals aren't in the language.

More Context-Free Grammars Chemicals! C 19 H 14 O 5 S Cu 3 (CO 3 ) 2 (OH) 2 - MnO 4 S 2- Form Cmp Cmp Ion Cmp Term Term Num Cmp Cmp Term Elem (Cmp) Elem H He Li Be B C Ion + - IonNum + IonNum - IonNum 2 3 4... Num 1 IonNum

CFGs for Chemistry Form Cmp Cmp Ion Cmp Term Term Num Cmp Cmp Term Elem (Cmp) Elem H He Li Be B C Ion + - IonNum + IonNum - IonNum 2 3 4... Num 1 IonNum Form Cmp Ion Cmp Cmp Ion Cmp Term Num Ion Term Term Num Ion Elem Term Num Ion Mn Term Num Ion Mn Elem Num Ion MnO Num Ion MnO IonNum Ion MnO 4 Ion MnO - 4

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR...

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK BLOCK EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR...

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK { } STMTS EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR...

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK { } STMT STMTS EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR...

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK { } EXPR; STMTS EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR...

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK { } EXPR = EXPR; STMTS EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR...

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK { } var = EXPR; STMTS EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR...

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK { } var = EXPR * EXPR; STMTS EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR...

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK { } var = var * EXPR; STMTS EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR...

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK { } var = var * var; STMTS EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR...

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK { } var = var * var; STMT STMTS EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR...

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK { } var = var * var; if (var) BLOCK STMTS EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR...

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK { } var = var * var; if (var) STMT STMTS EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR...

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK { } var = var * var; if (var) EXPR; STMTS EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR...

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK { } var = var * var; if (var) EXPR = EXPR; STMTS EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR...

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK { } var = var * var; if (var) var = EXPR; STMTS EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR...

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK { } var = var * var; if (var) var = const; STMTS EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR...

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR... { } var = var * var; if (var) var = const; STMT STMTS

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR... { } var = var * var; if (var) var = const; while (var) BLOCK STMTS

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR... { } var = var * var; if (var) var = const; while (var) { STMTS } STMTS

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR... { } var = var * var; if (var) var = const; while (var) { STMT STMTS } STMTS

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR... { } var = var * var; if (var) var = const; while (var) { EXPR; STMTS } STMTS

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR... { } var = var * var; if (var) var = const; while (var) { EXPR = EXPR; STMTS } STMTS

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR... { } var = var * var; if (var) var = const; while (var) { var = EXPR; STMTS } STMTS

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR... { } var = var * var; if (var) var = const; while (var) { var = EXPR + EXPR; STMTS } STMTS

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR... { } var = var * var; if (var) var = const; while (var) { var = var + EXPR; STMTS } STMTS

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR... { } var = var * var; if (var) var = const; while (var) { var = var + const; STMTS } STMTS

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR... { } var = var * var; if (var) var = const; while (var) { var = var + const; } STMTS

CFGs for Programming Languages BLOCK STMT { STMTS } STMTS ε STMT STMTS STMT EXPR; if (EXPR) BLOCK while (EXPR) BLOCK do BLOCK while (EXPR); BLOCK EXPR var const EXPR + EXPR EXPR EXPR EXPR = EXPR... { } var = var * var; if (var) var = const; while (var) { var = var + const; }

Context-Free Languages A language L is called a context-free language (or CFL) if there is a CFG G such that L = L( G). Questions: What languages are context-free? How are context-free and regular languages related?

From Regexes to CFGs CFGs don't have the Kleene star, parenthesized expressions, or internal operators. However, we can convert regular expressions to CFGs as follows: S a*b

From Regexes to CFGs CFGs don't have the Kleene star, parenthesized expressions, or internal operators. However, we can convert regular expressions to CFGs as follows: S Ab

From Regexes to CFGs CFGs don't have the Kleene star, parenthesized expressions, or internal operators. However, we can convert regular expressions to CFGs as follows: S Ab A Aa ε

From Regexes to CFGs CFGs don't have the Kleene star, parenthesized expressions, or internal operators. However, we can convert regular expressions to CFGs as follows: S a(b c*)

From Regexes to CFGs CFGs don't have the Kleene star, parenthesized expressions, or internal operators. However, we can convert regular expressions to CFGs as follows: S ax X (b c*)

From Regexes to CFGs CFGs don't have the Kleene star, parenthesized expressions, or internal operators. However, we can convert regular expressions to CFGs as follows: S ax X b c*

From Regexes to CFGs CFGs don't have the Kleene star, parenthesized expressions, or internal operators. However, we can convert regular expressions to CFGs as follows: S ax X b C

From Regexes to CFGs CFGs don't have the Kleene star, parenthesized expressions, or internal operators. However, we can convert regular expressions to CFGs as follows: S ax X b C C Cc ε

Regular Languages and CFLs Theorem: Every regular language is context-free. Proof Idea: Use the construction from the previous slides to convert a regular expression for L into a CFG for L.

The Language of a Grammar Consider the following CFG G: S asb ε What strings can this generate?

The Language of a Grammar Consider the following CFG G: S asb ε What strings can this generate? S

The Language of a Grammar Consider the following CFG G: S asb ε What strings can this generate? a S b

The Language of a Grammar Consider the following CFG G: S asb ε What strings can this generate? a S b

The Language of a Grammar Consider the following CFG G: S asb ε What strings can this generate? a a S b b

The Language of a Grammar Consider the following CFG G: S asb ε What strings can this generate? a a S b b

The Language of a Grammar Consider the following CFG G: S asb ε What strings can this generate? a a a S b b b

The Language of a Grammar Consider the following CFG G: S asb ε What strings can this generate? a a a S b b b

The Language of a Grammar Consider the following CFG G: S asb ε What strings can this generate? a a a a S b b b b

The Language of a Grammar Consider the following CFG G: S asb ε What strings can this generate? a a a a b b b b

The Language of a Grammar Consider the following CFG G: S asb ε What strings can this generate? a a a a b b b b

The Language of a Grammar Consider the following CFG G: S asb ε What strings can this generate? a a a a b b b b L(G) = { a n b n n N }

All Languages Regular Languages CFLs

Time-Out for Announcements!

Problem Set Six Problem Set Six goes out right now, is due next Monday at 2:15PM. Explore context-free languages and the limits of regular languages! Problem Set Five was due at the start of class; is due on Wednesday at 2:15PM sharp with a late day.

Extra Practice Problems Based on your feedback, I've assembled a set of eight extra practice problems on the material so far. Available on the course website; solutions will go out later this week.

Midterm Regrades All midterm regrades were due at the start of today's lecture. Brought a regrade to class? Feel free to hand it in to Keith right after class today. That's okay with us. SCPD students deadline is this Wednesday at the start of class.

Asking Questions We're getting a lot of great questions through the staff list and Piazza please keep asking them! If you have Piazza questions that don't give away key insights, consider making them public. There are a lot of great private questions right now! Please avoid asking questions after 10:00PM the night before a problem set comes due we will tend not to answer questions asked after this point.

Second Midterm Exam The second midterm exam is next Thursday, November 13 from 7PM 10PM. Covers material up through and including topics from PS6; focus is on topics from PS4 PS6. Same format as last time: closed-book, closedcomputer, open one page of notes. Location information TBA. Practice midterm next Monday night, November 10, from 7PM 10PM, location TBA. Need to take the exam at an alternate time? Email Maesen ASAP to set up an alternate exam time.

Your Questions!

It has been over a year since I took a CS class. I want to do CS107, but I am worried that my coding is too rusty. What are some good ways to brush up on coding over winter break?

Why is HTML not a regular language? I have always heard not to parse HTML with regular expressions because it is not a regular language, but why is this?

Do you have a dog?

Back to CS103!

Designing CFGs Like designing DFAs, NFAs, and regular expressions, designing CFGs is a craft. When thinking about CFGs: Think recursively: Build up bigger structures from smaller ones. Have a construction plan: Know in what order you will build up the string.

Designing CFGs Let Σ = {a, b} and let L = {w Σ* w is a palindrome } We can design a CFG for L by thinking inductively: Base case: ε, a, and b are palindromes. If ω is a palindrome, then aωa and bωb are palindromes. S ε a b asa bsb

Designing CFGs Let Σ = {(, )} and let L = {w Σ* w is a string of balanced parentheses } We can think about how we will build strings in this language as follows: The empty string is balanced. Any two strings of balanced parentheses can be concatenated. Any string of balanced parentheses can be parenthesized. S SS (S) ε

Designing CFGs: Watch Out! When designing CFGs, remember that each nonterminal can be expanded out independently of the others. Let Σ = {a, } and let L = {an a n n N }. Is the following a CFG for L? S X X X ax ε S X X ax X aax X aa X aa ax aa a

Finding a Build Order Let Σ = {a, } and let L = {an a n n N }. To build a CFG for L, we need to be more clever with how we construct the string. Idea: Build from the ends inward. Gives this grammar: S asa S asa aasaa aaasaaa aaa aaa

Designing CFGs: A Caveat Let Σ = {l, r} and let L = {w Σ* w has the same number of l's and r's } Is this a grammar for L? S lsr rsl ε Can you derive the string lrrl?

Designing CFGs: A Caveat When designing a CFG for a language, make sure that it generates all the strings in the language and never generates a string outside the language. The first of these can be tricky make sure to test your grammars! You'll design your own CFG for this language on the next problem set.

CFG Caveats II Is the following grammar a CFG for the language { a n b n n N }? S asb What strings can you derive? Answer: None! What is the language of the grammar? Answer: Ø When designing CFGs, make sure your recursion actually terminates!

Function Prototypes Let Σ = {void, int, double, name, (, ),,, ;}. Let's write a CFG for C-style function prototypes! Examples: void name(int name, double name); int name(); int name(double name); int name(int, int name, int); void name(void);

Function Prototypes Here's one possible grammar: S Ret name (Args); Ret Type void Type int double Args ε void ArgList ArgList OneArg ArgList, OneArg OneArg Type Type name

Summary of CFG Design Look for recursive structures where they exist it can help guide you toward a solution. Keep the build order in mind often, you'll build two totally different parts of the string concurrently. Use different nonterminals to represent different structures.

Next Time Turing Machines What does a computer with unbounded memory look like? How do you program them?