1 / 69

Syntax and related matters

Syntax and related matters. Programming Fundamentals 10 Feliks Klu ź niak. Consider the following collection of words: “John” , “ Mary” , “loves” , “bad” , “dog” , “hates” , “nice” , “children”. Consider the following collection of words:

tilden
Download Presentation

Syntax and related matters

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. Syntax and related matters Programming Fundamentals 10 Feliks Kluźniak Syntax etc.

  2. Consider the following collection of words: “John” , “Mary” , “loves” , “bad” , “dog” , “hates” , “nice” , “children” Syntax etc.

  3. Consider the following collection of words: “John” , “Mary” , “loves” , “bad” , “dog” , “hates” , “nice” , “children” One might want to construct simple sentences out of these words, by putting them together. Syntax etc.

  4. Consider the following collection of words: “John” , “Mary” , “loves” , “bad” , “dog” , “hates” , “nice” , “children” One might want to construct simple sentences out of these words, by putting them together. For example: “Mary loves bad dog” Syntax etc.

  5. Consider the following collection of words: “John” , “Mary” , “loves” , “bad” , “dog” , “hates” , “nice” , “children” One might want to construct simple sentences out of these words, by putting them together. For example: “Mary loves bad dog” “bad dog hates nice children” Syntax etc.

  6. Consider the following collection of words: “John” , “Mary” , “loves” , “bad” , “dog” , “hates” , “nice” , “children” One might want to construct simple sentences out of these words, by putting them together. For example: “Mary loves bad dog” “bad dog hates nice children” “John Mary dog bad loves nice” Syntax etc.

  7. Consider the following collection of words: “John” , “Mary” , “loves” , “bad” , “dog” , “hates” , “nice” , “children” One might want to construct simple sentences out of these words, by putting them together. For example: “Mary loves bad dog” “bad dog hates nice children” “John Mary dog bad loves nice” Hmm. Apparently not all sequences of words will do. Syntax etc.

  8. Consider the following collection of words: “John” , “Mary” , “loves” , “bad” , “dog” , “hates” , “nice” , “children” One might want to construct simple sentences out of these words, by putting them together. For example: “Mary loves bad dog” “bad dog hates nice children” “John Mary dog bad loves nice” Hmm. Apparently not all sequences of words will do. What is needed is some consideration of the syntax of English sentences: they must be constructed according to certain rules. Syntax etc.

  9. Consider the following collection of words: “John” , “Mary” , “loves” , “bad” , “dog” , “hates” , “nice” , “children” One might want to construct simple sentences out of these words, by putting them together. For example: “Mary loves bad dog” “bad dog hates nice children” “John Mary dog bad loves nice” Hmm. Apparently not all sequences of words will do. What is needed is some consideration of the syntax of English sentences: they must be constructed according to certain rules. Such a collection of rules is called a grammar. Syntax etc.

  10. “John” , “Mary” , “loves” , “bad” , “dog” , “hates” , “nice” , “children” Mary loves bad dog” “bad dog hates nice children” “John Mary dog bad loves nice” What is needed is some consideration of the syntax of English sentences: they must be constructed according to certain rules. Such a collection of rules is called a grammar. A very simple grammar, for a very small subset of English, could look like this: Sentence = NounPhrase Verb NounPhrase . This means that a sentence consists of a noun phrase, followed by a verb, followed by a noun phrase. Syntax etc.

  11. “John” , “Mary” , “loves” , “bad” , “dog” , “hates” , “nice” , “children” Mary loves bad dog” “bad dog hates nice children” “John Mary dog bad loves nice” What is needed is some consideration of the syntax of English sentences: they must be constructed according to certain rules. Such a collection of rules is called a grammar. A very simple grammar, for a very small subset of English, could look like this: Sentence = NounPhrase Verb NounPhrase . This means that a sentence consists of a noun phrase, followed by a verb, followed by a noun phrase. Sentence is being defined here: it is on the LHS of the equality sign. Syntax etc.

  12. “John” , “Mary” , “loves” , “bad” , “dog” , “hates” , “nice” , “children” Mary loves bad dog” “bad dog hates nice children” “John Mary dog bad loves nice” What is needed is some consideration of the syntax of English sentences: they must be constructed according to certain rules. Such a collection of rules is called a grammar. A very simple grammar, for a very small subset of English, could look like this: Sentence = NounPhrase Verb NounPhrase . This means that a sentence consists of a noun phrase, followed by a verb, followed by a noun phrase. Sentence is being defined here: it is on the LHS of the equality sign. The stuff on the RHS forms the definition. Syntax etc.

  13. “John” , “Mary” , “loves” , “bad” , “dog” , “hates” , “nice” , “children” Mary loves bad dog” “bad dog hates nice children” “John Mary dog bad loves nice” What is needed is some consideration of the syntax of English sentences: they must be constructed according to certain rules. Such a collection of rules is called a grammar. A very simple grammar, for a very small subset of English, could look like this: Sentence = NounPhrase Verb NounPhrase. This means that a sentence consists of a noun phrase, followed by a verb, followed by a noun phrase. Sentence is being defined here: it is on the LHS of the equality sign. The stuff on the RHS forms the definition. The rule ends with a period. Syntax etc.

  14. “John” , “Mary” , “loves” , “bad” , “dog” , “hates” , “nice” , “children” Mary loves bad dog” “bad dog hates nice children” “John Mary dog bad loves nice” What is needed is some consideration of the syntax of English sentences: they must be constructed according to certain rules. Such a collection of rules is called a grammar. A very simple grammar, for a very small subset of English, could look like this: Sentence = NounPhrase Verb NounPhrase . NounPhrase = [ Adjective ] Noun . A noun phrase is either an adjective followed by a noun, or just a noun. The square brackets mean that their contents is optional. Syntax etc.

  15. “John” , “Mary” , “loves” , “bad” , “dog” , “hates” , “nice” , “children” Mary loves bad dog” “bad dog hates nice children” “John Mary dog bad loves nice” What is needed is some consideration of the syntax of English sentences: they must be constructed according to certain rules. Such a collection of rules is called a grammar. A very simple grammar, for a very small subset of English, could look like this: Sentence = NounPhrase Verb NounPhrase . NounPhrase = [ Adjective ] Noun . Adjective = “bad” | “nice” . An adjective is either the word “bad” or the word “nice” . The vertical bar separates alternatives. Syntax etc.

  16. “John” , “Mary” , “loves” , “bad” , “dog” , “hates” , “nice” , “children” Mary loves bad dog” “bad dog hates nice children” “John Mary dog bad loves nice” What is needed is some consideration of the syntax of English sentences: they must be constructed according to certain rules. Such a collection of rules is called a grammar. A very simple grammar, for a very small subset of English, could look like this: Sentence = NounPhrase Verb NounPhrase . NounPhrase = [ Adjective ] Noun . Adjective = “bad” | “nice” . An adjective is either the word “bad” or the word “nice” . The vertical bar separates alternatives. The double quotes enclose actual symbols (words) that would appear in a sentence. Technically, these are called terminal symbols. Syntax etc.

  17. “John” , “Mary” , “loves” , “bad” , “dog” , “hates” , “nice” , “children” Mary loves bad dog” “bad dog hates nice children” “John Mary dog bad loves nice” What is needed is some consideration of the syntax of English sentences: they must be constructed according to certain rules. Such a collection of rules is called a grammar. A very simple grammar, for a very small subset of English, could look like this: Sentence = NounPhraseVerbNounPhrase . NounPhrase = [ Adjective ] Noun . Adjective = “bad” | “nice” . An adjective is either the word “bad” or the word “nice” . The vertical bar separates alternatives. The double quotes enclose actual symbols (words) that would appear in a sentence. Technically, these are called terminal symbols. The other symbols, such as Sentence, NounPhrase etc. are called nonterminalsymbols. Syntax etc.

  18. “John” , “Mary” , “loves” , “bad” , “dog” , “hates” , “nice” , “children” Mary loves bad dog” “bad dog hates nice children” “John Mary dog bad loves nice” What is needed is some consideration of the syntax of English sentences: they must be constructed according to certain rules. Such a collection of rules is called a grammar. A very simple grammar, for a very small subset of English, could look like this: Sentence = NounPhrase Verb NounPhrase . NounPhrase = [ Adjective ] Noun . Adjective = “bad” | “nice” . Verb = “loves” | “hates” . Syntax etc.

  19. “John” , “Mary” , “loves” , “bad” , “dog” , “hates” , “nice” , “children” Mary loves bad dog” “bad dog hates nice children” “John Mary dog bad loves nice” What is needed is some consideration of the syntax of English sentences: they must be constructed according to certain rules. Such a collection of rules is called a grammar. A very simple grammar, for a very small subset of English, could look like this: Sentence = NounPhrase Verb NounPhrase . NounPhrase = [ Adjective ] Noun . Adjective = “bad” | “nice” . Verb = “loves” | “hates” . Noun = ProperName | “dog” | “children” . Syntax etc.

  20. “John” , “Mary” , “loves” , “bad” , “dog” , “hates” , “nice” , “children” Mary loves bad dog” “bad dog hates nice children” “John Mary dog bad loves nice” What is needed is some consideration of the syntax of English sentences: they must be constructed according to certain rules. Such a collection of rules is called a grammar. A very simple grammar, for a very small subset of English, could look like this: Sentence = NounPhrase Verb NounPhrase . NounPhrase = [ Adjective ] Noun . Adjective = “bad” | “nice” . Verb = “loves” | “hates” . Noun = ProperName | “dog” | “children” . ProperName = “John” | “Mary” . Syntax etc.

  21. A very simple grammar, for a very small subset of English, could look like this: Sentence = NounPhrase Verb NounPhrase . NounPhrase = [ Adjective ] Noun . Adjective = “bad” | “nice” . Verb = “loves” | “hates” . Noun = ProperName | “dog” | “children” . ProperName = “John” | “Mary” . Here is an example of a sentence: Sentence NounPhrase Verb NounPhrase Noun ProperName Adjective Noun Mary loves bad dog Syntax etc.

  22. A very simple grammar, for a very small subset of English, could look like this: Sentence = NounPhrase Verb NounPhrase . NounPhrase = [ Adjective ] Noun . Adjective = “bad” | “nice” . Verb = “loves” | “hates” . Noun = ProperName | “dog” | “children” . ProperName = “John” | “Mary” . Here is an example of a sentence: Sentence NounPhrase Verb NounPhrase Noun ProperName Adjective Noun Mary loves bad dog However, we would not be able to construct such a tree for John Mary dog bad loves nice

  23. A very simple grammar, for a very small subset of English, could look like this: Sentence = NounPhrase Verb NounPhrase . NounPhrase = [ Adjective ] Noun . Adjective = “bad” | “nice” . Verb = “loves” | “hates” . Noun = ProperName | “dog” | “children” . ProperName = “John” | “Mary” . Here is another example: Sentence NounPhrase Verb NounPhrase Noun Adjective Noun children loves nice dog Syntax etc.

  24. A very simple grammar, for a very small subset of English, could look like this: Sentence = NounPhrase Verb NounPhrase . NounPhrase = [ Adjective ] Noun . Adjective = “bad” | “nice” . Verb = “loves” | “hates” . Noun = ProperName | “dog” | “children” . ProperName = “John” | “Mary” . Here is another example: Sentence NounPhrase Verb NounPhrase Noun Adjective Noun children loves nice dog So things get complicated rather quickly! Syntax etc.

  25. A very simple grammar, for a very small subset of English, could look like this: Sentence = NounPhrase Verb NounPhrase . NounPhrase = [ Adjective ] Noun . Adjective = “bad” | “nice” . Verb = “loves” | “hates” . Noun = ProperName | “dog” | “children” . ProperName = “John” | “Mary” . Here is another example: Sentence NounPhrase Verb NounPhrase Noun Adjective Noun children loves nice dog So things get complicated rather quickly! We could extend our grammar to handle number, but this would get quite hairy quite soon. It is better to keep this simple context-free grammar, and to enforce contextual constraints – such as maintaining number – by other means. Syntax etc.

  26. Of course, if we want to handle a reasonably complete subset of natural language, we have to take advantage not only of surface syntax (expressed by a context-free grammar) and simple contextual information, but also of usage etc. Syntax etc.

  27. Of course, if we want to handle a reasonably complete subset of natural language, we have to take advantage not only of surface syntax (expressed by a context-free grammar) and simple contextual information, but also of usage etc. Above all, we must be able to figure out and maintain some information about meaning. This is very, very difficult… Syntax etc.

  28. CACM cover, 1958… Syntax etc.

  29. CACM cover, 1958… 52 years later…. Syntax etc.

  30. Translating natural language is not easy: one has to deal not only with ambiguities that cannot be resolved without a deep understanding of the context, but also with idiosyncratic idioms, in which every language abounds. Syntax etc.

  31. Translating natural language is not easy: one has to deal not only with ambiguities that cannot be resolved without a deep understanding of the context, but also with idiosyncratic idioms, in which every language abounds. Note that this sentence is almost free of ambiguities and idiosyncratic idioms! Syntax etc.

  32. Translating natural language is not easy: one has to deal not only with ambiguities that cannot be resolved without a deep understanding of the context, but also with idiosyncratic idioms, in which every language abounds. Note that this sentence is almost free of ambiguities and idiosyncratic idioms! And now we submit it to babelfish.yahoo.com …. Syntax etc.

  33. Translating natural language is not easy: one has to deal not only with ambiguities that cannot be resolved without a deep understanding of the context, but also with idiosyncratic idioms, in which every language abounds. Dutch: Vertalen van natuurlijke taal is niet gemakkelijk: men moet niet alleen ambiguïteiten die niet zonder een diep inzicht in de context kunnen worden opgelost, maar ook eigenaardige idiomatische uitdrukkingen behandelen, waarin elke taal rijk is. Syntax etc.

  34. Translating natural language is not easy: one has to deal not only with ambiguities that cannot be resolved without a deep understanding of the context, but also with idiosyncratic idioms, in which every language abounds. Dutch: Vertalen van natuurlijke taal is niet gemakkelijk: men moet niet alleen ambiguïteiten die niet zonder een diep inzicht in de context kunnen worden opgelost, maar ook eigenaardige idiomatische uitdrukkingen behandelen, waarin elke taal rijk is. Back to English: Do not translate of natural language is easy: one must not only ambiguities which cannot be solved without a deep insight in the context, but also strange idiomatic expressions to treat, in which each language richly is. Syntax etc.

  35. Translating natural language is not easy: one has to deal not only with ambiguities that cannot be resolved without a deep understanding of the context, but also with idiosyncratic idioms, in which every language abounds. French: La traduction de langage naturel n'est pas facile : on doit traiter non seulement des ambiguïtés qui ne peuvent pas être resolved sans arrangement profond du contexte, mais également les idiomes idiosyncratiques, en lesquels chaque langue abonde. Syntax etc.

  36. Translating natural language is not easy: one has to deal not only with ambiguities that cannot be resolved without a deep understanding of the context, but also with idiosyncratic idioms, in which every language abounds. French: La traduction de langage naturel n'est pas facile : on doit traiter non seulement des ambiguïtés qui ne peuvent pas être resolved sans arrangement profond du contexte, mais également les idiomes idiosyncratiques, en lesquels chaque langue abonde. Back to English: Translation of natural language n' is not easy: one must treat not only ambiguities which cannot be resolved without major arrangement of the context, but also the idioms idiosyncratic, in which each language abounds. Syntax etc.

  37. Translating natural language is not easy: one has to deal not only with ambiguities that cannot be resolved without a deep understanding of the context, but also with idiosyncratic idioms, in which every language abounds. Spanish: El traducir de lenguaje natural no es fácil: uno tiene que ocuparse no sólo de las ambigüedades que no pueden ser resolved sin una comprensión profunda del contexto, pero también de los idiomas idiosincrásicos, en los cuales cada lengua abunda. Syntax etc.

  38. Translating natural language is not easy: one has to deal not only with ambiguities that cannot be resolved without a deep understanding of the context, but also with idiosyncratic idioms, in which every language abounds. Spanish: El traducir de lenguaje natural no es fácil: uno tiene que ocuparse no sólo de las ambigüedades que no pueden ser resolved sin una comprensión profunda del contexto, pero también de los idiomas idiosincrásicos, en los cuales cada lengua abunda. Back toEnglish: Translating of natural language is not easy: one must also take care not only of the ambiguities that cannot be you solve without a deep understanding of the context, but of the idiosyncratic languages, in which each language abounds. Syntax etc.

  39. Translating natural language is not easy: one has to deal not only with ambiguities that cannot be resolved without a deep understanding of the context, but also with idiosyncratic idioms, in which every language abounds. Russian: Переводить естественный язык не легок: одно должно общаться не только с неоднозначностями которые не могут быть resolved без глубокого вникания смысла, но также с идиосинкразическими идиоматизмами, в которых каждый язык изобилует. Syntax etc.

  40. Translating natural language is not easy: one has to deal not only with ambiguities that cannot be resolved without a deep understanding of the context, but also with idiosyncratic idioms, in which every language abounds. Russian: Переводить естественный язык не легок: одно должно общаться не только с неоднозначностями которые не могут быть resolved без глубокого вникания смысла, но также с идиосинкразическими идиоматизмами, в которых каждый язык изобилует. Back to English: To t[Perevodit] natural language am not light: one must associate not only with the ambiguities which cannot be resolved without the deep understanding of sense, but also with the idiosyncratic idioms, in which each language abounds. Syntax etc.

  41. Translating natural language is not easy: one has to deal not only with ambiguities that cannot be resolved without a deep understanding of the context, but also with idiosyncratic idioms, in which every language abounds. English --> Dutch --> French --> Spanish --> English: Not to translate of the natural language is easy: they must not only the ambiguities that cannot be solved without a deep understanding in the context, but the idiomatic expressions so rare to try in which each language is rich. Syntax etc.

  42. Obviously, proper translation often requires a deep understanding of the text. Syntax etc.

  43. Obviously, proper translation often requires a deep understanding of the text. • --> Japanese • 明らかに、適切な翻訳は頻繁にテキストの深い理解を要求する。 Syntax etc.

  44. Obviously, proper translation often requires a deep understanding of the text. • --> Japanese • 明らかに、適切な翻訳は頻繁にテキストの深い理解を要求する。 • --> back to English • Clearly, appropriate translation requires understanding where the text is deep frequently. Syntax etc.

  45. Obviously, proper translation often requires a deep understanding of the text. • --> Japanese • 明らかに、適切な翻訳は頻繁にテキストの深い理解を要求する。 • --> back to English • Clearly, appropriate translation requires understanding where the text is deep frequently. • Q. E. D. Syntax etc.

  46. The situation is radically different with artificially constructed languages, such as programming languages. These are constructed with the explicit goal of making it relatively easy to distinguish “sentences” from meaningless jumbles of “words”. Syntax etc.

  47. We have already seen the general form of a grammar rule, how to distinguish terminal symbols from nonterminal symbols, how to introduce alternatives (“|”) and optional constructs (“[]”). Syntax etc.

  48. We have already seen the general form of a grammar rule, how to distinguish terminal symbols from nonterminal symbols, how to introduce alternatives (“|”) and optional constructs (“[]”). There is also a way to specify constructs that can be repeatedzero or more times: these are enclosed in curly braces (“{}”). For example: Digit = “0” | “1” | “2” | “3” | “4” | “5” | “6” | “7” | “8” | “9” . Number = [ “+” | “–” ] Digit { Digit } . A number begins with an optional sign. It must contain at least one digit, and that digit can be followed by any number of digits. Syntax etc.

  49. We have already seen the general form of a grammar rule, how to distinguish terminal symbols from nonterminal symbols, how to introduce alternatives (“|”) and optional constructs (“[]”). There is also a way to specify constructs that can be repeatedzero or more times: these are enclosed in curly braces (“{}”). For example: Digit = “0” | “1” | “2” | “3” | “4” | “5” | “6” | “7” | “8” | “9” . Number = [ “+” | “–” ] Digit { Digit } . A number begins with an optional sign. It must contain at least one digit, and that digit can be followed by any number of digits. Note that the grammar does not specify the maximum size of an integer: that would be rather difficult to do, and would make the grammar unreadable. It is better to treat that issue separately. Syntax etc.

  50. Digit = “0” | “1” | “2” | “3” | “4” | “5” | “6” | “7” | “8” | “9” . Number = [ “+” | “–” ] Digit { Digit } . We might now want to define the syntax of simple arithmetic expressions: Expression = Term { ( “+” | “–” ) Term } . An expression is a number of terms separated by addition or subtraction operators. Note the use of parentheses to override the precedence of “|”. Syntax etc.

More Related