diff --git a/src/interfaces/ecpg/preproc/check_rules.pl b/src/interfaces/ecpg/preproc/check_rules.pl
index 1388d05..3daff88 100644
--- a/src/interfaces/ecpg/preproc/check_rules.pl
+++ b/src/interfaces/ecpg/preproc/check_rules.pl
@@ -1,7 +1,7 @@
 #!/usr/bin/perl
 # src/interfaces/ecpg/preproc/check_rules.pl
 # test parser generator for ecpg
-# call with backend parser as stdin
+# call with backend grammar as stdin
 #
 # Copyright (c) 2009-2019, PostgreSQL Global Development Group
 #
@@ -47,6 +47,7 @@ my %replace_line = (
 
 my $block        = '';
 my $yaccmode     = 0;
+my $in_rule      = 0;
 my $brace_indent = 0;
 my (@arr, %found);
 my $comment     = 0;
@@ -131,10 +132,13 @@ while (<$parser_fh>)
 			$found{$block} = 1;
 			$cc++;
 			$block = '';
+			$in_rule = 0 if $arr[$fieldIndexer] eq ';';
 		}
 		elsif (($arr[$fieldIndexer] =~ '[A-Za-z0-9]+:')
 			|| $arr[ $fieldIndexer + 1 ] eq ':')
 		{
+			die "unterminated rule" if $in_rule;
+			$in_rule     = 1;
 			$non_term_id = $arr[$fieldIndexer];
 			$non_term_id =~ tr/://d;
 		}
@@ -145,6 +149,8 @@ while (<$parser_fh>)
 	}
 }
 
+die "unterminated rule" if $in_rule;
+
 close $parser_fh;
 if ($verbose)
 {
diff --git a/src/interfaces/ecpg/preproc/parse.pl b/src/interfaces/ecpg/preproc/parse.pl
index 6dee500..e219398 100644
--- a/src/interfaces/ecpg/preproc/parse.pl
+++ b/src/interfaces/ecpg/preproc/parse.pl
@@ -22,6 +22,7 @@ $path = "." unless $path;
 my $copymode              = 0;
 my $brace_indent          = 0;
 my $yaccmode              = 0;
+my $in_rule               = 0;
 my $header_included       = 0;
 my $feature_not_supported = 0;
 my $tokenmode             = 0;
@@ -288,6 +289,7 @@ sub main
 				@fields  = ();
 				$infield = 0;
 				$line    = '';
+				$in_rule = 0;
 				next;
 			}
 
@@ -365,6 +367,8 @@ sub main
 				$line    = '';
 				@fields  = ();
 				$infield = 1;
+				die "unterminated rule" if $in_rule;
+				$in_rule = 1;
 				next;
 			}
 			elsif ($copymode)
@@ -415,6 +419,7 @@ sub main
 			}
 		}
 	}
+	die "unterminated rule" if $in_rule;
 	return;
 }
 
