12.07.2015 Views

Think Python - Denison University

Think Python - Denison University

Think Python - Denison University

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1.4. Formal and natural languages 5Formallanguagestendtohavestrictrulesaboutsyntax. Forexample,3+3=6isasyntacticallycorrectmathematical statement, but 3+=3$6 is not. H 2 O is a syntactically correct chemical formula,but 2 Zz isnot.Syntax rules come in two flavors, pertaining to tokens and structure. Tokens are the basic elementsof the language, such as words, numbers, and chemical elements. One of the problems with 3+=3$6isthat$isnotalegaltokeninmathematics(atleastasfarasIknow). Similarly, 2 Zzisnotlegalbecause there isno element withtheabbreviation Zz.The second type of syntax error pertains to the structure of a statement; that is, the way the tokensarearranged. Thestatement3+=3$6isillegalbecauseeventhough+and=arelegaltokens,youcan’t have one right after the other. Similarly, in a chemical formula the subscript comes after theelement name, not before.Exercise 1.1 Write a well-structured English sentence with invalid tokens in it. Then write anothersentence withall validtokens but withinvalidstructure.When you read a sentence in English or a statement in a formal language, you have to figure outwhat the structure of the sentence is (although in a natural language you do this subconsciously).This process iscalled parsing.For example, when you hear the sentence, “The penny dropped,” you understand that “the penny”is the subject and “dropped” is the predicate. Once you have parsed a sentence, you can figure outwhat it means, or the semantics of the sentence. Assuming that you know what a penny isand whatitmeans todrop, you willunderstand thegeneral implication of thissentence.Although formal and natural languages have many features in common—tokens, structure, syntax,and semantics—there are somedifferences:ambiguity: Natural languages are full of ambiguity, which people deal with by using contextualcluesandotherinformation. Formallanguagesaredesignedtobenearlyorcompletelyunambiguous,which means that any statement has exactly one meaning, regardless ofcontext.redundancy: In order to make up for ambiguity and reduce misunderstandings, natural languagesemploy lots of redundancy. As a result, they are often verbose. Formal languages are lessredundant and more concise.literalness: Natural languages arefullofidiomandmetaphor. IfIsay,“The penny dropped,” thereisprobably no penny and nothing dropping 1 . Formal languages mean exactly what they say.People who grow up speaking a natural language—everyone—often have a hard time adjusting toformal languages. In some ways, the difference between formal and natural language is like thedifference between poetry and prose, but more so:Poetry: Words areused fortheirsounds as wellas fortheir meaning, and thewhole poem togethercreates an effect or emotional response. Ambiguity isnot only common but oftendeliberate.Prose: Theliteralmeaningofwordsismoreimportant,andthestructurecontributesmoremeaning.Prose ismoreamenable toanalysis than poetry but stilloftenambiguous.Programs: Themeaningofacomputerprogramisunambiguousandliteral,andcanbeunderstoodentirely by analysis of thetokens and structure.1 This idiommeans that someonerealized somethingafter aperiodof confusion.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!