lecture-03-6up - Parsers Wednesday, August 31, 2011 How do...

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: 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 deFne this? Wednesday, August 31, 2011 Producing sentences Here are some possible rules for simpliFed 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 cant: 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 parsers job: do this in reverse! igure 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 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-...
View Full Document

This note was uploaded on 02/19/2012 for the course ECE 468 taught by Professor Test during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 11

lecture-03-6up - Parsers Wednesday, August 31, 2011 How do...

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

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