Computer Science 112 Fundamentals of Programming II Recursive

  • Slides: 35
Download presentation
Computer Science 112 Fundamentals of Programming II Recursive Processing of Languages

Computer Science 112 Fundamentals of Programming II Recursive Processing of Languages

Applications • Grammar checkers in word processors • Programming language compilers • Natural language

Applications • Grammar checkers in word processors • Programming language compilers • Natural language queries (Google, etc. )

Languages and Grammars • A grammar specifies the rules for constructing well-formed sentences in

Languages and Grammars • A grammar specifies the rules for constructing well-formed sentences in a language • Every language, including a programming language, has a grammar

Generate Sentences in English • Given a vocabulary and grammar rules, one can generate

Generate Sentences in English • Given a vocabulary and grammar rules, one can generate some random and perhaps rather silly sentences • Vocabulary - the set of words belonging to the parts of speech (nouns, verbs, articles, prepositions) • Grammar - the set of rules for building phrases in a sentence (noun phrase, verb phrase, prepositional phrase)

The Structure of a Sentence sentence noun phrase verb phrase A sentence is a

The Structure of a Sentence sentence noun phrase verb phrase A sentence is a noun phrase followed by a verb phrase

The Structure of a Sentence sentence noun phrase article verb phrase noun A noun

The Structure of a Sentence sentence noun phrase article verb phrase noun A noun phrase is an article followed by a noun

The Structure of a Sentence sentence noun phrase article the verb phrase noun girl

The Structure of a Sentence sentence noun phrase article the verb phrase noun girl Similar to the behavior of strings so far Pick actual words for those parts of speech at random

The Structure of a Sentence sentence noun phrase article the noun verb phrase verb

The Structure of a Sentence sentence noun phrase article the noun verb phrase verb girl Similar noun phrase prepositional phrase to the behavior of strings so far A verb phrase is a verb followed by a noun phrase and a prepositional phrase

The Structure of a Sentence sentence noun phrase article the noun verb phrase verb

The Structure of a Sentence sentence noun phrase article the noun verb phrase verb noun phrase girl Similarhit to the prepositional phrase behavior of strings so far Pick a verb at random

The Structure of a Sentence sentence noun phrase article noun verb phrase verb noun

The Structure of a Sentence sentence noun phrase article noun verb phrase verb noun phrase article the girl Similarhit to the prepositional phrase noun behavior of strings so far Expand a noun phrase again

The Structure of a Sentence sentence noun phrase article noun verb phrase verb noun

The Structure of a Sentence sentence noun phrase article noun verb phrase verb noun phrase article the prepositional phrase noun girl the boyof Similarhit to the behavior strings so far Pick an article and a noun at random

The Structure of a Sentence sentence noun phrase article noun verb phrase verb noun

The Structure of a Sentence sentence noun phrase article noun verb phrase verb noun phrase article the noun girl the boyof Similarhit to the behavior prepositional phrase preposition noun phrase strings so far A prepositional phrase is a preposition followed by a noun phrase

The Structure of a Sentence sentence noun phrase article noun verb phrase verb noun

The Structure of a Sentence sentence noun phrase article noun verb phrase verb noun phrase article the noun girl the boyof Similarhit to the behavior Pick a preposition at random prepositional phrase preposition stringswith so far noun phrase

The Structure of a Sentence sentence noun phrase article noun verb phrase verb noun

The Structure of a Sentence sentence noun phrase article noun verb phrase verb noun phrase article noun prepositional phrase preposition noun phrase article the girl the boyof Similarhit to the behavior Expand another noun phrase stringswith so far noun

The Structure of a Sentence sentence noun phrase article noun verb phrase verb noun

The Structure of a Sentence sentence noun phrase article noun verb phrase verb noun phrase article noun prepositional phrase preposition noun phrase article the girl the boyof Similarhit to the behavior stringswith so far More random words from the parts of speech a noun bat

