250 likes | 390 Vues
This document explores the fundamentals of context-free grammar (CFG), including key terminologies such as terminals, non-terminals, and rules. It defines CFG and discusses its components: a finite set of variables, terminal symbols, production rules, and the start symbol. Various examples illustrate derivations and syntax for generating syntactically correct strings. The analysis includes constructs like leftmost and rightmost derivations, and practical applications in language processing, showcasing different grammars and languages through sample derivations and parse trees.
E N D
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>
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
More rules • <adjective-list> -> <adjective><adjective-list> • -> λ • <adjective> -> big • -> fresh • -> brown • <direct-object-phrase> -> <adjective-list><proper-noun> • -> <determiner><adjective-list><common-noun>
More derivations Example: John eats a big fresh burger
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
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.
Terminologies • Context-free Languages • Directly recursive • A=> uAv • Indirectly recursive • A=> uB • B=>Av • Leftmost derivation • Rightmost derivation
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
Sample Derivations • A CFG • G = {V, Σ, P, S} • V = {S, A} • Σ = { a, b} • P: S-> AA A -> AAA| bA| Ab |a
Sample Derivations Sentences/String = ababaa (Draw the Parse Tree)
Sample Derivations Sentences/String = ababaa (Draw the Parse Tree)
Sample Derivations Sentences/String = ababaa (Draw the Parse Tree)
Sample Derivations Sentences/String = ababaa (Draw the Parse Tree)
Parse Tree • Draw the parse/derivation tree for : a * (b00)
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.
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
Examples of Grammars and languages • Palindrome i.e. w=wR • Σ = {a,b} S->a|b|λ S->aSa|bSb
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
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| λ
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
Examples of Grammars and languages • Odd length string over {a} • S->aSa| O • O->a