Skip to content
Back to skills

Tokenize Then Parse

ASecurity

For interpreters: separate lexing from parsing, token stream to AST, structured text processing in stages.

  • 4 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added November 7, 2026
ai-agentspythonexpress

Security analysis

A100/100

Scanned November 7, 2026

npx -y skills add jimmc414/claude-code-plugin-marketplace --skill tokenize-then-parse --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Tokenize Then Parse?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Tokenize Then Parse
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/jimmc414-tokenize-then-parse/badge)](https://www.skillsdirectory.com/skills/jimmc414-tokenize-then-parse)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: tokenize-then-parse
description: "For interpreters: separate lexing from parsing, token stream to AST, structured text processing in stages."
---

# tokenize-then-parse

## When to Use
- Building interpreters or compilers
- Processing parenthesized expressions
- Structured text with clear token boundaries
- Multi-stage processing pipeline

## When NOT to Use
- Simple fixed format (just split)
- Very complex grammar (use parser generator)
- No clear token boundaries

## The Pattern

Tokenize text into a stream of tokens, then parse tokens into a structure.

```python
def tokenize(s):
    """Convert string to list of tokens."""
    return s.replace('(', ' ( ').replace(')', ' ) ').split()

def parse(tokens):
    """Parse tokens into nested structure."""
    token = tokens.pop(0)
    if token == '(':
        result = []
        while tokens[0] != ')':
            result.append(parse(tokens))
        tokens.pop(0)  # Remove ')'
        return result
    else:
        return atom(token)

def atom(token):
    """Convert token to appropriate type."""
    try:
        return int(token)
    except ValueError:
        try:
            return float(token)
        except ValueError:
            return token
```

## Example (from pytudes lis.py)

```python
def tokenize(s):
    """Convert a string into a list of tokens."""
    return s.replace('(', ' ( ').replace(')', ' ) ').split()

def read_from_tokens(tokens):
    """Read an expression from a sequence of tokens."""
    if len(tokens) == 0:
        raise SyntaxError('unexpected EOF')

    token = tokens.pop(0)

    if token == '(':
        L = []
        while tokens[0] != ')':
            L.append(read_from_tokens(tokens))
        tokens.pop(0)  # Remove ')'
        return L
    elif token == ')':
        raise SyntaxError('unexpected )')
    else:
        return atom(token)

def atom(token):
    """Numbers become numbers; every other token is a symbol."""
    try:
        return int(token)
    except ValueError:
        try:
            return float(token)
        except ValueError:
            return Symbol(token)

def parse(program):
    """Read a Scheme expression from a string."""
    return read_from_tokens(tokenize(program))

# Usage
parse("(+ 2 (* 3 4))")
# Returns: ['+', 2, ['*', 3, 4]]

parse("(define square (lambda (x) (* x x)))")
# Returns: ['define', 'square', ['lambda', ['x'], ['*', 'x', 'x']]]
```

## Key Principles
1. **Tokenize first**: Separate lexing from parsing
2. **pop(0) consumes**: Mutable token list, pop as you parse
3. **Recursive descent**: Parse nested structures recursively
4. **Error on malformed**: Raise SyntaxError for bad input
5. **atom() for literals**: Handle number conversion

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.