Representing the Vocabulary nouns = ['bat', 'boy', 'girl', 'dog', 'cat', 'chair', 'fence', 'table', 'computer',

Representing the Vocabulary nouns = ['bat', 'boy', 'girl', 'dog', 'cat', 'chair', 'fence', 'table', 'computer', 'cake', 'field'] verbs = ['hit', 'threw', 'pushed', 'ate', 'dragged', 'jumped'] prepositions = ['with', 'to', 'from', 'on', 'below', 'above', 'beside'] articles = ['a', 'the'] Use a list of words for each part of speech (lexical category)

Picking a Word at Random nouns = ['bat', 'boy', 'girl', 'dog', 'cat', 'chair', 'fence',

Picking a Word at Random nouns = ['bat', 'boy', 'girl', 'dog', 'cat', 'chair', 'fence', 'table', 'computer', 'cake', 'field'] verbs = ['hit', 'threw', 'pushed', 'ate', 'dragged', 'jumped'] prepositions = ['with', 'to', 'from', 'on', 'below', 'above', 'beside'] articles = ['a', 'the'] import random print(random. choice(verbs)) # Prints a randomly chosen verb The random module includes functions to select numbers, sequence elements, etc. , at random

Grammar Rules sentence = nounphrase verbphrase nounphrase = article noun verbphrase = verb nounphrase

Grammar Rules sentence = nounphrase verbphrase nounphrase = article noun verbphrase = verb nounphrase prepositionalphrase prepositonalphrase = preposition nounphrase A sentence is a noun phrase followed by a verb phrase Etc. , etc.

Define a Function for Each Rule # sentence = nounphrase verbphrase def sentence(): return

Define a Function for Each Rule # sentence = nounphrase verbphrase def sentence(): return nounphrase() + ' ' + verbphrase() Each function builds and returns a string that is an instance of the phrase Separate phrases and words with a space

Define a Function for Each Rule # sentence = nounphrase verbphrase def sentence(): return

Define a Function for Each Rule # sentence = nounphrase verbphrase def sentence(): return nounphrase() + ' ' + verbphrase() # nounphrase = article noun def nounphrase(): return random. choice(articles) + ' ' + random. choice(nouns) When a part of speech is reached, select an instance at random from the relevant list of words

Call sentence() to Try It Out # sentence = nounphrase verbphrase def sentence(): return

Call sentence() to Try It Out # sentence = nounphrase verbphrase def sentence(): return nounphrase() + ' ' + verbphrase() # nounphrase = article noun def nounphrase(): return random. choice(articles) + ' ' + random. choice(nouns) … for x in range(10): print(sentence()) # Display 10 sentences You can also generate examples of the other phrases by calling their functions

Kinds of Symbols in a Grammar • Terminal symbols: words in the vocabulary of

Kinds of Symbols in a Grammar • Terminal symbols: words in the vocabulary of the language • Non-terminal symbols: words that describe phrases or portions of sentences • Metasymbols: used to construct rules

Metasymbols for a Grammar Metasymbols "" = [ ] { } ( ) |

Metasymbols for a Grammar Metasymbols "" = [ ] { } ( ) | Use Enclose literal items Means "is defined as" Enclose optional items Enclose zero or more items Group together required choices Indicates a choice

A Grammar of Arithmetic Expressions expression = term { adding. Operator term } term

A Grammar of Arithmetic Expressions expression = term { adding. Operator term } term = factor { multiply. Operator factor } factor = primary ["^" primary ] primary = number | "(" expression ")" number = digit { digit } digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" adding. Operator = "+" | "-" multiplying. Operator = "*" | "/" Example sentences: 3, 4 + 5, 5 + 2 * 3, (5 + 2) * 3 ^ 4

Alternative Notation: Train Track term = factor { multiplying. Operator factor } factor *

Alternative Notation: Train Track term = factor { multiplying. Operator factor } factor * / primary = number | "(" expression ")" number ( expression )

Parsing • A parser analyzes a source program to determine whether or not it

Parsing • A parser analyzes a source program to determine whether or not it is syntactically correct Source language program Parser OK or not OK Syntax error messages

Scanning • A scanner picks out words in a source program and sends these

Scanning • A scanner picks out words in a source program and sends these to the parser Source language program Scanner Tokens Lexical error messages Parser Ok or not OK Syntax error messages

The Scanner Interface Scanner(a. String) Creates a scanner on a source string get() Returns

The Scanner Interface Scanner(a. String) Creates a scanner on a source string get() Returns the current token (at the cursor) next() Advances the cursor to the next token

Tokens • A Token object has two attributes: – type (indicating an operand or

Tokens • A Token object has two attributes: – type (indicating an operand or operator) – value (an int if it’s an operand, or the source string otherwise) • Token types are – Token. EOE – Token. PLUS, Token. MINUS – Token. MUL, Token. DIV – Token. INT – Token. UNKNOWN

The Token Interface Token(source) Creates a token from a source string str(a. Token) String

The Token Interface Token(source) Creates a token from a source string str(a. Token) String representation is. Operator() True if an operator, false otherwise get. Type() Returns the type get. Value() Returns the value

Recursive Descent Parsing • Each rule in the grammar translates to a Python parsing

Recursive Descent Parsing • Each rule in the grammar translates to a Python parsing method expression = term { adding. Operator term } def expression(self): self. term() token = self. scanner. get() while token. get. Type() in (Token. PLUS, Token. MINUS): self. scanner. next() self. term() token = self. scanner. get()

Recursive Descent Parsing • Each method is responsible for a phrase in an expression

Recursive Descent Parsing • Each method is responsible for a phrase in an expression term = factor { multiplying. Operator factor } def term(self): self. factor() token = self. scanner. get() while token. get. Type() in (Token. MUL, Token. DIV): self. scanner. next() self. factor() token = self. scanner. get()

Recursive Descent Parsing primary = number | "(" expression ")" def primary(self): token =

Recursive Descent Parsing primary = number | "(" expression ")" def primary(self): token = self. scanner. get() if token. get. Type() == Token. INT: self. scanner. next() elif token. get. Type() == Token. L_PAR: self. scanner. next() self. expression() self. accept(self. _scanner. get(), Token. R_PAR, "')' expected") self. scanner. next() else: self. fatal. Error(token, "bad primary")

For Monday Expression Trees

For Monday Expression Trees