Awk.Info

"Cause a little auk awk
goes a long way."

About awk.info
 »  table of contents
 »  featured topics
 »  page tags


About Awk
 »  advocacy
 »  learning
 »  history
 »  Wikipedia entry
 »  mascot
Implementations
 »  Awk (rarely used)
 »  Nawk (the-one-true, old)
 »  Gawk (widely used)
 »  Mawk
 »  Xgawk (gawk + xml + ...)
 »  Spawk (SQL + awk)
 »  Jawk (Awk in Java JVM)
 »  QTawk (extensions to gawk)
 »  Runawk (a runtime tool)
 »  platform support
Coding
 »  one-liners
 »  ten-liners
 »  tips
 »  the Awk 100
Community
 »  read our blog
 »  read/write the awk wiki
 »  discussion news group

Libraries
 »  Gawk
 »  Xgawk
 »  the Lawker library
Online doc
 »  reference card
 »  cheat sheet
 »  manual pages
 »  FAQ

Reading
 »  articles
 »  books:

WHAT'S NEW?

Mar 01: Michael Sanders demos an X-windows GUI for AWK.

Mar 01: Awk100#24: A. Lahm and E. de Rinaldis' patent search, in AWK

Feb 28: Tim Menzies asks this community to write an AWK cookbook.

Feb 28: Arnold Robbins announces a new debugger for GAWK.

Feb 28: Awk100#23: Premysl Janouch offers a IRC bot, In AWK

Feb 28: Updated: the AWK FAQ

Feb 28: Tim Menzies offers a tiny content management system, in Awk.

Jan 31: Comment system added to awk.info. For example, see discussion bottom of ?keys2awk

Jan 31: Martin Cohen shows that Gawk can handle massively long strings (300 million characters).

Jan 31: The AWK FAQ is being updated. For comments/ corrections/ extensions, please mail tim@menzies.us

Jan 31: Martin Cohen finds Awk on the Android platform.

Jan 31: Aleksey Cheusov released a new version of runawk.

Jan 31: Hirofumi Saito contributes a candidate Awk mascot.

Jan 31: Michael Sanders shows how to quickly build an AWK GUI for windows.

Jan 31: Hyung-Hwan Chung offers QSE, an embeddable Awk Interpreter.

[More ...]

Bookmark and Share

categories: Oo,May,2009,Admin

OO tools in AWK

These pages focus on object-oriented tools in Awk.


categories: Awk100,Oo,Dsl,Mar,2009,Jimh

Awk++

Contents

Synopsis

 gawk -f awkpp file-name-of-awk++-program
This command is platform independent and sends the translated program to standard output (stdout). See Running awk++ for variations.

