Module 28 - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Module 28

Description:

The underdog Spartans beat the Wolverines. Diagram this sentence ... The underdog Spartans beat the Wolverines. article adj noun verb object pct ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 19
Provided by: erict9
Learn more at: http://www.cse.msu.edu
Category:
Tags: beat | english | grammar | it | module | strings

less

Transcript and Presenter's Notes

Title: Module 28


1
Module 28
  • Context Free Grammars
  • Examples of real-life grammars
  • Definition of a grammar G
  • Deriving strings and defining L(G)
  • Context-Free Language definition

2
Grammars
  • Examples of real-life grammars

3
English Grammar
  • The underdog Spartans beat the Wolverines.
  • Diagram this sentence
  • The underdog Spartans beat the Wolverines.
  • ltnoun phrasegt ltverb phrasegt ltpctgt
  • The underdog Spartans beat the Wolverines.
  • ltarticlegt ltadjgt ltnoungt ltverbgtltobjectgtltpctgt
  • Can we generalize these rules?
  • ltsentencegt --gt ltnoun phrasegt ltverb phrasegt ltpctgt
    ...
  • ltnoun phrasegt --gt ltarticlegt ltadjgt ltnoungt ltnoungt
    ...
  • ltverb phrasegt --gt ltverbgt ltobjectgt ltverbgt
    ltadverbgt ...
  • ltnoungt --gt Spartans man boy woman ...
  • ltverbgt --gt shock run catch ...
  • ...

4
Deriving a legal sentence
  • Grammar rules
  • ltsentencegt --gt ltnoun phrasegt ltverb phrasegt ltpctgt
    ...
  • ltnoun phrasegt --gt ltarticlegt ltadjgt ltnoungt ltnoungt
    ...
  • ltverb phrasegt --gt ltverbgt ltobjectgt ltverbgt
    ltadverbgt ...
  • ltnoungt --gt Spartans man boy woman ...
  • ltverbgt --gt shock run catch ...
  • ...

ltsentencegt gt ltnoun phrasegt ltverb phrasegt
ltpctgt gt ltarticlegt ltadjgt ltnoungt ltverb phrasegt
ltpctgt gt The ltadjgt ltnoungt ltverb phrasegt
ltpctgt gt ...
5
C Grammar
  • points td6 xp x2p2 fg3 safety 2
  • Diagram this statement
  • points td6 xp x2p2 fg3 safety 2
  • Can we generalize these rules?
  • ltstatementgt --gt ltassignment statementgt ltif-then
    statementgt ...
  • ltassignment statementgt --gt ltvariablegt
    ltexpressiongt
  • ltvariablegt --gt points td xp x2p fg
    safety ...
  • ltexpressiongt --gt ltexpressiongt ltexpressiongt
    ltvariablegt ...
  • ...

6
Deriving a legal statement
  • Grammar Rules
  • ltstatementgt --gt ltassignment statementgt ltif-then
    statementgt ...
  • ltassignment statementgt --gt ltvariablegt
    ltexpressiongt
  • ltvariablegt --gt points td xp x2p fg
    safety ...
  • ltexpressiongt --gt ltexpressiongt ltexpressiongt
    ltvariablegt ...
  • ...

ltstatementgt gt ltassignment statementgt gt
ltvariablegt ltexpressiongt gt points
ltexpressiongt gt points ltexpressiongt
ltexpressiongt ...
7
Context-Free Grammars
  • Definition

8
Definition
  • A context-free grammar G (V, S, S, P)
  • V finite set of variables (nonterminals)
  • S finite set of characters (terminals)
  • S start variable
  • element of V
  • role is similar to that of q0 for an FSA or NFA
  • P finite set of grammar rules or production
    rules
  • Syntax of a production
  • variable --gt string of variables and terminals

