programminglanguages programming languages language definition, translation and design

17
Programming Programming Languages Languages

Upload: george-charles

Post on 04-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ProgrammingLanguages Programming Languages Language Definition, Translation and Design

ProgrammingProgramming LanguagesLanguages

Page 2: ProgrammingLanguages Programming Languages Language Definition, Translation and Design

Language Definition, Language Definition, Translation and DesignTranslation and Design

Page 3: ProgrammingLanguages Programming Languages Language Definition, Translation and Design

Language DefinitionLanguage Definition• A programming language needs a complete, A programming language needs a complete,

precise description.precise description.

• The importance of a precise definition for a The importance of a precise definition for a programming language should be clear from programming language should be clear from its use to describe computation. its use to describe computation.

• Standards organizations such as ANSI and ISO Standards organizations such as ANSI and ISO have published definitions for several have published definitions for several languages, including Pascal, FORTRAN, C, C+languages, including Pascal, FORTRAN, C, C++, Ada and Prolog.+, Ada and Prolog.

Page 4: ProgrammingLanguages Programming Languages Language Definition, Translation and Design

Language SyntaxLanguage Syntax• Language definition can be loosely divided Language definition can be loosely divided

into two parts: syntax, or structure; and into two parts: syntax, or structure; and semantics, or meaning.semantics, or meaning.

• The syntax of a programming language is in many The syntax of a programming language is in many ways like the grammar of a natural language. ways like the grammar of a natural language.

• The syntax of almost all languages is now The syntax of almost all languages is now given using context-free grammars.given using context-free grammars.

• For example, a context-free grammar rule for the For example, a context-free grammar rule for the Pascal if-statement can be written as follows: Pascal if-statement can be written as follows: <if-statement>:: = if <condition> then <if-statement>:: = if <condition> then

<statement> [else <statement>] <statement> [else <statement>]

Page 5: ProgrammingLanguages Programming Languages Language Definition, Translation and Design

Lexical StructureLexical Structure• The lexical structure of a programming The lexical structure of a programming

language is the structure of the words of the language is the structure of the words of the language, which are usually called tokens.language, which are usually called tokens.

• The lexical structure is similar to spelling in The lexical structure is similar to spelling in a natural language. a natural language.

• The syntax of almost all languages is now The syntax of almost all languages is now given using context-free grammars.given using context-free grammars.

• Tokens in programming languages include Tokens in programming languages include identifiers (or names), symbols for operations, such identifiers (or names), symbols for operations, such as "+" and "< =," and special punctuation symbols as "+" and "< =," and special punctuation symbols such as the semicolon (";") and the period ("."). such as the semicolon (";") and the period (".").

Page 6: ProgrammingLanguages Programming Languages Language Definition, Translation and Design

Language SemanticsLanguage Semantics• The semantics, or meaning, of a language is much The semantics, or meaning, of a language is much

more complex and difficult to describe precisely. more complex and difficult to describe precisely.

• What is difficult about semantics is that the What is difficult about semantics is that the meaning of a particular mechanism may involve meaning of a particular mechanism may involve interactions with other mechanisms in the language. interactions with other mechanisms in the language.

• No generally accepted method, such as the use of No generally accepted method, such as the use of context-free grammars for syntax, exists.context-free grammars for syntax, exists.

• it is still not customary for a formal definition it is still not customary for a formal definition of the semantics of a programming of the semantics of a programming language to be given at all.language to be given at all.

Page 7: ProgrammingLanguages Programming Languages Language Definition, Translation and Design

Language TranslationLanguage Translation• For a programming language to be useful, it For a programming language to be useful, it

must have a translator, to transforms them must have a translator, to transforms them into a form suitable for execution. into a form suitable for execution.

• A translator that immediately executes a program is called A translator that immediately executes a program is called an interpreter, while a translator that changes a program an interpreter, while a translator that changes a program into a form suitable for execution is called a compiler. into a form suitable for execution is called a compiler.

• Interpretation is a one-step process: Interpretation is a one-step process: Source Code Source Code

| | input -> Interpreter -> output input -> Interpreter -> output

Page 8: ProgrammingLanguages Programming Languages Language Definition, Translation and Design

CompilationCompilation• Compilation is at least a two-step process: Compilation is at least a two-step process:

the original program (or source program) is the original program (or source program) is input to the compiler, and a new program (or input to the compiler, and a new program (or target program) is output from the compiler. target program) is output from the compiler.

• This target program may then be executed, if This target program may then be executed, if it is in a form suitable for direct execution it is in a form suitable for direct execution (i.e., in machine language). (i.e., in machine language).

• (source code )---> compile ------> (target (source code )---> compile ------> (target code) ------------> further translation with code) ------------> further translation with Linkage Editor and Loader ------------> Linkage Editor and Loader ------------> (executable code)(executable code)

Page 9: ProgrammingLanguages Programming Languages Language Definition, Translation and Design

JavaJava

• The Java programming language is unusual in that The Java programming language is unusual in that a program is both compiled and interpreted.a program is both compiled and interpreted.

• With most programming languages, you With most programming languages, you either compile or interpret a program so that either compile or interpret a program so that you can run it on your computer.you can run it on your computer.

Page 10: ProgrammingLanguages Programming Languages Language Definition, Translation and Design

Phases of TranslationPhases of Translation• Both compilers and interpreters must Both compilers and interpreters must

