Subject: ANTLR finally installed
Date: 4 Sep 1999 17:07:37 -0400
From: lorgon@cc.gatech.edu (Brian McNamara)
Organization: College of Computing, Georgia Tech
Newsgroups: git.cc.class.cs4240

I finally got ANTLR installed on the CoC machines; you can check the
newsgroup git.cc.systems for a recent post about the installation.

There is lots of documentation about using ANTLR on the antlr web pages at
   http://www.ANTLR.org/
You'll undoubtedly have to do some reading at some point to get familiar
with ANTLR (hopefully some of you have already).  If you've used
lex/yacc before, I think you'll be at an advantage.

In any case, in this post, I'll walk you through a very short example of
how to run ANTLR.  At the very end of this post is the short short short
version, which undoubtedly some of you hackers who hate to read
instructions have already skipped ahead to.

First off, you'll need to log into one of the CoC Cities machines, which
include

   edmonton fargo houston ithaca juneau kenosha lincoln newark
   ottawa toronto utica

Next make a temporary directory to work in, and copy the files from
~lorgon/Foo (which include a quick ANTLR grammar for the "ac" example

[Note: http://www.cc.gatech.edu/classes/AY2000/cs4240_fall/Foo/ --Brian]

you saw in lecture and Chapter 2, as well as some other files) there:

   fargo:/net/hc282/lorgon> mkdir Temp
   fargo:/net/hc282/lorgon> cd Temp
   fargo:/net/hc282/lorgon/Temp> cp ~lorgon/Foo/* .

You should ensure that "c" and "pf" are executable:

   fargo:/net/hc282/lorgon/Temp> chmod u+x c pf

"pf" is just a little script which will update a couple of your
environment variables for Java and C++ stuff that's needed for ANTLR.
It works for ksh; if you're using another shell, you'll have to make
minor modifications to the syntax.  (The default shell on CoC machines
is "csh".  You can use "chsh" to change your shell if you like.)

   fargo:/net/hc282/lorgon/Temp> . ./pf
   <echoes back happy garbage>

Now that that's set up, we can use ANTLR to generate a Java parser for the
"ac" grammar, based on the Java grammar spec I've created in ac.j.g:

   fargo:/net/hc282/lorgon/Temp> java antlr.Tool ac.j.g
   ANTLR Parser Generator   Version 2.6.0   1989-1999 MageLang Institute
   fargo:/net/hc282/lorgon/Temp> ls
   L.java                  PTokenTypes.txt         foo
   Main.java               ac.c.g                  main.cpp
   P.java                  ac.j.g                  pf*
   PTokenTypes.java        c*

This will create a few new .java files.  Now compile them (as well as
the small Main.java I've put with the files) and run the program on some
short "ac" programs:

   fargo:/net/hc282/lorgon/Temp> javac *java
   fargo:/net/hc282/lorgon/Temp> echo "ip" | java Main
   Error: line(1), expecting ID, found 'p'
   fargo:/net/hc282/lorgon/Temp> echo "ij  j=j+6  pj" | java Main
   fargo:/net/hc282/lorgon/Temp>

All the program should do is (silently) verify that the input is a
legal "ac" program, or print an error message about why it's not.  (In
the first example, 'p' is not a valid idenitifer--'p' is the "print"
command, as you may recall.)  No "dc" code is generated, as I only
created a recognizer, not a translator, as you could see from closer
isnpection of the grammar files (the ones ending in .g).  A more
complete example that actually "does something" (rather than just parse
the code) will be available in the near future (that is, when I sit
down and write it).

If you prefer C++ to Java, then you will want to generate the C++
version of the parser.  The grammar file is nearly identical, as you
can see by inspection.  Again, first run ANTLR to generate some .cpp
files, then compile them (I've a script called "c" for this), and
finally run the executable:

   fargo:/net/hc282/lorgon/Temp> java antlr.Tool ac.c.g
   ANTLR Parser Generator   Version 2.6.0   1989-1999 MageLang Institute
   fargo:/net/hc282/lorgon/Temp> ./c
   fargo:/net/hc282/lorgon/Temp> echo "ip" | ./a.out
   Error: line(1), expecting ID, found 'p'
   fargo:/net/hc282/lorgon/Temp> echo "ij  j=j+6  pj" | ./a.out
   fargo:/net/hc282/lorgon/Temp>

The behavior should be the same as the Java version (only perhaps faster).

So that's it.  I encourage you to try this out, and to spend some time
inspecting the code for this example as well as the ANTLR documentation.
If you have questions, mail me or ask on the newsgroup.  One of these
days I'll finally post some office hours, too.

Now, as promised, the "short short short version" to quick-start ANTLR:

   Everyone should do something like

      fargo:/net/hc282/lorgon> mkdir Temp
      fargo:/net/hc282/lorgon> cd Temp
      fargo:/net/hc282/lorgon/Temp> cp ~lorgon/Foo/* .
      fargo:/net/hc282/lorgon/Temp> chmod u+x c pf
      fargo:/net/hc282/lorgon/Temp> . ./pf

   If you want a Java parser, do

      fargo:/net/hc282/lorgon/Temp> java antlr.Tool ac.j.g
      fargo:/net/hc282/lorgon/Temp> javac *java
      fargo:/net/hc282/lorgon/Temp> echo "ip" | java Main

   Or do

      fargo:/net/hc282/lorgon/Temp> java antlr.Tool ac.c.g
      fargo:/net/hc282/lorgon/Temp> ./c
      fargo:/net/hc282/lorgon/Temp> echo "ip" | ./a.out

   for a C++ parser.

--
-Brian McNamara (lorgon@cc.gatech.edu), your friendly CS 4240 TA
