scan.l
上传用户:wqdrylgs
上传日期:2007-02-09
资源大小:65k
文件大小:3k
源码类别:

汇编语言

开发平台:

WINDOWS

  1. /****************************************************************************
  2. scan.l
  3. ParserWizard generated Lex file.
  4. ****************************************************************************/
  5. %{
  6. #include "globals.h"
  7. #include "util.h"
  8. #include "scan.h"
  9. #include "parse.h"
  10. char tokenString[MAXTOKENLEN+1];
  11. %}
  12. %include {
  13.     #define MAXTOKENLEN 40
  14. extern char tokenString[MAXTOKENLEN+1];
  15. TokenType getToken(void);
  16. }
  17. /////////////////////////////////////////////////////////////////////////////
  18. // declarations section
  19. letter      [_a-zA-Z]
  20. letnum [_a-zA-Z0-9]
  21. identifier  {letter}{letnum}*
  22. digit       [0-9]
  23. number      {digit}+
  24. fnumber     {number}"."{number}
  25. constchar '.'
  26. newline n
  27. whitespace  [ t]+
  28. %%
  29. /////////////////////////////////////////////////////////////////////////////
  30. // rules section
  31. "int"           {return INT;}
  32. "float"         {return FLOAT;}
  33. "char"          {return CHAR;}
  34. "void"          {return VOID;}
  35. "if"            {return IF;}
  36. "else"          {return ELSE;}
  37. "while"         {return WHILE;}
  38. "continue"      {return CONTINUE;}
  39. "break"         {return BREAK;}
  40. "return"        {return RETURN;}
  41. "+"             {return PLUS;}
  42. "-"             {return SUB;}
  43. "*"             {return MUT;}
  44. "/"             {return DIV;}
  45. "<"             {return LT;}
  46. "<="            {return LE;}
  47. ">"             {return GT;}
  48. ">="            {return GE;}
  49. "=="            {return EQ;}
  50. "!="            {return NEQ;}
  51. "&&"            {return AND;}
  52. "||"            {return OR;}
  53. "!"             {return NOT;}
  54. "="             {return ASSIGN;}
  55. ";"             {return SEMI;}
  56. ","             {return COMMA;}
  57. "("             {return LP;}
  58. ")"             {return RP;}
  59. "["             {return LSP;}
  60. "]"             {return RSP;}
  61. "{"             {return LFP;}
  62. "}"             {return RFP;}
  63. {identifier}    {return ID;}
  64. {number}        {return NUM;}
  65. {fnumber}       {return FNUM;}
  66. {constchar} {return SCHAR;}
  67. {newline} {lineno++;}
  68. {whitespace}    {}
  69. "//" { char c;
  70.   while ((c = yyinput()) != 'n')
  71.     if (c == EOF) return ENDFILE;
  72.   yyunput(c);
  73. }
  74. "/*"            { char c;
  75.                   int done = FALSE;
  76.                   do {
  77.                     while ((c = yyinput()) != '*')
  78.                       if (c == EOF) return ERROR;
  79.                     while ((c = yyinput()) == '*');
  80.                     if (c == '/') done = TRUE;
  81.                   } while (!done);
  82.                 }
  83. .               {return ERROR;}
  84. %%
  85. /////////////////////////////////////////////////////////////////////////////
  86. // programs section
  87. TokenType getToken(void)
  88. {
  89.     static int firstTime = TRUE;
  90.     TokenType currentToken;
  91.     if (firstTime) {
  92.         firstTime = FALSE;
  93.         lineno++;
  94.         yyin = source;
  95.         yyout = listing;
  96.     }
  97.     currentToken = yylex();
  98.     strncpy(tokenString, yytext, MAXTOKENLEN);
  99.     if (TraceScan) {
  100. lineno = yylineno;
  101.         fprintf(listing,"t%d: ",lineno);
  102.         printToken(currentToken,tokenString);
  103.     }
  104.     return currentToken;
  105. }