Homework and Exams
Homework and Exams
• Homework
Context Free Languages – Return Homework #2
– Homework #3 Due today
Grammars
1
Before We Start Plan for today
• Any questions? • Context Free Languages
– Next class of languages in our quest!
2
Plan for today Introduction
• Define and describe context free grammars • Grammars
– Think back to your days of learning English
– Rules for constructing a valid sentence
• Questions? • Sentence = noun phrase + verb phrase
• Noun phrase =
– Name (Joe)
– Article + noun (the car)
• Verb Phrase =
– Verb (runs)
– Verb + prepositional phrase
Introduction Introduction
• Grammars • Look at the sentence. Is this grammatically
– Rules for constructing a simple sentence correct?
• Sentence = noun phrase + verb phrase – Joe runs from the car.
• Noun phrase =
– Name (Joe) – Sentence = noun phrase + verb phrase
– Article + noun (the car) – = noun + verb phrase
• Verb Phrase = – = Name + verb phrase
– Verb (runs) – = Joe + verb phrase
– Verb + prepositional phrase – = Joe + verb + prepositional phrase
• Prepositional Phrase = – = Joe + verb + preposition + noun phrase
– Preposition + noun phrase (from the car) – = Joe + verb + from + noun phrase
– = Joe + verb + from + article +noun
Introduction Introduction
• Look at the sentence. Is this grammatically • Observations
correct? – Our definition of a valid simple sentence
– Joe runs from the car. includes:
• A set of grammar categories (sentence, noun phrase,
– Sentence = Joe + verb + from + article +noun verb phrase, noun, verb, etc)
– = Joe + verb + from + the + car • Rules for defining each category
– = Joe + ran + from + the + car • Set of “final strings” (Joe, ran, from, the, car)
• Initial category of what we wish to create (sentence)
• Valid sentence!
3
Back to CS Theory Back to CS Theory
• CFLs are defined using Context Free • Recall our friend: Palindromes
Grammars(CFG) – A palindrome is a string that is the same read
– Grammars, like their spoken language left to right or right to left
counterparts, provide a means to recursively – First half of a palindrome is a “mirror image”
“deriving” the strings in a language. of the second half
– Examples:
• a, b, aba, abba, babbab.
4
Context Free Grammars Context Free Grammars
• Let’s redefine grammars for CS Theory use: • Production Rules
1. Terminals = Set of symbols that form the strings of – Of the form A → B
the language being defined
• A is a variable
2. Variables = Set of symbols representing categories
• B is a string, combining terminals and variables
3. Start Symbol = variable that represents the “base
• To apply a rule, replace an occurance of A with the
category” that defines our language
string B.
4. Production rules = set of rules that recursively define
the language
5
Example Example
• Find a CFG to describe: • Find a CFG to describe:
– L = {x ∈ {0,1}* | n0(x) = n1(x)} – L = {x ∈ {0,1}* | n0(x) = n1(x)}
– Basic idea (define recursively) • S → ε (1)
• ε is certainly in the language • S → 0S1 (2)
• For all strings in the language, if we add a 0 and 1 to • S → 1S0 (3)
the string, the result is in the language. • S → SS (4)
• The concatenation of any two strings in the language
will also be in the language
6
One more example One more example
• Defining the grammar for algebraic • Defining the grammar for algebraic
expressions: expressions:
– Let a = a numeric constant – G = (V, T, P, S)
– Set of binary operators = {+, -, *, /}
– V = {S}
– Expressions can be parenthesized
– T = { a, -, +, *, /, (, ) }
– S=S
– P = see next slide
7
A real practical example Another real practical example
• Grammars for programming languages • Anybody here work with XML?
– Keywords and punctuation are terminals • DTDs in XML can be expressed as
– Program constructs are variables grammars.
– Production rules define the syntax of the – Use parsing technology to parse / represent
language XMK DTDs.
– This is really the second step in building a • See Section 5.3.4 for details.
compiler!
Summary
• Next class of languages Context Free
Languages.
– Described by Context Free Grammars
• G = (V, Σ, S, P)
• Questions?
• Let’s Break!