Top Banner
Programming Programming Languages Languages
21

Programming Languages

Jan 15, 2016

Download

Documents

tymon

Programming Languages. Language Syntax. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programming Languages

ProgrammingProgramming LanguagesLanguages

Page 2: Programming Languages

Language SyntaxLanguage Syntax

This lecture introduces the the lexical This lecture introduces the the lexical structure of programming languages; the structure of programming languages; the context-free grammars and their context-free grammars and their description in BNF; the representation of description in BNF; the representation of syntactic structure using trees; the issues syntactic structure using trees; the issues that arise in constructing BNFs for a that arise in constructing BNFs for a programming language; the EBNFs and programming language; the EBNFs and syntax diagrams.syntax diagrams.

Page 3: Programming Languages

• Syntax is the structure of a language.Syntax is the structure of a language.

• One of the great advances in programming One of the great advances in programming languages has been the development of a languages has been the development of a formal system for describing syntax that is formal system for describing syntax that is now almost universally in use.now almost universally in use.

Language Syntax Language Syntax

• In the 1950s Noam Chomsky developed the idea In the 1950s Noam Chomsky developed the idea of context-free grammars; and John Backus, with of context-free grammars; and John Backus, with contributions by Peter Naur, developed the contributions by Peter Naur, developed the Backus-Naur forms (BNFs) notational system for Backus-Naur forms (BNFs) notational system for describing these grammars.describing these grammars.

Page 4: Programming Languages

• The lexical structure of a programming language is The lexical structure of a programming language is the structure of its words, or tokens.the structure of its words, or tokens.

• Typically, the scanning phase of a translator Typically, the scanning phase of a translator collects sequences of characters from the collects sequences of characters from the input program into tokens;input program into tokens;

Lexical Structures Lexical Structures

• which are then processed by a parsing phase, which are then processed by a parsing phase, that determines the syntactic structure. that determines the syntactic structure.

Page 5: Programming Languages

TokensTokens• Typical token categories include the following: Typical token categories include the following:

• Reserved words, sometimes called Reserved words, sometimes called keywords, such as "begin," "if," and "while“.keywords, such as "begin," "if," and "while“.

• Constants or literals, such as 42 (a numeric Constants or literals, such as 42 (a numeric constant) or "hello" (a string constant). constant) or "hello" (a string constant).

• Special symbols, such as ";", "< =", or "+“. Special symbols, such as ";", "< =", or "+“.

• Identifiers, such as x24, monthly_balance, or write. Identifiers, such as x24, monthly_balance, or write.

Page 6: Programming Languages

Context-Free Grammars And Context-Free Grammars And BNFsBNFs

• We begin the description of grammars and BNFs with an example: We begin the description of grammars and BNFs with an example:

• 1. <sentence>:: = <noun-phrase> <verb-phrase>. 1. <sentence>:: = <noun-phrase> <verb-phrase>.

• 2. <noun-phrase> :: = <article> <noun> 2. <noun-phrase> :: = <article> <noun> 3. <article > ::= a | the 3. <article > ::= a | the 4. <noun > ::= girl | dog 4. <noun > ::= girl | dog 5. <verb-phrase> :: = <verb> <noun- 5. <verb-phrase> :: = <verb> <noun-phrase> 6. <verb> ::= sees | pets phrase> 6. <verb> ::= sees | pets

• In English, we can express sentences as: In English, we can express sentences as:

Page 7: Programming Languages

Context-Free Grammars And Context-Free Grammars And BNFsBNFs

• Thus we could construct, or derive, the Thus we could construct, or derive, the sentence "the girl sees a dog." as follows: sentence "the girl sees a dog." as follows:

Page 8: Programming Languages

Context-Free Grammars And Context-Free Grammars And BNFsBNFs

• A context-free grammar consist of a series A context-free grammar consist of a series grammar rules as described; the rules consist of grammar rules as described; the rules consist of a left-hand side that is a single structure name;a left-hand side that is a single structure name;

