Docsity

Understanding Context-Free Grammars: From Basics to Applications, Lecture notes of Programming Languages

Partial preview of the text

Download Understanding Context-Free Grammars: From Basics to Applications and more Lecture notes Programming Languages in PDF only on Docsity!

Context-Free Grammars

Describing Languages

● (^) We've seen two models for the regular languages: ● (^) Finite 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.

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

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** → /

Derivations

⇒ 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). E → E Op E | int | ( E ) Op → + | ***** | - | /

The Language of a Grammar

●

If G is a CFG with alphabet Σ and start

symbol S , then the language of G is the

set

ℒ ( G ) = { ω ∈ Σ* | S ⇒

●

That is, ℒ( 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.

From Regexes to CFGs

●

CFGs consist purely of production rules

of the form A → ω. They do not have the

regular expression operators * or ∪.

●

However, we can convert regular

expressions to CFGs as follows:

S → ab*

From Regexes to CFGs

●

CFGs consist purely of production rules

of the form A → ω. They do not have the

regular expression operators * or ∪.

●

However, we can convert regular

expressions to CFGs as follows:

S → Ab A → Aa | ε

From Regexes to CFGs

●

CFGs consist purely of production rules

of the form A → ω. They do not have the

regular expression operators * or ∪.

●

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. ■

●

Problem Set Exercise: Instead, show

how to convert a DFA/NFA into a CFG.

Regular Languages CFLs All Languages

Why the Extra Power?

●

Why do CFGs have more power than

regular expressions?

●

Intuition: Derivations of strings have

unbounded “memory.”

S → aSb | ε

a a a a b b b b