perform similar operations when translating perform similar operations when translating a source program.a source program.

• First, a lexical analyzer, or scanner, must translate First, a lexical analyzer, or scanner, must translate the incoming characters into tokens.the incoming characters into tokens.

• Then a syntax analyzer or parser must Then a syntax analyzer or parser must determine the structure of the sequence of determine the structure of the sequence of tokens provided to it by the scanner.tokens provided to it by the scanner.

• Finally, a semantic analyzer must determine enough Finally, a semantic analyzer must determine enough of the meaning of a program to allow execution or of the meaning of a program to allow execution or generation of a target program to take place. generation of a target program to take place.

Page 11: ProgrammingLanguages Programming Languages Language Definition, Translation and Design

Runtime EnvironmentRuntime Environment• Typically, these phases of translation do not occur Typically, these phases of translation do not occur

separately but are combined in various ways.separately but are combined in various ways.

• A language translator must also maintain a runtime A language translator must also maintain a runtime environment, in which suitable memory space for program environment, in which suitable memory space for program data is allocated, and that records the progress of the data is allocated, and that records the progress of the execution of the program.execution of the program.

• The properties of a programming language that can The properties of a programming language that can be determined prior to execution are called static be determined prior to execution are called static properties, while properties that can be determined properties, while properties that can be determined only during execution are called dynamic only during execution are called dynamic properties. properties.

Page 12: ProgrammingLanguages Programming Languages Language Definition, Translation and Design

Pre-ProcessorPre-Processor• A language may also require a preprocessor, A language may also require a preprocessor,

which is run prior to translation to transform a which is run prior to translation to transform a program into a form suitable for translation. program into a form suitable for translation.

• A C program with pre-processor:A C program with pre-processor:

• ## if ( unix-system) then ## if ( unix-system) then

• ## include <stdio.h>## include <stdio.h>

• ## else## else

• ## ……## ……

Page 13: ProgrammingLanguages Programming Languages Language Definition, Translation and Design

ErrorsErrors• An important property of a language An important property of a language

translator is its response to errors in a source translator is its response to errors in a source program. program.

• Lexical errors: occur during lexical analysis.Lexical errors: occur during lexical analysis.

• Syntax errors: include missing tokens and Syntax errors: include missing tokens and mal-formed expressions.mal-formed expressions.

• Semantic errors: can be either static, such as Semantic errors: can be either static, such as incompatible types or undeclared variables; or incompatible types or undeclared variables; or dynamic, such as divided by zero.dynamic, such as divided by zero.

• Logic errors: are errors that the programmer Logic errors: are errors that the programmer makes that cause the program to behave in an makes that cause the program to behave in an erroneous or undesirable way. (endless loop)erroneous or undesirable way. (endless loop)

Page 14: ProgrammingLanguages Programming Languages Language Definition, Translation and Design

DebuggingDebugging

• A language definition will often include a A language definition will often include a specification of what errors must be caught specification of what errors must be caught prior to execution:prior to execution:

• what errors must generate a runtime error, what errors must generate a runtime error, and what errors may go undetected.and what errors may go undetected.

• A translator needs to provide user options A translator needs to provide user options for debugging, for interfacing with the for debugging, for interfacing with the operating system, and perhaps with a operating system, and perhaps with a software development environment.software development environment.

Page 15: ProgrammingLanguages Programming Languages Language Definition, Translation and Design

Language DesignLanguage Design• It is the challenge of programming language It is the challenge of programming language

design to achieve the power, expressiveness, design to achieve the power, expressiveness, and comprehensibility that human readability and comprehensibility that human readability requires, while at the same time, retaining requires, while at the same time, retaining the precision and simplicity, that is needed the precision and simplicity, that is needed for machine translation.for machine translation.

• Human readability is a complex and subtle Human readability is a complex and subtle requirement. It depends to a large extent on requirement. It depends to a large extent on the facilities a programming language has for the facilities a programming language has for abstractionabstraction

Page 16: ProgrammingLanguages Programming Languages Language Definition, Translation and Design

Natural Expression & complexity Natural Expression & complexity controlcontrol

• A successful programming language has A successful programming language has facilities for the natural expression of the facilities for the natural expression of the structure of data; and for the structure of the structure of data; and for the structure of the computational process for the solution of a computational process for the solution of a problem.problem.

• The overriding goal of abstraction in programming The overriding goal of abstraction in programming language design is complexity control. language design is complexity control.

• A human being can retain only a certain A human being can retain only a certain amount of detail at once. amount of detail at once.

• To understand and construct complex systems, To understand and construct complex systems, humans must control how much detail needs to humans must control how much detail needs to be understood at any one time.be understood at any one time.

Page 17: ProgrammingLanguages Programming Languages Language Definition, Translation and Design

Abstraction: Complexity HidingAbstraction: Complexity Hiding• We control complexity by building abstractions We control complexity by building abstractions

that hide details when appropriate.that hide details when appropriate.

• We control complexity by establishing We control complexity by establishing conventional interfaces that enable us to conventional interfaces that enable us to construct systems by combining standard, well-construct systems by combining standard, well-understood pieces in a ‘mix and match’ way. understood pieces in a ‘mix and match’ way.

• We control complexity by establishing new We control complexity by establishing new languages for describing a design, each of languages for describing a design, each of which emphasizes particular aspects of the which emphasizes particular aspects of the design and de-emphasizes others.design and de-emphasizes others.