Skip to main content

Prettyprinter

Synopsis

Transform an Abstract Syntax Tree into a formatted string.

Description

A pretty printer formats the source code of programs. Alternative names are formatter or beautifier. Pretty printers differ in the inputs they accept:

  • The source text itself.
  • A ./parse tree that corresponds to the source text. This variant is also called unparser.
  • An Abstract Syntax Tree that corresponds to the source text.

Pretty printers also differ in flexibility. They differ in:

  • The source language(s) they can accept.
  • The adaptability of the formatting rules.

Examples

The program fragment

if(x > 10) { System.err.println("x > 10"); } else { System.err.println("x <= 10"); }

can be pretty printed in many different ways. Here are two variants examples:

if(x > 10) { 
System.err.println("x > 10");
} else {
System.err.println("x <= 10");
}
if( x > 10 )
{
System.err.println("x > 10");
} else
{
System.err.println("x <= 10");
}