pllab, nthu cs2403 programming languages implementation issues cs2403 programming language spring...
Post on 19-Dec-2015
237 views
TRANSCRIPT
![Page 1: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/1.jpg)
PLLab, NTHUCs2403 Programming Languages
Implementation Issues
Cs2403 Programming Language
Spring 2005
Kun-Yuan Hsieh
![Page 2: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/2.jpg)
PLLab, NTHUCs2403 Programming Languages
Why studying?
• Increased capacity to express ideas– Language feature limits the form of algorithm you
construct– Simulating other language’s feature to break this limit
• Improved ability of choosing right PL– To give you the ability to make informed language
choices• Increase ability to learn new PL
– Programming language is in a state of continuous evolution
– Vocabulary and fundamental concepts helps you learn
![Page 3: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/3.jpg)
PLLab, NTHUCs2403 Programming Languages
Why studying? (cont.)
• Better understanding of implementation– To use a language in the way it was designed to
be used
• Overall advancement of computing– Most popular languages are the best available?– Better educated programmer leads to better
language population?
![Page 4: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/4.jpg)
PLLab, NTHUCs2403 Programming Languages
Evaluation criteria
• Readability– Important because maintenance is cost– Overall simplicity since we tend to learn subset of it
• Feature multiplicity: more than one way to accomplish one operation
– Count = count +1– Count += 1– Count ++
• operation overloading– Reducing # of operations
• Less readable if carried too far– Assembly language is simple but less readable
• Orthogonality
![Page 5: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/5.jpg)
PLLab, NTHUCs2403 Programming Languages
Evaluation criteria
• Readability (cont.)– Control statement
• Read from top to bottom is easier to understand than jump to follow the execution order
– Data type and structure makes your meaning clear• timeOut = 1 v.s. timeOut = true!!
– Syntax– Identifier forms: using connotative names
– Special words: program appearance
– Form and meaning: statement indicates the purpose
![Page 6: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/6.jpg)
PLLab, NTHUCs2403 Programming Languages
Evaluation criteria
• Writability– What affects readability affect writability
– Simplicity and orthogonality• Simplicity avoid misusing but may leads to undetected error
– Support for abstraction to define then use complicated structures in a simple way
• Subprogram
• Tree (using point or array!!)
– Expressivity
![Page 7: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/7.jpg)
PLLab, NTHUCs2403 Programming Languages
Evaluation criteria
• Reliability– Performs to its specification under all
conditions– Type checking– Exception handling– Aliasing– Readability and writability
![Page 8: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/8.jpg)
PLLab, NTHUCs2403 Programming Languages
Evaluation criteria
• Cost
– Training programmers to use language
– Writing programs
– Compiling programs, optimization
– Executing programs
– Language implementation system• Java interpreter is free
– Reliability
– Maintaining programs
![Page 9: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/9.jpg)
PLLab, NTHUCs2403 Programming Languages
Influences on language design
• Computer architecture– Software executes on hardware
• Programming methodologies– 1950s and early 1960s: machine efficiency– Late 1960s: programming efficiency,
readability, better control structures– Late 1970s: data abstraction– Middle 1980s: object-oriented programming
![Page 10: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/10.jpg)
PLLab, NTHUCs2403 Programming Languages
The von Neumann Arch (cont.)
Control unitArithmetic and
logic unit
Instructions and dataResults of operations
Memory (stores both instructions and data)
Input and output devices
![Page 11: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/11.jpg)
PLLab, NTHUCs2403 Programming Languages
A grid-computer arch(non-Von Neumann)
CPU CPU CPU
CPU CPU CPU
CPU CPU CPU
![Page 12: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/12.jpg)
PLLab, NTHUCs2403 Programming Languages
Categories & trade-off
• PL categories:– Imperative (C, Pascal, FORTRAN)– Functional (LISP, Scheme)– Logic (Prolog)– Object-Oriented (C++, C Sharp, Java)
• Design trade-off– Reliability v.s. cost of execution– Writability v.s. readability– Flexibility v.s. safety
![Page 13: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/13.jpg)
PLLab, NTHUCs2403 Programming Languages
Implementation methods• Compilation (Pascal, C, C++, FORTRAN)
– Translate high-level program to machine code– Slow translation – Fast execution
• Interpretation (LISP, Scheme, scripts)– No translation, interpreted by interpreter– Slow execution (translated on execution)– Easy to debug, run-time error refers to source level units
• Hybrid method (Java, CSharp, Perl)– Translated to intermediate language (Java byte code, MSIL)– Medium execution speed
![Page 14: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/14.jpg)
PLLab, NTHUCs2403 Programming Languages
Compiler examples
![Page 15: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/15.jpg)
PLLab, NTHUCs2403 Programming Languages
Interpreter v.s. Compiler
Input data
ResultsInterpreterSource Program
CompilerExecutable(sample.exe)Source Program
![Page 16: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/16.jpg)
PLLab, NTHUCs2403 Programming Languages
Hybrid method
Java Byte Code
Java Source Code Java Compiler
Java Interpreter on Windows
Java Interpreter on Unix
(Stored in the disk & Publicly available)
(private property)
![Page 17: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/17.jpg)
PLLab, NTHUCs2403 Programming Languages
C# VB .NET Visual J# .NET
MSIL
Linux native code
Windows native code
Mac OS native code
Compile into
MSIL
CLR do this
Support now
Will Support soon
Will Support soon
Hybrid method example- .Net
![Page 18: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/18.jpg)
PLLab, NTHUCs2403 Programming Languages
Helloworld.il
![Page 19: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/19.jpg)
PLLab, NTHUCs2403 Programming Languages
Virtual computer conceptC++
compiler
FORTRAN compiler
C compiler
Ada compiler …
Assembler
OperatingSystem
Commandinterpreter
LISP interpreter
Operating system
Macroinstruction interpreter
Bare machine
Virtual CComputer
Virtual C++Computer
![Page 20: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/20.jpg)
PLLab, NTHUCs2403 Programming Languages
Levels of Programming Languages
High-level program class Triangle { ... float surface() return b*h/2; }
class Triangle { ... float surface() return b*h/2; }
Low-level program
ldloc.i4 bldloc.i4 hmulldc.i4 2divret
ldloc.i4 bldloc.i4 hmulldc.i4 2divret
Executable Machine code
0001001001000101001001001110110010101101001...
0001001001000101001001001110110010101101001...
![Page 21: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/21.jpg)
PLLab, NTHUCs2403 Programming Languages
Compiler phases
Lexical analyzer
Syntax analyzer
Intermediate codegenerator
Semantic analyser
Code optimizer
Code generator
Symbol-table manager
Error handler
Source program
Target program
![Page 22: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/22.jpg)
PLLab, NTHUCs2403 Programming Languages
Lexical analysis
• Source program is read from left-to-right and group into tokens
• Tokens: sequences of characters have a meaning• Ex:
– a= b + c * 10• identifier a• assignment symbol =• identifier b• plus sign +• identifier c• multiplication sign *• number 10
![Page 23: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/23.jpg)
PLLab, NTHUCs2403 Programming Languages
Syntax analysis
• Grouping the tokens into grammatical phrases (hierarchical)
• Usually presented as a parse tree
• Expressed by recursive rules– Any identifier is an
expression– Any number is an
expression– If exp1 and exp2 are
expressions, then so are• exp1 + exp2• exp1 * exp3
Assignment statement
expression
identifier expression
expression
expressionexpression
a
identifier
b
c
identifier
10
number
=
+
*
![Page 24: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/24.jpg)
PLLab, NTHUCs2403 Programming Languages
Semantics• Specification of semantics is concerned with
specifying the “meaning” of well-formed programs.
• Terminology:– Expressions are evaluated and yield values (and may or
may not perform side effects)– Commands are executed and perform side effects.– Declarations are elaborated to produce bindings
• Side effects:– change the values of variables– perform input/output
![Page 25: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/25.jpg)
PLLab, NTHUCs2403 Programming Languages
Syntax Analysis
Scanner
Source Program
Abstract Syntax Tree
Error Reports
Parser
Stream of “Tokens”
Stream of Characters
Error Reports
Dataflow chart
![Page 26: PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh](https://reader030.vdocuments.mx/reader030/viewer/2022033105/56649d395503460f94a13e04/html5/thumbnails/26.jpg)
PLLab, NTHUCs2403 Programming Languages
Type Checking• For most statically typed programming languages,
a bottom up algorithm over the AST:• Types of expression AST leaves are known
immediately:– literals => obvious– variables => from the ID table– named constants => from the ID table
• Types of internal nodes are inferred from the type of the children and the type rule for that kind of expression