1 / 75

Introduction to Programming Languages: Concepts and Tools

Learn the fundamentals of programming languages, including language paradigms and problem-solving techniques. Explore popular languages like C/C++, Scheme, Prolog, and Python. Evaluation based on participation, assignments, quizzes, and exams.

rshaw
Download Presentation

Introduction to Programming Languages: Concepts and Tools

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction Programming Language Concepts

  2. Objective of the Course • Understand concepts of programming languages. • Become familiar with different language paradigms and the kind of problems they are for. • Learn to use useful tools. • Learn basics of some widely used languages • C/C++ • Scheme (functional language like Lisp) • Prolog • Python • other language, depending on your interest

  3. Evaluation Criteria • Class participation and attendance • Homework • Programming assignments • Quizzes • Exams

  4. Your Responsibility • Perform the role of a student at a level suitable for a first-tier university. • Behavior and activities: • Read the assigned text before each lecture • Do homework assignments early • Participate in class • Self-directed learning • Analyze your own understanding

  5. Instructor's Responsibility • Provide clear guidance on the course of study • Make the requirements clear and unambiguous • Facilitate discussion, answer your questions • Provide helpful evaluation of your understanding • Listen to and respond to feedback

  6. Why Tools? "If the only tool you have is a hammer, then everything looks like a nail."

  7. What is a Programming Language? • A vocabulary, set of grammar rules, and associated meanings for communication between people and computers, or communication between computers. • Another definition: A programming language is a notational system for describing computation in machine-readable and human-readable form [K. Louden].

  8. Instructions to the CPU • A CPU executes instructions, and uses data, in binary format. An executable computer program may look like this: 0010110010100010 0001011110000000 0010110000011000 1101100001011001 0011001010001011 0000011101001111 1101100001100001 0010110100010001 0001011100001110 …etc… First instruction to CPU Second instruction an address for 2nd instruction 3rd instruction 4th instruction an argument (data) for 4th instruction another argument for 4th instruction 5th instruction 6th instruction … more instructions …

  9. Running A Program • The program and data is loaded into main memory. • The address of the program's first instruction is placed in a special register in the CPU: the program counter. • The CPU is told to fetch and execute that instruction. Memory PC Register

  10. What instructions does the CPU understand? • CPU instructions cause the CPU to implement logic which is designed (hardwired) into the CPU. • Most CPU instruction are very simple, such as: LOAD memory_address TO some register SAVE some register TO memory_address MOVE some register TO another register ADD some register TO another register MULT some register TO another register COMPARE register TO another register TEST some register’s value, jump to new instruction if true JUMPTO new instruction (unconditional branch)

  11. Generations of Computer Languages • Machine language: program as series of binary instructions (0 and 1's) • Assembly Language • "High level" or procedural Languages • this is the category we will study • 4GL - application specific languages

  12. Machine Language • These simple, binary instructions are called machine language instructions. • Writing machine language is slow and difficult, so people invented a symbolic notation for machine language, called assembly language. Assembly Language MOV AX,1F80H PUSH AX INT 21H POP AX Machine Language 0010110010100010 0001111110000000 0010110000011000 1101100001011001 0011001000011000

  13. Assembly Language • Simple assembly language instructions have a 1-to-1 translation to machine language. • A program called an assembler converts assembly language into machine language:unix: as -o output_programinput_program.s Assembly Language MOV AX,1F80H PUSH AX INT 21H POP AX Machine Language 0010110010100010 0001111110000000 0010110000011000 1101100001011001 0011001000011000 assembler

  14. Viewing Assembly Code on a PC • Find a small .exe or .com file, e.g. diskcopy.com • Open a “Command Prompt” (DOS) window. • Enter “debug filename”. • Enter “u” to unassemble and view the program. • Enter “q” to quit debug. C:\WINDOWS\system32> debug diskcopy.com - u 0B30:0000 0E PUSH CS 0B30:0001 1F POP DS 0B30:0002 BA0E00 MOV DX,000E 0B30:0005 B409 MOV AH,09 - q machine code(in hex) assembly language

  15. Macro Assembly Language • Simple assembly language instructions are still very tedious and time consuming to input (“code”). • Macro assembly language adds the use of symbolic variable names and compound instructions. A compound instruction is an instruction that is converted into several machine level instructions.

  16. Macro Assembly Example Here is a simple “Hello, World” program in macro assembly language: .stack .data message db "Hello world, I’m a program.”, "$" .code main proc mov ax,seg message mov ds,ax mov ah,09 lea dx,message int 21h mov ax,4c00h int 21h main endp end main

  17. Problems with Assembly Language Programming is still difficult in macro assembly language: • language does not match the way people think • programs are long and difficult to understand • program logicerrors are common and difficult to correct • difficult to divide a large problem into small problems using assembly language • assembly language is machine dependent -- won’t run on a different type of computer! • you have to rewrite all your software whenever you change to different hardware platform

  18. Low-level and high-level languages • Higher, more "abstract", languages aid programming and thinking about algorithms. • Separate program logic from hardware implementat'n Add 2 numbers in C: In Assembly Language: int sum( int x, int y ) { int z = x + y; return z; } .globl sum .type sum,@function sum: pushl %ebp movl %esp, %ebp subl $4, %esp movl 12(%ebp), %eax addl 8(%ebp), %eax movl %eax, -4(%ebp) movl -4(%ebp), %eax movl %eax, %eax leave ret Which one is easier to understand?

  19. High Level Languages Higher level languages (also called third generation languages) provide these features: • English-like syntax • descriptive names to represent data elements • concise representation of complex logic • use of standard math symbols: total = quantity * price * (1 + taxrate); • conditional execution and looping expressions: if ( total > 0 ) then writeln(“The total is “, total) else writeln(“No sale.”); • functional units, such as "functions" or "classes": radius = sqrt( x*x + y*x ); // call sqrt function

  20. High Level Language Benefits • programming is faster and easier • abstraction helps programmer think at a higher level • standardized languages enable code to be machine independent, and reduces training effort • complex problems can be divided into smaller problems, each coded and tested separately. • code re-use • provides standard programming interface for hardware interactions, such as input and output.

  21. Some higher level languages There are several broad categories: • Imperative: FORTRAN, COBOL, BASIC, C, Pascal • Functional: Scheme, Lisp • Logic Languages: Prolog • Object-oriented: • Pure object-oriented:Java, Python • Object-oriented & imperative: C++, Perl, Visual Basic

  22. Fourth Generation Languages Fourth Generation Languages (4GL) are application specific. They are tailored for a particular application. • SQL (structured query language) for databases • Postscript page description language for printers • PDF (portable document format) for online documents • HTML and PHP for World Wide Web content • Mathematica for symbolic math on a computer Mathematica is a proprietary language; the others are industry standards.

  23. 4GL Example: SQL • SQL is one of the most widely used 4GL. Here are some examples: • Insert data into a database table: INSERT INTO employees (id, lastName, firstName, Job)VALUES (1445, ‘John’,’Smith',’manager’); • Choose data from a table based on criteria: SELECT id, lastName, salary FROM employees WHERE ( Job = ‘manager’ ); • SQL is declarative: statements say what you want done but not how to do it. • Learn more about SQL at the Open Directory Project:http://dmoz.org/Computers/Programming/Languages/SQL

  24. Factors Influencing Language Many factors influence the nature of a programming language. Some major factors are: • Environment: the capabilities of the machine that we are communicating with. • early computers had very few instructions, no operating system, no memory management. • Application Domain: language is influenced by the kind of information we want to communicate. • most languages are used to specify algorithms. • language may contain only statements for the kind of problem we are interested in solving, e.g., Fortran for scientific/numerical computing.

  25. Factors Influencing Language (2) • Methodology: an evolving discipline. • new approaches to constructing software are developed based on experience and need. • O-O emerged as response to limitations of imperative languages in handling complexity. • Preference, Economics, and Patronage: • languages have been influenced by the backing of large companies (Fortran by IBM, Java by Sun) • government patronage: Ada by U.S. DoD. • preferred by experts: • "structured programming" and modularity made popular by C.S. professors

  26. Language Paradigms Imperative (procedural): traditional sequential programming: program statements operate on variables. • variable represents data in memory locations. • characterized by variables, assignment, and loops. • basic unit of imperative programs in the procedure or function • Examples: Algol, C, Pascal, Ada, FORTRAN

  27. Language Paradigms Functional: functions are first-class entities (can be used same as other forms of data); all execution is by function evaluation • characterized by recursion and functions as data • execution on values, not memory locations -- variables are not necessary! • a function can dynamically define and return a new function: self-evolving code. • Examples: Lisp, Scheme, ML, Haskell

  28. Language Paradigms Logic: program is declarative, it specifies what must be true but not how to compute it. • logic inference the basic control • no sequential operation • non-deterministic: may have many solutions or none • Example: Prolog

  29. Language Paradigms Object-oriented: an object contains its own state (data) and the functions that operate on that state. • program logic is instantiation of objects, messages between objects, encapsulation, and protection. • computing model mostly an extension of imperative. • Examples: C++, C#, Java, Smalltalk, Python

  30. More Language Paradigms (1) • Declarative: state what needs computing, not how to compute it (algorithm). • Many 4GL, like SQL and Mathematica share this property. • Prolog is also declarative

  31. More Language Paradigms (1) Concurrent or Parallel: Programming to utilize multiple CPU or multiple threads of execution. • Requires attention to task management, synchronization, and data conflict • sequence of execution may not be predictable. • parallel features are often added to existing programming languages. • Examples: threads in Java, C#, and other languages. MPI (Message Passing Interface) library for cluster and grid computing.

  32. Programming Languages used in Open Source projects at SourceForge.net Source: http://www.cs.berkeley.edu/~flab/languages.html

  33. Example: Euclid’s gcd algorithm Compute the greatest common divisor of two integers. For example, the gcd of 90 and 24 is 6.

  34. /* “functional”implementationof gcd uses recursion */ #include <stdio.h> int gcd(int u, int v) { if (v == 0) return u; else return gcd (v, u % v); //“tail” recursion } int main() /* test of gcd */ { int x, y; printf("Input two integers:"); // note: use references to read input into x, y scanf("%d%d",&x,&y); printf("The gcd of %d and %d is %d\n",x,y, gcd(x,y)); return 0; } C

  35. public class AnyClassAtAll { /** compute greatest common divisor. * @return the g.c.d. of m and n. * 1 if m and n are zero. */ private static long gcd(long u, long v) { long remainder; if (v < 0) v = -v; while ( v != 0 ) { remainder = u % v; u = v; v = remainder; } if ( u == 0 ) return 1; // gcd(0,x) = 1 else return (u>0)? u : -u; // absolute value } ... remainder of the class is irrelevant Java: imperative style GCD

  36. /** This class finds the GCD of one value (the state of * the object) with any other value given as parameter. */ public class GCD { // attribute: state of the object (immutable) private final int value; /** constructor */ public GCD( int value ) { this.value = value; } /** compute GCD of private state and param v */ public int gcd ( int v ) { int u = value; // don't modify object's state while ( v != 0 ) { int t = u % v; u = v; v = t; } return u; } } Java: Object-oriented GCD

  37. ; functional implementation of gcd ; uses recursion (define (gcd u v) (if (= v 0) u (gcd v (modulo u v) ) ) ) Scheme Scheme syntax for defining a function: ( define ( function-name param1 param2 ... ) body of function definition )

  38. ; using the gcd: perform I/O, invoke gcd (define (euclid) (display "enter two integers:") ; use of variables not really necessary ; and not "functional" style (let ((u (read)) (v (read))) (display "the gcd of ") (display u) (display " and ") (display v) (display " is ") (display (gcd u v)) (newline) ) ) Scheme application

  39. /* conditions for GCD */ gcd(U, V, U) :- V = 0. gcd(U, V, X) :- not (V = 0), Y is U mod V, gcd(V, Y, X). Prolog /* Goal: compute the GCD of 288 and 60. */ gcd(288, 60, X). In Prolog, a clause is an assertion that can succeed (be true) or fail of the form: consequence :- a, b, c. means: consequence is true if a, b, and c are true.

  40. C Greatest common denominator for real programmers INTEGER FUNCTION IGCD(U,V) INTEGER U, V, TMP DO WHILE ( V .NE. 0 ) TMP = V V = MOD(U,V) U = TMP END DO IGCD = V RETURN END PROGRAM MAIN WRITE(6,*) "Input two integers:" READ(5,*) I, J WRITE(6,100) I, J, IGCD(I,J) 100 FORMAT("GCD of ",I4," and ",I4," is ",I4) STOP END Assign returned value I, J implicitly integer FORTRAN 77

  41. Paradigm use is rarely “pure” • The C gcd() example defines gcd in a functional style, even though C is mainly imperative. • Java can be used to write purely imperative style programs (all static, no objects) • also, in Java primitive data types aren't objects • Scheme uses I/O operations, which depend on sequence and external effects (imperative style) • in a "pure" functional languages, the result of a function depends only the the parameters • this isn't true of I/O operations

  42. Language Design Some conflicting objectives, criteria, and goals

  43. Goals for language design • Power • Flexibility • Expressiveness • Writability • Efficient implementation • Support for abstraction • Simplicity • Clarity • Consistency (orthogonality) • Readability • Applicability to problem domain • Portability

  44. Readability or writability? • Should programming languages promote the writing of programs or the reading of programs? • Many people (including the writer!) may need to read a program after it is written.

  45. Readability or writability? Q: What does this Perl script do? #!/usr/bin/perl foreach $FILE ( @ARGV ) { open(FILE) || die "Couldn't open $FILE"; while($_ = <FILE>) { print $_; } close(FILE); }

  46. Language definition • Syntax: defines the grammar of a language. • what are valid statements, what is a valid program. • given in formal notation such as BNF or ENBF. • Semantics: the meaning of the elements of a language. • usually defined in human language • formal notations exist, but not widely used • can have a static component: type checking, definition checking, other consistency checks prior to execution. • dynamic: run-time checking of array indices, run-time type determination.

  47. Syntax • Defines symbols and grammar of a language. • Usually given in Backus-Naur Form or its extensions. if-statement ::= if ( expression ) statement-block [ else statement-block ] statement-block ::= statement ';' | '{' statement ';' [...] '}' statement ::= if-statement | assignment-statement | while-statement | ...etc...

  48. Language implementation strategies • Compiler: multi-step process that translates source code into target code; then the user executes the target code. • Interpreter: one-step process in which the source code is executed directly. • Hybrids: "just in time" compilers - Perl "virtual machine language" - Java, Microsoft .NET languages.

  49. Execute on machine Compiler versus Interpreter Source Program Input Interpreter Output Source Program Compiler Input Target Program Output Execute on machine

  50. Language processing: Interpreted • Interpreted: BASIC, Postscript, Scheme, Matlab • The interpreter reads the source program and executes each command as it reads. • The interpreter “knows” how to perform each instruction in the language. Source Program Interpreter Execution

More Related