• followd by a right-hand side consisting of a followd by a right-hand side consisting of a sequence of items that can be symbols or sequence of items that can be symbols or other structure names. other structure names.

• The names for structures (like <sentence>) The names for structures (like <sentence>) are called nonterminals, since they are are called nonterminals, since they are broken down into further structures.broken down into further structures.

Page 9: Programming Languages

ProductionsProductions• The words or token symbols are also called The words or token symbols are also called

terminals, since they are never broken down.terminals, since they are never broken down.

• Grarmmar rules are also called productions, Grarmmar rules are also called productions, since they "produce" the strings of the since they "produce" the strings of the language using derivations. language using derivations.

• Productions are in Backus-Naur form if they Productions are in Backus-Naur form if they are as given using only the metasymbols ":: are as given using only the metasymbols ":: = ", "|", "<", and ">". = ", "|", "<", and ">".

• ( Sometimes parentheses are also allowed to ( Sometimes parentheses are also allowed to group things together.) group things together.)

Page 10: Programming Languages

Context-free?Context-free?• Why is such a grammar context-free? Why is such a grammar context-free?

• The simple reason is that the nonterminals appear The simple reason is that the nonterminals appear singly on the left-hand sides of productions. singly on the left-hand sides of productions.

• This means that each nonterminal can be This means that each nonterminal can be replaced by any right-hand side alternative, no replaced by any right-hand side alternative, no matter where the nonterminal might appear.matter where the nonterminal might appear.

• In other words, there is no context under In other words, there is no context under which only certain replacements can occur. which only certain replacements can occur.

Page 11: Programming Languages

Context-free?Context-free?• Why is such a grammar context-free? Why is such a grammar context-free?

• The simple reason is that the nonterminals appear The simple reason is that the nonterminals appear singly on the left-hand sides of productions. singly on the left-hand sides of productions.

• This means that each nonterminal can be This means that each nonterminal can be replaced by any right-hand side alternative, no replaced by any right-hand side alternative, no matter where the nonterminal might appear.matter where the nonterminal might appear.

• We shall adopt the view that anything not We shall adopt the view that anything not expressable using context-free grammars is expressable using context-free grammars is a semantic, not a syntactic issue.a semantic, not a syntactic issue.

Page 12: Programming Languages

Context-sensitivityContext-sensitivity

• As an example of a context-sensitivity, we noted As an example of a context-sensitivity, we noted that articles that appear at the beginning of that articles that appear at the beginning of sentences in the preceding grammar should be sentences in the preceding grammar should be capitalized. capitalized.

• One way of doing this is to rewrite the first rule as: One way of doing this is to rewrite the first rule as:

• <sentence>:: = <beginning> <noun-<sentence>:: = <beginning> <noun-phrase> phrase> <verb-phrase> '.' <verb-phrase> '.'

• and then add the context-sensitive rule: and then add the context-sensitive rule: <beginning> <article>:: = The | A <beginning> <article>:: = The | A

Page 13: Programming Languages

Context-sensitivity (2)Context-sensitivity (2)

• Now the derivation would look as follows: Now the derivation would look as follows:

• <sentence> -> <beginning><noun-phrase> <sentence> -> <beginning><noun-phrase> <verb-phrase>. <verb-phrase>. (new (new rule 1)rule 1)

• -> <beginning> <article> <noun> -> <beginning> <article> <noun> <verb-phrase>. <verb-phrase>. (rule (rule 2)2)

• -> The <noun> <verb-phrase>. -> The <noun> <verb-phrase>. (new context-sensitive rule)(new context-sensitive rule)

• ->….. ->…..

Page 14: Programming Languages

BNF formBNF form• Context-free grammars have been studied Context-free grammars have been studied

extensively by formal language theorists extensively by formal language theorists and are now so well understood that it is and are now so well understood that it is natural to express the syntax of any natural to express the syntax of any programming language in BNF form.programming language in BNF form.

• By doing so makes it easier to write By doing so makes it easier to write translators for the language, since the translators for the language, since the parsing stage can be automated.parsing stage can be automated.

