Top Banner
Programming Languages Meeting 14 December 9/10, 2014
36

Programming Languages Meeting 14 December 9/10, 2014.

Jan 04, 2016

Download

Documents

Kerry Mitchell
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programming Languages Meeting 14 December 9/10, 2014.

Programming Languages

Meeting 14December 9/10, 2014

Page 2: Programming Languages Meeting 14 December 9/10, 2014.

CATS

Page 3: Programming Languages Meeting 14 December 9/10, 2014.

Planning

• Final Exam, next week, Tuesday, December 16, 2:30 – 5:00 pm, MSC 290

• Or Wednesday, December 17, 6:15 – 8:45 pm, MSC G86

Page 4: Programming Languages Meeting 14 December 9/10, 2014.

Topics for Final

• Syntax and syntax specification: EBNF– Nonterminal, terminal, production, start symbol– Grammar of expressions• Inferred precedence and association

– Syntax trees• Abstract syntax

Page 5: Programming Languages Meeting 14 December 9/10, 2014.

Topics (1.1)

• Semantics and semantic specification– Env, Loc, Store, Value, Memory– Program functions– Backus notation for functional programming

Page 6: Programming Languages Meeting 14 December 9/10, 2014.

Topics (2)

• Semantics examples– REF, a language of pointers– Multiple assignment statements

• Control structures– Sequence – While– If-then– For

Page 7: Programming Languages Meeting 14 December 9/10, 2014.

Topics (3)

• Parameter passing– Value– Result– Value-result– Reference– Name

Page 8: Programming Languages Meeting 14 December 9/10, 2014.

Topics (4)

• Functional programming– Lists – Atoms– Primitive functions– Functionals– User-defined functions– Predicates

Page 9: Programming Languages Meeting 14 December 9/10, 2014.

Topics (4.1)

• Developing from primitives and functionals– Use of recursion

• Catching error conditions– Proper error messages

• Measures of lists– Length– Depth

Page 10: Programming Languages Meeting 14 December 9/10, 2014.

Topics (5)

• Scripting languages– AWK– Perl

• Regular expressions

Page 11: Programming Languages Meeting 14 December 9/10, 2014.

AWK Questions

Page 12: Programming Languages Meeting 14 December 9/10, 2014.

AWK Programs

{for (w=1;w<=NF;w++) count[$w]++}END {for (w in count) print count[w],w}

/^$/ {count++}END {print count}

/<..*>/

Page 13: Programming Languages Meeting 14 December 9/10, 2014.

Paradigms

• Programming paradigm: pattern of problem-solving thought that underlies a particular genre of programs and languages.

• Four main programming paradigms:– Imperative– Object-oriented– Functional– Logic (declarative)

Page 14: Programming Languages Meeting 14 December 9/10, 2014.

Imperative

• Follows the classic von Neumann-Eckert model:– Program and data are indistinguishable in memory– Program: a sequence of commands– State: values of all variables when program runs– Large programs use procedural abstraction

• Examples of imperative languages: – Cobol, Fortran, C, Ada, Perl– Historically: PL1, Algol, Pascal

Page 15: Programming Languages Meeting 14 December 9/10, 2014.

Object-oriented

• Collection of objects (instantiated from classes) that interact by passing messages that transform the state.

• Need to know:– Ways of sending messages– Inheritance– Polymorphism

• Examples of OO languages:– Smalltalk, Modula, Java, C++, C#, Python

Page 16: Programming Languages Meeting 14 December 9/10, 2014.

Functional

• Models a computation with a collection of functions acting on a set of objects, usually lists.– Input: domain of the function– Output: codomain of the function

• Functional languages are characterized by:– Functional composition– Recursion

• Examples of functional languages:– Lisp, Scheme, ML, Haskell

Page 17: Programming Languages Meeting 14 December 9/10, 2014.

Logical

• Logic programming declares what outcome the program should accomplish, rather than how it should be accomplished.

• Programs are:– Sets of constraints on a problem– Achieve all possible solutions– Nondeterministic, in some cases

• Example of a logic programming language: – Prolog

Page 18: Programming Languages Meeting 14 December 9/10, 2014.

Special Languages

• Symbolic computation– Macsyma, Mathematica, Maple, Reduce

• Mathematical– APL, Basic, Cayley

• Music– ChucK

• Markup– HTML, MusicXML, LaTeX

• Scripting– AWK, Perl, JavaScript, PHP

• and hundreds more for every special circumstance

Page 19: Programming Languages Meeting 14 December 9/10, 2014.

PERL

• Practical Extraction and Support Language• A glue language under Unix• Written by Larry Wall• See www.perl.com

