Jawk

Implementation of programming language AWK

Jawk is an AWK implementation written in Java. It includes interpreter and compiler into JVM bytecode (Jawk Compiler Module). To run the interpreter, one has to download the .jar file and to run the command java -jar jawk.jar {command-line-arguments}.

Jawk implements all AWK features and adds some new ones:

  • Conversion of the script to a post-compiled, pre-interpreted format for both elimination of the compilation step and obfuscation of Jawk scripts.
  • Text dumps of the abstract syntax tree and intermediate code representation (tuples).
  • Maintenance of associative arrays in key-sorted order.
  • Error detection for printf/sprintf format parameters (via the -r argument).
  • Compilation of scripts to bytecode(executable) on any modern JVM.
  • An opt-in, flexible extension facility with event blocking capabilities (in interpreted mode, only).

There are also a couple of differences due to usage of Java, mainly in regular expressions and printf/sprintf.

Examples:

Hello, World!:

Example for versions Jawk 1.02, gawk 3.1.6, mawk 1.3.3

The printing is done with BEGIN pattern, i.e., before processing the input.

BEGIN { print "Hello, World!" }

Factorial:

Example for versions Jawk 1.02, gawk 3.1.6, mawk 1.3.3

This example uses iterative factorial definition. Individual statements within code block can be separated with semicolons (;) or new lines.

BEGIN {
    f = 1
    print "0! = " f
    for (i=1; i<17; i++) {
        f *= i
        print i "! = " f
    }
}

Fibonacci numbers:

Example for versions Jawk 1.02, gawk 3.1.6, mawk 1.3.3

This example uses iterative definition of Fibonacci numbers. fib is an associative array, and pr is a string.

BEGIN {
    fib[1] = 1
    fib[2] = 1
    for (i=3; i<17; i++)
        fib[i] = fib[i-1]+fib[i-2]
    pr = ""
    for (i=1; i<17; i++)
        pr = pr fib[i] ", "
    print pr "..." 
}

Quadratic equation:

Example for versions Jawk 1.02, gawk 3.1.6, mawk 1.3.3
{   A = $1
    B = $2
    C = $3
    if (A == 0) 
        print "Not a quadratic equation"
    else
    {   D = B*B-4*A*C
        if (D == 0)
            print "x = " (-B/2/A)
        else if (D > 0)
        {   print "x1 = " ((-B+sqrt(D))/2/A)
            print "x2 = " ((-B-sqrt(D))/2/A)
        }
        else
        {   print "x1 = (" (-B/2/A) "," (sqrt(-D)/2/A) ")"
            print "x2 = (" (-B/2/A) "," (-sqrt(-D)/2/A) ")"
        }
    }
}

CamelCase:

Example for versions Jawk 1.02, gawk 3.1.6, mawk 1.3.3

mawk provides no function length to get the size of the array, neither it can be used in Jawk — an attempt results in “Cannot evaluate an unindexed array.” runtime error.

Instead we can use the fact that function split returns the number of string fragments it extracted from the string. Otherwise this example is identical to this one.

{   text = $0;
    N = split(text, words, /[^a-zA-Z]+/);
    for (i=1; i<=N; i++) {
        res = res toupper(substr(words[i],1,1)) tolower(substr(words[i],2));
    }
    print res
}