Docsity

Context-Free Grammars: A Comprehensive Guide to Describing Languages, Slides of Compiler Design

Partial preview of the text

Download Context-Free Grammars: A Comprehensive Guide to Describing Languages and more Slides Compiler Design 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 specifc 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

● In today’s slides, capital letters in Bold Red Uppercase will represent nonterminals. ● e.g. A , B , C , D ● Lowercase letters in blue monospace will represent terminals. ● e.g. t , u , v , w ● Lowercase Greek letters in gray italics will represent arbitrary strings of terminals and nonterminals. ● (^) e.g. α , γ , ω ● You don't need to use these conventions on your own; just make sure whatever you do is readable. ☺

A Notational Shorthand

E → int | E Op E | (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 of terminals derivable from the start symbol.

Answer at PollEv.com/cs103 or text CS103 to 22333 once to join, then a number. Answer at PollEv.com/cs103 or text CS103 to 22333 once to join, then a number.

Consider the following CFG G over Σ = { a , b , c , d }:

S → Sa | dT

T → bTb | c

How many of the following strings are in ℒ( G )?

dca

cad

bcb

dTaa

Consider the following CFG G over Σ = { a , b , c , d }:

S → Sa | dT

T → bTb | c

How many of the following strings are in ℒ( G )?

dca

cad

bcb

dTaa

If G is a CFG with alphabet Σ and start symbol S , then the language of G is the set ℒ( G ) = { ω ∈ Σ* | S ⇒* ω } If G is a CFG with alphabet Σ and start symbol S , then the language of G is the set ℒ( G ) = { ω ∈ Σ* | S ⇒* ω }

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 8 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