Optimization of grepper.
[idzebra-moved-to-github.git] / dfa / lexer.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: lexer.c,v $
7  * Revision 1.3  1994-10-03 17:22:19  adam
8  * Optimization of grepper.
9  *
10  * Revision 1.2  1994/09/27  16:31:20  adam
11  * First version of grepper: grep with error correction.
12  *
13  * Revision 1.1  1994/09/26  10:16:55  adam
14  * First version of dfa module in alex. This version uses yacc to parse
15  * regular expressions. This should be hand-made instead.
16  *
17  *
18  * Adam Dickmeiss.      1992-1993
19  * This module is actually very old...
20  */
21 #include <stdio.h>
22 #include <assert.h>
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdarg.h>
27
28 #include <util.h>
29 #include <dfa.h>
30 #include "imalloc.h"
31 #include "lexer.h"
32
33 static char *prog;
34
35
36 void error (const char *format, ...)
37 {
38     va_list argptr;
39     va_start (argptr, format);
40     fprintf (stderr, "%s error: ", prog);
41     (void) vfprintf (stderr, format, argptr);
42     putc ('\n', stderr);
43     exit (1);
44 }
45
46 #ifdef YACC
47 extern int yydebug;
48 #else
49 extern int alexdebug;
50 #endif
51 int ccluse = 0;
52
53 static int lexer_options (int argc, char **argv)
54 {
55     while (--argc > 0)
56         if (**++argv == '-')
57             while (*++*argv)
58             {
59                 switch (**argv)
60                 {
61                 case 'V':
62                     fprintf (stderr, "%s: %s %s\n", prog, __DATE__, __TIME__);
63                     continue;
64                 case 's':
65                     dfa_verbose = 1;
66                     continue;
67                 case 't':
68 #ifdef YACC
69                     yydebug = 1;
70 #else
71                     alexdebug = 1;
72 #endif
73                     continue;
74                 case 'c':
75                     ccluse = 1;
76                     continue;
77                 case 'd':
78                     switch (*++*argv)
79                     {
80                     case 's':
81                         debug_dfa_tran = 1;
82                         break;
83                     case 't':
84                         debug_dfa_trav = 1;
85                         break;
86                     case 'f':
87                         debug_dfa_followpos = 1;
88                         break;
89                     default:
90                         --*argv;
91                         debug_dfa_tran = 1;
92                         debug_dfa_followpos = 1;
93                         debug_dfa_trav = 1;
94                     }
95                     continue;
96                 default:
97                     fprintf (stderr, "%s: unknown option `-%s'\n",
98                              prog, *argv);
99                     return 1;
100                 }
101                 break;
102             }
103     return 0;
104 }
105
106 int main (int argc, char **argv)
107 {
108     int i, no = 0;
109     DFA *dfa;
110     DFA_states *dfas;
111
112     prog = *argv;
113 #ifdef YACC
114     yydebug = 0;
115 #else
116     alexdebug = 0;
117 #endif
118     i = lexer_options (argc, argv);
119     if (i)
120         return i;
121
122     if (argc < 2)
123     {
124         fprintf (stderr, "usage\n  %s [-c] [-V] [-s] [-t] [-d[stf]] file\n",
125                  prog);
126         return 1;
127     }
128     else while (--argc > 0)
129             if (**++argv != '-' && **argv)
130             {
131                 ++no;
132                 i = read_file (*argv, &dfa);
133                 if (i)
134                     return i;
135                 dfas = mk_dfas (dfa, 2000);
136                 rm_dfa (&dfa);
137                 rm_dfas (&dfas);
138             }
139 #ifdef MEMDEBUG
140     imemstat();
141 #endif
142     if (!no)
143     {
144         fprintf (stderr, "%s: no files specified\n", prog);
145         return 2;
146     }
147     return 0;
148 }
149