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
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
6. Program Structure
7. Execution Model
8. See Also
-
Command Reference - All operators
-
PostScript Levels - Version features
-
Basic Usage - Getting started
-
Examples - Code samples