1 /* ------------------------------------------------
2 Copyright 2014 AT&T Intellectual Property
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
7 http://www.apache.org/licenses/LICENSE-2.0
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ------------------------------------------- */
19 flex -PResParser -oreslexer.cc res.l
25 * AT&T lex can't handle this lexer due to lex bugs. It works with flex
26 * 2.3.7, pclex 2.0.5, and MKS lex 3.1a.
29 #include "parse_fta.h"
34 #include "res.tab.cc.h"
37 Some includes that flex doesn't include as standard,
45 // Prevent flex from defining yywrap as extern "C"
47 #define YY_SKIP_YYWRAP
49 /* No lex lib, supply the yywrap fcn. that normally resides there
52 int ResParserwrap(){return(1);}
54 extern int ResParserdebug;
58 These variables are used for error reporting:
59 flex_res_lineno : the line currently being parsed when the error occurs.
60 flex_res_ch : the character on the line where the error occurs
61 flex_res_linebuf : store the line for reporting.
63 NOTE : 1) the fixed size flex_res_linebuf buffer is dangerous.
64 2) You might get pointed to a place shortly after
65 where the syntax error occurs. It is close enough
69 int flex_res_lineno = 1;
71 char flex_res_linebuf[20000];
73 char *flex_res_stringinput = NULL;
74 int flex_res_stringinput_ptr = 0;
75 FILE *flex_res_fileinput = NULL;
76 int my_ResParser_yyinput(char *buf, int max_size);
80 void ResParsererror(char *s){
82 fprintf(stderr,"On line %d, char %d: %s (token %s):\n%s\n",
83 flex_res_lineno, flex_res_ch, s, ResParsertext, flex_res_linebuf );
84 for(i=0;i<flex_res_ch;i++){
85 if(flex_res_linebuf[i] == '\t'){
91 fprintf(stderr,"^\n");
92 // fprintf(stderr,"%*s\n",1+flex_res_ch,"^");
96 #define YY_INPUT(b, r, ms) (r = my_ResParser_yyinput(b,ms))
99 /* MKS needs the next line to increase the NFA table */
105 /* literal keyword tokens */
108 The actions associated with each text token are to
109 keep track of the current location (for syntax error reporting)
110 and to report any necessary info to the emf.y parse tree builder
112 Its likely that there are a number of omissions, inconsistencies
113 (some keywords do not need to be in caps), and relics
114 (keywords such as BETWEEN, INDICATOR, etc., are not used
116 This parser is somewhat of a work in progress.
124 [<>=/] { flex_res_ch+=ResParserleng; return yytext[0]; }
128 [A-Za-z_][A-Za-z0-9_]* { flex_res_ch+=ResParserleng; ResParserlval.strval = strdup(yytext); return NAME; }
137 ResParserlval.strval = strdup(ResParsertext+1);
141 unput(c); /* just peeking */
143 flex_res_ch+=ResParserleng;
144 ResParserlval.strval[ResParserleng-2] = '\0';
150 '[^'\n]*$ { flex_res_ch+=ResParserleng; ResParsererror("Unterminated string"); }
153 /* Newline : advance the error reporting line number */
154 /* and grab the next line into flex_res_linebuf */
157 \n.* {flex_res_ch=0; flex_res_lineno++;
158 strcpy(flex_res_linebuf,ResParsertext+1);
162 [ \t\r]+ {flex_res_ch+=ResParserleng; } /* white space */
164 "--".*$ {flex_res_ch+=ResParserleng; }; /* comment */
165 "//".*$ {flex_res_ch+=ResParserleng; }; /* comment */
166 "<?xml".*$ {flex_res_ch+=ResParserleng; }; /* comment */
168 .|\n {flex_res_ch+=ResParserleng; fprintf(stderr,"Warning: unknown token (ignored)\n"); ResParsererror(yytext);}
172 int my_ResParser_yyinput(char *buf, int max_size){
176 if(flex_res_stringinput != NULL){
178 if(flex_res_stringinput[flex_res_stringinput_ptr] != '\0'){
179 buf[c++] = flex_res_stringinput[flex_res_stringinput_ptr++];
187 if(flex_res_fileinput != NULL){
189 inchar = getc(flex_res_fileinput);
202 void ResParser_setfileinput(FILE *f){
203 ResParserrestart(NULL);
205 flex_res_fileinput = f;
206 flex_res_stringinput = NULL;
211 void ResParser_setstringinput(char *s){
212 ResParserrestart(NULL);
214 flex_res_fileinput = NULL;
215 flex_res_stringinput = s;
216 flex_res_stringinput_ptr = 0;