CSI 3104 Winter 2006: Introduction to Formal Languages Chapter 12: ContextFree Grammars - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

CSI 3104 Winter 2006: Introduction to Formal Languages Chapter 12: ContextFree Grammars

Description:

A AAA | bA | Ab | a. Parse Trees for the word bbaaaab. S ... X ab | b. S. bX. aXX. aa. aabX. abX. aXab. aXb. aabb. abab. aabab. abb. abab. aabb. aabab. abb. bab ... – PowerPoint PPT presentation

Number of Views:127
Avg rating:3.0/5.0
Slides: 27
Provided by: Universi57
Category:

less

Transcript and Presenter's Notes

Title: CSI 3104 Winter 2006: Introduction to Formal Languages Chapter 12: ContextFree Grammars


1
CSI 3104 /Winter 2006 Introduction to Formal
Languages Chapter 12 Context-Free Grammars
  • Chapter 12 Context-Free Grammar
  • I. Theory of Automata
  • ? II. Theory of Formal Languages
  • III. Theory of Turing Machines

2
Chapter 12 Context-Free Grammars
  • programming languages
  • compiling a program an operation that generates
    an equivalent program in machine or assembler
    language.
  • 2 phases
  • parsing ?
  • translation to machine language

3
Chapter 12 Context-Free Grammars
  • Example AE (Arithmetic Expressions)
  • Rule 1 Any number is in AE
  • Rule 2 If x and y are in AE, then so are
  • (x) (x) (xy) (xy)
    (xy)
  • A different way for defining the set AE is to use
    a set of substitutions rules similar to the
    grammatical rules

4
Chapter 12 Context-Free Grammars
  • Substitution rules that define the AEs
  • S ? AE
  • AE ? (AE AE)
  • AE ? (AEAE)
  • AE ? (AEAE)
  • AE ? (AE)
  • AE ? (AE)
  • AE ? NUMBER
  • NUMBERS??
  • NUMBER ? FIRST-DIGIT
  • FIRST-DIGIT ? FIRST-DIGIT OTHER-DIGIT
  • FIRST-DIGIT ? 1 2 3 4 5 6 7 8 9
  • OTHER-DIGIT ? 0 1 2 3 4 5 6 7 8 9

5
Chapter 12 Context-Free Grammars
S ? AE ? (AEAE) ? ((AEAE)AE) ?
((AEAE)(AEAE)) ? ((34)(67))
AE S ? AE AE ?
(AE AE) AE ? (AEAE) AE ? (AEAE) AE ? (AE) AE
? (AE) AE ? NUMBER
  • How to generate the number 1066?
  • NUMBER ? FIRST-DIGIT
  • ? FIRST-DIGIT OTHER-DIGIT
  • ? FIRST-DIGIT OTHER-DIGIT
    OTHER-DIGIT
  • ? FIRST-DIGIT OTHER-DIGIT
    OTHER-DIGIT OTHER-DIGIT
  • ? 1 0 6 6
  • NUMBERS
  • NUMBER ? FIRST-DIGIT
  • FIRST-DIGIT ? FIRST-DIGIT OTHER-DIGIT
  • FIRST-DIGIT ? 1 2 3 4 5 6 7 8 9
  • OTHER-DIGIT ? 0 1 2 3 4 5 6 7 8 9

6
Chapter 12 Context-Free Grammars
  • Definition A context free grammar (CFG) is
  • an alphabet S of letters, called terminals.
  • a set of symbols, called nonterminals or
    variables. One symbol S is called the start
    symbol.
  • a finite set of productions of the form
  • A ? a
  • where A is a nonterminal and a is a finite
    sequence (word) of nonterminals and terminals.

7
Chapter 12 Context-Free Grammars
  • Examples

Terminals (, ), , -, , numbers Nonterminals
S, AE
NUMBERS NUMBER ? FIRST-DIGIT FIRST-DIGIT ?
FIRST-DIGIT OTHER-DIGIT FIRST-DIGIT ? 1 2 3
4 5 6 7 8 9 OTHER-DIGIT ? 0 1 2 3 4 5 6 7 8 9
Terminals 0, 1, 2, 3, 4, 5, 6, 7, 8,
9 Nonterminals S, FIRST-DIGIT, OTHER-DIGIT
8
Chapter 12 Context-Free Grammars
  • Definition A sequence of applications of
    productions starting with the start symbol and
    ending in a sequence of terminals is called a
    derivation.
  • Definition The language generated by a CFG is
    the set of all sequences of terminals produced
    by derivations. We also say language defined by,
    language derived from, or language produced by
    the CFG.
  • Definition A language generated by a CFG is
    called a context-free language.

