Building a Pretty Printer

A pretty printer:

  • parses input source
  • prints it, perhaps with improved formatting
    • e.g. HTML listing, documentation, latex

Simple pretty printer is in tree.scala


lex   : List[Char] -> List[Token] 
parse : List[Token] -> Tree
print : Tree -> List[Char]

What should we expect to hold:

print(parse(lex(charList)) == charList
parse(lex(print(tree))) == tree

Use of pretty printer to test a parser

Possible improvements to simple pretty printer

  • emit fewer parantheses
  • retain comments and some formatting
  • perform semantic checks - a diagnostic tool