Programming Languages

Similar documents
Programming Languages

Scope and Parameter Passing

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

The Pairwise-Comparison Method

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

The Critical-Path Algorithm

Introduction to Compiler Design

Scope and Parameter Passing

Recursive Descent Parsers

Friends and Unary Operators

Minimal Spanning Trees

LR Parsing - Conflicts

Scheduling and Digraphs

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Fundamental Data Types

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

The Decreasing-Time Algorithm

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Recognition of Tokens

Abstract Syntax Trees Synthetic and Inherited Attributes

The x86 Instruction Set

Recursive Linked Lists

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

Function Usage. Lecture 15 Sections 6.3, 6.4. Robb T. Koether. Hampden-Sydney College. Mon, Oct 1, 2018

Solving Recursive Sequences by Iteration

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

The Class Construct Part 2

The Class Construct Part 1

Implementing Linked Lists

Building the Abstract Syntax Trees

The Graphics Pipeline

The Traveling Salesman Problem Brute Force Method

The Graphics Pipeline

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

Density Curves Sections

Sampling Distribution Examples Sections 15.4, 15.5

The x87 Floating-Point Unit

The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

The Plurality-with-Elimination Method

Rotations and Translations

Inheritance: The Fundamental Functions

Basic PHP Lecture 17

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

The Model Stack. Lecture 8. Robb T. Koether. Hampden-Sydney College. Wed, Sep 6, 2017

Stacks and their Applications

Dynamic Allocation of Memory

Binary Tree Applications

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

The Projection Matrix

1a Computers, Problem Solving!

The Normal Distribution

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

Ambient and Diffuse Light

Shading Triangles. Lecture 37. Robb T. Koether. Hampden-Sydney College. Mon, Nov 30, 2015

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

List Iterator Implementation

The Mesh Class. Lecture 26. Robb T. Koether. Wed, Oct 28, Hampden-Sydney College

Magnification and Minification

The Coefficient of Determination

Displaying Distributions - Quantitative Variables

Function Definition Syntax Tree

The CYK Parsing Algorithm

The View Frustum. Lecture 9 Sections 2.6, 5.1, 5.2. Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2011

The Constructors. Lecture 6 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 26, 2018

Binary Tree Implementation

Binary Tree Implementation

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

The Projection Matrix

Inheritance: The Fundamental Functions

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

The x86 Architecture

MySQL Creating a Database Lecture 3

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

Basic PHP. Lecture 19. Robb T. Koether. Hampden-Sydney College. Mon, Feb 26, 2108

Dynamic Allocation of Memory

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

XQuery FLOWR Expressions Lecture 35

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

The Mesh Class. Lecture 23. Robb T. Koether. Hampden-Sydney College. Wed, Oct 18, 2017

Nondeterministic Programming in C++

Relational Databases

Specular Reflection. Lecture 19. Robb T. Koether. Hampden-Sydney College. Wed, Oct 4, 2017

CSI32 Object-Oriented Programming

DTDs and XML Attributes

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

PHP Arrays. Lecture 18. Robb T. Koether. Hampden-Sydney College. Mon, Mar 4, 2013

PHP Queries and HTML Forms Lecture 23

LR Parsing - The Items

Introduction to Databases

Street-Routing Problems

Transcription:

Programming Languages Lecture 3 Section 1.3 Robb T. Koether Hampden-Sydney College Mon, Sep 2, 2013 Robb T. Koether (Hampden-Sydney College) Programming Languages Mon, Sep 2, 2013 1 / 25

1 Programming Languages 2 Building an Executable Program 3 Program Organization 4 Example 5 Assignment Robb T. Koether (Hampden-Sydney College) Programming Languages Mon, Sep 2, 2013 2 / 25

Outline 1 Programming Languages 2 Building an Executable Program 3 Program Organization 4 Example 5 Assignment Robb T. Koether (Hampden-Sydney College) Programming Languages Mon, Sep 2, 2013 3 / 25

High-Level Languages C++ Code cin >> cost; if (cost > 100.0) { discount = 0.10 * cost; price = cost - discount; } else price = cost; cout << price; Instructions use some English words (if, else, while, etc.) Expressions are written in something resembling standard algebraic notation. Robb T. Koether (Hampden-Sydney College) Programming Languages Mon, Sep 2, 2013 4 / 25

High-Level Languages C++ Code int c = 3*(10 + 20); C code to add 10 and 20 and multiply by 3. Robb T. Koether (Hampden-Sydney College) Programming Languages Mon, Sep 2, 2013 5 / 25

Assembly Language Assembly Code li $s0,10 addi $s1,$s0,20 li $s0,3 mult $s1,$s0 mflo $s1 Each instruction represents a single basic operation at the machine level (add, multiply, compare, etc.) Mnemonics are used instead of words. Robb T. Koether (Hampden-Sydney College) Programming Languages Mon, Sep 2, 2013 6 / 25

Machine Language Machine Code Hex Binary Assembly 2410000A 00100100000100000000000000001010 li $s0,10 22110014 00100010000100010000000000010100 addi $s1,$s0,20 24100003 00100100000100000000000000000011 li $s0,3 02300018 00000010001100000000000000011000 mult $s1,$s0 00008812 00000000000000001000100000010010 mflo $s1 Each instruction represents a single basic operation. Each instruction is written entirely numerically. Robb T. Koether (Hampden-Sydney College) Programming Languages Mon, Sep 2, 2013 7 / 25

