Grammar


examples/regex2/add.p6
use v6;

BEGIN {
	@*INC.push('examples/regex2/');
}
use Add1;

my @experssions = (
	"2 + 3",
	"2 + 4 ",
	"2 + 3 x",
	"2 +",
	"2 3",
	"2 - 3",
);

for @experssions -> $exp {
	print $exp, " ";
	my $result = Add1.parse($exp);
	say $result ?? 'OK' !! 'NOT OK'; 
	CATCH {
		say "exception received: $!";
	}
}


examples/regex2/Add1.pm
grammar Add1 {
	rule TOP { ^ <math> $ }
	rule math { <operand> <operator> <operand> }
	token operand { \d+ }
	token operator { <[\+\*]> }
}

examples/regex2/Add1.pm.out
2 + 3 OK
2 + 4  OK
2 + 3 x NOT OK
2 + NOT OK
2 3 NOT OK
2 - 3 NOT OK

Copyright 2006, 2007, 2008, 2009, 2010 Gabor Szabo http://szabgab.com/ Index | TOC
If you are interested in on-site trainings by the author, please contact me directly.

Online courses:

Would you like to get
updated when I publish
the next article?

Follow me:

Google Plus Twitter RSS feed