1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
|
/*
* This file contains code for
*
* int rexpr(char *expr, char *s);
*
* which answers
*
* 1 if 's' is in the language described by the regular expression 'expr'
* 0 if it is not
* -1 if the regular expression is invalid
*
* Language membership is determined by constructing a non-deterministic
* finite automata (NFA) from the regular expression. A depth-
* first-search is performed on the NFA (graph) to check for a match of 's'.
* Each non-epsilon arc consumes one character from 's'. Backtracking is
* performed to check all possible paths through the NFA.
*
* Regular expressions follow the meta-language:
*
* <regExpr> ::= <andExpr> ( '|' <andExpr> )*
*
* <andExpr> ::= <expr> ( <expr> )*
*
* <expr> ::= {'~'} '[' <atomList> ']' <repeatSymbol>
* | '(' <regExpr> ')' <repeatSymbol>
* | '{' <regExpr> '}' <repeatSymbol>
* | <atom> <repeatSymbol>
*
* <repeatSymbol> ::= { '*' | '+' }
*
* <atomList> ::= <atom> ( <atom> )*
* | { <atomList> } <atom> '-' <atom> { <atomList> }
*
* <atom> ::= Token[Atom]
*
* Notes:
* ~ means complement the set in [..]. i.e. all characters not listed
* * means match 0 or more times (can be on expression or atom)
* + means match 1 or more times (can be on expression or atom)
* {} optional
* () grouping
* [] set of atoms
* x-y all characters from x to y (found only in [..])
* \xx the character with value xx
*
* Examples:
* [a-z]+
* match 1 or more lower-case letters (e.g. variable)
*
* 0x[0-9A-Fa-f]+
* match a hex number with 0x on front (e.g. 0xA1FF)
*
* [0-9]+.[0-9]+{e[0-9]+}
* match a floating point number (e.g. 3.14e21)
*
* Code example:
* if ( rexpr("[a-zA-Z][a-zA-Z0-9]+", str) ) then str is keyword
*
* Terence Parr
* Purdue University
* April 1991
*/
#include <stdio.h>
#include <ctype.h>
#ifdef __STDC__
#include <stdlib.h>
#else
#include <malloc.h>
#endif
#include "rexpr.h"
#ifdef __USE_PROTOS
static int regExpr( GraphPtr g );
static int andExpr( GraphPtr g );
static int expr( GraphPtr g );
static int repeatSymbol( GraphPtr g );
static int atomList( char *p, int complement );
static void next( void );
static ArcPtr newGraphArc( void );
static NodePtr newNode( void );
static int ArcBetweenGraphNode( NodePtr i, NodePtr j, int label );
static Graph BuildNFA_atom( int label );
static Graph BuildNFA_AB( Graph A, Graph B );
static Graph BuildNFA_AorB( Graph A, Graph B );
static Graph BuildNFA_set( char *s );
static Graph BuildNFA_Astar( Graph A );
static Graph BuildNFA_Aplus( Graph A );
static Graph BuildNFA_Aoptional( Graph A );
#else
static int regExpr();
static int andExpr();
static int expr();
static int repeatSymbol();
static int atomList();
static void next();
static ArcPtr newGraphArc();
static NodePtr newNode();
static int ArcBetweenGraphNode();
static Graph BuildNFA_atom();
static Graph BuildNFA_AB();
static Graph BuildNFA_AorB();
static Graph BuildNFA_set();
static Graph BuildNFA_Astar();
static Graph BuildNFA_Aplus();
static Graph BuildNFA_Aoptional();
#endif
static char *_c;
static int token, tokchar;
static NodePtr accept;
static NodePtr freelist = NULL;
/*
* return 1 if s in language described by expr
* 0 if s is not
* -1 if expr is an invalid regular expression
*/
#ifdef __USE_PROTOS
static int rexpr(char *expr,char *s)
#else
static int rexpr(expr, s)
char *expr, *s;
#endif
{
NodePtr p,q;
Graph nfa;
int result;
fprintf(stderr, "rexpr(%s,%s);\n", expr,s);
freelist = NULL;
_c = expr;
next();
if ( regExpr(&nfa) == -1 ) return -1;
accept = nfa.right;
result = match(nfa.left, s);
/* free all your memory */
p = q = freelist;
while ( p!=NULL ) { q = p->track; free(p); p = q; }
return result;
}
/*
* do a depth-first-search on the NFA looking for a path from start to
* accept state labelled with the characters of 's'.
*/
#ifdef __USE_PROTOS
static int match(NodePtr automaton,char *s)
#else
static int match(automaton, s)
NodePtr automaton;
char *s;
#endif
{
ArcPtr p;
if ( automaton == accept && *s == '\0' ) return 1; /* match */
for (p=automaton->arcs; p!=NULL; p=p->next) /* try all arcs */
{
if ( p->label == Epsilon )
{
if ( match(p->target, s) ) return 1;
}
else if ( p->label == *s )
if ( match(p->target, s+1) ) return 1;
}
return 0;
}
/*
* <regExpr> ::= <andExpr> ( '|' {<andExpr>} )*
*
* Return -1 if syntax error
* Return 0 if none found
* Return 1 if a regExrp was found
*/
#ifdef __USE_PROTOS
static int regExpr(GraphPtr g)
#else
static int regExpr(g)
GraphPtr g;
#endif
{
Graph g1, g2;
if ( andExpr(&g1) == -1 )
{
return -1;
}
while ( token == '|' )
{
int a;
next();
a = andExpr(&g2);
if ( a == -1 ) return -1; /* syntax error below */
else if ( !a ) return 1; /* empty alternative */
g1 = BuildNFA_AorB(g1, g2);
}
if ( token!='\0' ) return -1;
*g = g1;
return 1;
}
/*
* <andExpr> ::= <expr> ( <expr> )*
*/
#ifdef __USE_PROTOS
static int andExpr(GraphPtr g)
#else
static int andExpr(g)
GraphPtr g;
#endif
{
Graph g1, g2;
if ( expr(&g1) == -1 )
{
return -1;
}
while ( token==Atom || token=='{' || token=='(' || token=='~' || token=='[' )
{
if (expr(&g2) == -1) return -1;
g1 = BuildNFA_AB(g1, g2);
}
*g = g1;
return 1;
}
/*
* <expr> ::= {'~'} '[' <atomList> ']' <repeatSymbol>
* | '(' <regExpr> ')' <repeatSymbol>
* | '{' <regExpr> '}' <repeatSymbol>
* | <atom> <repeatSymbol>
*/
#ifdef __USE_PROTOS
static int expr(GraphPtr g)
#else
static int expr(g)
GraphPtr g;
#endif
{
int complement = 0;
char s[257]; /* alloc space for string of char in [] */
if ( token == '~' || token == '[' )
{
if ( token == '~' ) {complement = 1; next();}
if ( token != '[' ) return -1;
next();
if ( atomList( s, complement ) == -1 ) return -1;
*g = BuildNFA_set( s );
if ( token != ']' ) return -1;
next();
repeatSymbol( g );
return 1;
}
if ( token == '(' )
{
next();
if ( regExpr( g ) == -1 ) return -1;
if ( token != ')' ) return -1;
next();
repeatSymbol( g );
return 1;
}
if ( token == '{' )
{
next();
if ( regExpr( g ) == -1 ) return -1;
if ( token != '}' ) return -1;
next();
/* S p e c i a l C a s e O p t i o n a l { } */
if ( token != '*' && token != '+' )
{
*g = BuildNFA_Aoptional( *g );
}
repeatSymbol( g );
return 1;
}
if ( token == Atom )
{
*g = BuildNFA_atom( tokchar );
next();
repeatSymbol( g );
return 1;
}
return -1;
}
/*
* <repeatSymbol> ::= { '*' | '+' }
*/
#ifdef __USE_PROTOS
static int repeatSymbol(GraphPtr g)
#else
static int repeatSymbol(g)
GraphPtr g;
#endif
{
switch ( token )
{
case '*' : *g = BuildNFA_Astar( *g ); next(); break;
case '+' : *g = BuildNFA_Aplus( *g ); next(); break;
}
return 1;
}
/*
* <atomList> ::= <atom> { <atom> }*
* { <atomList> } <atom> '-' <atom> { <atomList> }
*
* a-b is same as ab
* q-a is same as q
*/
#ifdef __USE_PROTOS
static int atomList(char *p, int complement)
#else
static int atomList(p, complement)
char *p;
int complement;
#endif
{
static unsigned char set[256]; /* no duplicates */
int first, last, i;
char *s = p;
if ( token != Atom ) return -1;
for (i=0; i<256; i++) set[i] = 0;
while ( token == Atom )
{
if ( !set[tokchar] ) *s++ = tokchar;
set[tokchar] = 1; /* Add atom to set */
next();
if ( token == '-' ) /* have we found '-' */
{
first = *(s-1); /* Get last char */
next();
if ( token != Atom ) return -1;
else
{
last = tokchar;
}
for (i = first+1; i <= last; i++)
{
if ( !set[tokchar] ) *s++ = i;
set[i] = 1; /* Add atom to set */
}
next();
}
}
*s = '\0';
if ( complement )
{
for (i=0; i<256; i++) set[i] = !set[i];
for (i=1,s=p; i<256; i++) if ( set[i] ) *s++ = i;
*s = '\0';
}
return 1;
}
/* a somewhat stupid lexical analyzer */
#ifdef __USE_PROTOS
static void next(void)
#else
static void next()
#endif
{
while ( *_c==' ' || *_c=='\t' || *_c=='\n' ) _c++;
if ( *_c=='\\' )
{
_c++;
if ( isdigit(*_c) )
{
int n=0;
while ( isdigit(*_c) )
{
n = n*10 + (*_c++ - '0');
}
if ( n>255 ) n=255;
tokchar = n;
}
else
{
switch (*_c)
{
case 'n' : tokchar = '\n'; break;
case 't' : tokchar = '\t'; break;
case 'r' : tokchar = '\r'; break;
default : tokchar = *_c;
}
_c++;
}
token = Atom;
}
else if ( isgraph(*_c) && *_c!='[' && *_c!='(' && *_c!='{' &&
*_c!='-' && *_c!='}' && *_c!=')' && *_c!=']' &&
*_c!='+' && *_c!='*' && *_c!='~' && *_c!='|' )
{
token = Atom;
tokchar = *_c++;
}
else
{
token = tokchar = *_c++;
}
}
/* N F A B u i l d i n g R o u t i n e s */
#ifdef __USE_PROTOS
static ArcPtr newGraphArc(void)
#else
static ArcPtr newGraphArc()
#endif
{
ArcPtr p;
p = (ArcPtr) calloc(1, sizeof(Arc));
if ( p==NULL ) {fprintf(stderr,"rexpr: out of memory\n"); exit(-1);}
if ( freelist != NULL ) p->track = (ArcPtr) freelist;
freelist = (NodePtr) p;
return p;
}
#ifdef __USE_PROTOS
static NodePtr newNode(void)
#else
static NodePtr newNode()
#endif
{
NodePtr p;
p = (NodePtr) calloc(1, sizeof(Node));
if ( p==NULL ) {fprintf(stderr,"rexpr: out of memory\n"); exit(-1);}
if ( freelist != NULL ) p->track = freelist;
freelist = p;
return p;
}
#ifdef __USE_PROTOS
static void ArcBetweenGraphNodes(NodePtr i,NodePtr j,int label)
#else
static void ArcBetweenGraphNodes(i, j, label)
NodePtr i, j;
int label;
#endif
{
ArcPtr a;
a = newGraphArc();
if ( i->arcs == NULL ) i->arctail = i->arcs = a;
else {(i->arctail)->next = a; i->arctail = a;}
a->label = label;
a->target = j;
}
#ifdef __USE_PROTOS
static Graph BuildNFA_atom(int label)
#else
static Graph BuildNFA_atom(label)
int label;
#endif
{
Graph g;
g.left = newNode();
g.right = newNode();
ArcBetweenGraphNodes(g.left, g.right, label);
return( g );
}
#ifdef __USE_PROTOS
static Graph BuildNFA_AB(Graph A,Graph B)
#else
static Graph BuildNFA_AB(A, B)
Graph A, B;
#endif
{
Graph g;
ArcBetweenGraphNodes(A.right, B.left, Epsilon);
g.left = A.left;
g.right = B.right;
return( g );
}
#ifdef __USE_PROTOS
static Graph BuildNFA_AorB(Graph A,Graph B)
#else
static Graph BuildNFA_AorB(A, B)
Graph A, B;
#endif
{
Graph g;
g.left = newNode();
ArcBetweenGraphNodes(g.left, A.left, Epsilon);
ArcBetweenGraphNodes(g.left, B.left, Epsilon);
g.right = newNode();
ArcBetweenGraphNodes(A.right, g.right, Epsilon);
ArcBetweenGraphNodes(B.right, g.right, Epsilon);
return( g );
}
#ifdef __USE_PROTOS
static Graph BuildNFA_set(char *s)
#else
static Graph BuildNFA_set( s )
char *s;
#endif
{
Graph g;
if ( s == NULL ) return g;
g.left = newNode();
g.right = newNode();
while ( *s != '\0' )
{
ArcBetweenGraphNodes(g.left, g.right, *s++);
}
return g;
}
#ifdef __USE_PROTOS
static Graph BuildNFA_Astar(Graph A)
#else
static Graph BuildNFA_Astar( A )
Graph A;
#endif
{
Graph g;
g.left = newNode();
g.right = newNode();
ArcBetweenGraphNodes(g.left, A.left, Epsilon);
ArcBetweenGraphNodes(g.left, g.right, Epsilon);
ArcBetweenGraphNodes(A.right, g.right, Epsilon);
ArcBetweenGraphNodes(A.right, A.left, Epsilon);
return( g );
}
#ifdef __USE_PROTOS
static Graph BuildNFA_Aplus(Graph A)
#else
static Graph BuildNFA_Aplus( A )
Graph A;
#endif
{
ArcBetweenGraphNodes(A.right, A.left, Epsilon);
return( A );
}
#ifdef __USE_PROTOS
static Graph BuildNFA_Aoptional(Graph A)
#else
static Graph BuildNFA_Aoptional( A )
Graph A;
#endif
{
Graph g;
g.left = newNode();
g.right = newNode();
ArcBetweenGraphNodes(g.left, A.left, Epsilon);
ArcBetweenGraphNodes(g.left, g.right, Epsilon);
ArcBetweenGraphNodes(A.right, g.right, Epsilon);
return( g );
}
|