JastAdd
This is our old website.
Broken links can occur.
A new website is under construction.

HelloWorld on JavaCC

This is a "Hello world" example of a JastAdd project that uses JavaCC as its underlying parser generator. An extremely small language "Hello" is defined. Each program in the language consists of two identifiers. An extremely simple compiler called SongGenerator generates a very short song from the two identifiers. For example, from the program "goodbye hello", the following song is generated: "You say goodbye and I say hello". From the program "yes no", the following song is generated: "You say yes and I say no".

You can try out this project either from a terminal window or from Eclipse.

To create your own project, it is useful to take a copy of this project and rename it. The name of the language ("Hello") is used in a few places. The following commands are useful to find out which files you need to edit:

grep -i Hello * */*
ls * | grep -i Hello

Contents of this directory

  • HelloJastAddOnJavaCC.zip for downloading the example to your computer
  • tools: directory containing all the tools you need to run this JastAdd project
    • JavaCC.zip: contains the parser generator javacc and its tree building tool jjtree
    • jastadd2.jar contains the jastadd2 system
  • Hello.ast: abstract grammar for the Hello language (input to jastadd).
  • Hello.jjt: parsing and AST-building grammar for the Hello language (input to jjtree). A larger example explaining how jjtree interacts with JastAdd is available here
  • SongGeneration.jadd: A simple jastadd aspect defining how to generate songs from Hello programs
  • build.xml: Ant build file to be used from a terminal window and from Eclipse
    • Use the targets build (default), test, and clean, when working from a terminal window. For example, type
      ant

      generate all java files and compile them

      ant test

      run all test cases

      ant clean

      remove all generated files and all the class files for both generated and handwritten java files

    • Use the targets gen and cleanGen when working from eclipse (see Running JastAdd Under Eclipse):
      ant gen

      generate all java files

      ant cleanGen

      remove all generated files and their class files.

  • AST: package containing files generated by the tools (JavaCC and JastAdd). This directory is not present when the project is "clean". The directory will contain parser files generated by JavaCC and AST files generated by JastAdd.
  • programs: package containing main programs that parse and do various things with Hello programs.
    • Parser.java: Used as a superclass for the programs. Contains utility methods for parsing a Hello program, either supplied in a file or on System.in. Produces an AST for that program.
    • SongGenerator.java: An example program that generates a song from a Hello program according to the definition in SongGeneration.jadd. To try it out, type
      java programs.SongGenerator

      and type in, for example, the following Hello program

      goodbye hello
    • Dumper.java: A program that "dumps" the AST as indented text on standard output. To try it out, type
      java programs.Dumper

      and type in an example program. This program is useful in order to take a look at the AST built for a program.

  • tests: package containing JUnit test cases and related input and output files
    • SongTests.java: test cases for generating songs. Tests the functionality added in the jadd file SongGeneration.jadd.
    • DumpTests.java: test cases for parsing in source files like allsyntax.hello and "dumping" the AST on a text format where indentation shows the tree structure, like in the file allsyntax.res
    • allsyntax.hello: a test program in the Hello language that includes all syntax of the language.
    • allsyntax.res: the resulting AST "dump" for the allsyntax.hello program
  • testframework: package containing classes useful for testing
    • TestCaseParse.java: Can parse a String into an AST. Useful for testing computations on ASTs.
    • TestCaseOutput.java: Can parse input files and compare the result of an output computation with an existing output file. Useful when doing file-based tests.
    • TestAll.java: Used for finding all testcases when running tests from a terminal window.
  • release.txt Release and version information about the example