#!/usr/bin/perl -w use v5.14; no if $] >= 5.017011, warnings => 'experimental::smartmatch'; sub ok{ chomp; # Remove the final newline y/+/-/; # (1) Replace + with - return if /^-|-$|\[-|-\]/; # (2) ERROR if the string starts/ends with a - or if it contains [- or -] 1 while s/\[([^[\]]+)]/-$1/; # (3) Replace [...] with +... my ($base, $index, $vars) = (0, 0, 0); for (split '-'){ # (4) Tokenize the expression, using '-' as the delimiter $base++ when m/^bx$|^bp$/i; # (5) If the current token is bx or bp, increment $base $index++ when m/^si$|^di$/i; # (6) If the current token is si or di, increment $index $vars++ when m/^[a-z]$/; # (7) If the current token is a lowercase letter, increment $vars 1 when m/^\d*$/i; # (8) If the current token is a number, do nothing default { return } # (9) Otherwise ERROR } $base < 2 && $index < 2 && $vars < 2 # (10) } say ok() ? 'OK' : 'ERROR' while <>;