Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Context-Free Grammars - Automata and Complexity Theory - Lecture Slides, Slides of Theory of Automata

Some concept of Automata and Complexity Theory are Administrivia, Closure Properties, Context-Free Grammars, Decision Properties, Deterministic Finite Automata, Intractable Problems, More Undecidable Problems. Main points of this lecture are: Context-Free Grammars, Formalism, Derivations, Backus-Naur Form, Left- and Rightmost Derivations, Powerful, Nested Structures, Programming Languages, Strings, Recursive Rules

Typology: Slides

2012/2013

Uploaded on 04/29/2013

juni
juni 🇮🇳

4

(17)

122 documents

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Context-Free Grammars
Formalism
Derivations
Backus-Naur Form
Left- and Rightmost Derivations
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download Context-Free Grammars - Automata and Complexity Theory - Lecture Slides and more Slides Theory of Automata in PDF only on Docsity!

1

Context-Free Grammars

Formalism Derivations

Backus-Naur Form

Left- and Rightmost Derivations

2

Informal Comments

A

context-free grammar

is a notation

for describing languages.

It is more powerful than finite automata or RE’s, but still cannot defineall possible languages.

Useful for nested structures, e.g., parentheses in programming languages.

4

Example: CFG for { 0

n

n

| n > 1}

Productions:

S -> 01S -> 0S

Basis: 01 is in the language.

Induction: if w is in the language, then so is 0w1.

5

CFG Formalism

Terminals

= symbols of the alphabet

of the language being defined.

Variables

nonterminals

= a finite

set of other symbols, each of whichrepresents a language.

Start symbol

= the variable whose

language is the one being defined.

7

Example: Formal CFG

Here is a formal CFG for { 0

n

n

| n > 1}.

Terminals = {0, 1}.

Variables = {S}.

Start symbol = S.

Productions =

S -> 01S -> 0S

8

Derivations – Intuition

We

derive

strings in the language of a

CFG by starting with the start symbol,and repeatedly replacing some variableA by the right side of one of itsproductions.

That is, the “productions for A” are thosethat have A on the left side of the ->.

10

Iterated Derivation

=>* means “zero or more derivation steps.”

Basis:

for any string

Induction: if

and

, then

11

Example: Iterated Derivation

S -> 01; S -> 0S1.

S => 0S1 => 00S11 => 000111.

So S =>* S; S =>* 0S1; S =>* 00S11; S =>* 000111.

13

Language of a Grammar

If G is a CFG, then L(G), the

language

of G, is {w | S =>* w}.

Note: w must be a terminal string, S is thestart symbol.

Example: G has productions S ->

ε

and

S -> 0S1.

L(G) = {

n

n

| n > 0}.

Note:

ε

is a legitimate

right side.

14

Context-Free Languages

A language that is defined by some CFG is called a

context-free language.

There are CFL’s that are not regular languages, such as the example justgiven.

But not all languages are CFL’s.

Intuitively: CFL’s can count two things, not three.

16

BNF Notation – (2)

Symbol ::= is often used for ->.

Symbol | is used for “or.”

A shorthand for a list of productions withthe same left side.

Example: S -> 0S1 | 01 is shorthand for S -> 0S1 and S -> 01.

17

BNF Notation – Kleene Closure

Symbol … is used for “one or more.”

Example: ::= 0|1|2|3|4|5|6|7|8|

::=

Note: that’s not exactly the * of RE’s.

Translation: Replace

… with a new

variable A and productions A -> A

19

BNF Notation: Optional Elements

Surround one or more symbols by […] to make them optional.

Example: ::=

if

then

[;

else

]

Translation: replace [

] by a new

variable A with productions A ->

ε

20

Example: Optional Elements

Grammar for if-then-else can be replaced by:

S -> iCtSAA -> ;eS |

ε