static void compile_grep_patterns_real(struct grep_opt *opt) { struct grep_pat *p; struct grep_expr *header_expr = prep_header_patterns(opt); for (p = opt->pattern_list; p; p = p->next) { switch (p->token) { case GREP_PATTERN: /* atom */ case GREP_PATTERN_HEAD: case GREP_PATTERN_BODY: compile_regexp(p, opt); break; default: opt->extended = 1; break; } } if (opt->all_match || header_expr) opt->extended = 1; else if (!opt->extended && !opt->debug) return; p = opt->pattern_list; if (p) opt->pattern_expression = compile_pattern_expr(&p); if (p) die("incomplete pattern expression: %s", p->pattern); if (!header_expr) return; if (!opt->pattern_expression) opt->pattern_expression = header_expr; else if (opt->all_match) opt->pattern_expression = grep_splice_or(header_expr, opt->pattern_expression); else opt->pattern_expression = grep_or_expr(opt->pattern_expression, header_expr); opt->all_match = 1; }
int main(int argc, char *argv[]) { static const char *PAN_REGEX = "^[A-Z]{5}[0-9]{4}[A-Z]$"; regex_t regex; compile_regexp(®ex, PAN_REGEX); int n; scanf("%d\n", &n); for (int i = 0; i < n; ++i) { char buf[1024]; fgets(buf, 1024, stdin); if (regexec(®ex, buf, 0, NULL, 0) == 0) { printf("YES\n"); } else { printf("NO\n"); } } regfree(®ex); return 0; }
/*------------------------------------------------------------*/ void select_atom_id (const char *item) { mol3d *mol; res3d *res; at3d *at; regexp *rx; int *flags; #ifndef NDEBUG int old = count_atom_selections(); #endif assert (item); assert (*item); rx = compile_regexp (item); push_atom_selection(); flags = current_atom_sel->flags; for (mol = first_molecule; mol; mol = mol->next) { for (res = mol->first; res; res = res->next) { for (at = res->first; at; at = at->next) { *flags++ = (regexec (rx, at->name) != 0); } } } free (rx); #ifdef SELECT_DEBUG fprintf (stderr, "atom select id: %i\n", select_atom_count()); #endif #ifndef NDEBUG assert (count_atom_selections() == old + 1); #endif }
void compile_grep_patterns(struct grep_opt *opt) { struct grep_pat *p; struct grep_expr *header_expr = NULL; if (opt->header_list) { p = opt->header_list; header_expr = compile_pattern_expr(&p); if (p) die("incomplete pattern expression: %s", p->pattern); for (p = opt->header_list; p; p = p->next) { switch (p->token) { case GREP_PATTERN: /* atom */ case GREP_PATTERN_HEAD: case GREP_PATTERN_BODY: compile_regexp(p, opt); break; default: opt->extended = 1; break; } } } for (p = opt->pattern_list; p; p = p->next) { switch (p->token) { case GREP_PATTERN: /* atom */ case GREP_PATTERN_HEAD: case GREP_PATTERN_BODY: compile_regexp(p, opt); break; default: opt->extended = 1; break; } } if (opt->all_match || header_expr) opt->extended = 1; else if (!opt->extended) return; /* Then bundle them up in an expression. * A classic recursive descent parser would do. */ p = opt->pattern_list; if (p) opt->pattern_expression = compile_pattern_expr(&p); if (p) die("incomplete pattern expression: %s", p->pattern); if (!header_expr) return; if (opt->pattern_expression) { struct grep_expr *z; z = xcalloc(1, sizeof(*z)); z->node = GREP_NODE_OR; z->u.binary.left = opt->pattern_expression; z->u.binary.right = header_expr; opt->pattern_expression = z; } else { opt->pattern_expression = header_expr; } opt->all_match = 1; }
/*------------------------------------------------------------*/ void select_residue_from_to (const char *item1, const char *item2) { mol3d *mol; res3d *res; boolean within_sequence = FALSE; boolean first_is_known = FALSE; boolean first_was_aa; regexp *rx1, *rx2; int *flags; #ifndef NDEBUG int old = count_residue_selections(); #endif assert (item1); assert (*item1); assert (item2); assert (*item2); if (str_eq (item1, item2)) { yyerror ("invalid from-to selection: same residue"); return; } rx1 = compile_regexp (item1); rx2 = compile_regexp (item2); push_residue_selection(); flags = current_residue_sel->flags; for (mol = first_molecule; mol; mol = mol->next) { for (res = mol->first; res; res = res->next) { if (within_sequence) { *flags++ = TRUE; if (regexec (rx2, res->name) != 0) within_sequence = FALSE; } else { within_sequence = regexec (rx1, res->name) != 0; if (within_sequence) { /* kludge to deal with the silly PDB */ if (first_is_known) { /* notion of allowing multiple residues */ if (first_was_aa) { /* with the same name in a file */ if (res->code == 'X') within_sequence = FALSE; } } else { first_was_aa = res->code != 'X'; first_is_known = TRUE; } } *flags++ = within_sequence; } } } if (within_sequence) yywarning ("sequence segment not ended (residue selection \"from to\")"); free (rx1); free (rx2); #ifdef SELECT_DEBUG fprintf (stderr, "residue select from to: %i\n", select_residue_count()); #endif #ifndef NDEBUG assert (count_residue_selections() == old + 1); #endif }
struct parameters get_params(int argc, char *argv[]) { struct parameters params; params.check_monophyly = FALSE; params.siblings = FALSE; params.mode = EXACT; params.context = 0; int opt_char; while ((opt_char = getopt(argc, argv, "c:hmrs")) != -1) { switch (opt_char) { case 'c': params.context = atoi(optarg); break; case 'h': help(argv); exit(EXIT_SUCCESS); case 'm': params.check_monophyly = TRUE; break; case 'r': params.mode = REGEXP; break; case 's': params.siblings = TRUE; break; default: fprintf (stderr, "Unknown option '-%c'\n", opt_char); exit(EXIT_FAILURE); break; /* ok, not very useful... but I might later decide to ignore the bad option rather than fail. */ } } /* check arguments */ if ((argc - optind) >= 2) { if (0 != strcmp("-", argv[optind])) { if (! set_parser_input_filename(argv[optind])) { perror(NULL); exit(EXIT_FAILURE); } } struct llist *lbl_list; switch (params.mode) { case EXACT: lbl_list = create_llist(); if (NULL == lbl_list) { perror(NULL); exit(EXIT_FAILURE); } optind++; /* optind is now index of 1st label */ for (; optind < argc; optind++) { if (! append_element(lbl_list, argv[optind])) { perror(NULL); exit(EXIT_FAILURE); } } params.labels = lbl_list; break; case REGEXP: optind++; /* optind is now index of regexp */ params.regexp_string = argv[optind]; params.regexp = compile_regexp(params.regexp_string); break; default: fprintf (stderr, "Unknown mode %d\n", params.mode); exit(EXIT_FAILURE); } } else { fprintf(stderr, "Usage: %s [-hm] <filename|-> <label> [label+]\n", argv[0]); exit(EXIT_FAILURE); } return params; }