Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 363 – Chapter 1 What is a programming language? Kinds of languages

Similar presentations


Presentation on theme: "CS 363 – Chapter 1 What is a programming language? Kinds of languages"— Presentation transcript:

1 CS 363 – Chapter 1 What is a programming language? Kinds of languages
Some history

2 Languages Carl Sagan once said that to make a recipe from scratch, __________ Programming is difficult, but the language should help us Human language vs. prog. language No one gets job for knowing 12 prog lang  What languages have you worked with? Language should help us; just like how a car helps us get to work.

3 Definition A programming language is a language intended for expressing a computer program, and any program can be expressed in it. So conceivably, a program can be written in any language, but it’s important to choose appropriately.

4 Kinds of languages (1) Most are “imperative”
Program = seq of statements: begin & end Variables, memory locations a. Von Neuman (verb centered) * b. Object oriented (noun centered) (for short programs, we tend to think von Neumann)

5 Kinds of languages (2) 2. Most miscellaneous lang are “declarative”
NOT a sequence of statements, no order Variables sometimes not used often interactive a. Logical: enter facts, make queries b. Functional: everything is function call ! With effort, can make a declarative language behave imperatively

6 Some history Originally, all coding was binary
Possible even today Instruction consists of opcode & operands Assembly & “pseudocode” languages Replace opcode with word (add, sub, …) Operands are still numbers Ex. load r1, 16 Ex. add r1, r2, r3 Machine dependent  Note that many operations are opposites + - * / etc.

7 General-purpose languages
Starting around 1957 Our own variable names Mathematical operators “x = y+z” instead of “add r1, r2, r3” Can focus on abstraction/algorithm more *** Needs a compiler to translate to machine code! Each language has its own story…

8 Some influential languages
Year Language Features/purpose 1957 FORTRAN Fast, numerical applications, arrays, mistakes 1960 Algol Nested structures, recursion, tries to do everything 1970 Pascal Simple & efficient; pointers 1972 C economical syntax, lots of pointers 1980 Ada Exceptions, concurrency, packages (classes) We’ll finish chapter 1 tomorrow on compilation

9 Compilation Read sections 1.4 – 1.6
Most languages are compiled; some are interpreted Compile = translate Input to compiler is source code Output is usually machine code

10 Interpreting and compiling
Phases of a compiler

11 Interpreting A few languages are interpreted
After editing program, can immediately run. Each statement is interpreted one at a time as the program is run. Run-time is slower than compiled language. Every time you run program, everything has to be interpreted from scratch. Interpreter doesn’t know what to expect, such as waste. Examples: SPIM simulator, Web browser, Word Although HTML is not a programming language

12 Compiling Most languages use a compiler… Step 1: Compile = translate
Input is source code Output is machine code or assembly code Step 2: The machine code is decoded by machine (very fast)

13 Some compiling features
Note: Not all compilers have these features. 1. Pre-processor Substitutes constants Expands macros Supports conditional compilation 2. Assembler 3. Linker

14 C Pre-processing example
#define MAX 100 #define SWAP(x,y) {int t=x;x=y;y=t;} // sort ascending for (i = 0; i < MAX; ++i) for (j = i+1; j < MAX; ++j) if (a[i] > a[j]) SWAP(a[i], a[j]); // sort descending if (a[i] < a[j]) Conditional compilation: for (i = 0; i < MAX; ++i) for (j = i+1; j < MAX; ++j) #ifdef ASCENDING if (a[i] > a[j]) #else if (a[i] < a[j]) #endif SWAP(a[i], a[j]);

15 Writing a compiler Suppose we have new language – Sumatra
1. Write first compiler in existing language, Java. Now, we have a Sumatra compiler, so any Sumatra program can be translated into machine language. From now on, we don’t need Java anymore. 2. Write next Sumatra compiler in Sumatra. Compile it using the first compiler. Another strategy is bootstrapping (p.21) kind of ugly

16 Steps in Compilation Front end (machine-independent)
Scan Parse Semantic analysis Optimization Back end (machine-dependent) Code generation Minor point: We can do some machine-independent optimizations

17 Scanning & Parsing Scan = “lexical analysis”
Break up code into tokens Ignore whitespace & comments Lexical errors are rare Parse = create a parse tree rep’n (p.30-31) Using tokens, understand program’s structure Enforce grammatical rules of language Generates syntax error messages  Why a tree rep’n? Because program is hierarchical (nested) structure.

18 Semantic analysis Create symbol table
Check to see if variables used correctly All variables declared (exactly once) Check types in expressions & statements Ex. Can’t use ++ on JFrame object Check function parameters If all’s well, create syntax tree (p.34) Notice that many tokens no longer needed There are actually more examples of semantic errors!

19 Static vs. dynamic semantics
Compiler can only check static semantics We are NOT running the program Dynamic semantics = behavior at run-time Arithmetic overflow? Exception? These should be handled by program, or else OS or run-time system might intervene

20 Code generation From symbol table, allocate registers or memory locations Traverse syntax tree to generate instructions Optimize = make code more efficient Eliminate redundancy or waste Replace long operations with short ones It’s worth it ! Re-state the 5 different phases of the compiler (scan, parse, semantic analysis, code gen, opt)


Download ppt "CS 363 – Chapter 1 What is a programming language? Kinds of languages"

Similar presentations


Ads by Google