-- grammar: # grammar: order: quantity color item quantity: number color: id | qstring item: id # tokens: number: /\d+/ id: /[a-zA-Z][-_a-zA-Z0-9]*/ qstring: /"(?:\\["\\]|[^"\\])*"/ { $return = unquote($item[1]); } | /'(?:\\['\\]|[^'\\])*'/ { $return = unquote($item[1]); } -- helper (non-grammar) code: sub unquote { local $_ = $_[0]; s/^['"]//; s/['"]$//; s/\\(.)/\1/g; return $_; } -- #### -- helper (non-grammar) code: my @COLORS = ( 'red' 'green' 'blue' 'cyan' 'magenta' 'burnt umber' ); my %COLORS; { my $idx = 0; %COLORS = map { $_ => $idx++ } @COLORS; } sub unquote { local $_ = $_[0]; s/^['"]//; s/['"]$//; s/\\(.)/\1/g; return $_; } my $error = 0; ... parse ... die("Validation errors were found.\n") if ($error); -- grammar: # grammar: order: quantity color item quantity: number color: id | qstring { $return = $COLORS{$item[1]}; # Uncomment the following if you want # to have multiple errors listed. # unless (defined($return)) # { # warn("Line $thisline: Invalid color.\n"); # $error = 1; # $return = undef; # } # 1; } item: id { ($return) = $item[1] =~ /^(pen|pencil)$/; # Uncomment the following if you want # to have multiple errors listed. # unless (defined($return)) # { # warn("Line $thisline: Invalid item.\n"); # $error = 1; # $return = undef; # } # 1; } # tokens: number: /\d+/ id: /[a-zA-Z][-_a-zA-Z0-9]*/ qstring: /"(?:\\["\\]|[^"\\])*"/ { $return = unquote($item[1]); } | /'(?:\\['\\]|[^'\\])*'/ { $return = unquote($item[1]); } --