Outline 1 Programming Languages 2 Building an Executable Program 3 Program Organization 4 Example 5 Assignment Robb T. Koether (Hampden-Sydney College) Programming Languages Mon, Sep 2, 2013 8 / 25

Program Translation A computer is capable of interpreting and executing only machine language (numerical) instructions. Humans (almost always) write programs in high-level languages such as C++. Therefore, a C++ program must be translated into machine language in order to be run. Robb T. Koether (Hampden-Sydney College) Programming Languages Mon, Sep 2, 2013 9 / 25

Compilers A compiler is a program that translates a high-level language such as C++ into machine language. The compiler must first check the syntax of the source code. A syntax error is a violation of the grammar rules of the language. If there are syntax errors, then error messages are displayed and the process stops. If there are no syntax errors, then the compiler produces the object code. Robb T. Koether (Hampden-Sydney College) Programming Languages Mon, Sep 2, 2013 10 / 25

Linkers A linker combines the object code produced by the compiler with library functions (e.g., the square root function) needed by the program. If the linker is unable to locate a function, it reports a link error and the process stops. If there are no link errors, the linker produces a fully functioning program. Robb T. Koether (Hampden-Sydney College) Programming Languages Mon, Sep 2, 2013 11 / 25

Program Translation Source code Preprocessor Object code Modified source code Linker Compiler Executable code Robb T. Koether (Hampden-Sydney College) Programming Languages Mon, Sep 2, 2013 12 / 25

Loaders The loader copies the program into a suitable location in main memory and begins execution. A run-time (or logical) error is an error that occurs during execution. Typically, run-time errors cannot be detected by the compiler or the linker before execution. Division by zero would be a run-time error. Robb T. Koether (Hampden-Sydney College) Programming Languages Mon, Sep 2, 2013 13 / 25

Assemblers Many compilers produce assembly code as an intermediate step, and then invoke an assembler to finish the translation. An assembler translates an assembly language program into machine language. Assembly code is more readable than machine code. Robb T. Koether (Hampden-Sydney College) Programming Languages Mon, Sep 2, 2013 14 / 25

Outline 1 Programming Languages 2 Building an Executable Program 3 Program Organization 4 Example 5 Assignment Robb T. Koether (Hampden-Sydney College) Programming Languages Mon, Sep 2, 2013 15 / 25

The C++ Program Hierarchy A statement typically performs a single task. For example, evaluate a formula. A function is a group of statements that accomplishes a task requiring several steps. For example, insert a new name into a list of names. A file contains a group of related functions. For example, all functions that manage lists of names. A program consists of a collection of related files. Robb T. Koether (Hampden-Sydney College) Programming Languages Mon, Sep 2, 2013 16 / 25

Statements A statement is a one-line instruction. A statement typically performs a single action. Examples Evaluate a formula. Print a line of output. Make a yes/no decision. Robb T. Koether (Hampden-Sydney College) Programming Languages Mon, Sep 2, 2013 17 / 25

Functions A function consists of several statements which together perform a single task as a component of a larger problem. Each function is given a name. Program execution begins with the function main(). Examples average(): Find the average of a set of numbers. insert(): Insert a new name into a list of names. Robb T. Koether (Hampden-Sydney College) Programming Languages Mon, Sep 2, 2013 18 / 25

Files A file consists of a set of functions having something in common. Each file is stored separately under its own name. For example, list.cpp. Files are compiled separately from each other, to be linked later. Robb T. Koether (Hampden-Sydney College) Programming Languages Mon, Sep 2, 2013 19 / 25

Projects A project consists of several files containing all the functions which, when linked together, will form a complete program. The project is given a name. For example, ListManager.vcxproj. Robb T. Koether (Hampden-Sydney College) Programming Languages Mon, Sep 2, 2013 20 / 25

The C++ Program Hierarchy MyProg.vcxproj // MyProg.cpp int main() { : } : // stats.cpp float avg(float a, float b) { return (a + b)/2.0; } float max(float a, float b) { if (a > b) return a; else return b; } : // list.cpp void insert(...) { : } void delete(...) { : } int search(...) { : } : Robb T. Koether (Hampden-Sydney College) Programming Languages Mon, Sep 2, 2013 21 / 25

Outline 1 Programming Languages 2 Building an Executable Program 3 Program Organization 4 Example 5 Assignment Robb T. Koether (Hampden-Sydney College) Programming Languages Mon, Sep 2, 2013 22 / 25

Example of a C++ Program Example GradeReport.cpp GradeStats.cpp Robb T. Koether (Hampden-Sydney College) Programming Languages Mon, Sep 2, 2013 23 / 25

Outline 1 Programming Languages 2 Building an Executable Program 3 Program Organization 4 Example 5 Assignment Robb T. Koether (Hampden-Sydney College) Programming Languages Mon, Sep 2, 2013 24 / 25

Assignment Assignment Read Section 1.3. Robb T. Koether (Hampden-Sydney College) Programming Languages Mon, Sep 2, 2013 25 / 25