{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture-03-6up

lecture-03-6up - How do we combine tokens Not all...

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

View Full Document Right Arrow Icon
Parsers Wednesday, August 31, 2011 How do we combine tokens? Combine tokens (“words” in a language) to form programs (“sentences” in a language) Not all combinations of tokens are correct programs (not all sentences are grammatically correct) How do we define this? Wednesday, August 31, 2011 Producing sentences Here are some possible rules for simplified English: All sentences have a noun phrase, then a verb, then a noun phrase (a subject, a verb, an object) Noun phrases are an article (“a” or “the”), an adjective (“black” or “big”) and a noun (“cat” or “dog”) Verbs can be “eats” or “scratches” Sentences we can create: “a black cat bites the big dog.” “the big dog eats the black cat.” Sentences we can’t: “cat scratches black dog.” “dog the cat bites black.” Wednesday, August 31, 2011 More formally S [entence] ! P V P [noun ] P [hrase] ! R A N [a] R [ticle] ! a | the A [djective] ! big | black N [oun] ! cat | dog V [erb] ! bites | scratches Wednesday, August 31, 2011 Generating strings Productions tell us how to rewrite a non-terminal into a different set of symbols Can rewrite non-terminals until we generate the string we want A parser’s job: do this in reverse! Figure out how a string was produced S ! A B $ A ! A a A ! a B ! B b B ! b To derive the string “ a a b b b ” we can do the following rewrites: S A B $ A a B $ a a B $ a a B b $ a a B b b $ a a b b b $ Wednesday, August 31, 2011 Generalize Grammar G = (V t , V n , S, P) V t is the set of terminals V n is the set of non-terminals S is the start symbol P is the set of productions Each production takes the form: V n ! ! | (V n | V t )+ Grammar is context-free (why?) A simple grammar: G = ({a, b}, {S, A, B}, {S ! A B $, A ! A a, A ! a, B ! B b, B ! b}, S) Wednesday, August 31, 2011
Image of page 1

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

View Full Document Right Arrow Icon
Terminology V is the vocabulary of a grammar, consisting of terminal (V t ) and non-terminal (V n ) symbols For our sample grammar V n = {S, A, B} Non-terminals are symbols on the LHS of a production Non-terminals are constructs in the language that are recognized during parsing V t = {a, b} Terminals are the tokens recognized by the scanner They correspond to symbols in the text of the program Wednesday, August 31, 2011 Terminology Strings are composed of symbols A A a a B b b A a is a string We will use Greek letters to represent strings composed of both terminals and non-terminals L(G) is the language produced by the grammar G All strings consisting of only terminals that can be produced by G In our example, L(G) = a+b+$ All regular expressions can be expressed as grammars for context-free languages, but not vice-versa Consider: a i b i $ (what is the grammar for this?) Wednesday, August 31, 2011 Parse trees Tree which shows how a string was produced by a language Interior nodes of tree: non- terminals Children: the terminals and non-terminals generated by applying a production rule Leaf nodes: terminals S A B A a B b B b b a Wednesday, August 31, 2011 Leftmost derivation
Image of page 2
Image of page 3
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