Simple error reporting when parsing regular expressions.
[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.6  1995-01-25 11:30:51  adam
8  * Simple error reporting when parsing regular expressions.
9  * Memory usage reduced.
10  *
11  * Revision 1.5  1995/01/24  16:00:22  adam
12  * Added -ansi to CFLAGS.
13  * Some changes to the dfa module.
14  *
15  * Revision 1.4  1994/10/04  17:46:44  adam
16  * Function options now returns arg with error option.
17  *
18  * Revision 1.3  1994/10/03  17:22:19  adam
19  * Optimization of grepper.
20  *
21  * Revision 1.2  1994/09/27  16:31:20  adam
22  * First version of grepper: grep with error correction.
23  *
24  * Revision 1.1  1994/09/26  10:16:55  adam
25  * First version of dfa module in alex. This version uses yacc to parse
26  * regular expressions. This should be hand-made instead.
27  *
28  *
29  * Adam Dickmeiss.      1992-1993
30  * This module is actually very old...
31  */
32 #include <stdio.h>
33 #include <assert.h>
34
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stdarg.h>
38
39 #include <util.h>
40 #include <dfa.h>
41 #include "imalloc.h"
42 #include "lexer.h"
43
44 static char *prog;
45
46
47 void error (const char *format, ...)
48 {
49     va_list argptr;
50     va_start (argptr, format);
51     fprintf (stderr, "%s error: ", prog);
52     (void) vfprintf (stderr, format, argptr);
53     putc ('\n', stderr);
54     exit (1);
55 }
56
57 #ifdef YACC
58 extern int yydebug;
59 #else
60 extern int alexdebug;
61 #endif
62 int ccluse = 0;
63
64 static int lexer_options (int argc, char **argv)
65 {
66     while (--argc > 0)
67         if (**++argv == '-')
68             while (*++*argv)
69             {
70                 switch (**argv)
71                 {
72                 case 'V':
73                     fprintf (stderr, "%s: %s %s\n", prog, __DATE__, __TIME__);
74                     continue;
75                 case 's':
76                     dfa_verbose = 1;
77                     continue;
78                 case 't':
79 #ifdef YACC
80                     yydebug = 1;
81 #else
82                     alexdebug = 1;
83 #endif
84                     continue;
85                 case 'c':
86                     ccluse = 1;
87                     continue;
88                 case 'd':
89                     switch (*++*argv)
90                     {
91                     case 's':
92                         debug_dfa_tran = 1;
93                         break;
94                     case 't':
95                         debug_dfa_trav = 1;
96                         break;
97                     case 'f':
98                         debug_dfa_followpos = 1;
99                         break;
100                     default:
101                         --*argv;
102                         debug_dfa_tran = 1;
103                         debug_dfa_followpos = 1;
104                         debug_dfa_trav = 1;
105                     }
106                     continue;
107                 default:
108                     fprintf (stderr, "%s: unknown option `-%s'\n",
109                              prog, *argv);
110                     return 1;
111                 }
112                 break;
113             }
114     return 0;
115 }
116
117 int main (int argc, char **argv)
118 {
119     int i, no = 0;
120     struct DFA *dfa;
121
122     prog = *argv;
123 #ifdef YACC
124     yydebug = 0;
125 #else
126     alexdebug = 0;
127 #endif
128     dfa = dfa_init ();
129     i = lexer_options (argc, argv);
130     if (i)
131         return i;
132
133     if (argc < 2)
134     {
135         fprintf (stderr, "usage\n  %s [-c] [-V] [-s] [-t] [-d[stf]] file\n",
136                  prog);
137         return 1;
138     }
139     else while (--argc > 0)
140         if (**++argv != '-' && **argv)
141         {
142             ++no;
143             
144             i = read_file (*argv, dfa);
145             if (i)
146                 return i;
147             dfa_mkstate (dfa);
148
149 #ifdef MEMDEBUG
150     imemstat();
151 #endif
152         }
153     dfa_delete (&dfa);
154 #ifdef MEMDEBUG
155     imemstat();
156 #endif
157     if (!no)
158     {
159         fprintf (stderr, "%s: no files specified\n", prog);
160         return 2;
161     }
162     return 0;
163 }
164