1. Language Syntax

PostScript’s syntax is unique among programming languages, combining stack-based execution with a simple, token-based grammar.

2. Overview

PostScript programs consist of sequences of tokens separated by whitespace. The interpreter reads tokens, executes operators, and manipulates the operand stack.

3. Fundamental Elements

3.1. Tokens

The basic lexical elements of PostScript:

  • Numbers: 42, 3.14159, 1.5E-3

  • Names: /name, name (literal vs executable)

  • Strings: (text), <68656C6C6F> (hex)

  • Arrays: [ 1 2 3 ]

  • Procedures: { code }

  • Comments: % comment

3.2. Objects

Everything in PostScript is an object with:

  • Type: integer, real, boolean, string, name, array, dictionary, operator, etc.

  • Value: The object’s data

  • Attributes: Literal/executable, access permissions

3.3. Operators

Built-in operations that manipulate the stack:

3 4 add    % Arithmetic operator
dup        % Stack operator
moveto     % Graphics operator

4. Data Types

4.1. Simple Types

Numbers

42         % Integer
3.14159    % Real
1.5E-3     % Scientific notation
16#FF      % Hexadecimal (255)

Booleans

true false

Null

null       % Placeholder value

4.2. Composite Types

Arrays

[ 1 2 3 ]              % Literal array
[ /add /mul /div ]     % Array of names

Strings

(Hello, World!)        % ASCII string
<48656C6C6F>          % Hex string (Hello)

Dictionaries

5 dict begin
  /key1 value1 def
  /key2 value2 def
end

Procedures

{ 1 2 add }           % Executable array
/proc { code } def    % Named procedure

5. Syntax Rules

5.1. Whitespace

Whitespace (space, tab, newline, formfeed) separates tokens but is otherwise ignored:

1 2 add    % Same as
1    2
   add

5.2. Comments

% begins a comment that extends to end of line:

% This is a comment
1 2 add  % Inline comment

5.3. Names

Names can be literal or executable:

/name      % Literal name (starts with /)
name       % Executable name (looked up)

Valid name characters: letters, digits, and most punctuation except ()[]{}/<>%

5.4. Strings

ASCII Strings: (…​)

(Hello)
(Line 1\nLine 2)      % \n = newline
(Nested \(parens\))   % Escape parens

Hex Strings: <…​>

<48656C6C6F>          % "Hello"
<48 65 6C 6C 6F>      % Spaces ignored

5.5. Arrays

Literal Arrays: [ …​ ]

[ 1 2 3 ]
[ /name (string) 42 ]
[ nested [ arrays ] ]

Procedures: { …​ }

{ 1 2 add }           % Executable array
/{ code } cvlit       % Convert to literal

5.6. Special Characters

Character Meaning

%

Comment to end of line

( )

String delimiters

[ ]

Array construction

{ }

Procedure (executable array)

/

Literal name prefix

< >

Hex string delimiters

<< >>

Dictionary construction (Level 2)

6. Program Structure

6.1. Typical PostScript File

%!PS-Adobe-3.0
%%Title: My Document
%%Creator: Author Name
%%Pages: 1
%%EndComments

% Procedure definitions
/myproc { implementation } def

% Main content
100 100 moveto
(Text) show

%%Trailer
showpage
%%EOF

6.2. Document Structuring Conventions (DSC)

Comments beginning with %% provide metadata:

  • %!PS-Adobe-3.0 - Version identifier

  • %%Title: - Document title

  • %%Pages: - Page count

  • %%EndComments - End of header

  • %%EOF - End of file

7. Execution Model

7.1. Stack-Based Evaluation

% Execution trace:
10         % Stack: 10
20         % Stack: 10 20
add        % Stack: 30
2          % Stack: 30 2
div        % Stack: 15

7.2. Name Lookup

The interpreter searches the dictionary stack:

/x 42 def        % Define x in current dict
x                % Look up x, push value 42
x 2 mul          % Use x: 84

7.3. Procedure Execution

Procedures execute their elements sequentially:

{ 1 2 add 3 mul }  % Procedure object
exec               % Execute: pushes 9

8. See Also


Table of contents


Back to top

Copyright © 2025 Ribose. PostScript is a trademark of Adobe. Distributed under the MIT License.