bootlex.l 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. %{
  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. * Author: Hans van Staveren
  7. */
  8. #define MAXBACKUP 50
  9. #include <stdio.h>
  10. #include <stdarg.h>
  11. #include <assert.h>
  12. #include <em_spec.h>
  13. #include <em_flag.h>
  14. #include <em_reg.h>
  15. #include "booth.h"
  16. #include "y.tab.h"
  17. int lineno = 1;
  18. extern char *iname;
  19. %}
  20. %p 2000
  21. %%
  22. "/*" { char c;
  23. c = input();
  24. do {
  25. while (c!='*')
  26. c = input();
  27. c = input();
  28. } while (c!='/');
  29. }
  30. "REGISTERS:" return(REGISTERHEAD);
  31. "TOKENS:" return(TOKENHEAD);
  32. "TOKENEXPRESSIONS:" return(EXPRESSIONHEAD);
  33. "CODE:" return(CODEHEAD);
  34. "MOVES:" return(MOVEHEAD);
  35. "TESTS:" return(TESTHEAD);
  36. "STACKS:" return(STACKHEAD);
  37. "SIZEFACTOR" return(SIZEFAC);
  38. "TIMEFACTOR" return(TIMEFAC);
  39. "FORMAT" return(FORMAT);
  40. "cost" return(COST);
  41. "remove" return(REMOVE);
  42. "|" return(SEP);
  43. "samesign" return(SAMESIGN);
  44. "inreg" return(INREG);
  45. "sfit" return(SFIT);
  46. "ufit" return(UFIT);
  47. "defined" return(DEFINED);
  48. "rom" return(ROM);
  49. "loww" return(LOWW);
  50. "highw" return(HIGHW);
  51. "move" return(MOVE);
  52. "erase" return(ERASE);
  53. "allocate" return(ALLOCATE);
  54. "tostring" return(TOSTRING);
  55. "nocc" return(NOCC);
  56. "setcc" return(SETCC);
  57. "samecc" return(SAMECC);
  58. "test" return(TEST);
  59. "STACK" return(STACK);
  60. "nocoercions" return(NOCOERC);
  61. "&&" return(AND2);
  62. "||" return(OR2);
  63. "==" return(CMPEQ);
  64. "!=" return(CMPNE);
  65. "<=" return(CMPLE);
  66. "<" return(CMPLT);
  67. ">" return(CMPGT);
  68. ">=" return(CMPGE);
  69. ">>" return(RSHIFT);
  70. "<<" return(LSHIFT);
  71. "!" return(NOT);
  72. "~" return(COMP);
  73. "..." return(ELLIPS);
  74. EM_WSIZE { yylval.yy_intp = &wsize; return(CIDENT); }
  75. EM_PSIZE { yylval.yy_intp = &psize; return(CIDENT); }
  76. EM_BSIZE { yylval.yy_intp = &bsize; return(CIDENT); }
  77. REGISTER { yylval.yy_string = "REGISTER"; return(TYPENAME); }
  78. INT { yylval.yy_string = "INT"; return(TYPENAME); }
  79. STRING { yylval.yy_string = "STRING"; return(TYPENAME); }
  80. regvar return(REGVAR);
  81. loop return(LOOP);
  82. pointer return(POINTER);
  83. float return(FLOAT);
  84. return return(RETURN);
  85. [_A-Za-z][_A-Za-z0-9]+ {register ident_p ip;
  86. if(!lookident || (ip=ilookup(yytext,JUSTLOOKING))==0) {
  87. yylval.yy_string = scopy(yytext);return(IDENT);
  88. } else {
  89. yylval.yy_ident = ip;
  90. switch(ip->i_type) {
  91. default:assert(0);
  92. case IREG:return(RIDENT);
  93. case IPRP:return(PIDENT);
  94. case ITOK:return(TIDENT);
  95. case IEXP:return(EIDENT);
  96. }
  97. }
  98. }
  99. [a-z] {yylval.yy_char = yytext[0]; return(LCASELETTER);}
  100. [0-9]* {yylval.yy_int = atoi(yytext);return(NUMBER);}
  101. (\"|"%)") { char *p; int c,tipe; char stringbuf[BUFSIZ];
  102. p=stringbuf;
  103. for (;;) {
  104. c = input();
  105. switch(c) {
  106. default: *p++=c;break;
  107. case '\\':
  108. *p++=c; *p++=input(); break;
  109. case '\n':
  110. yyerror("Unterminated string");
  111. unput(c);
  112. /* fall through */
  113. case '"':
  114. tipe=STRING; goto endstr;
  115. case '%':
  116. c=input();
  117. if (c == '(') {
  118. tipe=LSTRING;goto endstr;
  119. } else {
  120. *p++ = '%'; unput(c); break;
  121. }
  122. }
  123. }
  124. endstr:
  125. *p++ = 0;
  126. yylval.yy_string = scopy(stringbuf);
  127. return(tipe);
  128. }
  129. ^\#(line)?[ \t]*[0-9]+[ \t]+\".*\".*$ {
  130. int ind,ind2;
  131. for (ind=0; yytext[ind] < '0' || yytext[ind]>'9'; ind++)
  132. ;
  133. lineno=atoi(&yytext[ind])-1;
  134. for(;yytext[ind]!='"';ind++)
  135. ;
  136. for(ind2=ind+1;yytext[ind2]!='"';ind2++)
  137. ;
  138. yytext[ind2]=0;
  139. if (!iname || strcmp(yytext+ind+1,iname)!=0)
  140. iname=scopy(yytext+ind+1);
  141. }
  142. [ \t]* ;
  143. \n { lineno++; }
  144. . return(yytext[0]);
  145. %%
  146. void yyerror(string s, ...)
  147. {
  148. va_list va;
  149. va_start(va, s);
  150. fprintf(stderr,"\"%s\", line %d:",iname ? iname : "",lineno);
  151. vfprintf(stderr, s, va);
  152. va_end(va);
  153. fprintf(stderr,"\n");
  154. nerrors++;
  155. }