Various cleanup. YAZ util used instead.
[idzebra-moved-to-github.git] / dfa / readfile.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: readfile.c,v $
7  * Revision 1.5  1995-09-04 12:33:27  adam
8  * Various cleanup. YAZ util used instead.
9  *
10  * Revision 1.4  1995/01/25  11:30:51  adam
11  * Simple error reporting when parsing regular expressions.
12  * Memory usage reduced.
13  *
14  * Revision 1.3  1995/01/24  16:00:22  adam
15  * Added -ansi to CFLAGS.
16  * Some changes to the dfa module.
17  *
18  * Revision 1.2  1994/09/26  16:30:57  adam
19  * Minor changes. imalloc uses xmalloc now.
20  *
21  * Revision 1.1  1994/09/26  10:16:56  adam
22  * First version of dfa module in alex. This version uses yacc to parse
23  * regular expressions. This should be hand-made instead.
24  *
25  */
26 #include <stdio.h>
27 #include <assert.h>
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include <ctype.h>
32
33 #include <alexutil.h>
34 #include <dfa.h>
35 #include "lexer.h"
36
37 #define MAXLINE 512
38
39 static FILE *inf;
40 static FILE *outf;
41 static const char *inf_name;
42 static int line_no;
43 static int err_no;
44
45 static void
46     prep        (char **s),
47     read_defs   (void),
48     read_rules  (struct DFA *dfap),
49     read_tail   (void);
50
51 static char
52     *read_line  (void);
53
54 static void prep (char **s)
55 {
56     static char expr_buf[MAXLINE+1];
57     char *dst = expr_buf;
58     const char *src = *s;
59     int c;
60
61     while ((c = *src++))
62         *dst++ = c;
63
64     *dst = '\0';
65     *s = expr_buf;
66 }
67
68 static char *read_line (void)
69 {
70     static char linebuf[MAXLINE+1];
71     ++line_no;
72     return fgets (linebuf, MAXLINE, inf);
73 }
74
75 static void read_defs (void)
76 {
77     const char *s;
78     while ((s=read_line()))
79     {
80         if (*s == '%' && s[1] == '%')
81             return;
82         else if (*s == '\0' || isspace (*s))
83             fputs (s, outf);
84     }
85     error ("missing rule section");
86 }
87
88 static void read_rules (struct DFA *dfa)
89 {
90     char *s;
91     int i;
92     int no = 0;
93
94     fputs ("\n#ifndef YY_BREAK\n#define YY_BREAK break;\n#endif\n", outf);
95     fputs ("void lexact (int no)\n{\n", outf);
96     fputs (  "\tswitch (no)\n\t{\n", outf);
97     while ((s=read_line()))
98     {
99         if (*s == '%' && s[1] == '%')
100             break;
101         else if (*s == '\0' || isspace (*s))
102             /* copy rest of line to output */
103             fputs (s, outf);
104         else
105         { 
106             /* preprocess regular expression */
107             prep (&s);                   
108             /* now parse regular expression */
109             i = dfa_parse (dfa, &s);
110             if (i)
111             {
112                 fprintf (stderr, "%s #%d: regular expression syntax error\n",
113                         inf_name, line_no);
114                 assert (0);
115                 err_no++;
116             }
117             else
118             {
119                 if (no)
120                     fputs ("\t\tYY_BREAK\n", outf);
121                 no++;
122                 fprintf (outf, "\tcase %d:\n#line %d\n\t\t", no, line_no);
123             }
124             while (*s == '\t' || *s == ' ')
125                 s++;
126             fputs (s, outf);
127         }
128     }
129     fputs ("\tYY_BREAK\n\t}\n}\n", outf);
130     if (!no)
131         error ("no regular expressions in rule section");
132 }
133
134 static void read_tail (void)
135 {
136     const char *s;
137     while ((s=read_line()))
138         fputs (s, outf);
139 }
140
141 int read_file (const char *s, struct DFA *dfa)
142 {
143     inf_name = s;
144     if (!(inf=fopen (s,"r")))
145     {
146         error ("cannot open `%s'", s);
147         return -1;
148     }
149
150     if (!(outf=fopen ("lex.yy.c", "w")))
151     {
152         error ("cannot open `%s'", "lex.yy.c");
153         return -2;
154     }
155
156     line_no = 0;
157     err_no = 0;
158
159     read_defs ();
160     read_rules (dfa);
161     read_tail ();
162
163     fclose (outf);
164     fclose (inf);
165     return err_no;
166 }