3. CONTEXTFREE GRAMMARS - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

3. CONTEXTFREE GRAMMARS

Description:

... number of adjectives, including, preceding ... 3' noun phrase adjective-list proper-noun 4' noun phrase determiner adjective-list common noun ... – PowerPoint PPT presentation

Number of Views:89
Avg rating:3.0/5.0
Slides: 21
Provided by: Naz2
Category:

less

Transcript and Presenter's Notes

Title: 3. CONTEXTFREE GRAMMARS


1
3. CONTEXT-FREE GRAMMARS
2
  • 3.1 Introduction
  • An approach generating the string of the
    language.
  • A sentence of the language a syntactically
    correct string.
  • Example
  • The alphabet of our language is the set. a,
    the, Ahmad, Ali, burger, car, drivers, eats,
    slowly, frequently, big, brown
  • The element of the alphabet are called the
    terminal symbols of the language.
  • 2 rules of generating a sentence, e.g. var
    ltsentencegt
  • ltsentencegt ? ltnoun phrasegtltverb-phrasegt
  • ltsentencegt ? ltnoun phrasegtltverb-phrasegtltdirect-obj
    ect-phrasegt
  • ltnoun phrasegt ? ltproper noungt
  • ltnoun phrasegt ? ltdeterminergtlt common-noungt
  • ltproper noungt ? Ahmad
  • ltproper noungt ? Ali
  • lt common-noungt ? car
  • lt common-noungt ? burger

3
  • ltdeterminergt ? a
  • ltdeterminergt ? the
  • ltverb-phrasegt ? ltverbgtltadverbgt
  • ltverb-phrasegt ? ltverbgt
  • ltverbgt ? drivers
  • ltverbgt ? eats
  • ltadverbgt ? slowly
  • ltadverbgt ? frequently

4
  • The sentence Ahmad drives frequently is
    generated by the following transformations
  • Derivation Rule Applied
  • ltsentencegt ? ltnoun phrasegt ltverb-phrasegt 1
  • ? ltproper-noungt ltverb-phrasegt 3
  • ? Ahmad ltverb-phrasegt 6
  • ? Ahmad ltverbgtltadverbgt 11
  • ? Ahmad drives ltadverbgt 13
  • ? Ahmad drives frequently 16
  • To complete the set of rules, the transformation
    for ltdirect-object-phrasegt
  • must be given.
  • e.g. To include any number of adjectives,
    including, preceding the direct object as
    shown in the following strings.

5
  • Ali eats burger
  • Ali eats a big burger
  • Ali eats a big juicy burger
  • Ali eats a big brown juicy burger
  • Ali eats a big big brown juicy burger
  • Rules of grammar must be cup able of generating
    strings of arbitrary length.The use of recursive
    definition.
  • ltadjective-listgt ? ltadjectivegt ltadjective-listgt
  • ltadjective-listgt ? ?
  • ltadjectivegt ? big
  • ltadjectivegt ? juicy
  • ltadjectivegt ? brown
  • ltdirect-object-phrasegt ?ltadjective-listgtltcommongt
  • ltdirect-object-phrasegt ?ltdeterminergtltadjective-lis
    tgtltcommongt

6
  • Exercise
  • Write the sequence of rule applications for this
    sentence
  • Ali eats a big juicy burger.
  • modification
  • 3 ltnoun phrasegt ? ltadjective-listgt
    ltproper-noungt
  • 4 ltnoun phrasegt ? ltdeterminergt
    ltadjective-listgtltcommon noungt
  • With this modification, the string big Ali eats
    frequently can be derived from the variable
    ltsentencegt.

7
  • 3.1 Context-Free Grammars and Languages
  • Formal system of generating the strings of a
    language.
  • Definition 3.1.1
  • A context-free grammar is a quadruple ( V,S,P,S)
  • where - V is a finite set of variables
  • - S (the alphabet) is a finite set of
    terminal symbols.
  • - P is a finite set of rules.
  • - S is a distinguished element of V called
    the start symbol.
  • The set V and S are assumed to be disjoint.
  • A rule, called production is an element of the
    set VX (V ? S).
  • The rule A,w is usually written A ? w
  • A rule, A ? x, is called a null or lambda rule.

