[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

SableCC Question



For the benefit of all readers;-)

Étienne
-------- Original Message --------
From: "Etienne M. Gagnon" <egagnon@j-meg.com>
To: Othman Alaoui 
Othman Alaoui wrote:
> 
> Hi,
> 
> I have made a transformation on my AST to make lists more abstract, and have
> saved toString()ed versions of the original subtrees in the internal
> hashtable of my transformer analysis class, for the convenience of our
> pretty printer.

OK.  For one thing, you should NEVER directly modify any SableCC
generated class.  Imagine that a new version comes out that lays out
private fields differently...

This being said, you may create new classes.

> Now my problem is that up to now we have just been using the built-in pretty
> printer obtained by start.toString(). And unfortunately this does not seem
> to be implemented as a subclassable analysis. So does it mean that I would
> have to rewrite a DepthFirstAdapter for custom pretty printing? That's a
> scary thought as your code for that class is more than 3000 lines of code!!!

The toString() has simply been implemented for convenience.  For
example, when you use the Jikes debugger, you may select an object, and
the "toString" method will automatically be called on it.  In the AST,
the result is pretty informative with SableCC's toString
implementation.  If, on the other hand, SableCC didn't provide
"toString", a node representation would be some meaningless
"object@1234".

The implementation of toString IS NOT an example to follow, for
implementing a real pretty printer.  You should simply write a new class
that "extends" DepthFirstAdapter, and that overwrites only a few
methods.  This is feasible.  FOr example, overriding a single method
allows you to mimic "toString".  What is it?

Trick:

class PrettyPrint extends D.F.A.
{
   StringBuffer buff;

  public static String pp(... ast)
  {
    ... p =  new Pretty...()
    ast.apply(p);
    return p.buff.toString();
  }
  
  public void GUESS???(...)
  {
    ... t = (Token) ...;
    buff.append(t.getText() + " ");
  }
}

...
PrettyPrint.pp(ast);
...

Etienne
-- 
----------------------------------------------------------------------
Etienne M. Gagnon, M.Sc.                     e-mail: egagnon@j-meg.com
Author of SableCC:                 http://www.sable.mcgill.ca/sablecc/
----------------------------------------------------------------------