1 / 23

Context-Free Grammars

Context-Free Grammars. Basic terminologies. Sentences – syntactically correct string Terminal symbols – The elements of the alphabet (Jill, drives, frequently,......) Variables or non-terminals Rules <sentences> -> <noun-phrase><verb-phrase> <sentences> -> <noun-phrase><verb> <direct-

rasha
Download Presentation

Context-Free Grammars

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Context-Free Grammars

  2. Basic terminologies • Sentences – syntactically correct string • Terminal symbols – The elements of the alphabet (Jill, drives, frequently,......) • Variables or non-terminals • Rules • <sentences> -> <noun-phrase><verb-phrase> • <sentences> -> <noun-phrase><verb> <direct- object-phrase>

  3. Rules, variables & terminals • <noun-phrase> -> <proper-noun> • -> <determiner><common-noun> • <proper-noun> -> John • -> Jill • <comon-noun> -> car • -> burger • <determiner> -> a • -> the • <verb-phrase> -> <verb><adverb> • -> <verb> • <verb> -> drives • -> eats • <adverb> -> slowly • -> frequently

  4. derivations

  5. More rules • <adjective-list> -> <adjective><adjective-list> • -> λ • <adjective> -> big • -> fresh • -> brown • <direct-object-phrase> -> <adjective-list><proper-noun> • -> <determiner><adjective-list><common-noun>

  6. More derivations Example: John eats a big fresh burger

  7. definition • Context-Free Grammar A context-free grammar is a quadruple {V, Σ, P, S} where V is a finite set of variables Σ {the alphabet} is a finite set of terminal symbols P is a finite set of rules And S is a distinguished element of V called start symbol. NB: The sets V and Σ are assumed to be disjoint

  8. Conventions • Terminals are represented by lowercase occurring at the beginning of the alphabet, that is a, b, c, ……. • Variables will be denoted by capital letters. • Variables are referred as non-terminals • A rules is often called a production.

  9. Terminologies • Context-free Languages • Directly recursive • A=> uAv • Indirectly recursive • A=> uB • B=>Av • Leftmost derivation • Rightmost derivation

  10. A CFG Sample Derivation (leftmost and rightmost): a * (b00) • leftmost • E -> E * E • E -> I * E • E -> a * E • E -> a * (E) • E -> a * (E + E) • E -> a * (I+ E) • E -> a * (a + E) • E -> a * (a+ I) • E -> a * (a+ I0) • E -> a * (a+ I00) • E -> a * (a+ b00) • Reftmost ??? A CFG for simple expression: • E -> I • E -> E + E • E -> E * E • E -> ( E ) • I -> a • I -> b • I -> Ia • I -> Ib • I -> I0 • I -> I1

  11. Sample Derivations • A CFG • G = {V, Σ, P, S} • V = {S, A} • Σ = { a, b} • P: S-> AA A -> AAA| bA| Ab |a

  12. Sample Derivations Sentences/String = ababaa (Draw the Parse Tree)

  13. Sample Derivations Sentences/String = ababaa (Draw the Parse Tree)

  14. Sample Derivations Sentences/String = ababaa (Draw the Parse Tree)

  15. Sample Derivations Sentences/String = ababaa (Draw the Parse Tree)

  16. Parse Tree • Draw the parse/derivation tree for : a * (b00)

  17. Examples of Grammars and languages • S->aSa|aBa • B-> bB|b L(G) = {anbman| n>o, m>o} • Equal number a’s leading and trailing • Any number of b’s • At least one b.

  18. Examples of Grammars and languages • S-> aSdd|A • A->bAc|bc L(G)={anbmcmd2n|n>=0, m>0} • No. of d’s are twice as many as a’s • Equal no. of b’s and c’s

  19. Examples of Grammars and languages • Palindrome i.e. w=wR • Σ = {a,b} S->a|b|λ S->aSa|bSb

  20. Examples of Grammars and languages • At least one b for every a and at most two. S-> aSb|aSbb| λ • Equal number of ab’s (leading) and c’s (trailing). • Any number of b+ outside S-> abScB| λ B-> bB|b

  21. Examples of Grammars and languages • Grammar for a+b* • S->AB • A->aA|a • B->bB| λ • Grammar for a*ba*ba* • S->AbAbA • A->aA| λ

  22. Examples of Grammars and languages • Even length string over {a,b} • S->aO|bO| λ • O->aS|bS • Even length string over {a} • S->aO| λ • O->aS

  23. Examples of Grammars and languages • Odd length string over {a} • S->aSa| O • O->a

More Related