Viable Prefixes

Published
Length
3 min read

Luis Felipe Dias

All posts

You identify viable prefixes by tracking valid symbol stacks during bottom-up parsing. A prefix is viable if it never passes the end of a handle. Engineering teams use these prefixes to build fast query parsers and compilers. They help the parser catch bad syntax as soon as a token arrives. Still, teams often rely too much on basic syntax checks and broad roles. When database roles have wide permissions, any valid query can run dangerous commands. How viable prefixes work in bottom-up parsers

Prefixes: the complete guide

Architecture patterns, implementation strategies, and security best practices. Delivered to your inbox.

Free white paper. No spam. Unsubscribe anytime.

You identify viable prefixes by tracking valid symbol stacks during bottom-up parsing. A prefix is viable if it never passes the end of a handle. Engineering teams use these prefixes to build fast query parsers and compilers. They help the parser catch bad syntax as soon as a token arrives. Still, teams often rely too much on basic syntax checks and broad roles. When database roles have wide permissions, any valid query can run dangerous commands.

How viable prefixes work in bottom-up parsers

A bottom-up parser reads input tokens from left to right. It pushes each new token onto a parse stack as it goes. This action is called a shift step in the parsing process. When the top of the stack matches a grammar rule, it reduces. That matching part on the stack is called the handle of the string.

Viable prefixes are the stack strings that can still form a valid sentence. They never extend past the right end of the current handle. You can add more tokens to them to complete a valid statement. If a new token breaks all grammar rules, the prefix becomes invalid. The parser stops right away and reports a syntax error to the user.

A deterministic finite automaton tracks these prefixes inside the parser at all times. Each state in the machine represents a set of grammar rules and positions. As the parser shifts tokens, it moves from one state to the next. If a valid transition exists for the next token, the prefix stays viable. Once the parser reduces a handle, it steps back and takes another transition. Because a finite state machine recognizes these prefixes, they always form a regular language.

Why syntax checks and broad roles leave gaps

Native parsers and role checks solve two basic problems for your team. Role checks decide which users or apps can log in to the system. Then the parser checks that every incoming command follows the right grammar rules. It rejects broken statements before the database engine tries to run them.

Prefixes: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

This native setup leaves a major security gap in live systems. A command can have valid syntax and still cause serious harm to data. For example, a query that drops a table has a valid prefix stack. A broad query that reads every row also passes all parser checks. Native roles only check who logs in at the start of a session. They do not inspect what each statement or command does in flight.

When engineers share wide admin roles for daily debugging, the risk grows fast. Any valid command they type will pass the parser and run right away. Native database logs often miss who ran a specific query on shared accounts. Also, valid queries return raw data directly to the client screen. Without extra controls, results show raw emails, phone numbers, card numbers, and national IDs. Data masking is needed to rewrite matched fields in results before output returns. Teams need a way to inspect valid commands and add approvals per query.

Add runtime guardrails and masking beside your databases and APIs

Login rules, role grants, and CI checks govern who connects to your stack. Yet any allowed script, engineer, or AI agent still runs commands with the full scope of its role. For workloads on Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes, SSH, or internal HTTP services, hoop.dev sits beside the service as an open-source proxy, a sidecar for your database or API.

Before a query runs, hoop.dev guardrails check the statement against your rules. An AI Analyzer you configure can score intent or hold risky calls for human review. Next, hoop.dev data masking rewrites sensitive fields in responses, and each verdict is logged per listener. Explore the MIT-licensed project on GitHub.

Frequently asked questions

What is the difference between a handle and a viable prefix?

A handle is the exact substring that matches a grammar rule for reduction. A viable prefix is any stack prefix that does not pass that handle.

How does a parser check if a prefix is still viable?

The parser uses a finite state machine to read symbols on the stack. If the next input token has a valid state transition, the prefix works.

Can a query have valid syntax and still break security rules?

Yes, a destructive command follows grammar rules and builds a valid parse stack. You need command checks and field masking to stop unsafe actions in flight.

Save the open-source gateway.

hoop is MIT-licensed infrastructure for controlling how AI agents reach production data. Star hoophq/hoop so you can inspect it, deploy it, or share it when your team starts governing agent access.