[PDF] [PDF] Context-Free Grammars

A language is context-free if it is generated by a CFG Goddard 6a: 4 Page 5 Example Continued S → 0S1



Previous PDF Next PDF





[PDF] Context-Free Grammars (CFG)

3 P is a set of production rules that represent the recursive definition of the language 4 S is the start symbol that represents the 



[PDF] Homework 5 Solutions

Homework 5 Solutions 1 Give context-free grammars that generate the following languages S is the start variable; set of terminals Σ = {a, b, c}; and rules



[PDF] Context-free Languages: Grammars and Automata

Formal definition of CFG • A Context-free grammar is a 4-tuple (V, Σ, R, S) where 1 V is a finite set called the variables (non- terminals) 2 Σ is a finite set 



[PDF] 06 Context-free grammars - CS:4330 Theory of Computation

Context-free grammars provide a more powerful mechanism for language specification Example > CFG G1 has the following rules: A → 0A1 A → B B → # What is a program generated with this grammar that solves the following problem:



[PDF] Solutions to Written Assignment 2

Give a context-free grammar (CFG) for each of the following languages over the alphabet Σ Hint: have a look at the calculator examples presented in class



[PDF] Context-Free Grammars

alphabet of the CFG) ○ A set of production rules saying how each nonterminal can be replaced by a string of terminals and nonterminals, and ○



[PDF] Chapter 3 Context-Free Grammars, Context-Free - UPenn CIS

In order to define grammar rules, we assume that we have two kinds of symbols: the terminals, which are the symbols of the alphabet underlying the languages 



[PDF] CMSC 330, Fall 2009, Practice Problem 3 Solutions 1 Context Free

List the 4 components of a context free grammar Terminals Productions are rules for replacing a single non-terminal with a string of terminals and non- 



[PDF] Context-Free Grammars

A language is context-free if it is generated by a CFG Goddard 6a: 4 Page 5 Example Continued S → 0S1



[PDF] CS 341 Homework 11 Context-Free Grammars

Construct a context-free grammar that generates all strings in (b) Write a context-free grammar to generate L Solutions 1 (a) We can do an Many of these correspond to the same parse trees, just applying the rules in different orders

[PDF] continents and countries

[PDF] contour diabetes app for android

[PDF] contour diabetes app for pc

[PDF] contour3d python

[PDF] contract hours

[PDF] contrast the development of absolutism in england and france

[PDF] contre la constipation remede naturel

[PDF] contre la constipation remèdes

[PDF] contre la gastro remede

[PDF] contre la toux remede

[PDF] contre la toux remede naturel

[PDF] control and regulation

[PDF] control packet in ns2

[PDF] control statements in fortran 77

[PDF] controle fraction 6ème

Context-Free Grammars

A grammar is a set of rules for putting strings

together and so corresponds to a language.

Grammars

Agrammarconsists of:

a set of variables(also called nonterminals), one of which is designated the start variable;

It is customary to use upper-case letters for

variables; a set of terminals(from the alphabet); and a list of productions(also called rules).

Goddard 6a: 2

Example:0n1nHere is a grammar:

S!0S1 S!"

Sis the only variable. The terminals are0and1.

There are two productions.

Goddard 6a: 3

Using a Grammar

A production allows one to take a string con-

taining a variable and replace the variable by the RHS of the production.

Stringwof terminals isgeneratedby the gram-

mar if, starting with the start variable, one can apply productions and end up withw. The se- quence of strings so obtained is aderivation ofw.

We focus on a special version of grammars called

acontext-free grammar(CFG). A language is context-freeif it is generated by a CFG.

Goddard 6a: 4

Example Continued

S!0S1 S!"

The string0011is in the language generated.

The derivation is:

S=)0S1=)00S11=)0011

For compactness, we write

S!0S1j"

where the vertical bar meansor.

Goddard 6a: 5

Example: Palindromes

LetPbe language of palindromes with alpha-

betfa;bg. One can determine a CFG forPby finding a recursive decomposition.

If we peel first and last symbols from a palin-

