540 likes | 697 Views
CS 403: Programming Languages. Lecture 16 Fall 2003 Department of Computer Science University of Alabama Joel Jones. Overview. Announcements. M4 Macro Processor. Geoffrey Haynes.
E N D
CS 403: Programming Languages Lecture 16 Fall 2003 Department of Computer Science University of Alabama Joel Jones
Overview • Announcements
M4 Macro Processor Geoffrey Haynes
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
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
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
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
PythonBin Qiao • Python 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.
Considerations of Using Python • Programmability. 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 created by: Yukihiro Matsumoto • 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 Perl • Process text files • 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
Features • Simple syntax • Error handling • Blocks in syntax • Surrounded by either { } or do … end • Can be passed to methods • Integers can be used without counting internal representation • No variable declarations, naming conventions denote scope • 'var' = local variable • '@var' = instance variable • '$var' = global variable
Code Analysis • First… a definition of Fibonacci • 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: • Parallel assignment • Local variable creation • Looping structure • Function definition • Output
Tool Command Language(Tcl) By: Steven Kemp
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.
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
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
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? The Creator of Ruby Yukihiro Matsumoto, a.k.a Matz matz@netlab.jp 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. R. Allen Sanford
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/
Documentation All 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
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).
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
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 Pros and Cons of TCL
Fundamental aspects of the Ruby programming language Kunal Vyas
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
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
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
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
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
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
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
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
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
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?
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
XSLT • eXtensible Stylesheet Language Transform • Used to translate XML (eXtensible Markup Language) into another format (e.g. HTML, Word File, Dot)
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)
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.
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" fi echo "<node s=\"ellipse\" c=\"$color\" value=\"$i\"/>" done for i in $(ls -l -1 | grep ^d | gawk '{print $9}') do dirExp "$1/$2" $i done echo "</node>" } dirExp $dir . > "$dir/dir.xml" cd $dir xsltproc -o structure.dot graph.xsl dir.xml dot -Tgif -o structure.gif structure.dot mv structure.gif ~/public_html
The C file #include <stdio.h> int main (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; }
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>