Pretty Printing AST
Consider a pretty printer that:
- parses input source code
- builds abstract syntax tree
- prints abstract syntax tree, perhaps with improved formatting
- generate again source code
- create javadoc
- create input for latex that produces nice listing
Simple pretty printer is in the apply method of Tree.scala
- in Scala, f(x) is a shorthand for f.apply(x)
Operations:
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
We can use this to test a parser and pretty printer
Possible improvements to simple pretty printer
- emit fewer parantheses
- retain comments and some formatting
- perform semantic checks - a diagnostic tool