9
Chapter 12 Context-Free Grammars
  • Examples S ? aS S ? ?
  • S ? aS
  • ? aaS
  • ? aaaS
  • ? aaaaS
  • ? aaaaaS
  • ? aaaaaaS
  • ? aaaaaa? aaaaaa
  • Generated Language
  • ?, a, aa, aaa, language(a).

10
Chapter 12 Context-Free Grammars
  • S ? SS S ? a S ?
    ?
  • S ? SS
  • ? SSS
  • ? SaS
  • ? SaSS
  • ? ?aSS
  • ? ?aaS
  • ? ?aa?aa
  • (An infinite number of derivations for the word
    aa.)

Generated Language ?, a, aa, aaa,
language(a).
11
Chapter 12 Context-Free Grammars
  • In general variables Upper case letters
  • terminals Lower case letters
  • The empty word
  • is it a nonterminal? L ? ...
  • no
  • a terminal? LaaL aa
  • not exactly, because it is erased.
  • N ? L N can simply be deleted.

12
Chapter 12 Context-Free Grammars
  • S ? aS S ? bS S ? a S ? b
  • S ? aS ? abS ? abbS ? abba
  • S ? X S ? Y X ? ? Y ? aY
  • Y? bY Y ? a Y ? b
  • S ? aS S ? bS S ? a S ? b S ? ?
  • S ? aS ? abS ? abbS ? abbaS ? abba
  • S ? aS S ? bS S ? ?

13
Chapter 12 Context-Free Grammars
  • S ? XaaX X ? aX X ? bX X ? ?
  • S ? XaaX ? aXaaX ? abXaaX ? abXaabX ? abaab
  • How many derivations are possible for the word
    baabaab?

S ? XY X ? aX X ? bX X ? a Y ? Ya Y? Yb Y ? a
S ? XY X ? aX bX a Y ? Ya Yb a
Abbreviation
14
Chapter 12 Context-Free Grammars
  • S ? SS ES SE ? DSD
  • E ? aa bb
  • D ? ab ba
  • EVEN-EVENlanguage(aabb(ab
    ba)(aabb)(abba))
  • S ? aSb ?
  • S ? aSb ? aaSbb ? aaaSbbb ? aaaaSbbbb
  • ? aaaaaSbbbbb ? aaaaabbbbb
  • S ? aSa bSb ? ?

15
Chapter 12 Context-Free Grammars
S ? AA A ? AAA bA Ab a Parse Trees for the
word bbaaaab
bbaaaab
16
Chapter 12 Context-Free Grammars
  • Parse trees are also called syntax trees,
    generation trees, production trees, or derivation
    trees.

Remark In a parse tree every internal nodes is
labelled with a variable (nonterminal) and every
leaf is labelled with a terminal.
17
Chapter 12 Context-Free Grammars
  • Example
  • S ? (SS) (SS) NUMBER
  • NUMBER ?
  • S ? (SS) ? (S(SS)) ? ? (3(45))
  • S ? (SS) ? ((SS)S) ? ? ((34)5)

18
Chapter 12 Context-Free Grammars
?
?
?
23
?
?
?
35
19
Chapter 12 Context-Free Grammars
Lukasiewicz (Prefix) Notation
345
20
Chapter 12 Context-Free Grammars
345
21
Chapter 12 Context-Free Grammars
1 2 3 4 5 6 1 2
? 3 3 4 5 6
3 4 ? 3 7 5 6
3 7 ?
21 5 6 21 5
? 26 6
26 6 ? 156
22
Chapter 12 Context-Free Grammars
  • Example S ? AB A ? a B ? b
  • S ? AB ? aB ? ab
  • S ? AB ? Ab ? ab
  • A CFG is ambiguous if there is at least one word
  • in the language that has at least two derivation
    trees. It is called unambiguous otherwise.
  • Example S ? aSa S ? bSb
  • S ? a S ? b S ? L
  • S ? aSa ? aaSaa ? aabaa

23
Chapter 12 Context-Free Grammars
Language(a)
  • Example S ? aS Sa a

Example S ? aS a
24
Chapter 12 Context-Free Grammars
  • Total Language Trees
  • S ? aa bX aXX
  • X ? ab b

25
Chapter 12 Context-Free Grammars
Total language tree is infinite since S can call
itself recursively
  • S ? aSb bS a

26
Chapter 12 Context-Free Grammars
S ? X b X ? aX
Total language tree is infinite but language is
finite
Write a Comment
User Comments (0)
About PowerShow.com