 %{
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

// Declare yylval as a union type to hold numbers (doubles)
union {
    double num;
} yylval;

// Function to handle errors
void yyerror(const char *s);
%}

%option noyywrap

DIGIT [0-9]
NUMBER   {DIGIT}+(\.{DIGIT}+)?
ID      [a-zA-Z_][a-zA-Z0-9_]*
%%

"1"   { return 1; }
"2"   { return 2; }
"3"   { return 3; }
"4"   { return 4; }
"5"   { return 5; }
"6"   { return 6; }
"7"   { return 7; }
"+"   { return '+'; }
"-"   { return '-'; }
"*"   { return '*'; }
"SQRT" { return 'S'; }
"CQRT" { return 'C'; }
{NUMBER}  { yylval.num = atof(yytext); return 'N'; }
.   { return yytext[0]; }

%%

// Main function to interact with the user and execute the chosen operation
int main() {
    int choice;
    double num1, num2, result;

    printf("Select an operation:\n");
    printf("1. Addition (+)\n");
    printf("2. Subtraction (-)\n");
    printf("3. Multiplication (*)\n");
    printf("4. Square Root (SQRT)\n");
    printf("5. Cube Root (CQRT)\n");
    printf("6. Exit\n");

    // Read the user's choice
    scanf("%d", &choice);
   
    switch(choice) {
        case 1: // Addition
            printf("Enter two numbers: ");
            scanf("%lf %lf", &num1, &num2);
            result = num1 + num2;
            printf("Result: %.2f\n", result);
            break;

        case 2: // Subtraction
            printf("Enter two numbers: ");
            scanf("%lf %lf", &num1, &num2);
            result = num1 - num2;
            printf("Result: %.2f\n", result);
            break;

        case 3: // Multiplication
            printf("Enter two numbers: ");
            scanf("%lf %lf", &num1, &num2);
            result = num1 * num2;
            printf("Result: %.2f\n", result);
            break;

        case 4: // Square Root
            printf("Enter a number: ");
            scanf("%lf", &num1);
            if (num1 < 0) {
                printf("Error: Cannot take square root of a negative number.\n");
            } else {
                result = sqrt(num1);
                printf("Square Root: %.2f\n", result);
            }
            break;

        case 5: // Cube Root
            printf("Enter a number: ");
            scanf("%lf", &num1);
            result = cbrt(num1);
            printf("Cube Root: %.2f\n", result);
            break;

        case 6: // Exit
            printf("Exiting the program.\n");
            return 0;

        default:
            printf("Invalid choice!\n");
            break;
    }

    return 0;
}

// Error handling function
void yyerror(const char *s) {
    fprintf(stderr, "%s\n", s);
    exit(1);