25. CFG - Maggie Johnson Handout #25 CS103B Context-Free...

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

View Full Document Right Arrow Icon
Maggie Johnson Handout #25 CS103B Context-Free Grammars Key Topics * Introduction * Context Free Grammars * Parse Trees ______________________________________________________________________ Thanks to Mehran Sahami for portions of this handout Introduction As you may know, the earliest programs were written in either machine or assembly language since these were the only languages that computers directly understood. Programming in these languages was (and is) quite cumbersome and prone to error, so early researchers developed English-like high-level languages which were easier for human programmers to work with. Unfortunately, computers still understood only the lower-level languages. So, programs written in high-level languages had to be translated back into a low-level language before they could be executed. The designers of the first high-level languages realized that the problem of translation is analogous to the problems humans face every day when they decipher the sentences that they hear and read in English. Elementary schools used to be called Grammar schools because one of the most important subjects taught was English grammar. A grammar is a set of rules by which valid sentences in a language are constructed. Our ability to understand the meaning of a sentence depends on our ability to understand how it was formed from the rules of grammar. Determining how a sentence can be formed from the rules is called parsing . There are two sets of rules in English grammar. One set is the semantic rules which are concerned only with the meaning of the sentence. The other set is the syntax rules which are concerned only with the form of the sentence. The following sentences are syntactically valid in English but are not semantically valid: Trees fly. Fred melts. Cars swim. Some of the rules of English grammar are: 1. A sentence can be a subject followed by a predicate . 2. A subject can be a noun-phrase . 3. A noun-phrase can be an adjective followed by a noun-phrase 4. A noun-phrase can be an article followed by a noun-phrase . 5. A noun-phrase can be a noun . 6. A predicate can be a verb followed by a noun-phrase . 7. A noun can be: apple | bear | cat | dog 8. A verb can be: eats | follows | gets | hugs
Background image of page 1

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

View Full DocumentRight Arrow Icon
9. An adjective can be: itchy | jumpy 10. An article can be: a | an | the Is there anything wrong with this grammar? How might you fix it? _______ Now, consider the following sentence: The itchy bear hugs the jumpy dog . The method by which this sentence can be generated is illustrated below: Rule 1 1. sentence -> subject predicate 2 2. noun-phrase predicate 6 3. noun-phrase verb noun-phrase 4 4. article noun-phrase verb noun-phrase 3 5. article adjective noun-phrase verb noun-phrase 5 6. article adjective noun verb noun-phrase 4 7. article adjective noun verb article noun-phrase 3 8. article adjective noun verb article adjective noun-phrase 5 9. article adjective noun verb article adjective noun 10 10. the adjective noun verb article adjective noun
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/01/2011 for the course CS 103B taught by Professor Sahami,m during the Winter '08 term at Stanford.

Page1 / 11

25. CFG - Maggie Johnson Handout #25 CS103B Context-Free...

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