Prettyprint

Prettyprint

Prettyprint (or pretty-print) is the application of any of various stylistic formatting conventions to text, source code, markup, and other similar kinds of content. These formatting conventions usually consist of changes in positioning, spacing, color, contrast, size and similar modifications intended to make the content easier for people to view, read, and understand. Prettyprinters for programming language source code are sometimes called code beautifiers or syntax highlighters.

Contents

Pretty-printing mathematics

A typeset mathematical expression

Pretty-printing usually refers to displaying mathematical expressions in a way that is similar to the way they are typeset professionally. For example, in computer algebra systems such as Maxima or Mathematica the system may write output like "x ^ 2 + 3 * x" as "x2 + 3x". Some graphing calculators, such as the Casio 9860 series, HP-49 series, TI-89, and TI-Nspire, or the TI-83 Plus/TI-84 Plus with the PrettyPt add-on, can perform pretty-printing. Additionally, a number of newer scientific calculators are equipped with dot matrix screens capable of pretty-printing such as the Casio FX-ES series (Natural Display), Sharp EL-W series (WriteView), HP SmartCalc 300s, and TI-30XB.

Many text formatting programs can also typeset mathematics: TeX was developed specifically for high-quality mathematical typesetting.

Code formatting and beautification

Programmers often use tools to format programming language source code in a particular manner. Proper code formatting makes it easier to read and understand. Moreover, often different programmers have different preferred styles of formatting, such as the use of code indentation and whitespace or positioning of braces. A code formatter converts source code from one format style to another. This is relatively straightforward because of the unambiguous syntax of programming languages. Code beautification involves parsing the source code into component structures, such as assignment statements, if blocks, loops, etc. (see also control flow), and formatting them in a manner specified by the user in a configuration file.

There exist both standalone code beautifiers and ones built into integrated development environments and text editors. For example, Emacs' various language modes can correctly indent blocks of code attractively.[1]

Early Example

An early example of pretty-printing was Bill Gosper's "GRIND" program, which used combinatorial search with pruning to format LISP programs. The term "grind" was used in some Lisp circles as a synonym for pretty-printing.[2]

Project style rules

Many open source projects have established rules for code layout. The most typical are the GNU style and the BSD style. The biggest difference between the two is the location of the braces: in the GNU style, opening and closing braces are on lines by themselves, with the same indent. BSD style places an opening brace at the end of the preceding line, and the closing braces can be followed by else. The size of indent and location of white space also differs.

Example of formatting and beautifying code

The following example shows some typical C structures and how various indentation style rules format them. Without any formatting at all, it looks like this:

int foo(int k) { if (k < 1 || k > 2) { printf("out of range\n");
printf("this function requires a value of 1 or 2\n"); } else {
printf("Switching\n"); switch (k) { case 1: printf("1\n"); break; case
2: printf("2\n"); break; } } }

The GNU indent program produces the following output when asked to indent according to the GNU rules:

int
foo (int k)
{
  if (k < 1 || k > 2)
    {
      printf ("out of range\n");
      printf ("this function requires a value of 1 or 2\n");
    }
  else
    {
      printf ("Switching\n");
      switch (k)
        {
        case 1:
          printf ("1\n");
          break;
        case 2:
          printf ("2\n");
          break;
        }
    }
}

It produces this output when formatting according to BSD rules:

int
foo(int k)
{
        if (k < 1 || k > 2) {
                printf("out of range\n");
                printf("this function requires a value of 1 or 2\n");
        } else {
                printf("Switching\n");
                switch (k) {
                case 1:
                        printf("1\n");
                        break;
                case 2:
                        printf("2\n");
                        break;
                }
        }
}

Text formatting

Text formatting can be considered a generalized form of pretty-printing.

Notes

  1. ^ Stallman, Richard M.. "Indentation for Programs". GNU Emacs Manual. Free Software Foundation. https://www.gnu.org/software/emacs/manual/html_node/emacs/Program-Indent.html#Program-Indent. Retrieved 20 October 2011. 
  2. ^ Jargon File, s.v. grind

See also

External links


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать курсовую

Look at other dictionaries:

  • PrettyPrint — Darstellung von PrettyPrint auf einem TI nspire CAS Taschenrechner. PrettyPrint auf einem Taschenrechner vom Typ TI 89 PrettyP …   Deutsch Wikipedia

  • High-availability cluster — High availability clusters (also known as HA clusters or failover clusters) are groups of computers that support server applications that can be reliably utilized with a minimum of down time. They operate by harnessing redundant computers in… …   Wikipedia

  • XML Pretty Printer — XML Pretty Printers are a type of Prettyprint or code beautifier that specifically improve the readability of XML. XML as a standard is designed to be human readable, but is sometimes generated by a computer as tightly compressed or compacted,… …   Wikipedia

  • Io (Programmiersprache) — Io ist eine kompakte Prototypen basierte, objektorientierte Programmiersprache. Der Erfinder und Hauptentwickler, der Amerikaner Steve Dekorte, legt bei der Entwicklung großen Wert auf Minimalismus, sowohl was die Codegröße als auch die… …   Deutsch Wikipedia

  • BASIC09 — is a structured BASIC programming language dialect developed by Microware for the then new Motorola 6809 CPU. Somewhat in the fashion of UCSD Pascal it was implemented via compilation into an intermediate representation. It was paired with the OS …   Wikipedia

  • XPath 1.0 — XPath (XML Path Language) is a language for selecting nodes from an XML document. In addition, XPath may be used to compute values (strings, numbers, or boolean values) from the content of an XML document. The current version of the language is… …   Wikipedia

  • Standard Template Library — C++ Standard Library fstream iomanip ios iostream sstream string …   Wikipedia

  • Beta BASIC — was a BASIC interpreter for the Sinclair Research ZX Spectrum microcomputer, written by Dr Andy Wright and sold by his one man software house BetaSoft. BetaSoft also produced a regular newsletter/magazine, BetaNews, which was one of the main fora …   Wikipedia

  • Chord (software) — Chord Developer(s) Martin Leclerc, Mario Dorion Stable release 3.5 / 1994 April Operating system Cross platform Type staffless lead sheet typesetting …   Wikipedia

  • TI-89 — Ein TI 89 der zweiten Generation Ein TI 89 der zweiten Generation mit anderer Farbgebun …   Deutsch Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”