Download presentation
Presentation is loading. Please wait.
1
PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh
2
PLLab, NTHU Cs2403 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
3
PLLab, NTHU Cs2403 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?
4
PLLab, NTHU Cs2403 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
5
PLLab, NTHU Cs2403 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
6
PLLab, NTHU Cs2403 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
7
PLLab, NTHU Cs2403 Programming Languages Evaluation criteria Reliability –Performs to its specification under all conditions –Type checking –Exception handling –Aliasing –Readability and writability
8
PLLab, NTHU Cs2403 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
9
PLLab, NTHU Cs2403 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
10
PLLab, NTHU Cs2403 Programming Languages The von Neumann Arch (cont.) Control unit Arithmetic and logic unit Instructions and data Results of operations Memory (stores both instructions and data) Input and output devices
11
PLLab, NTHU Cs2403 Programming Languages A grid-computer arch (non-Von Neumann) CPU
12
PLLab, NTHU Cs2403 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
13
PLLab, NTHU Cs2403 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
14
PLLab, NTHU Cs2403 Programming Languages Compiler examples
15
PLLab, NTHU Cs2403 Programming Languages Interpreter v.s. Compiler Input data Results Interpreter Source Program Compiler Executable (sample.exe) Source Program
16
PLLab, NTHU Cs2403 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)
17
PLLab, NTHU Cs2403 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 Hybrid method example-.Net
18
PLLab, NTHU Cs2403 Programming Languages Helloworld.il
19
PLLab, NTHU Cs2403 Programming Languages Virtual computer concept C++ compiler FORTRAN compiler C compiler Ada compiler … Assembler Operating System Command interpreter LISP interpreter Operating system Macroinstruction interpreter Bare machine Virtual C Computer Virtual C++ Computer
20
PLLab, NTHU Cs2403 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 b ldloc.i4 h mul ldc.i4 2 div ret ldloc.i4 b ldloc.i4 h mul ldc.i4 2 div ret Executable Machine code 0001001001000101001 0010011101100101011 01001...
21
PLLab, NTHU Cs2403 Programming Languages Compiler phases Lexical analyzer Syntax analyzer Intermediate code generator Semantic analyser Code optimizer Code generator Symbol-table manager Error handler Source program Target program
22
PLLab, NTHU Cs2403 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
23
PLLab, NTHU Cs2403 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 identifierexpression a identifier b c 10 number = + *
24
PLLab, NTHU Cs2403 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
25
PLLab, NTHU Cs2403 Programming Languages Syntax Analysis Scanner Source Program Abstract Syntax Tree Error Reports Parser Stream of “Tokens” Stream of Characters Error Reports Dataflow chart
26
PLLab, NTHU Cs2403 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.