[PDF] Context-Free Grammars (CFG) Other variables represent auxiliary classes





Previous PDF Next PDF



CS 208: Automata Theory and Logic - Lecture 6: Context-Free CS 208: Automata Theory and Logic - Lecture 6: Context-Free

“ A grammar can be regarded as a device that enumerates the sentences of a language. We study a sequence of restrictions that limit grammars first to Turing 



Homework 5 Solutions

Define another CFG G3 = (V3 Σ



Solutions to Written Assignment 2

Give a context-free grammar (CFG) for each of the following languages over Hint: have a look at the calculator examples presented in class. S → xTU {S ...



CS481F01 Solutions 4 – CFGs

1. For each of the following languages give a context-free grammar that gen- erates the language. (a). { 



Theory of Computation - CSE 105 Context-free Languages Sample

Context-free Languages. Sample Problems and Solutions. Designing CFLs. Problem 1 Give a context-free grammar that generates the following language over {01}∗:.



Theory of Computation - (Context-Free Grammars)

24-Jan-2021 Solve the problem completely by constructing CFG's for L1. L2



CMSC330-Context Free Grammar

Context-Free Grammar (CFG). A way of describing sets of strings (= languages) Example grammar G is S → 0S



Context-Free Grammars

strings in the language. ○ CFGs are best explained by example Page 4. Arithmetic Expressions.



Chapter 3 Context-Free Grammars Context-Free Languages

https://www.cis.upenn.edu/~jean/gbooks/tcbookpdf2.pdf



Context-Free Grammars (CFG)

Other variables represent auxiliary classes of strings that are used to help define the language of the start symbol. Automata Theory Languages and Computation 



Homework 5 Solutions

Homework 5 Solutions. 1. Give context-free grammars that generate the following languages. in the problem and define other languages.



Chapter 3 Context-Free Grammars Context-Free Languages

https://www.cis.upenn.edu/~jean/gbooks/tcbookpdf2.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.



CS 208: Automata Theory and Logic - Lecture 6: Context-Free

Context-Free Grammars. Noam Chomsky. (linguist philosopher



CS481F01 Solutions 4 – CFGs

1. For each of the following languages give a context-free grammar that gen- erates the language. (a). { 



Context-Free Grammars

a set of terminals (from the alphabet); and. • a list of productions (also called rules). Goddard 6a: 2. Page 3. Example: 0 n.



Lecture 17 – April 15 Using & Augmenting Context-Free Grammars

In using CFGs it is important to keep in mind that the ultimate goal is to recover the underlying structure of natural language sentences. Although we will 



Theory of Computation - CSE 105 Context-free Languages Sample

Sample Problems and Solutions. Designing CFLs. Problem 1 Give a context-free grammar that generates the following language over {01}?:.



A Practical Tutorial on Context Free Grammars

Mar 17 2021 Here is an example of a context free grammar using BNF for simple ... Let's put it all together by solving a problem taken from an exam for ...

Context-Free Grammars(CFG)

SITE : http://www.sir.blois.univ-tours.fr/˜mirian/ Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 1/26

An informal example

Language of palindromes:Lpal

A palindrome is a string that reads the same forward and backwardEx:otto,madamimadam,0110,11011,?

Lpalis not a regular language (can be proved by using the pumping lemma)We considerΣ ={0,1}. There is a natural, recursive definition of when a string

of0and1is inLpal.

Basis:?,0and1are palindromes

Induction:Ifwis a palindrome, so are0w0and1w1. No string is palindrome of

0and1, unless it follows from this basis and inductive rule.A CFG is a formal notation for expressing such recursive definitions of languages

Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 2/26

What is a grammar?

A grammar consists of one or more variables that represent classes of strings (i.e., languages)There are rules that say how the strings in each class are constructed. The construction can use :

1. symbols of the alphabet

2. strings that are already known to be in one of the classes

3. or both

Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 3/26

A grammar for palindromes

In the example of palindromes, we need one variablePwhich represents the set of palindromes;i.e., the class of strings forming the languageLpalRules:

P→?

P→0

P→1

P→0P0

P→1P1The first three rules form thebasis.

They tell us that the class of palindromes includes the strings?,0and1None of the right sides of theses rules contains a variable, which is why

they form a basis for the definition The last two rules form theinductivepart of the definition. For instance, rule 4 says that if we take any stringwfrom the classP, then0w0 is also in classP. Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 4/26

Definition of Context-Free Grammar

A GFG (or just a grammar)Gis a tupleG= (V,T,P,S)where

1.Vis the (finite) set of variables (or nonterminals or syntactic categories).

Each variable represents a language,i.e., a set of strings

2.Tis a finite set of terminals,i.e., the symbols that form the strings of the

language being defined

3.Pis a set of production rules that represent the recursive definition of the

language.

4.Sis the start symbol that represents the language being defined.

Other variables represent auxiliary classes of strings that are used to help define the language of the start symbol. Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 5/26

Production rules

Each production rule consists of:

1. A variable that is being (partially) defined by the production. This variable is often

called theheadof the production.

2. The production symbol→.

3. A string of zero or more terminals and variables. This string, called thebodyof

the production, represents one way to form strings in the language of the variable of the head. In doing so, we leave terminals unchanged and substitute foreach variable of the body any string that is known to be in the language of that variable Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 6/26

Compact Notation for Productions

We often refers to the production whose head isAas "productions forA" or "A-productions"Moreover, the productions

A→α1,A→α2...A→αn

can be replaced by the notation

A→α1|α2|...|αn

Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 7/26

Examples: CFG for palindromes

Gpal= ({P},{0,1},A,P)

whereArepresents the production rules:

P→?

P→0

P→1

P→0P0

P→1P1

We can also write:P→?|0|1|0P0|1P1

Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 8/26 Examples: CFG for expressions in a typicalprogramming language

Operators:+(addition) and?(multiplication)

Identifiers: must begin withaorb, which may be followed by any string in{a,b,0,1}?

We need two variables:

E: represents expressions. It is the start symbol. I: represents the identifiers. Its language is regular and is the language of the regular expression:(a+b)(a+b+0+1)? Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 9/26

Exemples (cont.): The grammar

GrammarG1= ({E,I},T,P,E)where:T={+,?,(,),a,b,0,1}andPis the set of productions: 1

E→I

2E→E+E

3E→E?E

4E→(E)

5I→a

6I→b

7I→Ia

8I→Ib

9I→I0

10I→I1

Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 10/26

Derivations Using a Grammar

We apply the productions of a CFG to infer that certain strings are in the language of a certain variableTwo inference approaches:

1.Recursive inference, using productions from body to head

2.Derivations, using productions from head to body

Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 11/26

Recursive inference - an exemple

We consider some inferences we can make usingG1

RecallG1:

E→I|E+E|E?E|(E)I→a|b|Ia|Ib|I0|I1

String

Lang Prod

String(s) used

(i) a I 5 (ii) b I 6 (iii) b0 I 9 (ii) (iv) b00 I 9 (iii) (v) a E 1 (i) (vi) b00 E 1 (iv) (vii) a + b00 E 2 (v), (vi) (viii) (a + b00) E 4 (vii) (ix) a (a + b00) E 3 (v), (viii) Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 12/26

Derivations

Applying productions from head to body

requires the definition of a new relational symbol:? Let: G= (V,T,P,S)be a CFGA?Vα,β?(V?T)?andA→γ?P

Then we write

αAβ?Gαγβ

or, if G is understood

αAβ?αγβ

and say thatαAβderivesαγβ. Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 13/26

Zero or more derivation steps

We define??to be the reflexive and transitive closure of?(i.e., to denote zero or more derivation steps):

Basis:Letα?(V?T)?. Thenα??α.

Induction:Ifα??βandβ?γ, thenα??γ. Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 14/26

Examples of derivation

Derivation ofa?(a+b000)byG1

E?E?E?I?E?a?E?a?(E)?

a?(E+E)?a?(I+E)?a?(a+E)?a?(a+I)? a?(a+I0)?a?(a+I00)?a?(a+b00)

Note 1

: At each step we might have several rules to choose from, e.g.

I?E?a?E?a?(E), versus

I?E?I?(E)?a?(E).

Note 2: Not all choices lead to successful derivations of a particular string, for instance

E?E+E(at the first step)

won't lead to a derivation ofa?(a+b000).

Important:

Recursive inference and derivation are equivalent. A string of terminalswis infered to be in the language of some variableAiffA??w Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 15/26

Leftmost and Rightmost derivation

In other to restrict the number of choices we have in derivinga string, it is often useful to require that at each step we replace the leftmost (or rightmost) variable

by one of its production rulesLeftmost derivation?lm: Always replace the left-most variable by one of its

rule-bodiesRightmost derivation?rm: Always replace the rightmost variable by one of its rule-bodies.

EXAMPLES

1-Leftmost derivation:

previous example

2-Rightmost derivation:

E?rmE?E?rmE?(E)?rm

E?(E+E)?rmE?(E+I)?rmE?(E+I0)?rm

E?(E+I00)?rmE?(E+b00)?rmE?(I+b00)?rm

E?(a+b00)?rmI?(a+b00)?rma?(a+b00)

We can conclude thatE??rma?(a+b00)

Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 16/26

The Language of the Grammar

IfG(V,T,P,S)is a CFG, then the language ofGis

L(G) ={w in T?|S??Gw}

i.e., the set of strings overTderivable from the start symbol.

IfGis a CFG, we callL(G)a context-free language.

Example:L(Gpal)is a context-free language.

Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 17/26

Theorem

A stringw? {0,1}?is inL(Gpal)iffw=wR.

Proof: (?-direction.) Supposew=wR,i.e., thatwis a palindrome. We show by induction on|w|thatw?L(Gpal) Basis:Basis:|w|= 0, or|w|= 1. Thenwis?,0, or1. SinceP→?,P→0, and P→1are productions, we conclude thatP??win all base cases. Induction:Suppose|w| ≥2. Sincew=wR, we havew= 0x0, orw= 1x1, and x=xR.

Ifw= 0x0we know from the IH thatP??x. Then

P?0P0??0x0 =w

Thusw?L(Gpal). The case for w = 1x1 is similar.

Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 18/26

Proof (cont.)

(?-direction.) We assumew?L(Gpal)and we prove thatw=wR. Sincew?L(Gpal), we haveP??w. We do an induction of the length of??. Basis:The derivationP??wis done in one step. Thenwmust be?,0, or1, all palindromes. Induction:Letn≥1, and suppose the derivation takesn+1steps. Then we must have w= 0x0??0P0?P or w= 1x1??1P1?P where the second derivation is done innsteps. By the IHxis a palindrome, and the inductive proof is complete. Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 19/26

Sentential Forms

LetG= (V,T,P,S)be a CFG, andα?(V?T)?. If

S we sayαis a sentential form. IfS?lmαwe say thatαis aleft-sentential form, and ifS?rmαwe say that is a right-sentential form.

Note:L(G)is those sentential forms that are inT?.

Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 20/26

Example

RecallG1:

E→I|E+E|E?E|(E)I→a|b|Ia|Ib|I0|I1

1-ThenE?(I+E)is a sentential form since

E?E?E?E?(E)?E?(E+E)?E(I+E)

This derivation is neither leftmost, nor right-most.

2-a?Eleft-sentential form, since

E?E?E?I?E?a?E

3-E?(E+E)is a right-sentential form since

E?E?E?E?(E)?E?(E+E)

Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 21/26

Parse Trees

Ifw?L(G), for some CFG, thenwhas a parse tree, which tells us the

(syntactic) struc- ture ofw.wcould be a program, a SQL-query, an XML- document, etc.Parse trees are an alternative representation to derivations and recursive

inferences.There can be several parse trees for the same string.Ideally there should be only one parse tree (the "true" structure) for each string,

i.e. the language should be unambiguous.Unfortunately, we cannot always remove the ambiguity. Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 22/26

Gosta Grahne...

Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 23/26 Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 24/26 Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 25/26 Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari - p. 26/26quotesdbs_dbs9.pdfusesText_15
[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