Advertisement
Salvioner

calc.l

Feb 6th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 KB | None | 0 0
  1. /** 171517
  2.  * calculator calc.l
  3.  * origin source by Tom Niemann at epaperpress.com/lexandyacc
  4.  * revision Lorenzo Massimo Gramola (2014)
  5.  * revision Lorenzo Massimo Gramola (2015)
  6.  * revision Lorenzo Massimo Gramola (2016)
  7.  */
  8. %{
  9.     #include <stdlib.h>
  10.     #include "calc.h"
  11.     #include "y.tab.h"
  12.  
  13.     void yyerror(char *);
  14. %}
  15.  
  16. %%
  17.  
  18. "while"         return WHILE;
  19. "if"            return IF;
  20. "else"          return ELSE;
  21. "print"         return PRINT;
  22.  
  23. [a-z][a-z0-9]*  {
  24.                     int l=0;
  25.                     int i=0;
  26.                     for(; yytext[l] != '\0'; l++){
  27.                     }
  28.                     yylval.sIndex = malloc((l+1)*sizeof(char));
  29.                     for(i=0; yytext[i] != '\0'; i++){
  30.                         yylval.sIndex[i]=yytext[i];
  31.                     }
  32.                     yylval.sIndex[l]='\0';
  33.                    
  34.                     return VARIABLE;
  35.                 }
  36. 0               {
  37.                     yylval.iValue = atoi(yytext);
  38.                     return INTEGER;
  39.                 }
  40. [1-9][0-9]*     {
  41.                     yylval.iValue = atoi(yytext);
  42.                     return INTEGER;
  43.                 }
  44.  
  45. [-+()<>=*/;{}.@]   { return *yytext; }
  46.  
  47. ">="            return GE;
  48. "<="            return LE;
  49. "=="            return EQ;
  50. "!="            return NE;
  51.  
  52.  
  53.  
  54. [ \t\n]         ;/* skip whitespace */
  55.  
  56. .               yyerror("Unknown character");
  57.  
  58. %%
  59.  
  60. int yywrap(void) {
  61.     return 1;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement