2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.2 1994-09-26 16:30:57 adam
8 * Minor changes. imalloc uses xmalloc now.
10 * Revision 1.1 1994/09/26 10:16:56 adam
11 * First version of dfa module in alex. This version uses yacc to parse
12 * regular expressions. This should be hand-made instead.
30 static const char *inf_name;
37 read_rules (DFA **dfap),
43 static void prep (char **s)
45 static char expr_buf[MAXLINE+1];
57 static char *read_line (void)
59 static char linebuf[MAXLINE+1];
61 return fgets( linebuf, MAXLINE, inf );
64 static void read_defs (void)
67 while( (s=read_line()) )
69 if( *s == '%' && s[1] == '%' )
71 else if( *s == '\0' || isspace( *s ) )
74 error( "missing rule section" );
77 static void read_rules (DFA **dfap)
84 *dfap = dfa = init_dfa();
85 fputs( "\n#ifndef YY_BREAK\n#define YY_BREAK break;\n#endif\n", outf );
86 fputs( "void lexact( int no )\n{\n", outf );
87 fputs( "\tswitch( no )\n\t{\n", outf );
89 while( (s=read_line()) )
91 if( *s == '%' && s[1] == '%' )
93 else if( *s == '\0' || isspace( *s ) )
94 /* copy rest of line to output */
98 /* preprocess regular expression */
100 /* now parse regular expression */
102 i = parse_dfa( dfa, &s, dfa_ccl_chars );
104 i = parse_dfa( dfa, &s, dfa_thompson_chars );
107 fputs( "\t\tYY_BREAK\n", outf );
108 fprintf( outf, "\tcase %d:\n#line %d\n\t\t", dfa->rule, line_no );
111 fprintf( stderr, "%s #%d: regular expression syntax error\n",
115 else if( !dfa->root )
116 dfa->root = dfa->top;
121 n->u.p[0] = dfa->root;
122 n->u.p[1] = dfa->top;
125 while( *s == '\t' || *s == ' ' )
130 fputs( "\tYY_BREAK\n\t}\n}\n", outf );
132 error( "no regular expressions in rule section" );
135 static void read_tail (void)
138 while( (s=read_line()) )
142 int read_file (const char *s, DFA **dfap)
145 if( !(inf=fopen( s,"r" )) )
147 error( "cannot open `%s'", s );
151 if( !(outf=fopen( "lex.yy.c", "w" )) )
153 error( "cannot open `%s'", "lex.yy.c" );