10 - 10: Context-Free Languages Context-free grammars is a...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
10: Context-Free Languages Context-free grammars is a language generator that is more powerful than regular expressions. important for parsing programs Example: Σ= { a, b } The language L represented by a ( a b ) b can be generated by the following set of grammar rules. S aMb S begins with a and ends with b and has a middle part M . M A Them idd lepartcanbeastr ing A ,or M B a string B . A e A string A can be the empty string, or A aA any number of a ’s. B e A string B can be the empty string, or B bB any number of b ’s. 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Context-free grammar A context-free grammar G is a 4-tuple ( V, Σ ,R,S )where V is an alphabet (containing nonterminals and termi- nals), Σ( V ) is a nonempty set of terminals (elements of V Σ are the nonterminals ), R is a nonempty, Fnite set of rules ,with R ( V Σ) × V . S is the start symbol ( V Σ). Example: G =( Σ ) where V = { a, b, M, A, B } Σ= { a, b } R = { ( S, aMb ) , ( M, A ) , ( M, B ) , ( A, e ) , ( A, aA ) , ( B,e ) , ( B,bB ) } instead of writing ( A, u ), we will simply write A u . Note: in general, a grammar (not necessarily a CFG) can have rules that replace any string of terminals or nonterminals by any other string of terminals or nonterminals. For example, aB bA could be a rule. CFG restricts the rules to be R ( V Σ) × V , which means the left-hand side of each rule must be a single nonterminal. 2
Background image of page 2
Derivation If A u and x, y V , we write xAy xuy w 0 w 1 w 2 ... w n is called a derivation in G of w n from w 0 ,where n is the length of the derivation.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 14

10 - 10: Context-Free Languages Context-free grammars is a...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online