notes20 Context-free Grammars and Ambiguity

notes20 Context-free Grammars and Ambiguity - CS 373 Theory...

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

View Full Document Right Arrow Icon
CS 373: Theory of Computation Gul Agha Mahesh Viswanathan Fall 2010 1
Image of page 1

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

View Full Document Right Arrow Icon
1 Context Free Grammars 1.1 Definitions Context-Free Grammars Definition 1. A context-free grammar (CFG) is G = ( V, Σ , R, S ) where V is a finite set of variables/non-terminals. Σ is a finite set of terminals. Σ is disjoint from V . R is a finite set of rules or productions of the form A α where A V and α ( V Σ) * S V is the start symbol Example: Palindromes Example 2 . A string w is a palindrome if w = w R . G pal = ( { S } , { 0 , 1 } , R, S ) defines palindromes over { 0 , 1 } , where R is S S 0 S 1 S 0 S 0 S 1 S 1 Or more briefly, R = { S | 0 | 1 | 0 S 0 | 1 S 1 } Language of a CFG Derivations Expand the start symbol using one of its rules. Further expand the resulting string by expanding one of the variables in the string, by the RHS of one of its rules. Repeat until you get a string of terminals. For the grammar G pal = ( { S } , { 0 , 1 } , { S | 0 | 1 | 0 S 0 | 1 S 1 } , S ) we have S 0 S 0 00 S 00 001 S 100 0010100 Derivations Formal Definition Definition 3. Let G = ( V, Σ , R, S ) be a CFG. We say αAβ G αγβ , where α, β, γ ( V Σ) * and A V if A γ is a rule of G . We say α * G β if either α = β or there are α 0 , α 1 , . . . α n such that α = α 0 G α 1 G α 2 G · · · ⇒ G α n = β 2
Image of page 2
Notation When G is clear from the context, we will write and * instead of G and * G . Context-Free Language Definition 4. The language of CFG G = ( V, Σ , R, S ), denoted L ( G ) is the collection of strings over the terminals derivable from S using the rules in R . In other words, L ( G ) = { w Σ * | S * w } Definition 5. A language L is said to be context-free if there is a CFG G such that L = L ( G ). 1.2 Proving Properties Palindromes Revisited Recall, L pal = { w ∈ { 0 , 1 } * | w = w R } is the language of palindromes. Consider G pal = ( { S } , { 0 , 1 } , R, S ) defines palindromes over { 0 , 1 } , where R = { S | 0 | 1 | 0 S 0 | 1 S 1 } Proposition 6. L ( G pal ) = L pal Proving Correctness of CFG L pal L ( G pal ) Proof. Let w L pal . We prove that S * w by induction on | w | . Base Cases: If | w | = 0 or | w | = 1 then w = or 0 or 1. And S | 0 | 1.
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern