Context Free Languages-Formal Languages, Automata and Computation-Lecture 07 Slides-Computer Science-, Slides of Theory of Formal Languages for Automata

This course is about: Formal Languages, Automata and Complexity. Context Free Languages, Dfa, Regular Expression, Basis, Recursion, Grammars, Nonterminal Symbol, Terminal Symbols, Start Variable, Parse Trees, Structural Ambiguity

Typology: Slides

2011/2012

Uploaded on 02/03/2012

abid
abid 🇶🇦

19 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
FOR MA L LAN GUAGES, AU TOMATA AND
COM PU TATI ON
CON TEXT FR EE LAN GUAG ES
Carnegie Mellon University in Qatar
(CARNEGIE MEL LON UNIVERSITY IN QATAR) SLI DES FOR 15-453 LEC TURE 7 SPRING 20 11 1 / 24
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download Context Free Languages-Formal Languages, Automata and Computation-Lecture 07 Slides-Computer Science- and more Slides Theory of Formal Languages for Automata in PDF only on Docsity!

FORMAL LANGUAGES, AUTOMATA AND

COMPUTATION

CONTEXT FREE LANGUAGES

Carnegie Mellon University in Qatar

WHERE ARE WE?

A NONREGULAR LANGUAGE

Consider L = {anbn^ | n ≥ 0 } again. How can we generate such strings? Remember DFAs did recognition, not generation. Consider the following inductive way to generate elements of L Basis:  is in the language Recursion: If the string w is in the language, then so is the string awb.  → ab → aabb · · · → a^55 b^55 · · · Looks like we have simple and finite length process to generate all the strings in L How can we generalize this kind of description?

ANOTHER NONREGULAR LANGUAGE

Consider L = {w | na(w) = nb(w)}. Now consider the following inductive way to generate elements of L Basis:  is in the language Recursion 1: If the string w is in the language, then so are awb and bwa Recursion 2: If the strings w and v are in the language, so is wv. The first recursion rules makes sure that the a’s and b’s are generated in the same number (regardless of order) The second recursion takes any two strings each with equal number of a’s and b’s and generates a

(CARNEGIE new such string by concatenating them.MELLON UNIVERSITY IN QATAR) SLIDES FOR 15-453 LECTURE 7 SPRING 2011 5 / 24

GRAMMARS - AN EXAMPLE

Consider once again L = {anbn^ | n ≥ 0 } Basis:  is in the language Production: S →  Recursion: If w is in the language, then so is the string awb. Production: S → aSb S is called a variable or a nonterminal symbol a, b etc., are called terminal symbols One variable is designated as the start variable or start symbol.

HOW DOES A GRAMMAR WORK?

Consider the set of rules R = {S → , S → aSb} Start with the start variable S Apply the following until all remaining symbols are terminal. Choose a production in R whose left-hand sides matches one of the variables. Replace the variable with the rule’s right hand side. S ⇒ aSb ⇒ aaSbb ⇒ aaaSbbb ⇒ aaaaSbbbb ⇒ aaaabbbb The string aaaabbbb is in the language L The sequence of rule applications above is called a derivation.

LANGUAGE OF A GRAMMAR

All strings generated this way starting with the start variable constitute the language of the grammar. We write L(G) for the language of the grammar G.

A GRAMMAR FOR A FRAGMENT OF ENGLISH

S → NP VP

NP → CN | CN PP

VP → CV | CV PP

PP → P NP

CN → DT N

CV → V | V NP

DT → a | the N → boy | girl | flower | telescope V → touches | likes | sees | gives P → with | to

Nomenclature: S: Sentence NP: Noun Phrase CN: Complex Noun PP: Prepositional Phrase VP: Verb Phrase CV: Complex Verb P: Preposition DT: Determiner

ENGLISH PARSE TREE

S NP CN DT a

N boy

PP P with

NP CN DT a

N flower

VP CV V sees

NP CN DT a

N girl

PP P with

NP CN DT a

N

telescope

This structure is for the interpretation where the boy is seeing with the telescope!

ENGLISH PARSE TREE

ALTERNATE STRUCTURE

S NP CN DT a

N boy

PP P with

NP CN DT a

N flower

VP CV V sees

NP CN DT a

N girl

PP P with

NP CN DT a

N

telescope This is for the interpretation where the girl is carrying a telescope.

OTHER EXAMPLES OF GRAMMAR

APPLICATIONS

Programming Languages Users need to how to “generate” correct programs. Compilers need to know how to “check” and “translate” programs. XML Documents Documents need to have a structure defined by a DTD grammar. Natural Language Processing, Machine Translation

FORMAL DEFINITION OF A GRAMMAR

A Grammar is a 4-tuple G = (V , Σ, R, S) where V is a finite set of variables Σ is a finite set of terminals, disjoint from V. R is a set of rules of the X → Y S ∈ V is the start variable In general X ∈ (V ∪ Σ)+^ and Y ∈ (V ∪ Σ)∗ A context-free grammar is a grammar where all rules have X ∈ V (remember V ⊂ (V ∪ Σ)+) The substitution is independent of the context V appears in. The right hand side of the rules can be any combination of variables and terminals, including  (hence Y ∈ (V ∪ Σ)∗).

DESIGNING CONTEXT FREE GRAMMARS

Consider once again the language L = {w | na(w) = nb(w)}. The grammar for this language is G = ({S}, {a, b}, R, S) with R as follows: (^1) S → aSb (^2) S → bSa (^3) S → SS (^4) S →  From now we will only list the productions, the others will be implicit. We will also combine productions with the same left-hand side using | symbol. S → aSb | bSa | SS | 

DESIGNING CONTEXT FREE GRAMMARS

L = {w | na(w) = nb(w)}. S → aSb | bSa | SS | 

Clearly the strings generated by G have equal number of a’s and b’s. (Obvious from the rules!) We also have to show that all strings in L can be generated with this grammar.