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