Page 15: Programming Languages

Syntax-directed SemanticsSyntax-directed Semantics• Syntax establishes structure, not meaning.Syntax establishes structure, not meaning.

• But the meaning of a sentence (or program) But the meaning of a sentence (or program) must be related to its syntax.must be related to its syntax.

• To make use of the syntactic structure of a To make use of the syntactic structure of a program to determine its semantics we must program to determine its semantics we must have a way of expressing this structure as have a way of expressing this structure as determined by a derivation. determined by a derivation.

• A standard method for doing this is with a A standard method for doing this is with a parse tree. parse tree.

Page 16: Programming Languages

Parse Tree Parse Tree • The parse tree describes graphically the The parse tree describes graphically the

replacement process in a derivation. replacement process in a derivation.

• For example, the parse tree for the sentence For example, the parse tree for the sentence "the girl sees a dog." is as follows: "the girl sees a dog." is as follows:

Page 17: Programming Languages

A Simple Arithmetic A Simple Arithmetic Expression Grammar Expression Grammar

• A typical simple example of the use of a context-A typical simple example of the use of a context-free grammar in programming languages is the free grammar in programming languages is the description of simple integer arithmetic description of simple integer arithmetic expressions with addition and multiplication:expressions with addition and multiplication:

• <exp>::=<exp>+<exp>|<exp>*<exp>|<exp>::=<exp>+<exp>|<exp>*<exp>|(<exp>) | <number> (<exp>) | <number>

• <number> :: = <number><digit > | <digit <number> :: = <number><digit > | <digit > >

• <digit> :: = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 <digit> :: = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Page 18: Programming Languages

AmbiguityAmbiguity• A grammar A grammar

for which for which two distinct two distinct parse are parse are possible for possible for the same the same string is string is ambiguous.ambiguous.

• For For example, if example, if we we construct: construct: 3 + 4 * 53 + 4 * 5

Page 19: Programming Languages

Precedence and AssociativityPrecedence and Associativity• The revised disambiguating grammar for The revised disambiguating grammar for

simple arithmetic expression that expresses simple arithmetic expression that expresses both precedence and associativity is given as:both precedence and associativity is given as:

• <exp> ::= <exp> + <term> | <term> <exp> ::= <exp> + <term> | <term> <term> ::= <term>* <factor> | <factor> <term> ::= <term>* <factor> | <factor> <factor> ::= (<exp>) | <number> <factor> ::= (<exp>) | <number> <number> ::= <number><digit> | <digit> <number> ::= <number><digit> | <digit> <digit> ::= 0|1|2|3|4|5|6|7|8|9 <digit> ::= 0|1|2|3|4|5|6|7|8|9

• The above disambiguating rules define the The above disambiguating rules define the precedence for * and + operators; and apply precedence for * and + operators; and apply the left-recursive associative rule.the left-recursive associative rule.

Page 20: Programming Languages

EBNFsEBNFs

• A special notation for grammar rules is A special notation for grammar rules is adopted that expresses more clearly the adopted that expresses more clearly the repetitive nature of their structures:repetitive nature of their structures:

• <exp> ::= <term> { + <term>} <exp> ::= <term> { + <term>} <term> ::= <factor> { * <factor>} <term> ::= <factor> { * <factor>} <factor> ::= (<exp>) | <number> <factor> ::= (<exp>) | <number> <number> ::= <digit> { <digit>} <number> ::= <digit> { <digit>} <digit> ::= 0|1|2|3|4|5|6|7|8|9 <digit> ::= 0|1|2|3|4|5|6|7|8|9

• We assume that any operator involved in a We assume that any operator involved in a curly bracket repetition is left-associative.curly bracket repetition is left-associative.

Page 21: Programming Languages

Syntax Syntax DiagramsDiagrams

• A useful A useful graphical graphical representrepresentation for ation for a a grammar grammar rule is the rule is the syntax syntax diagram.diagram.