8
  • A ? w
  • produces the string uwr
  • Sentence uAr
  • uAr ? uwr u prefix ,
  • v suffix define the context in
    which variable A
    occurs.
  • free ? general applicability of the rules.
  • A string w is derivable from V if there is a
    finite seq. of rule applications that transform v
    to w. i. e. If a seq. of
  • i.e v ?wi ?w2 ?. wn w
  • transformation can be constructed from the rules
    of the grammar.
  • v ?w

9
  • Definition 3.1.2
  • Let G (V,S,P,S) be a context-free grammar
  • and V ? ( V ? S ).
  • The set of string derivable from v is defined
    recursively as follows
  • Basis V is derivable from v.
  • Recursive step If u xAy is derivable from v
    and A ?w ?P,
  • then xwy is derivable from v.
  • Closure Precisely those strings constructed
    from v by finitely many applications of
    the recursive step are derivable from v.
  • Some rotations
  • ? definition of rule
  • ? application ( transforms one string to
    another)
  • ? derivability utilizing one or more rule
    application.
  • n? derivation length from one to other
  • G? derivation utilizes the rule of grammar G

10
  • Definition 3.1.3
  • Let G (V,S,P,S) be a context-free grammar.
  • A string w ? (V? S) is a sentential form of G if
    there is s derivatives S?w in G.
  • A string w ? S is a sentence of G if there is a
    derivation S ?w in G
  • The language of G, demoted L (G), is the set w
    ? S S ?w
  • Sentential forms-string derivable from the start
    symbols of the grammar.
  • The sentences are the sentential form that
    contain only terminal symbols.
  • The language of grammar is the set of sentences
    generated by the grammar.
  • A set of strings over an alphabet S is called a
    context-free language if there is a context-free
    grammar that generate it.

11
  • Rule A ? u Av directly recursive
  • This rule can generate any member of copies of
    the string u followed by an A and an equal number
    of Vs.
  • A variable A is called recursive if there is
    derivation
  • A ? u Av
  • A w ? u Av , A is not in w ? indirectly
    recursive.
  • Example Derivation of ababaa in G A grammar
    G that generates the language consisting of
    strings with a positive, even number of as.
  • G ( V,S,P,S )
  • V S,A
  • S a,b
  • P S ? AA
  • A ? AAA bAAba

12
  • S ? AA S ? AA S ? AA
  • ? aA ? AAA ? Aa
  • ? aAAA ? aAAA ? AAAa
  • ? abAAA ? abAAA ? AAbAa
  • ? abaAA ? abaAA ? AAbaa
  • ? ababAA ? ababAA ? AbAbaa
  • ? ababaA ? ababaA ? Ababaa
  • ? ababaa ? ababaa ? ababaa
  • (a) (b) (c)
  • left most left most right most
  • S ? AA
  • ? aA
  • ? aAAA
  • ? aAAa
  • ? abAAa (d) left most right most.
  • ? abAbAa
  • ? ababAa
  • ? ababaa

13
  • WEEK ? 3
  • Definition 3.1.4
  • Let G ( V,?,P,S ) be a context-free grammar and
    S G? w a derivation.
  • The derivation tree, DT, of S G? w is an
    ordered tree that can be built interactively as
    follows
  • Initialize DT with roots.
  • If A ? x1x2 xn with xi (V ??) is the rule in
    the derivation applied to the string uAv, then
    add x1,x2, xn as the children of A in the tree.
  • If A ?? is the rule in the derivation applied to
    the string uAv, then add ? as the only child of A
    in the tree.

