Principle:20000419 Fauxnix Bash Grammar Parsing
| Knowledge Sources | |
|---|---|
| Domains | Parsing, Compilers, Bash_Grammar |
| Last Updated | 2026-10-26 00:00 GMT |
Overview
A tokenizer and recursive-descent parser that converts a subset of POSIX/bash command syntax into an abstract syntax tree, failing loudly with a named error on constructs it does not support rather than silently producing wrong output.
Description
Bash Grammar Parsing is the front-end of any source-to-source translator that targets bash. The parser must handle the pragmatically useful subset of bash — pipelines, lists (; && ||), redirections, quoting (single/double), variables ($VAR ${VAR}), command substitution ($(...) `...`), arithmetic ($((...))), env-assignment prefixes, control flow (if/for/while/case), and array assignment — while explicitly rejecting unsupported constructs (heredocs, subshells, background &, process substitution). The fail-loud discipline is critical: a translator that silently approximates unsupported syntax would produce output that differs from real bash, breaking agent expectations. The parser produces a typed AST (CommandList → ListSegment → Pipeline → ShellCommand) that the translator walks to emit target-language code.
Usage
Use this principle whenever you need to deterministically translate bash commands into another language. The fail-loud subset approach is the right choice when full bash grammar support is impractical but the common subset covers the vast majority of real agent commands.
Theoretical Basis
The parser is a classic two-stage design: a tokenizer (lexer) that recognizes tokens (words, operators, redirections) and a recursive-descent parser that builds the AST.
tokens = tokenize(input)
ast = parseList(tokens)
parseList → parsePipeline (op ; | && | ||)*
parsePipeline → parseCommand (| parseCommand)*
parseCommand → SimpleCommand | IfCommand | ForCommand | WhileCommand | CaseCommand
The subset is defined by what the translator can emit correctly; everything else throws a FauxnixParseError at parse time, not at runtime.