Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** 171517
- * calculator calc.l
- * origin source by Tom Niemann at epaperpress.com/lexandyacc
- * revision Lorenzo Massimo Gramola (2014)
- * revision Lorenzo Massimo Gramola (2015)
- * revision Lorenzo Massimo Gramola (2016)
- */
- %{
- #include <stdlib.h>
- #include "calc.h"
- #include "y.tab.h"
- void yyerror(char *);
- %}
- %%
- "while" return WHILE;
- "if" return IF;
- "else" return ELSE;
- "print" return PRINT;
- [a-z][a-z0-9]* {
- int l=0;
- int i=0;
- for(; yytext[l] != '\0'; l++){
- }
- yylval.sIndex = malloc((l+1)*sizeof(char));
- for(i=0; yytext[i] != '\0'; i++){
- yylval.sIndex[i]=yytext[i];
- }
- yylval.sIndex[l]='\0';
- return VARIABLE;
- }
- 0 {
- yylval.iValue = atoi(yytext);
- return INTEGER;
- }
- [1-9][0-9]* {
- yylval.iValue = atoi(yytext);
- return INTEGER;
- }
- [-+()<>=*/;{}.@] { return *yytext; }
- ">=" return GE;
- "<=" return LE;
- "==" return EQ;
- "!=" return NE;
- [ \t\n] ;/* skip whitespace */
- . yyerror("Unknown character");
- %%
- int yywrap(void) {
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement