Req, Spec, Design and Proof of a Pretty Printer for Pascal
1 Requirements
- The design and construction of pretty printers is a solved problem
for decades.
- indent for C, C++ (
apt-get install indent
)
- Builtin pretty printers in Eclipse, Intellij, et al.
- Stand alone pretty printer, PPP.
- fout = PPP(fin), fin and fout are text files.
- The fout is very useful particularly when fin syntactically
invalid.
- The semantics of the source code in fout and fin must be
equivalent.
- Speed: Comparable to
cat
. Not discussed further.
- Example of a pretty printed listing: ./ppp-Spec-166.pdf
2 A Specification Schema For Indenting Programs
- Abstract
- "A two level specification of the functional behaviour of a
class of indenting programs for Pascal is presented."
- The high level specification is given as a stylized Pascal grammar,
p168 and p169 in Extended BNF.
- The low level specifications define how syntactically invalid text
is dealt with.
- These specs require several mathematical functions.
- Most indentation styles appearing in the literature can be
specified with precision using methods developed in this paper.
2.1 Lexical Equivalence
- lex(fin) == lex(fout), assuming a proper def of lexical analysis.
- The def of lex must not ignore comments.
2.2 Spec of a Lexical Analyzer
- Delimiters
- Set of reserved words
- Constants: integers, real numbers, characters, strings
- Definition of LEX
- See Ref#1
2.3 Spec of a Lex Including Comments
- Pascal Comments: {} and (* *)
- Requirements Analysis: Should we "mess" with comments in pretty
printing"?
- See Ref#1
2.4 Selected Pieces of Specs
- Def of Text File p165 bottom
- The high level specification is given as a stylized Pascal grammar,
p168 and p169 in Extended BNF.
- Predicate Properly-Laid-Out p170.
- Def of Tokens p172 and p173.
- Reduced Toekn Sequences p174.
- Segments p176.
- Left Margin of a Segment p177.
3 A Design of PPP
- The transformation that these programs perform on the input text is
a composition of
- splitting input lines,
- altering the blank space between tokens and
- computing the margin required in front of each of the split
lines.
- Four modules: io, lex, stack and indent. See Fig 1
./ppp-Spec-Schema-1983.pdf p203.
- Lexical Analyzer: lex
- Pretty Printer: indent
- See Ref#2 and Ref#3. From these you should be able to extract the
design in our VHLL. Exercise!
4 An Implementation of PPP
- The program is approximately 330 lines long and consists of four
modules: io, lex, stack and indent.
- The local copy ./ppp-Correctness-1983.pdf includes source
code listing and assertions.
- Exercise: Implement the standard indent (
apt-get install indent
)
of Linux for C source code based on the design above using C as the
implementation language. Minor deviations are permitted.
5 A Proof of PPP
5.1 Overview
- http://onlinelibrary.wiley.com/doi/10.1002/spe.4380130302/abstract
The correctness of an indenting program for Pascal is proved at an
intermediate level of rigour. The specifications of the program are
given in the companion paper.
- Correctness Proof using entry-/exit-assertions. See Ref#2 for
a local copy ./ppp-Correctness-1983.pdf.
- We prove first that the individual procedures contained in the four
modules meet their specifications as given by the entry and exit
assertions. A global proof of the main routine then establishes
that the interaction between modules is such that the main routine
meets the specification of the entire program.
- We argue that correctness proofs at the level of rigour used here
serve very well to transfer one's understanding of a program to
others. We believe proofs at this level should become commonplace
before more formal proofs can take over to reduce traditional
testing to an inconsequential place.
5.2 Selected Assertions
- Fig 2. The Line buffer. ./ppp-Correctness-1983.pdf p205.
- Lines 40..43 p207.
- Class Invariant of Lex, lexINV(uu) , p209.
- Entry- Exit assertions of function nexttoken, p213.
- Loop invariant in indent, p220.
6 References
- Prabhaker Mateti, "A Specification Schema for Indenting Programs],"
Software – Practice and Experience, Vol.13, pp. 163–179, 1983.
(Reprinted in {\it Software Specification Techniques}, McGettrick
and Gehani (Eds.), Addison-Wesley 1986, 269–288, ISBN:
0-201-14230-9.) Links: ./Pretty-Printer-links.html Local copy of
the paper: ./ppp-Spec-Schema-1983.pdf
Required Reading.
- Prabhaker Mateti and Joxan Jaffar, "A Correctness Proof of an
Indenting Program", Software: Practice and Experience, Vol. 13,
pp 199-226, 1983. Local copy ./ppp-Correctness-1983.pdf
Recommended Reading.
- Prabhaker Mateti, "Documenting Program indent: An Example," 1984.
Local copy of Tech Report ./ppp-doc.html. Required Reading.