Page 20: Programming Languages Meeting 14 December 9/10, 2014.

PERL

• Scripting language• More powerful than AWK• More flexible than shell scripts• Syntax derived from C• Large language• Later versions contain classes• Many irregularities in design• Many alternatives for syntax

Page 21: Programming Languages Meeting 14 December 9/10, 2014.

PERL

Beginning assumptions and notations• Statements are terminated by ; (semicolon)• Comments start with # and occupy the rest of

the line.– NO two-line comments

• Parentheses around arguments to built-in functions are optional

Page 22: Programming Languages Meeting 14 December 9/10, 2014.

PERL

• First line of program is an interpreter directive, written as a special type of comment, giving the full path to the Perl interpreter

#!/usr/local/bin/perl -w

Page 23: Programming Languages Meeting 14 December 9/10, 2014.

Example 1

#!/usr/local/bin/perl –wprint(“Hello, world!\n”);

Page 24: Programming Languages Meeting 14 December 9/10, 2014.

Example 1

#!/usr/local/bin/perl –wprint(“Hello, world!\n”);

Inserts new line character

Page 25: Programming Languages Meeting 14 December 9/10, 2014.

PERL

• Scalars– Integer, real, string• Identifier must start with $

• Arrays– Indexed by integers• Identifier must start with @

– Indexed by strings• Identifier must start with %

Page 26: Programming Languages Meeting 14 December 9/10, 2014.

Example 2

#!/usr/local/bin/perl –w

print(“What is your name? ”);$name = <STDIN>;chomp($name);print “Hello, $name!\n”;

Page 27: Programming Languages Meeting 14 December 9/10, 2014.

Example 2

#!/usr/local/bin/perl –w

print(“What is your name? ”);$name = <STDIN>;chomp($name);print “Hello, $name!\n”;

Critical space

Page 28: Programming Languages Meeting 14 December 9/10, 2014.

Example 2

#!/usr/local/bin/perl –w

print(“What is your name? ”);$name = <STDIN>; #Reads one line chomp($name); #Cuts off EOLprint “Hello, $name!\n”;

Page 29: Programming Languages Meeting 14 December 9/10, 2014.

PERL

• Control structuresif-then-elsifforeach $element (%list) { … }

Page 30: Programming Languages Meeting 14 December 9/10, 2014.

Example 3

#!/usr/local/bin/perl –w

print “What is your name? ”;$name = <STDIN>;chomp($name);if ($name eq “Nico”)

{print “Hello, $name! Glad you’re here.\n”}

elsif ($name eq “Daphne”){print “Going outside, $name?\n”}

else{print “Hello, $name.\n”};

Page 31: Programming Languages Meeting 14 December 9/10, 2014.

PERL

• Dynamically typed– Type inferred from operations– Compare

2 < 10 (true)2 lt 10 (false)2 < “10” (true)“2” lt 10 (false)

• Numbers, strings, regular expressions• Operators are not overloaded

Page 32: Programming Languages Meeting 14 December 9/10, 2014.

Arrays of Strings

• Can be initialized with a list of strings @words = (“camel”,”llama”,”alpaca”);• Or use the keystroke saving function qw

@words=qw(camel llama alpaca);• Note that an individual element is a string so that the

string identifier syntax must be used $words[0] has value camel

Page 33: Programming Languages Meeting 14 December 9/10, 2014.

Array Bounds

• Out of bounds arrays– Impossible with associative arrays: strings are not

presumed to have order (in the sense of a next function)

– Impossible with integer indexed arrays@a = (2,3,5,7);$a[7] = 17;

We’ll check the Env and Store

Page 34: Programming Languages Meeting 14 December 9/10, 2014.

Scalar Operators

• Numeric: + - * / ** %• Numeric comparison: < <= == >= > !=• String: . (for concat) x (for multiple concat)• String comparison: lt le eq ge gt ne• Assignment:

= (carries value of expression)binary assignment: += *= .= etcincrement: ++$i $i++

(increment then assign vs. assign then increment)

Page 35: Programming Languages Meeting 14 December 9/10, 2014.

Parameter Passing

• Parameter passing– Formal parameters not included– Values of arguments to a function come in the

array @_– Example, the call range(1,74,3) puts 1 in $_[0], 74

in $_[1], and 3 in $_[2]– Thus, a function can have a varying number of

arguments.

Page 36: Programming Languages Meeting 14 December 9/10, 2014.

Parameter Passing (2)

• If parameter values are passed to a procedure (subroutine in PERL language) in an array local to the procedure, how are values returned to the calling program?

• Options– – –