CS 403: Programming Languages Lecture 16 Fall 2003 Department of Computer Science University of Alabama Joel Jones
Dec 30, 2015
CS 403: Programming Languages
Lecture 16
Fall 2003
Department of Computer Science
University of Alabama
Joel Jones
Lecture 16 2
Overview
Announcements
M4 Macro Processor
Geoffrey Haynes
Lecture 16 4
m4 is a general purpose macro processor that can be used to preprocess C and assembly language programs, among other things. Besides the straightforward replacement of one
string of text by another, m4 lets you perform integer arithmetic file inclusion conditional macro expansion string and substring manipulation
Lecture 16 5
You can use built-in macros to perform these tasks or define your own macros. Built-in and user-defined macros work exactly the same way except that some of the built-in macros have side effects on the state of the process.
The basic operation of m4 is to read every legal token (string of ASCII letters and digits and possibly supplementary characters) and determine if the token is the name of a macro. The name of the macro is replaced by its defining text, and the resulting string is pushed back onto the input to be rescanned. Macros may be called with arguments. The arguments are collected and substituted into the right places in the defining text before the defining text is rescanned.
A small review of PythonJesse Booth
Named after Monty Python
Released to public in 1991
Syntax: cross between C and the Modula family
Lecture 16 7
Why Use Python?
Simple Syntax Easy to learn and quick to code
Portable between Unixes and even other operating systems
Large Standard Library Used for large complex projects with
many different developers
Lecture 16 8
Why is Python different?
Choice of Object-oriented programming Block structure is controlled by actual
indentation not brackets Easy to combine with C for performance-
critical applications Substantial speed gains
Lecture 16 9
PythonBin QiaoPython is a scripting language designed for close integration with C. Its syntax is rather like a cross between that of C and the Modula family.
It has a type system comparable in expressive power to Perl’s.
Python is generally thought to be the least efficient and slowest of the major scripting languages.
Lecture 16 10
Considerations of Using PythonProgrammability. Python is well suited to functioning as a glue language which is commonly used to implement the lower level operations.
Prototyping. Python provides a good environment for quickly developing an initial prototype.
Simplicity and ease of understanding. Python is powerful but very simple, which lets you learn the language more quickly, and then rapidly write code.
Java integration. Jython is a re-implementation of Python in Java that complies Python codes into Java bytecodes. The resulting environment has very tight, almost seamless, integration with Java.
RubyRobert Bradford
What is Ruby?
Ruby is a new object oriented programming language.
Though rarely used in the United States, it has become very popular in other areas of the world.
What are the advantages to using Ruby as a language?
Features similar to Perlo Process text files o Perform system management tasks
Few lines of code, but not cryptic
Syntax and semantics are simple
Ruby is open source
Platform independent… can be used on Windows, Unix or Linux
created by: created by: Yukihiro MatsumotoYukihiro Matsumoto
Lecture 16 12
Features Simple syntax Error handling Blocks in syntax
o Surrounded by either { } or do … endo Can be passed to methods
Integers can be used without counting internal representation No variable declarations, naming conventions denote scope
o 'var' = local variableo '@var' = instance variableo '$var' = global variable
Lecture 16 13
Code Analysis First… a definition of Fibonacci
o The basic Fibonacci series is a sequence of integers, starting with two 1's, in which each subsequent term is the sum of the two preceding terms. The series is sometimes used in sorting algorithms and in analyzing natural phenomena.
Demonstrated by this example:o Parallel assignmento Local variable creationo Looping structureo Function definitiono Output
Tool Command Language(Tcl)
By: Steven Kemp
Lecture 16 15
What is Tcl?
Tcl is a small language interpreter that can be linked with compiled C libraries.
It can also be used for embedded scripts, which are scripts that are called from C programs.
It was first released publicly in 1990.
Lecture 16 16
Advantages of Tcl It is extremely flexible and radically
simple. It has a totally consistent syntax. The interpreter itself can be
redefined from within Tcl. It’s compact design makes it more
efficient for smaller projects
Lecture 16 17
Minor Disadvantages of Tcl
The only data structure is association lists
Not good for use on large programs
Syntax is slightly odd, even though it is consistant
Lecture 16 18
JavaEric Jackson Developed by Sun Microsystems Created to be “write once, run anywhere” Automatically handles memory management. Can inherit from just about anything. Easy to learn. Style very similar to C and C++ MySet.java code to implement Set ADT
Brad Hutchinson
PHP Open source, server-side, HTML embedded scripting language used to create dynamic web pages.
Created in 1994, but during mid 1997 the development changed hands and the parser was rewritten from scratch to create PHP version 3.
Syntax is similar to that of Perl or C++.
The script is enclosed within special PHP tags, and because PHP is embedded within tags it is possible to jump between HTML and PHP.
This prevents having to actually write large amounts of HTML code using PHP.
Since it is executed on the server, the script cannot be viewed by someone accessing the web page.
Can perform any task that any CGI program can do, but its strength lies in its compatibility with many types of databases.
Can talk across networks using IMAP, SNMP, NNTP, POP3, or HTTP.
Little Ruby? Big Ruby?
What is Ruby?
Ruby is the interpreted scripting language for quick and easy object-oriented programming. It has many features to process text files and to do system management tasks (as in Perl). It is simple, straight-forward, extensible, and portable.
The Creator of RubyYukihiro Matsumoto, a.k.a Matz [email protected]
R. Allen Sanford
Lecture 16 21
Features of Ruby
Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy to handle errors. Ruby's operators are syntax sugar for the methods. You can redefine them easily. Ruby is a complete, full, pure object oriented language: OOL. This means all data in Ruby is an object. Ruby's OO is carefully designed to be both complete and open for improvements. Ruby features single inheritance only, *on purpose*. Ruby features blocks in its syntax (code surrounded by '{' ... '}' or 'do' ... 'end'). These blocks can be passed to methods, or
converted into closures. Ruby features a true mark-and-sweep garbage collector. It works with all Ruby objects. You don't have to care about
maintaining reference counts in extension libraries. This is better for your health. ;-) Writing C extensions in Ruby is easier than in Perl or Python, due partly to the garbage collector, and partly to the fine
extension API. SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. Ruby needs no variable declarations. It uses simple naming conventions to denote the scope of variables. Examples: simple
'var' = local variable, '@var' = instance variable, '$var' = global variable. So it is also not necessary to use a tiresome 'self.' prepended to every instance member.
Ruby can load extension libraries dynamically if an OS allows. Ruby features OS independent threading. Thus, for all platforms on which Ruby runs, you also have multithreading,
regardless of if the OS supports it or not, even on MS-DOS! ;-) Ruby is highly portable: it is developed mostly on Linux, but works on many types of UNIX, DOS, Windows 95/98/NT, Mac,
BeOS, OS/2, etc.
R. Allen Sanford
Information obtain from http://www.ruby-lang.org/en/
Lecture 16 22
DocumentationAll of the Previous information and more can be found at:
http://www.ruby-lang.org/en/
Other useful links are:
Great intro to ruby!
http://www.math.umd.edu/~dcarrera/ruby/0.3/
Ruby, Gui’s, and Tk toolkit.
http://httpd.chello.nl/k.vangelger/ruby/learntk/
Demonstration on generating html templates using ruby.
http://www.hillmanimages.com/912/tutor01.html
R. Allen Sanford
Lecture 16 23
RubyCharles B. Ward Ruby is an interpreted language, similar in a number of
ways to perl, php, and python. Ruby is an purely object oriented programming
language, like Smalltalk. Everything is an object, including numbers.
Ruby is *not* strongly typed. Ruby features only single-inheritance, instead allowing
classes to import modules of methods. Ruby has automatic memory management (garbage
collector).
Lecture 16 24
More Ruby Ruby naming conventions to denote the scope
of variables. 'var' = local variable '@var' = instance variable '$var' = global variable.
Ruby supports internal threading. (programs can be multithreaded independently of the operating system)
Ruby is compatible with LINUX, UNIX, DOS, Windows 95/98/NT, Mac, BeOS, OS/2, etc.
TCL (Tool Command Language)
Charles Lemont Howard
developed mainly by John Ousterhout
Lecture 16 26
Pros and Cons of TCL
PROS It is a high-level scripting language
~ lot less code to write to get job done
It runs on many platforms It is interpreted ~ execute code
without compiling and linking It is extensible It is embeddable in your
applications ~ meaning you can use TCL as an application language
terrific language for database applications
Tcl is free
CONS
TCL is interpreted ~ programs written in TCL are slow because TCL only deals with strings
Syntax checking only at runtime
Fundamental aspects of the Ruby programming language
Kunal Vyas
Lecture 16 28
About Ruby Yukihiro Matsumoto (Matz), a professional programmer working for and
open source company in Japan (netlab.co.ip) invented the Ruby programming language.
Ruby is a "pure Object Oriented Language". Everything in Ruby is an object
Ruby is a "pure Object Oriented Language". Everything in Ruby is an object
Ruby uses pass-by-reference. Ruby also supports default formal argument assignment
Lecture 16 29
Ruby's features are as follows: Interpretive Variables have no type (dynamic typing) No declaration needed Simple syntax No user-level memory management Everything is object Class, inheritance, methods Singleton methods Mix-in by modules Iterators Closures Text processing and regular expression Bignums Dynamic loading Exception handling
Lecture 16 30
What made ruby different from other Programming Languages
Language: Ruby Added: Ruby Took Away:
C *Operatoroverloading
Object Orientation In light of the fact that C is the defining language for Ruby and Ruby is extendable using C, all the functionality of C is supported.
Smalltalk *Object Orientation*Everythingis an object!
1.Multiple Inheritance.
2. Ruby isn't as strict as Smalltalk with regard to object, inheritanceand class hierarchy
Lecture 16 31
Continue
Language: Ruby Added: Ruby Took Away: Perl *ScriptingPower
1. Clean Object Orientation - Ruby was initially designed with OO in mind, whereas Perl was extended to
support objects.
1.Scope rules in Ruby (default: local) eliminate the need for any 'my'
declarations. 2. Ruby eliminates a lot of Perl's syntax sugar without losingfunctionality making
Ruby less cryptic.
Lecture 16 32
Continue
Language: Ruby Added: Ruby Took Away:
Python *ExceptionHandling
1. Exception handling 2. Type coercion between small and long integers3. Mark and sweep garbage collection instead of aRef-counter garbage collection.
4. Inheritance / subclassing
1. Ruby's instance variables are prefixed by a at eliminating the need for 'self' declarations.
2. Python treats types and classes differently, Ruby treats them the same.
3. Tuples
Lecture 16 33
Continue
Language: Ruby Added: Ruby Took Away: Eiffel *Objectorientation and
simple syntax
Ruby is interpreted Compilation is not Required for Ruby.
Lisp *Lambda expressions
Object Orientation All those Parentheses
Lecture 16 34
For what applications would this language be suitable?
Text processing CGI programming Network programming XML programming GUI applications AI and Exploratory Mathematics General programming Prototyping Programming education eXtreme programming
Lecture 16 35
For what would it not be suitable? High traffic web applications
Operating systems implementation
Compiler implementation
RubyAndré Taylor
“The object oriented scripting language”
André Taylor
Lecture 16 37
Introduction Ruby is the interpreted scripting language for quick
and easy object-oriented programming. It has many features to process text files and to do system management tasks (as in Perl). It is simple, straight-forward, extensible, and portable.
Ruby is highly portable: it is developed mostly on Linux, but works on many types of UNIX, DOS, Windows 95/98/NT, Mac, BeOS, OS/2, etc.
André Taylor
Lecture 16 38
Important features Ruby has simple syntax, partially inspired by Eiffel
and Ada. Ruby has exception handling features, like Java or
Python, to make it easy to handle errors. Ruby features a true mark-and-sweep garbage
collector. It works with all Ruby objects. You don't have to care about maintaining reference counts in extension libraries. This is better for your health. ;-)
André Taylor
Lecture 16 39
Important features cont. Ruby needs no variable declarations. Examples:
'var' = local variable '@var' = instance variable '$var' = global variable.
Ruby features blocks in its syntax (code surrounded by '{' ... '}' or 'do' ... 'end'). These blocks can be passed to methods, or converted into closures.
André Taylor
Lecture 16 40
Tcl/TkDavid Marshall
• Tcl (Tool Command Language) Created in 1989 by John Ousterhout An interpreted language with the main
purpose of integrating or tying together other applications
Tk An extension of Tcl created for component-
based GUI design
Lecture 16 41
Features of Tcl/Tk Extensible – Easy to add your own features
Designed so that added features feel as natural as the basic features of the language
Embeddable – Interpreter exists as C Library GUI building with Tk toolkit
Extremely fast to design and implement a GUI out of components provided by Tk
Cross Platform Runs on Windows, Mac, and Unix platforms
Lecture 16 42
Tcl Compared Often compared to Perl
Syntax is much simpler than Perl and has less of a learning curve
Easier to maintain – Perl syntax is much more complex, so reading and maintaining Tcl code is an easier task
Tk is available for Perl, but is less intuitive and clumsier because it was designed for Tcl
More information available at http://www.tcl.tk
Bash, XSLT, Dot, and CNathan Wiegand
What to do with
all those tools?
Lecture 16 44
Bash
The Bourne Again SHell - Created by Ritchie and Thompson from the Bourne Shell(sh).
Designed in order for people to better communicate with the system they were developing at AT&T
Lecture 16 45
XSLT
eXtensible Stylesheet Language Transform
Used to translate XML (eXtensible Markup Language) into another format (e.g. HTML, Word File, Dot)
Lecture 16 46
Dot
Open graph drawing software developed at AT&T
Used to present datastructures and other things which can be represented as graphs (i.e. file trees)
Lecture 16 47
File Tree Viewer
Start with a Bash script which walks your file tree and creates and XML file from the files it observes.
Next, use an XSL Transform to convert that XML document into a Dot Script.
Use Dot to convert this script into a nice Gif image for viewing.
Lecture 16 48
The Bash Script#!/bin/bash
dir=$(pwd) function dirExp {
echo "<node s=\"box\" c=\"red\" value=\"$2\">"cd "$1/$2"for i in $(ls -l -1 | grep ^- | gawk '{print $9}')do
VAL=$($dir/conv $(ls -l -1 $i | gawk '{print $1}'))if [ $(($VAL/100)) = 7 ]; then
color="green"else
color="white"fiecho "<node s=\"ellipse\" c=\"$color\" value=\"$i\"/>"
done
for i in $(ls -l -1 | grep ^d | gawk '{print $9}')do
dirExp "$1/$2" $i doneecho "</node>"
}dirExp $dir . > "$dir/dir.xml"cd $dirxsltproc -o structure.dot graph.xsl dir.xmldot -Tgif -o structure.gif structure.dotmv structure.gif ~/public_html
Lecture 16 49
The C file#include <stdio.h>
intmain
(int argc, char *argv[])
{int perm[3],
i;
if(argc!=2)fprintf(stderr,"Error\n");
for(i=1,perm[0]=perm[1]=perm[2]=0;i<strlen(argv[1]);i++) {perm[(i-1)/3]*=2;if(argv[1][i]!='-')
perm[(i-1)/3]++;}
fprintf(stdout,"%d%d%d\n",perm[0],perm[1],perm[2]);
return 0;}
Lecture 16 50
The Produced XML<node s="box" c="red" value="."><node s="ellipse" c="green" value="conv"/><node s="ellipse" c="white" value="conv.c"/><node s="ellipse" c="white" value="dir.xml"/><node s="ellipse" c="white" value="graph.xsl"/><node s="ellipse" c="white" value="Makefile"/><node s="ellipse" c="green" value="new.sh"/><node s="ellipse" c="white" value="structure.dot"/><node s="box" c="red" value="Project1"><node s="ellipse" c="green" value="a.out"/><node s="ellipse" c="green" value="conv"/><node s="ellipse" c="white" value="dir.tree"/><node s="ellipse" c="white" value="dir.xml"/><node s="ellipse" c="white" value="graph.xsl"/><node s="ellipse" c="green" value="NathanWiegand"/><node s="ellipse" c="white" value="NathanWiegand.cpp"/><node s="ellipse" c="green" value="NathanWiegand.gdb"/><node s="ellipse" c="green" value="output.txt"/><node s="ellipse" c="green" value="runme.sh"/><node s="ellipse" c="white" value="structure.dot"/><node s="ellipse" c="white" value="t"/></node><node s="box" c="red" value="Project2"><node s="ellipse" c="white" value="dir.tree"/><node s="ellipse" c="white" value="dir.xml"/><node s="ellipse" c="green" value="example.sh"/><node s="ellipse" c="white" value="file.dot"/><node s="ellipse" c="white" value="Makefile"/><node s="ellipse" c="white" value="structure.gif"/><node s="ellipse" c="white" value="t"/></node></node>
XSLT<?xml version = "1.0"?><xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method="text" omit-xml-declaration="yes"/>
<xsl:template match="/" xml:space="preserve">digraph treestruct {
<xsl:for-each select="//node"><xsl:call-template name="print-node"/></xsl:for-each>
}</xsl:template>
<xsl:template name="print-node"><xsl:variable name="this">
<xsl:value-of select="generate-id()"/></xsl:variable>
<xsl:variable name="label" xml:space="default"><xsl:text disable-output-escaping="yes">"</xsl:text><xsl:value-of select="@value"/><xsl:text disable-output-escaping="yes">"</xsl:text>,shape=<xsl:value-of
select="@s"/>,style=filled,fillcolor=<xsl:value-of select="@c"/></xsl:variable>
<xsl:value-of select="$this"/> [label=<xsl:value-of select="$label"/>];
<xsl:for-each select="node" xml:space="default"><xsl:value-of select="$this"/><xsl:text disable-output-escaping="yes"> -> </xsl:text><xsl:value-of select="generate-id()"/>;
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Lecture 16 52
Excerpt of the Dot filedigraph treestruct {
id2588904 [label=".",shape=box,style=filled,fillcolor=red];id2588904 -> id2588686;id2588904 -> id2587734;id2588904 -> id2587745;id2588904 -> id2587755;id2588904 -> id2588236;id2588904 -> id2588246;id2588904 -> id2588257;id2588904 -> id2588267;id2588904 -> id2588406;id2588686 [label="conv",shape=ellipse,style=filled,fillcolor=green];id2587734 [label="conv.c",shape=ellipse,style=filled,fillcolor=white];id2587745 [label="dir.xml",shape=ellipse,style=filled,fillcolor=white];
Lecture 16 53
The Product