1 / 20

Grammars

Grammars. Grammars. A formal definition of the syntactic structure of a language Set of rules that tell you whether a sentence is correctly structured a sentence is a well-formed string in the language rules specify the order of components and their sub-components in a sentence.

odin
Download Presentation

Grammars

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Grammars

  2. Grammars • A formal definition of the syntactic structure of a language • Set of rules that tell you whether a sentence is correctly structured • a sentence is a well-formed string in the language • rules specify the order of components and their sub-components in a sentence

  3. Languages • Natural language • language spoken or written by humans • English, French, Italian, Spanish, … • English and other natural languages clearly have structure • subjects, nouns, verbs, … • In contrast we have programming languages • languages used to communicate with computers • Java, C, C++, … • programming languages also have structure • expressions, statements, methods, …

  4. English grammar • A set of rules for combining words into well-formed phrases and clauses

  5. Clauses • A clause is a group of related words containing a subject and a verb • building blocks of sentences • Independent clause • can stand alone as complete sentence Tammy is great! • Dependent clause • unable to stand alone as complete sentence Although Tammy is great • “Although” is a dependent word: allows clause to be embedded in another sentence Although Tammy is great, this class is still boring.

  6. Phrases • A phrase is a group of related words that does not contain a subject-verb relationship • can consist of a single word or a group of words • noun-phrases and verb-phrases Dogsbite. Some dogsbite. The mean dog in the parkbites. Some dogsbite people. • a noun-phrase can be contained in a verb-phrase bite people • the noun people is the object of the verb bite

  7. An English grammar <SENTENCE> => <NOUN-PHRASE> <VERB-PHRASE> <NOUN-PHRASE> => <NOUN> | <ARTICLE> <NOUN> | <ARTICLE> <ADJECTIVE> <NOUN> <VERB-PHRASE> => <VERB> | <VERB> <NOUN-PHRASE> • The above grammar specifies the rules for generating a syntactically correct English sentence • This means … • given lists of nouns, articles, adjectives, and verbs, we can generate any number of syntactically correct English sentences

  8. Example <NOUN> => DOG | CAT | WATER <VERB> => BIT | SNIFFED | DRANK | SCRATCHED <ARTICLE> => THE | A <ADJECTIVE> => STINKY | HAPPY | MEAN • According to our grammar, the following sentences are syntactically correct: THE HAPPY DOG SNIFFED THE STINKY CAT A MEAN CAT SCRATCHED THE DOG THE CAT BIT THE DOG A STINKY DOG DRANK THE WATER THE HAPPY WATER DRANK THE STINKY DOG THE MEAN WATER SCRATCHED THE HAPPY CAT

  9. <SENTENCE> <NOUN-PHRASE> <VERB-PHRASE> <NOUN-PHRASE> <ARTICLE> <ADJECTIVE> <NOUN> <VERB> <ARTICLE> <ADJECTIVE> <NOUN> THE HAPPY DOG SNIFFED THE STINKY CAT Recognition • Grammars can also be used to recognize syntactically correct sentences. For example, the sentence THE HAPPY DOG SNIFFED THE STINKY CAT can be fit to the grammar in the following way

  10. <SENTENCE> <NOUN-PHRASE> <VERB-PHRASE> <NOUN-PHRASE> <ARTICLE> <NOUN> <VERB> <ARTICLE> <NOUN> THE DOG SNIFFED THE CAT Optional parts of speech • Our grammar provided us with options for the phrases, we can also fit the sentence THE DOG SNIFFED THE CAT to the grammar

  11. Syntax and semantics • Syntax only tells you if the sentence is constructed correctly • Semantics tells you whether a correctly structured sentence makes any sense • The sentences THE HAPPY WATER DRANK THE STINKY DOG THE MEAN WATER SCRATCHED THE HAPPY CAT are correct syntactically speaking but something appears wrong … • water usually isn’t happy or mean and usually doesn’t drink or scratch either

  12. Parsing • Process of taking sentence and fitting it to grammar is called parsing DOG BIT CAT <NOUN> <VERB> <NOUN> <NOUN-PHRASE> <VERB-PHRASE> <SENTENCE> • Parsing English is complex because of context dependence • Natural language understanding is one of the hardest problems of artificial intelligence • human language is complex, irregular and diverse • philosophical problems of meaning

  13. Formal specifications • Need a precise notation of syntax of a language • grammars can be used for generation and recognition • Context-free grammars <name> => sequence of letters and/or digits that begins with a letter <name> => gikB <name> => msg42 • Substitute as many times as necessary. All legal statements can be generated this way 

  14. Context-free grammar person = firstname + " " + lastname; • How do we get this from our grammar? • Unlike natural languages like English, all the legal strings in a programming language can be specified using a context-free grammar

  15. Recursive Sentence Generator • Constructs sentences, paragraphs, and even papers that fit a prescribed format. • The format is specified by a set of rules called a grammar • A grammar consists of a set of definitions • Each definition is a set of productions • Examples of grammars (Lab 7) • Extension request • College rejection letter • Poem

  16. Poem Grammar • All grammars begin with start rule { <start> The <object> <verb> tonight. ; } • Nonterminals are indicated by angle brackets { <object> waves ; big yellow flowers ; slugs ; }

  17. More on the poem grammar • Nonterminals can refer to other nonterminals { <verb> sigh <adverb> ; portend like <object> ; } { <adverb> warily ; grumpily and <adverb> ; }

  18. Generating a poem <start> • All sentences start with <start> • Only one production in the definition of <start> The <object> <verb> tonight. • Expand each grammar element from left to right • The is a terminal, so it is simply printed • <object> is a non-terminal, so it must be expanded • Choose one: • waves • big yellow flowers • slugs • Suppose that slugs is chosen

  19. Generating a poem The slugs <verb> tonight. • <verb>is a non-terminal, so it must be expanded • Choose one: • sigh <adverb> • portend like <object> The slugs sigh <adverb> tonight. • <adverb> is a non-terminal, so it must be expanded • Choose one: • warily • grumpily • Suppose that grumpily is chosen

  20. A complete poem The slugs sigh grumpily tonight. • tonight. is a terminal so it is simply printed • There are no more non-terminals to expand! • The grammar has generated a complete poem Question: • Why is this called a recursive sentence generator?

More Related