Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Make 1.49 KB | None | 0 0
  1. %{
  2. #include "y.tab.h"
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. #define lexeme strcpy((char *)malloc(yyleng+1),yytext)
  7.  
  8. %}
  9.     int line_count = 0;
  10.  
  11. DIGIT              [0-9]
  12. HEX        0x[0-9a-fA-F]+
  13. NUMBER         {DIGIT}+
  14. IDENTIFIER         [_a-zA-Z][_a-zA-Z0-9]*
  15. COMMENT_START      "/*"
  16. COMMENT_END    "*/"
  17. WHITESPACE     [ \t]+
  18. NEWLINE        "\n"
  19. IGNORE         {WHITESPACE}
  20. ANY        .
  21.  
  22.  
  23.  
  24. %x COMMENT
  25.  
  26. %%
  27.  
  28. {COMMENT_START}     BEGIN(COMMENT);
  29. <COMMENT>{COMMENT_END}  BEGIN(INITIAL);
  30. <COMMENT><<EOF>>    {
  31.                 fprintf(stderr, "Comment not Closed\n");
  32.                 exit(1);
  33.             }
  34. <COMMENT>{ANY}      /* ignore comments */
  35. {IGNORE}        /* ignore whitespace */
  36. {NEWLINE}       line_count++;
  37. <COMMENT>{NEWLINE}  line_count++;
  38. ;           return ';';
  39. \(          return '(';
  40. \)          return ')';
  41. \.          return '.';
  42. \-          return '-';
  43. \*          return '*';
  44. \<          return '<';
  45. =           return '=';
  46. ,           return ',';
  47. :=                      return L_ASSIGN;
  48. struct          return K_STRUCT;
  49. end         return K_END;
  50. method          return K_METHOD;
  51. var         return K_VAR;
  52. if          return K_IF;
  53. then            return K_THEN;
  54. else            return K_ELSE;
  55. while           return K_WHILE;
  56. do          return K_DO;
  57. return          return K_RETURN;
  58. not         return K_NOT;
  59. or          return K_OR;
  60. this            return K_THIS;
  61. {IDENTIFIER}        return IDENT; @{ @IDENT.name@ = lexeme; @}
  62. {NUMBER}        return NUM; @{ @NUM.val@ = strtol(yytext, (char**)NULL, 10); @}
  63. {HEX}           return NUM; @{ @NUM.val@ = strtol(yytext, (char**)NULL, 16); @}
  64. {ANY}           {
  65.                 fprintf(stderr, "Syntax-Error in Line %d\n\"%s\" not know\n\n", line_count, yytext);
  66.                 exit(1);
  67.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement