#!/usr/bin/perl -w 

################################################################
# Mastering Regex pg 313.
################################################################        
#  Defing base types:
#---------------------
$esc        = '\\\\';          #$Period         = '\.';
$space      = '\040';          $tab            = '\t';
$OpenBR     = '\[';            $CloseBR        = '\]';
$OpenParen  = '\(';            $CloseParen     = '\)';
$NonASCII   = '\x80-\xff';     $ctrl           = '\000-\037';
$CRlist     = '\n\015';
################################################################        
# From the base types we build More complex types 
#-------------------------------------------------
# items 19, 20, 21
$qtext       = qq/[^$esc$NonASCII$CRlist"]/;                 # for within "..."
#$dtext       = qq/[^$esc$NonASCII$CRlist$OpenBR$CloseBR]/;   # for within [...]
$quoted_pair = qq<$esc [^$NonASCII] >;                       # an escaped character

################################################################
$ctext       = qq< [^$esc$NonASCII$CRlist()] >;

################################################################
#Matches one non-nested comment.
$Cnested = qq<
	$OpenParen				# (
	  $ctext*				#   normal*
	  (?: $quoted_pair $ctext* )*		#   (special normal*)*
	$CloseParen				#      )
>;

###############################################################
# Allows one level of nexted parentheses
$comment     = qq<				#
	$OpenParen				# (
	  $ctext*				#  normal*
	    (?:					#    special
	    (?: $quoted_pair | $Cnested )   	#    normal*
	    $ctext*                         	#      )*
	   )*					#        )*
	$CloseParen				#          )
>;

################################################################        
# Optional Whitespace/comments
$X=qq<
	[$space$tab]*                     	# No whitespace
	(?: $comment [ $space$tab]* )*    	# If comment found, allow more white space.
>;
################################################################        
$atom_char = qq/[^($space)<>\@,;:".$esc$OpenBR$CloseBR$ctrl$NonASCII]/;
$atom = qq<
	 $atom_char+				# Nab whitespace.
	 (?!$atom_char)				# If comment found, allow more white space.   
>;

###############################################################
# Matches doublely quoted string
$quoted_str= qq<
	"				  	# "
	  $qtext *                        	#   normal
	  (?: $quoted_pair $qtext * )*		#   (special normal* )*
	"					#      "
>;

################################################################        
# Word
$word = qq<
	(?:  
	  $atom					# Atom
	  |					# or
	  $quoted_str				# Quoted String
	)
>;

################################################################
# Phrase Control - like ctrl without tab
$phrase_ctrl = '\000-\010\012-\037';		# like ctrl, but without tab
$phrase_char = qq/[^()<>\@,;:".$esc$OpenBR$CloseBR$NonASCII$phrase_ctrl]/;
$phrase = qq<
	$word					#leading word
	$phrase_char *				# normal atoms and/or spaces
	(?:						
	  (?: $comment| $quoted_str )		# special comment or quoted string
	  $phrase_char *				# more normal
	)*
>;

################################################################
#  Sample Data
####################################################################
$ascii = qq< Main Entry: 1dog [IMAGE] [IMAGE]
Pronunciation: 'dog, 'däg
Function: noun
Usage: often attributive
Etymology: Middle English, from Old English docga
Date: before 12th century
1 a : CANID; especially : a highly variable domestic mammal (Canis
familiaris) closely related to the common wolf (Canis lupus)
b : a male dog; also : a male usually carnivorous mammal
2 a : a worthless person b : FELLOW, CHAP <a lazy dog> <you
lucky dog>
3 a : any of various usually simple mechanical devices for
holding, gripping, or fastening that consist of a spike, bar,
or hook b : ANDIRON
4 : uncharacteristic or affected stylishness or dignity <put
on the dog>
5 capitalized : either of the constellations Canis Major or
Canis Minor
6 plural : FEET
7 plural : RUIN <going to the dogs>
8 : one inferior of its kind: as a : an investment not worth
its price b : an undesirable piece of merchandise
9 : an unattractive person and especially a girl or woman
10 : HOT DOG 1
- dog·like [IMAGE] /'dog-"lIk/ adjective
[dog illustration]
>;

####################################################################
$ascii =~ m/$X($phrase)?/;
print  "$1\n";
####################################################################



################################################################
 


