lex.l 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /* $Header$ */
  2. /*
  3. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  4. * See the copyright notice in the ACK home directory, in the file "Copyright".
  5. */
  6. %{
  7. /* lex.l */
  8. # include <ctype.h>
  9. # include "token.h"
  10. # include "Lpars.h"
  11. # define TAB 8 /* Size of a acsii tab (\t) in spaces */
  12. # if (TAB&(TAB-1))!=0
  13. # define TABSTOP(ind) ((ind)+TAB-(ind)%TAB)
  14. # else
  15. # define TABSTOP(ind) (((ind)+TAB)&(~(TAB-1)))
  16. # endif
  17. char *malloc(), *strcpy();
  18. struct token token;
  19. int ind=0; /* Indentation level of current line */
  20. static int tab=0; /* First indentation found */
  21. int included=0; /* Is current file included? */
  22. %}
  23. %%
  24. '((\*[^\n])|([^'\n*]))*' {
  25. if ((token.t_lval=char_constant(yytext+1))== -1L)
  26. report("%s not a character constant", yytext);
  27. return CHAR_CONST;
  28. }
  29. '[^'\n]*'? {
  30. report("missing '.");
  31. token.t_lval= -1L;
  32. return CHAR_CONST;
  33. }
  34. \"((\*[^\n])|([^"\n*]))*\" {
  35. char *string();
  36. token.t_sval=string(yytext);
  37. return STRING;
  38. }
  39. \"[^"\n]*\"? {
  40. report("missing \".");
  41. token.t_sval="";
  42. return STRING;
  43. }
  44. #[ \t]*"line"?[ \t]*[0-9]+[ \t]*\"[^"\n]*\" {
  45. set_line_file(yytext);
  46. tab=0;
  47. }
  48. #[A-Fa-f0-9]+ {
  49. long hex_number();
  50. token.t_lval=hex_number(yytext+1);
  51. return NUMBER;
  52. }
  53. [0-9]+ {
  54. long number();
  55. token.t_lval=number(yytext);
  56. return NUMBER;
  57. }
  58. [A-Za-z][A-Za-z0-9.]* {
  59. register key;
  60. if ((key=keyword(yytext))==IDENTIFIER)
  61. token.t_sval=strcpy(malloc(yyleng+1), yytext);
  62. return key;
  63. }
  64. \n[ \f\t]*/"--" {/* Line with only a comment, don't set tab */}
  65. \n[ \f\t]* {
  66. ind=indentation(yytext+1);
  67. if (tab==0)
  68. tab=ind;
  69. else
  70. if (ind%tab!=0)
  71. warning("indentation not on a %d space boundary", tab);
  72. }
  73. [ \f\t] { /* Nothing */ }
  74. [-=<>:,;+*/\[\]()?!&] return yytext[0];
  75. "\\" return BS;
  76. ":=" return AS;
  77. "<=" return LE;
  78. ">=" return GE;
  79. "<>" return NE;
  80. "<<" return LS;
  81. ">>" return RS;
  82. "/\\" return BA;
  83. "\\/" return BO;
  84. "><" return BX;
  85. "--"[^\n]* { /* Comment is skipped */ }
  86. . {
  87. warning((' '<=yytext[0] && yytext[0]<0177) ? "%s'%c')" : "%soctal: %o)",
  88. "bad character seen (", yytext[0]&0377);
  89. }
  90. %%
  91. char *string(s) char *s;
  92. {
  93. register c;
  94. register char *p= s;
  95. char *str= s;
  96. str++; p++;
  97. while (*str != '"') {
  98. if ((c=character(&str)) != -1)
  99. *p++= c;
  100. else
  101. return "";
  102. }
  103. *p=0;
  104. *s=p-(s+1);
  105. return s;
  106. }
  107. long number(s) register char *s;
  108. {
  109. static char max_str[]="2147483647";
  110. int maxlen=sizeof max_str-1;
  111. long atol();
  112. long num;
  113. while (*s=='0') { /* skip leading nulls */
  114. *s++;
  115. yyleng--;
  116. }
  117. if (*s==0)
  118. num=0L;
  119. else {
  120. if ((yyleng>maxlen) || (yyleng==maxlen && strcmp(s, max_str)>0))
  121. warning("integer constant overflow.");
  122. num=atol(s);
  123. }
  124. return num;
  125. }
  126. long hex_number(s) register char *s;
  127. {
  128. long number=0L;
  129. while (*s)
  130. number=(number<<4)+hextoint(*s++);
  131. return number;
  132. }
  133. int hextoint(c) register c;
  134. {
  135. register val;
  136. if (islower(c))
  137. val=(c-'a')+10;
  138. else
  139. if (isupper(c))
  140. val=(c-'A')+10;
  141. else
  142. val=c-'0';
  143. return val;
  144. }
  145. int character(S) register char **S;
  146. {
  147. register char *s= *S;
  148. register c, cc;
  149. if ((c= *s++)=='*') {
  150. switch (c= *s++) {
  151. case 'c':
  152. cc='\r';
  153. break;
  154. case 'n':
  155. cc='\n';
  156. break;
  157. case 't':
  158. cc='\t';
  159. break;
  160. case 's':
  161. cc=' ';
  162. break;
  163. case '#':
  164. if (isxdigit(c= *s++) && isxdigit(*s)) {
  165. cc= (hextoint(c)<<4)+hextoint(*s++);
  166. break;
  167. } else {
  168. report("two digit hexadecimal const expected.");
  169. return -1;
  170. }
  171. default:
  172. cc=c;
  173. break;
  174. }
  175. } else
  176. cc=c;
  177. *S=s;
  178. return cc;
  179. }
  180. int char_constant(s) char *s;
  181. {
  182. register cc;
  183. cc=character(&s);
  184. return (*s=='\'' && cc!= -1) ? cc : -1;
  185. }
  186. int indentation(s) register char *s;
  187. {
  188. register in=0, c;
  189. while (c= *s++) {
  190. if (c=='\t')
  191. in=TABSTOP(in);
  192. else
  193. if (c=='\f')
  194. in=0;
  195. else
  196. in++;
  197. }
  198. return in;
  199. }
  200. int tabulated(oind, ind) register oind, ind;
  201. {
  202. if (tab>0 && ind>oind+tab)
  203. warning("process' indentation too large (changed to %d tab%s)",
  204. oind/tab+1, oind>=tab ? "s" : "");
  205. return ind>oind;
  206. }
  207. int rep_tk=0;
  208. struct token rep_token;
  209. void repeat_token(tk)
  210. {
  211. rep_tk=tk;
  212. rep_token=token;
  213. }
  214. scanner()
  215. {
  216. register tk;
  217. if (rep_tk>0) {
  218. tk=rep_tk;;
  219. rep_tk=0;
  220. token=rep_token;
  221. return tk;
  222. } else
  223. return yylex();
  224. }
  225. char *tokenname(tk, inst) register tk, inst;
  226. {
  227. if (tk<0400) {
  228. static char c[7];
  229. if (' '<tk && tk<='~')
  230. sprint(c, "'%c'", tk);
  231. else
  232. sprint(c, "'*#%02x'", tk);
  233. return c;
  234. } else {
  235. switch (tk) {
  236. char *keyname();
  237. char fake_id[1+sizeof(int)*3+1];
  238. static fake_cnt=0;
  239. default:
  240. return keyname(tk);
  241. case IDENTIFIER:
  242. if (inst) {
  243. sprint(fake_id, "_%d", ++fake_cnt);
  244. token.t_sval=strcpy(malloc(strlen(fake_id)+1),
  245. fake_id);
  246. return "IDENTIFIER";
  247. } else
  248. return token.t_sval;
  249. case NUMBER:
  250. case CHAR_CONST:
  251. token.t_lval=0L;
  252. return "NUMBER";
  253. case STRING:
  254. if (inst) {
  255. token.t_sval=malloc(1);
  256. token.t_sval[0]=0;
  257. } else
  258. free(token.t_sval);
  259. return "STRING";
  260. case AS: case LE: case GE: case NE:
  261. case LS: case RS: case BA: case BO:
  262. case BX: case BS: {
  263. static int op[]= {
  264. AS, LE, GE, NE, LS, RS, BA, BO, BX, BS
  265. };
  266. static char *opc[]= {
  267. ":=", "<=", ">=", "<>", "<<", ">>", "/\\",
  268. "\\/", "><", "\\"
  269. };
  270. register i;
  271. static char qopc[5];
  272. for (i=0; op[i]!=tk; i++) ;
  273. sprint(qopc, "'%s'", opc[i]);
  274. return qopc;
  275. }
  276. }
  277. }
  278. }
  279. set_line_file(l) register char *l;
  280. {
  281. register char *file;
  282. while (*l<'0' || *l>'9') l++;
  283. yylineno=0;
  284. while ('0'<=*l && *l<='9')
  285. yylineno=yylineno*10+(*l++ - '0');
  286. yylineno--;
  287. while (*l++!='"');
  288. file=l;
  289. while (*l++!='"');
  290. *--l=0;
  291. included=set_file(file);
  292. }