pllab, nthu cs2403 programming languages implementation issues cs2403 programming language spring...
Post on 19-Dec-2015
235 views
TRANSCRIPT
PLLab, NTHUCs2403 Programming Languages
Implementation Issues
Cs2403 Programming Language
Spring 2005
Kun-Yuan Hsieh
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
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?
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
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
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
PLLab, NTHUCs2403 Programming Languages
Evaluation criteria
• Reliability– Performs to its specification under all
conditions– Type checking– Exception handling– Aliasing– Readability and writability
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
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
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
PLLab, NTHUCs2403 Programming Languages
A grid-computer arch(non-Von Neumann)
CPU CPU CPU
CPU CPU CPU
CPU CPU CPU
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
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
PLLab, NTHUCs2403 Programming Languages
Compiler examples
PLLab, NTHUCs2403 Programming Languages
Interpreter v.s. Compiler
Input data
ResultsInterpreterSource Program
CompilerExecutable(sample.exe)Source Program
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)
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
PLLab, NTHUCs2403 Programming Languages
Helloworld.il
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
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...
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
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
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
=
+
*
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
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
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