0% found this document useful (0 votes)
48 views8 pages

Homework and Exams

This document discusses context free grammars and context free languages. It begins by introducing context free grammars as a means of recursively deriving strings in a language using production rules. An example context free grammar for the language of palindromes is presented to illustrate how grammars can be used to define languages. The document then redefines some key concepts for using grammars in computer science theory, including terminals, variables, production rules, and a start symbol.

Uploaded by

Rupesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views8 pages

Homework and Exams

This document discusses context free grammars and context free languages. It begins by introducing context free grammars as a means of recursively deriving strings in a language using production rules. An example context free grammar for the language of palindromes is presented to illustrate how grammars can be used to define languages. The document then redefines some key concepts for using grammars in computer science theory, including terminals, variables, production rules, and a start symbol.

Uploaded by

Rupesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Homework and Exams

• Homework
Context Free Languages – Return Homework #2
– Homework #3 Due today

Grammars

Homework and Exams Homework and Exams


– Homework #4 (due 10/13) • …and Exams
• Exercise 5.1.2 b, c (pg 180)
– Exam 1 is Wednesday!!
• Exercise 5.1.7 a,b (pg 181)
• Exercise 5.2.1 b,c (pg 191)
• Design a CFG for the following languages
– Set of odd length strings in {a,b}* with middle symbol a
– Set of even length strings in {a,b}* with the 2 middle
– Questions?
symbols equal
• Show that the CFG with the productions below is
ambiguous:
– S→a | Sa | bSS | SSb | SbS

Schedule Mangling Schedule Mangling


• Today • Perhaps more mangling to come in Week 8
– Chapter 5: CFGs, Parse Trees, Ambiguity
• Wednesday – Move exam 2 to Wednesday?
– Exam 1
– Problem Session for Chapter 5
• Monday, Oct 13th – Stay tuned
– Homework #4 due – Homework #5 Assigned • Watch the schedule page
– Chapter 6: Pushdown Automata
• Wednesday, Oct 15th
– Chapter 6 (cont)
– Problem Session for Chapter 6

1
Before We Start Plan for today
• Any questions? • Context Free Languages
– Next class of languages in our quest!

Languages Regular Languages


• Recall. • For the past several weeks, we have been
– What is a language? looking at Regular Languages:
– What is a class of languages? – Means of describing: Regular Expression
– Machine for accepting: Finite Automata

Last class we discovered Context Free Languages


• Venn-diagram of languages • Context Free Languages(CFL) is the next
Is there class of languages outside of Regular
something Regular Languages Languages:
out here?
– Means for defining: Context Free Grammar
– Machine for accepting: Pushdown Automata
YES!
Finite
Languages

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.

Back to CS Theory Back to CS Theory


• Recursive definition for palindromes (pal) • A CFG for palindromes over {a,b}
over Σ – Base cases
1.ε ∈ pal 1. P → ε
2.For any a ∈ Σ, a ∈ pal 2. P → a
3. P → b
3.For any x ∈pal and a ∈ Σ, axa ∈ pal
– Recursion
4.No string is in pal unless it can be obtained by
4. P → aPa
rules 1-3
5. P → bPb

Back to CS Theory Back to CS Theory


• Building the palindrome abba using • What we did:
grammar – At each step, we replaced the symbol P on the
• P ⇒ aPa (Rule 4) right with one of the definitions of P from our
rule set.
• P ⇒ abPba (Rule 5)
• P ⇒ ab ε ba (Rule 1)
– Relating to our sentence example
• Categories were replaced by Symbols
• P ⇒ abba
• Final strings were replaced by Symbols
• Start category was replaced by a symbol
• We still have our set of rules.

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

Context Free Grammars Context Free Grammars


• Let’s formalize this a bit: • Let’s formalize this a bit:
– A context free grammar (CFG) is a 4-tuple: (V, – Production rules
T, P, S) where • Of the form A → β where
– A ∈V
• V is a set of variables – β ∈ (V ∪ T)* string with symbols from V and T
• T is a set of terminals • We say that γ can be derived from α in one step:
• P is a set of production rules – A → β is a rule
• V and T are disjoint (I.e. V ∩ T = ∅) – α = α1A α2
– γ = α1 β α2
• S ∈V, is your start symbol – α⇒γ

Context Free Grammars Context Free Grammars


• Let’s formalize this a bit: • The language generated by a CFG
– Production rules – Let G = (V, T, P, S)
• We say that the grammar is context-free since this – The language generated by G, L(G)
substitution can take place regardless of where A is. • L(G) = { x ∈ T* | S ⇒* x}
• We write α ⇒* γ if γ can be derived from α in zero
or more steps.
– A language L is a Context Free Language
(CFL) iff there is a CFG G, such that
• L = L(G)

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

Example Another example


• Let’s derive a string from L • Find a CFG to describe:
– 00110011 – L = {aibjck | i = k}
– S ⇒ SS rule 4 • Number of a’s equals the number of c’s with any
number of b’s between them
⇒ 0S1 0S1 rule 2
• Use variable B to represent bj
⇒ 00S11 00S11 rule 2 • Every time you add a to the left of B you need to
⇒ 00 ε 11 00 ε 11 rule 1 add c to the right.
= 00110011

Another example Another example


• Find a CFG to describe: • Let’s derive a string from L: aabbbcc
– L = {aibjck | i = k} – S ⇒ aSc rule 2
• S→B (1) S ⇒ aaScc rule 2
• S → aSc (2) S ⇒ aaBcc rule 1
• B → bB (3) S ⇒ aabBcc rule 3
• B→ε (4) S ⇒ aabbBcc rule 3
– Can also write as S ⇒ aabbbBcc rule 3
• S → B | aSc S ⇒ aabbb ε cc rule 4
• B → bB | ε = aabbb ε cc

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

One more example One more example


• Defining the grammar for algebraic • Show derivation for a + (a * a) / a
expressions – Production rules – S⇒S+S rule 1
–S→S+S (1) S⇒a+S rule 6
S→S–S (2) S⇒a+S/S rule 4
S→S*S (3) S ⇒ a + (S) / S rule 5
S→S/S (4) S ⇒ a + (S * S) / S rule 3
S ⇒ a + (a * S) / S rule 6
S → (S) (5)
S ⇒ a + (a * a) / S rule 6
S→a (6)
S ⇒ a + (a * a) / a rule 6

Practical uses for grammars A real practical example


• How a compiler works • Grammars for programming languages
– <stmt> → … | <for-stmt> | <if-stmt> | …
lexer
Stream
parser
Parse – <stmt> → { <stmt> <stmt> } | ε
of tokens codegen
Tree – <if-stmt> → if ( <expr> ) then <stmt>
Object – <for-stmt> → for ( <expr>; <expr>; <expr>)
Source
code <stmt>
file

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!

You might also like