Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 12 Imperative Programming I really hate this.

Similar presentations


Presentation on theme: "Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 12 Imperative Programming I really hate this."— Presentation transcript:

1 Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 12 Imperative Programming I really hate this darn machine; I wish they would sell it; It won’t do what I want it to, but only what I tell it. Programmer’s lament (anonymous)

2 Copyright © 2006 The McGraw-Hill Companies, Inc. Contents 12.1 What Makes a Language Imperative? 12.2 Procedural Abstraction 12.3 Expressions and Assignment 12.4 Library Support for Data Structures 12.5 C 12.6 Ada 12.7 Perl

3 Copyright © 2006 The McGraw-Hill Companies, Inc. 12.5 C “C was originally designed for and implemented on the UNIX operating system on the DEC PDP-11, by Dennis Ritchie. The operating system, the C compiler, and essentially all UNIX applications programs (including all of the software used to prepare this book) are written in C.... C is not tied to any particular hardware or system, however, and it is easy to write programs that will run without change on any machine that supports C.”

4 Copyright © 2006 The McGraw-Hill Companies, Inc. Influences Multics, PL/I Application: typesetting documentation PDP-11: 16-bit minicomputer; 32 KB memory BCPL: typeless Portability: big-endian vs. little-endian machines Good code from a non-optimizing compiler Hardware support for: ++, --, +=, etc.

5 Copyright © 2006 The McGraw-Hill Companies, Inc. General Characteristics Relatively low level language Macro facility Conditional compilation Lacks: iterators, generics, exception handling, overloading Assignments are expression; ex: strcpy

6 Copyright © 2006 The McGraw-Hill Companies, Inc. Dynamic Allocation int *a;... a = malloc(sizeof(int) *size); /* ANSI C: a = (int *) malloc(sizeof(int) *size); C++: a = new int[size]; */ /* deallocation left to programmer */

7 Copyright © 2006 The McGraw-Hill Companies, Inc. Ex: Grep #include libraries line 6 is a function signature (or prototype) main processes command line arguments function find –reads file –search each line –write line if match –fgets

8 Copyright © 2006 The McGraw-Hill Companies, Inc. Ex: Average compute min, max, average of a set of numbers formatting: scanf, printf conditional assignment 2nd argument to scanf must be an address –caught by some compilers –segment violation at run time

9 Copyright © 2006 The McGraw-Hill Companies, Inc. Ex: Symbolic Differentiation simple calculus formulas no simplification represent expressions using abstract syntax

10 Copyright © 2006 The McGraw-Hill Companies, Inc. Symbolic Differentiation Rules Fig 12.4

11 Copyright © 2006 The McGraw-Hill Companies, Inc. Abstract Syntax Expression = Variable | Value | Binary Variable = char id Value = int value Binary = char op; Expression left, right

12 Copyright © 2006 The McGraw-Hill Companies, Inc. #include #include "node.h" struct node *mknodebin(char op1, struct node *left, struct node * right) { struct node *result; result = (struct node*) malloc(sizeof(struct node)); result->kind = binary; result->op = op1; result->term1 = left; result->term2 = right; return result; }

13 Copyright © 2006 The McGraw-Hill Companies, Inc. struct node *diff(char x, struct node *root){ struct node *result; switch (root->kind) { case value: result = mknodeval(0); break; case var: result = mknodeval(root->id == x?1:0); break;

14 Copyright © 2006 The McGraw-Hill Companies, Inc. case binary: switch (root->op) { case '+': result = mknodebin(plus, diff(x, root->term1), diff(x, root->term2)); break;... return result; }


Download ppt "Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 12 Imperative Programming I really hate this."

Similar presentations


Ads by Google