
Parsing Filter Expressions in NestJS with a Context-Free Grammar
Flat query param filtering breaks down fast. ?status=active&age_gte=18 works until someone needs (status=active OR status=pending) AND age>=18 . At that point you're either inventing your own encoding convention or telling the client it can't do that. The underlying issue is that boolean logic is recursive. AND and OR nest arbitrarily, and flat key-value params have no way to express that structure. What a Grammar Actually Is A Context-Free Grammar (CFG) defines a language through production rules. Each rule describes how a named concept (non-terminal) expands into tokens (terminals) or other non-terminals. The arithmetic grammar is the classic example: expression → term ( ("+" | "-") term )* term → factor ( ("*" | "/") factor )* factor → NUMBER | "(" expression ")" Operator precedence isn't hardcoded, it falls out of the nesting. term sits inside expression , so * binds tighter than + automatically. factor → "(" expression ")" lets expressions nest to any depth. The grammar describes
Continue reading on Dev.to Webdev
Opens in a new tab