9
English Context-Free Grammar
  • ECFG (V, S, S, P)
  • V ltsentencegt, ltnoun phrasegt, ltverb phrasegt,
    ...
  • people sometimes use lt gt to delimit variables
  • In this course, we generally will use capital
    letters to denote variables
  • S a, b, c, ..., z, , ,, ., ...
  • S ltsentencegt
  • P ltsentencegt --gt ltnoun phrasegt ltverb phrasegt
    ltpctgt, ltnoun phrasegt --gt ltarticlegt ltadjgt ltnoungt,
    ...

10
aibi igt0 CFG
  • ABG (V, S, S, P)
  • V S
  • S a, b
  • S S
  • P S --gt aSb, S --gt ab or S --gt aSb ab
  • second format saves some space

11
Context-Free Grammars
  • Deriving strings, defining L(G), and defining
    context-free languages

12
Defining --gt, gt notation
  • First --gt notation
  • This is used to define the productions of a
    grammar
  • S --gt aSb ab
  • Second gtG notation
  • This is used to denote the application of a
    production rule from a grammar G
  • S gtABG aSb gtABG aaSbb gtABG aaabbb
  • We say that string S derives string aSb (in one
    step)
  • We say that string aSb derives string aaSbb (in
    one step)
  • We say that string aaSbb derives string aaabbb
    (in one step)
  • We often omit the grammar subscript when the
    intended grammar is unambiguous

13
Defining gt continued
  • Third gtkG notation
  • This is used to denote k applications of
    production rules from a grammar G
  • S gt2ABG aaSbb
  • We say that string S derives string aaSbb in two
    steps
  • aSb gt2ABG aaabbb
  • We say that string aSb derives string aaabbb in
    two steps
  • We often omit the grammar subscript when the
    intended grammar is unambiguous

14
Defining gt continued
  • Fourth gtG notation
  • This is used to denote 0 or more applications of
    production rules from a grammar G
  • S gtABG S
  • We say that string S derives string S in 0 or
    more steps
  • S gtABG aaSbb
  • We say that string S derives string aaSbb in 0 or
    more steps
  • aSb gtABG aaSbb
  • We say that string aSb derives string aaSbb in 0
    or more steps
  • aSb gtABG aaabbb
  • We say that string aSb derives string aaabbb in 0
    or more steps
  • We often omit the grammar subscript when the
    intended grammar is unambiguous

15
Defining derivations
  • Derivation of a string x
  • The complete step by step derivation of a string
    x from the start variable S
  • Key fact each step in a derivation makes only
    one application of a production rule from G
  • Example Derivation of string aaabbb using ABG
  • S gtABG aSb gtABG aaSbb gtABG aaabbb
  • Example 2 AG (V, S, S, P) where P S --gtSS a
  • Deriving string aaa
  • S gt SS gt Sa gt SSa gt aSa gt aaa

16
Defining L(G)
  • Generating strings
  • If S gtG x, then grammar G generates string x
  • Note G generates strings which contain terminals
    and nonterminals
  • aSb contains nonterminals and terminals
  • S contains only nonterminals
  • aaabbb contains only terminals
  • L(G)
  • The set of strings over S generated by grammar G
  • Note we only consider terminal strings generated
    by G
  • aibi i gt 0 L(ABG)
  • ai i gt 0 L(AG)

17
Context-Free Languages
  • Context-Free Languages
  • A language L is a context-free language (CFL) iff
    there exists a CFG G such that L(G) L
  • Results so far
  • ai i gt 0 is a CFL
  • One CFG G such that L(G) this language is AG
  • Note this language is also regular
  • aibi i gt 0 is a CFL
  • One CFG G such that L(G) this language is ABG
  • Note this language is NOT regular

18
Example
  • Let BAL the set of strings over (,) in which
    the parentheses are balanced
  • Prove that BAL is a CFL
  • To prove this, you need to come up with a CFG
    BALG such that L(BALG) BAL
  • BALG (V, S, S, P)
  • V S
  • S (, )
  • S S
  • P ?
  • Give derivations of ((( ))) and ( )(( )) with
    your grammar
Write a Comment
User Comments (0)
About PowerShow.com