drome, what remains is a palindrome; and if we wrap a palindrome with the same symbol front and back, then it is still a palindrome.

CFG is

P!aPajbPbj"

Actually, this generates only those of even length...

Goddard 6a: 6

Formal Definition

One can provide aformal definitionof a context-

free grammar. It is a 4-tuple(V;;S;P)where: •Vis a finite set of variables; •is a finite alphabet of terminals; •Sis the start variable; and •Pis the finite set of productions. Each production has the formV!(V[).

Goddard 6a: 7

Further Examples: Even0"sA CFG for all binary strings with an even num- ber of0"s.

Find the decomposition. If first symbol is1,

then even number of0"s remain. If first sym- bol is0, then go to next0; after that again an even number of0"s remain. This yields:

S!1Sj0A0Sj"

A!1Aj"

Goddard 6a: 8

Alternate CFG for Even0"sHere is another CFG for the same language.

Note that when first symbol is0, what remains

has odd number of0"s.

Goddard 6a: 9

Alternate CFG for Even0"sHere is another CFG for the same language.

Note that when first symbol is0, what remains

has odd number of0"s.

S!1Sj0Tj"

T!1Tj0S

Goddard 6a: 10

Example

A CFG for the regular language corresponding

to the RE0011.

Goddard 6a: 11

Example

A CFG for the regular language corresponding

to the RE0011.

The language is the concatenation of two lan-

guages: all strings of zeroes with all strings of ones. S!CD

C!0Cj0

D!1Dj1

Goddard 6a: 12

Example Complement

A CFG for the complement of RE0011.

CFGs don"t do "and"s, but they do do "or"s". A

stringnotof the form0i1jwherei;j >0is one of the following: contains10; is only zeroes; or is only ones. This yields CFG:

S!AjBjC

A!D10D

D!0Dj1Dj"

B!0Bj0

C!1Cj1

Goddard 6a: 13

Consistency and Completeness

Note that to check a grammar and description

match, one must check two things: that every- thing the grammar generates fits the description (consistency), and everything in the description is generated by the grammar (completeness).

Goddard 6a: 14

Example

Consider the CFG

S!0S1Sj1S0Sj"

The string011100is generated:

S=)0S1S=)01S=)011S0S=)0111S0S0S

=)01110S0S=)011100S=)011100

What does this language contain? Certainly ev-

ery string generated has equal0"s and1"s...

But can any string with equal0"s and1"s be

generated?

Goddard 6a: 15

Example Argument for Completeness

Yes. All strings with equal0"s &1"s are gener-

ated:

Well, at some point, equality between0"s and1"s

is reached. The key is that if string starts with0, then equality is first reached at a1. So the por- tion between first0and this1is itself an ex- ample of equality, as is the portion after this1.

That is, one can break up string as0w1xwith

bothwandxin the language.

The break-up of00101101:

0 0 1 0 1 1 0 1

w xGoddard 6a: 16

A Silly Language CFG

This CFG generates sentences as composed of

noun- and verb-phrases:

S!NP VP

NP!theN

VP!VNP

V!singsjeats

N!catjsongjcanary

This generates "the canary sings the song", but

also "the song eats the cat".

This CFG generates all "legal" sentences, not

just meaningful ones.

Goddard 6a: 17

Practice

Give grammars for the following two languages:

1.

All binary strings with both an even number

of zeroes and an even number of ones. 2.

All strings of the for m0a1b0cwherea+c=b.

(Hint: it"s the concatenation of two simpler languages.)

Goddard 6a: 18

Practice Solutions

1)

S!0Xj1Yj"

X!0Sj1Z(odd zeroes, even ones)

Y!1Sj0Z(odd ones, even zeroes)

Z!0Yj1X(odd ones, odd zeroes)

2) S!TU

T!0T1j"

U!1U0j"

Goddard 6a: 19

Summary

A context-free grammar (CFG) consists of a set

of productions that you use to replace a vari- able by a string of variables and terminals. The language of a grammar is the set of strings it generates. A language is context-free if there is a CFG for it.

Goddard 6a: 20

quotesdbs_dbs17.pdfusesText_23