UP | HOME
../../

Req, Spec, Design and Proof of a Pretty Printer for Pascal

Table of Contents

1 Requirements

  1. The design and construction of pretty printers is a solved problem for decades.
    1. indent for C, C++ (apt-get install indent)
    2. Builtin pretty printers in Eclipse, Intellij, et al.
  2. Stand alone pretty printer, PPP.
  3. fout = PPP(fin), fin and fout are text files.
  4. The fout is very useful particularly when fin syntactically invalid.
  5. The semantics of the source code in fout and fin must be equivalent.
  6. Speed: Comparable to cat. Not discussed further.
  7. Example of a pretty printed listing: ./ppp-Spec-166.pdf

2 A Specification Schema For Indenting Programs

  1. Abstract
  2. "A two level specification of the functional behaviour of a class of indenting programs for Pascal is presented."
  3. The high level specification is given as a stylized Pascal grammar, p168 and p169 in Extended BNF.
  4. The low level specifications define how syntactically invalid text is dealt with.
  5. These specs require several mathematical functions.
  6. Most indentation styles appearing in the literature can be specified with precision using methods developed in this paper.

2.1 Lexical Equivalence

  1. lex(fin) == lex(fout), assuming a proper def of lexical analysis.
  2. The def of lex must not ignore comments.

2.2 Spec of a Lexical Analyzer

  1. Delimiters
  2. Set of reserved words
  3. Constants: integers, real numbers, characters, strings
  4. Definition of LEX
  5. See Ref#1

2.3 Spec of a Lex Including Comments

  1. Pascal Comments: {} and (* *)
  2. Requirements Analysis: Should we "mess" with comments in pretty printing"?
  3. See Ref#1

2.4 Selected Pieces of Specs

  1. Def of Text File p165 bottom
  2. The high level specification is given as a stylized Pascal grammar, p168 and p169 in Extended BNF.
  3. Predicate Properly-Laid-Out p170.
  4. Def of Tokens p172 and p173.
  5. Reduced Toekn Sequences p174.
  6. Segments p176.
  7. Left Margin of a Segment p177.

3 A Design of PPP

  1. The transformation that these programs perform on the input text is a composition of
    1. splitting input lines,
    2. altering the blank space between tokens and
    3. computing the margin required in front of each of the split lines.
  2. Four modules: io, lex, stack and indent. See Fig 1 ./ppp-Spec-Schema-1983.pdf p203.
  3. Lexical Analyzer: lex
  4. Pretty Printer: indent
  5. 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

  1. The program is approximately 330 lines long and consists of four modules: io, lex, stack and indent.
  2. The local copy ./ppp-Correctness-1983.pdf includes source code listing and assertions.
  3. 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

  1. 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.
  2. Correctness Proof using entry-/exit-assertions. See Ref#2 for a local copy ./ppp-Correctness-1983.pdf.
  3. 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.
  4. 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

  1. Fig 2. The Line buffer. ./ppp-Correctness-1983.pdf p205.
  2. Lines 40..43 p207.
  3. Class Invariant of Lex, lexINV(uu) , p209.
  4. Entry- Exit assertions of function nexttoken, p213.
  5. Loop invariant in indent, p220.

6 References

  1. 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.
  2. 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.
  3. Prabhaker Mateti, "Documenting Program indent: An Example," 1984. Local copy of Tech Report ./ppp-doc.html. Required Reading.

Copyright © 2016 pmateti@wright.edu www.wright.edu/~pmateti • 2016-06-20