700 likes | 843 Views
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:
E N D
Syntax and related matters Programming Fundamentals 10 Feliks Kluźniak Syntax etc.
Consider the following collection of words: “John” , “Mary” , “loves” , “bad” , “dog” , “hates” , “nice” , “children” Syntax etc.
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.
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.
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.
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.
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.
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.
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.
“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.
“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.
“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.
“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.
“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.
“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.
“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.
“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.
“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.
“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.
“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.
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.
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
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.
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.
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.
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.
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.
CACM cover, 1958… Syntax etc.
CACM cover, 1958… 52 years later…. Syntax etc.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Obviously, proper translation often requires a deep understanding of the text. Syntax etc.
Obviously, proper translation often requires a deep understanding of the text. • --> Japanese • 明らかに、適切な翻訳は頻繁にテキストの深い理解を要求する。 Syntax etc.
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.
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.
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.
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.
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.
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.
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.