14
  • Example 3.2.1
  • Let G be the grammar given by the productions.
  • S ? aSaaBa
  • B?bBb
  • Then L (G) anbman ngt0, mgt0
  • Explanation
  • The rule S?aSa recursively builds an equal
    number of as on each end of the string.
  • The recursion is terminated by the application of
    the rule S?aBa, ensuring at least one leading and
    one trailing a.
  • The recursive B rule then generates any number of
    bs. To remove the variable B from the string and
    obtain a sentence of the language, the rule B ?b
    must be applied, forcing the presence of at least
    one b.

15
  • Exercise
  • L (G) anbmcmd2n n0, mgt0
  • is given by the production
  • S ? aSdd A
  • A ? bAc bc
  • give ? valid string
  • example
  • A string w is a palindrome if w wR , ?
    a,b
  • write the rules of s to generate such string.
  • S ? a b ?
  • Consider the grammar
  • S ? abscB ?
  • B ? bB b,
  • Explain what kind of string generated by that
    rules.

16
  • Example 3.2.6
  • Let G1 and G2 be the grammars
  • G1 S ?AB G2 S ?aS aB
  • A ?aA a B
    ? bB ?
  • B ? bB ?
  • Both grammars generated the language
  • a b
  • Note a aa , non null string of as
  • The A rules on G1 provide the standard method of
    generately a non null
  • string of as use of ? rule to terminate the
    recursion allow the posibility of having no bs.
  • Grammar G2 builds the same language in a
    left-to-right manner.

17
  • Exercise
  • Write grammar G1 and G2 generate the strings
    over a,b that contain exactly two bs
  • ? a baba8
  • G1 S ? AbAbA G2 S ? as bA
  • A ? aA ? A ? aA bc
  • C ? aC ?
  • (2) Modify the above grammar to generate with
    at least two bs.
  • G1 S ? AbAbA G2 S ? as bA
  • A ? aA bA ? A ? aA bc
  • C ? aC bc ?

18
A Context-Free Grammar For Pascal
  • John Backus (1959) and Peter Naur (1963) used a
    system of rules to define the programming
    language ALGOL 60,
  • Known as BNF (Backus-Naur form)
  • BNF definition of Pascal
  • ltprogramgt ? ltprogram headinggt ltprogram blockgt
  • ltprogram blockgt ? ltblockgt
  • ltprogram headinggt ? program ltidentifiergtltfile
    identifiergt , ltfile
    identifiergt)
  • ltfile identifiergt ? ltidentifiergt
  • ltidentifiergt ? ltlettergt ltletter or digitgt
  • ltletter or digitgt ? ltlettergt ltdigitgt
  • ltlettergt ? abcz
  • ltdigitgt ? 012..9

19
Example 3.6.1 The constant 5 is generated by the
derivation
  • ltexpressiongt ? ltsimple expressiongt
  • ? lttermgt
  • ? ltfactorgt
  • ? ltunsigned constantgt
  • ? ltunsigned numbergt
  • ? ltunsigned integergt
  • ? ltdigitgt
  • ? 5

20
Example 3.6.2 The expression x 5
  • ltexpressiongt ? ltsimple expressiongt
  • ? ltsimple
    expressiongtltadding operatorgtlttermgt
  • ? lttermgtltadding
    operatorgtlttermgt
  • ? ltfactorgt gtltadding
    operatorgtlttermgt
  • ? ltvariablegt gtltadding
    operatorgtlttermgt
  • ? ltentire variablegt
    gtltadding operatorgtlttermgt
  • ? ltvariable identifiergt
    gtltadding operatorgtlttermgt
  • ? ltidentifiergt gtltadding operatorgtlttermgt
  • ? ltlettergtltidentifier-tailgt
    gtltadding operatorgtlttermgt
  • ? x ltidentifier-tailgtltaddin
    g operatorgtlttermgt
  • ? x ltadding
    operatorgtlttermgt
  • ? x lttermgt
  • ? x 5
Write a Comment
User Comments (0)
About PowerShow.com