This is an updated revision (#21), released August 1, 2009. In this new version:

  • The code no longer needs a shell script or batch file to launch awkpp
  • Multiple inheritance improved
  • added configuration items at the top of the program
This document may be copied only as part of an awk++ distribution and in unmodified form.

Download

Download awkpp21.zip from LAWKER

Description

Awk++ is a preprocessor, that is it reads in a program written in the awk++ language and outputs a new program. However, it's different than awka. The output from the awk++ preprocessor is awk code, not C or an executable program. So, some version of AWK, such as awk or gawk, has to be used to run the preprocessed program. awka can be used, in a second step, to turn the preprocessed awk++ program into an executable, if desired.

OO in AWK++

The awk++ language provides object oriented programming for AWK that includes:

  • classes
  • class properties (persistent object variables)
  • methods
  • inheritance, including multiple inheritance

Awk++ adds new keywords to standard Awk:

  • class
  • method
  • prop
  • property
  • attr
  • attribute
  • elem
  • element
  • var
  • variable

Syntax

Samples:

 a = class1.new[(optional parameters)] *** similar to Ruby
 b = a.get("aProperty")
 a.delete

 class class1 {
 property aProperty
 method new([optional parameters]) {
 # put initialization stuff here
 }

 method get(propName) {
 if(propName = "aProperty")
 return aProperty ### Note the use of 'return'. It behaves
 ### exactly the same as in an AWK function.
 }
 }

Details

To define a class (similar to C++ but no public/private):

class class_name {.....}

To define a class with inheritance:

class class_name : inherited_class_name [ : inherited_class_name...] {.....}

To add local/private variables (persistent variables; syntax is unique to awk++):

class class_name {
 attribute|attr|property|prop|element|elem|variable|var variable_name
 ..... }

To help programmers who are used to other OO languages, "attribute", "property", "element", and "variable", along with their 4-letter abbreviations, are interchangeable.

Note: these persistent variables cannot be accessed directly. The programmer must define method(s) to return them, if their values are to be made available to code that's outside the class.

To add methods

class class_name {
 attribute variable_name1

 method method_name(parameters) {
 ...any awk code....
 }
 ..other method definitions...
 }

To create an object

 object_variable = class_name.new[(optional parameters)]
(runs the method named "new", if it exists; returns the object ID)

To call an object method

object_variable.method_name(parameters)

The dot isn't used for concatenation in awk/gawk, so it's a natural choice for the separator between the object and method.

To reclaim the memory used by an object, use the delete method, i.e.:

object_variable.delete

but don't define delete() in your classes. awk++ recognizes delete() as a special method and will take care of deleting the object. Deleting objects is only necessary, though, if they hold a lot of data. Overhead for objects themselves is insignificant.

Naming and behavior rules:

  • Class names must obey the same rules as user defined function names.
  • Method names must follow the same rules as AWK user defined function names.
  • Class "local" variables (properties, attributes, etc.) must follow the same
  • naming rules as AWK variables.
  • Objects are number variables, so they must obey number variable rules. However,
  • the values in variables holding objects should never be changed, as they are simply identifiers. Performing math operations on them is meaningless.

Syntax notes

OO syntax goals:

  • easy to parse and match to awk code using an awk program as the "preprocessor"
  • easy to understand
  • easy to remember
  • easy and fast to type
  • distinct from existing AWK syntax

The OO syntax is based partly on C++, partly on Javascript, partly on Ruby and partly on the book "The Object-Oriented Thought Process". It isn't lifted in toto from one langauage because other languages provide features that gawk can't accomplish or have syntax that is hard to parse.

Multiple Inheritance

In awk++, if a method is called that isn't in the object's class and there are inherited classes (superclasses) specified, the inherited classes are called in left to right order until one of them returns a value. That value becomes the result of the method call. This is the way awk++ resolves the diamond problem. As a programmer, you control the sequence in which superclasses are called by the left to right order of the list of inherited classes in the class definition.

There are two important things to note.

  1. The search will proceed up through as many ancestors as it takes to find a matching method.
  2. A "match" is made when a value is returned. If a superclass has a matching
  3. method that returns nothing, the search will continue. Thus, it's possible that more than one method could be executed resulting in unintended consequences. Be careful!

Calls to undefined methods do nothing and return nothing, silently.

Running awk++

The command to preprocess an awk++ program looks like this:

gawk -f awkpp file-name-of-awk++-program
or, if the "she-bang" line (line 1 in awkpp) has the right path to gawk, and awkpp is executable and in a directory in PATH,
awkpp file-name-of-awk++-program
To run the output program immediately,
gawk -f awkpp -r file-name-of-awk++-program [awk options] data-files-to-be-processed
or
awkpp -r file-name-of-awk++-program [awk options] data-files-to-be-processed
When running an awk++ program immediately, standard input (stdin) cannot be used for data. One or more data file paths must be listed on the command line.

Bugs

There is a bug in the standard AWK distributions that affects the preprocessor. Additionally, the preprocessor uses the 3rd array option of the match() function. So, it's best to use GAWK to run the preprocessor.

On the other hand, the AWK code created by translating awk++ is intended to work with all versions of AWK. If you find otherwise, please notify the developer(s).

Copyright

Copyright (c) 2008, 2009 Jim Hart, jhart@mail.avcnet.org All rights reserved. The awk++ code is licensed under the GNU Public license (GPL) any version. awk++ documentation, including this page, may be copied only in unmodified form, subject to fair use guidelines.

Author

Jim Hart, jhart@mail.avcnet.org

categories: Awk100,Oo,Dsl,May,2009,AlexS

Awk + ANSI-C = OO

Description

ooc is an awk program which reads class descriptions and performs the routine coding tasks necessary to do object-oriented coding in ANSI C.

The tool is exceptionally well documented in Object oriented programming with ANSI-C.

Download

Download a 2002 copy of this code from LAWKER.

Or go to the author's web site.

Description

ooc is a technique to do object-oriented programming (classes, methods, dynamic linkage, simple inheritance, polymorphisms, persistent objects, method existence testing, message forwarding, exception handling, etc.) using ANSI-C.

ooc is a preprocessor to simplify the coding task by converting class descriptions and method implementations into ANSI-C as required by the technique. You implement the algorithms inside the methods and the ooc preprocessor produces the boilerplate.

ooc consists of a shell script driving a modular awk script (with provisions for debugging), a set of reports -- code generation templates -- interpreted by the script, and the source of a root class to provide basic functionality. Everything is designed to be changed if desired. There are manual pages, lots of examples, among them a calculator based on curses and X11, and you can ask me about the book.

ooc as a technique requires an ANSI-C system -- classic C would necessitate substantial changes. The preprocessor needs a healthy Bourne-Shell and "new" awk as described in Aho, Weinberger, and Kernighan's book.

ooc was developed primarily to teach about object-oriented programming without having to learn a new language. If you see how it is done in a familiar setting, it is much easier to grasp the concepts and to know what miracles to expect from the technique and what not. Conceivably, the preprocessor can be used for production programming but this was not the original intent. Being able to roll your own object-oriented coding techniques has its possibilities, however...

Technical Details

Most sources should be viewed with tab stops set at 4 characters.

The original system ran on NeXTSTEP 3.2 and older, ESIX (System V) 4.0.4, and Linux 0.99.pl4-49. This rerelease was tested on MacOS X version 10.1.2 and Solaris version 5.8. You need to review paths in the script 'ooc/ooc' before running anything. Make sure the first line of this script points to a Bourne-style shell. Also make sure that the first line of '09/munch' points to a (new) awk.

The rereleased 'ooc' awk-programs have been tested with GNU awk versions 3.0.1 and 3.0.3. Previous versions did not support AWKPATH properly (but this is not essential).

The makefiles could be smarter but they are naive enough for all systems. This is a heterogeneous system -- set the environment variable $OSTYPE to an architecture-specific name. 'make' in the current directory will create everything by calling 'make' in the various subdirectories. Each 'makefile' includes 'make/Makefile.$OSTYPE', review your 'make/Makefile.$OSTYPE' before you start.

The following make calls are supported throughout:

make [all]	create examples
make test	[make and] run examples
make clean	remove all but sources
make depend	make dependencies (if makefile.$OSTYPE supports it)

Make dependencies can be built with the -MM option of the GNU C compiler. They are stored in a file 'depend' in each subdirectory. They should apply to all systems. 'makefile.$OSTYPE' may include a target 'depend' to recreate 'depend' -- check 'makefile.darwin1.4' for an example.

Contents

The following is a walk through the file hierarchy in the order of the book:

makefile
dispatch standard make calls to known directories
make/
Makefile: boilerplate code for makefiles
01/*
chapter 1: abstract data types
  • sets: Set demo
  • bags: Bag demo: Set with reference count
02/*
chapter 2: dynamic linkage
  • strings: String demo
  • atoms: Atom demo: unique String
03/*
chapter 3: manipulating expressions with dyn. linkage
  • postfix: postfix output of expression
  • value: expression evaluation
  • infix: infix output of expression
04/*
chapter 4: inheritance
  • points: Point demo
  • circles: Circle demo: Circle: Point with radius
05/*
chapter 5: symbol table with inheritance
  • value: expression evaluation with vars, consts, functions
06/*
chapter 6: class hierarchy and meta classes
  • any: objects that do not differ from any object
07/*
chapter 7: ooc preprocessor; use ooc -7
  • points: Point demo: PointClass is a new metaclass
  • circles: Circle demo: Circle is a new class
  • queue: Queue demo: List is an abstract base class
  • stack: Stack demo: another subclass of List
08/*
chapter 8: dynamic type checking; use ooc -8
  • circles: Circle demo: nothing changed
  • list: List demo: traps insertion of numbers or strings
09/*
chapter 9: automatic initialization; use ooc -9
  • munch: awk program to collect class list from nm -p output
  • circles: Circle demo: no more init calls
  • list: List demo: no more init calls
10/*
chapter 10: respondsTo method; use ooc -10
  • cmd: Filter demo: how flags and options are handled
  • wc: word count filter
  • sort: sorting filter, adds sort method to List
11/*
chapter 11: class methods
  • value: expression evaluator, based on class hierarchy
  • value: x memory reclamation enabled
12/*
chapter 12: persistent objects
  • value: expression evaluator, with save and load
13/*
chapter 13: exception handling
  • value: expression evaluator with exception handler
  • except: Exception demo
14/*
chapter 14: message forwarding
  • makefile.etc: (naive) generated rules for the library
  • Xapp: resources for X11-based programs
  • hello: LineOut demo: hello, world
  • button: Button demo
  • run: terminal-oriented calculator
  • cbutton: Crt demo: hello, world changes into a
  • crun: curses-based caluclator
  • xhello: XLineOut demo: hello, world
  • xbutton: XButton demo with XawBox and XawForm
  • xrun: X11-based calculator with callbacks
man/*
manual pages
  • *.1: tools
  • *.2: functions
  • *.3: some classes
  • *.4: classes in chapter 14
ooc/*
ooc preprocessor
  • ooc: command script; review 'home' 'OOCPATH' 'AWKPATH'
  • awk/*.awk: modules
  • awk/*.dbg: debugging modules
  • rep/*.rep: reports
  • rep-*/*.rep: reports for early chapters

Copyright

Copyright (c) 1993

While you may use this software package, neither I nor my employers can be made responsible for whatever problems you might cause or encounter.

While you may give away this package and/or software derived with it, you should not charge for it, you should not claim that ooc is your work, and I have published my own book about ooc before you did.

The same restrictions apply to whoever might get this package from you.

Author

Axel T. Schreiner, http://www.cs.rit.edu/~ats/
blog comments powered by Disqus