Untitled

 avatar
unknown
plain_text
20 days ago
655 B
2
Indexable
%{
#include <stdio.h>
#include <stdlib.h>

int result = 0;
int temp;
char op = '+';
%}

%%
[0-9]+     { 
              temp = atoi(yytext);
              switch(op) {
                  case '+': result += temp; break;
                  case '-': result -= temp; break;
                  case '*': result *= temp; break;
                  case '/': result /= temp; break;
              }
           }

[\+\-\*/]  { op = yytext[0]; }
[ \t\n]    { /* Ignore whitespace */ }
.          { printf("Invalid character: %s\n", yytext); }
%%

int main() {
    printf("Enter arithmetic expression: ");
    yylex();
    printf("Result: %d\n", result);
    return 0;
}
Editor is loading...
Leave a Comment