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