SNOBOL4
1967fragletMCP + fragletc
SNOBOL4 (StriNg Oriented and symBOlic Language) is a high-level programming language developed at Bell Labs in the 1960s by David J. Farber, Ralph E. Griswold, and Ivan P. Polonsky. It is particularly renowned for its powerful string processing and pattern matching capabilities.
Language Features
- Pattern Matching: SNOBOL4's most distinctive feature is its sophisticated pattern matching system, which is more powerful than regular expressions and can handle context-sensitive patterns
- Dynamic Typing: Variables can hold any type of data and types are determined at runtime
- String Processing: Exceptionally strong string manipulation capabilities with built-in functions for searching, replacing, and analyzing text
- Goto-based Control Flow: Uses labels and goto statements for program flow control, reflecting its 1960s origins
- Associative Arrays: Tables that can use any data type as indices
Hello World Program Structure
* SNOBOL4 Hello World - Demonstrating Language Features
GREETING = "Hello"
TARGET = "World"
PUNCTUATION = "!"
* Use pattern matching to build message
MESSAGE = GREETING " " TARGET PUNCTUATION
* Demonstrate pattern replacement
MESSAGE "World" = "wonderful World"
MESSAGE "wonderful " = ""
* Show conditional pattern matching with success/failure
MESSAGE "Hello" = "Hello" :S(FOUND) F(NOTFOUND)
FOUND OUTPUT = MESSAGE
:(END)
NOTFOUND OUTPUT = "Pattern not found"
ENDThe program demonstrates several key SNOBOL4 features:
- Comments: Lines starting with
*in column 1 are comments - String Concatenation: Adjacent strings are automatically concatenated (
GREETING " " TARGET) - Pattern Replacement:
MESSAGE "World" = "wonderful World"finds "World" and replaces it - Success/Failure Branching:
:S(FOUND) F(NOTFOUND)branches based on pattern match success - Labels and Goto:
FOUNDandNOTFOUNDare labels for program flow control - Unconditional Goto:
:(END)jumps to the END label
Historical Significance
SNOBOL4 was groundbreaking for its time and influenced many later languages:
- Pioneered advanced string processing concepts
- Introduced powerful pattern matching that inspired features in languages like Perl and Python
- Demonstrated the viability of very high-level programming languages
- Influenced the development of regular expressions in Unix tools
Pattern Matching Power
SNOBOL4's pattern matching goes far beyond regular expressions. It can match nested structures, perform arithmetic during matching, and even modify patterns dynamically during execution. This makes it particularly suitable for parsing complex text formats, language processing, and symbolic computation.
Modern Implementations
This container uses CSNOBOL4, a modern C implementation that maintains compatibility with the original Bell Labs SNOBOL4 while adding portability to modern operating systems.
Hello World
#!/usr/bin/env sh
# If this file is present, this is the file that runs when you add the
# RUN=1 option.
#
# Otherwise, the default behavior is to run the first file in the
# directory that matches the pattern `hello-world.*``.
# Build it (no compilation needed for SNOBOL4)
# Run it
cd /hello-world
snobol4 hello-world.sno "$@"Coding Guide
Language Version
CSNOBOL4 2.3.3
Execution Model
- Interpreted language, runs via
snobol4command - Code executes sequentially from top to bottom
- Pattern matching and string manipulation are core features
- Execution happens through a wrapper script that invokes the snobol4 interpreter
Key Characteristics
- Pattern-matching oriented
- String manipulation is fundamental
- Labels are used for control flow (goto-like behavior)
- Case-sensitive
- Whitespace is significant in some contexts
Fragment Authoring
Write valid SNOBOL4 statements. Your fragment becomes the script body. Code runs sequentially.
Key SNOBOL4 Concepts
- Assignment:
VARIABLE = "value" - Pattern matching:
STRING PATTERN = REPLACEMENT - Labels:
LABEL OUTPUT = "message" - Success/Failure:
:S(LABEL)(on success),:F(LABEL)(on failure) - Unconditional goto:
:(LABEL) - Output:
OUTPUT = "message" - Concatenation:
RESULT = STR1 " " STR2
Common Patterns
- Output:
OUTPUT = "message" - Assignment:
VAR = "value" - Pattern matching:
STRING "pattern" = "replacement" - Conditional:
CONDITION :S(SUCCESS) F(FAILURE) - String concatenation:
RESULT = STR1 " " STR2 - Pattern replacement:
STRING "old" = "new"
Examples
* Simple output
OUTPUT = "Hello, World!"
* Variable assignment and concatenation
GREETING = "Hello"
TARGET = "World"
MESSAGE = GREETING " " TARGET "!"
OUTPUT = MESSAGE
* Pattern matching and replacement
TEXT = "Hello World"
TEXT "World" = "Universe"
OUTPUT = TEXT
* Conditional pattern matching
TEXT = "Hello World"
TEXT "Hello" = "Hi" :S(SUCCESS) F(FAILURE)
SUCCESS OUTPUT = TEXT
:(END)
FAILURE OUTPUT = "Pattern not found"
ENDCaveats
- Fragments must be valid SNOBOL4 that executes without errors
- Labels must be unique within the program
- Pattern matching uses SNOBOL4's pattern syntax (not regex)
- Remember that SNOBOL4 is a pattern-matching language - many operations are based on pattern matching
- Variables are global by default
- Make fragments idempotent—repeated runs should succeed without manual cleanup
Fraglet Scripts
Echo Args
#!/usr/bin/env -S fragletc --vein=snobol4
RESULT = "Args:"
I = 2
NEXT ARG = HOST(2, I) :F(DONE)
RESULT = RESULT " " ARG
I = I + 1 :(NEXT)
DONE OUTPUT = RESULTPattern
#!/usr/bin/env -S fragletc --vein=snobol4
TEXT = "Hello World"
TEXT "World" = "Universe"
OUTPUT = TEXT
ENDStdin Upper
#!/usr/bin/env -S fragletc --vein=snobol4
LINE = INPUT
OUTPUT = LINETest
#!/usr/bin/env -S fragletc --vein=snobol4
OUTPUT = "Hello World!"