Untitled

 avatar
unknown
plain_text
10 months ago
3.7 kB
12
Indexable
#include<stdio.h>
#include<string.h>
#include<ctype.h>
char input[10];
int i,error;
void t();
void tprime();
void f();
void e();
void eprime();
int main()
{
i=0;
error=0;
printf("enter an arithmetic exprn:");
scanf("%s",input);
e();
if(strlen(input)==i && error==0)
printf("\naccepted  ...\n");
else
printf("rejected..\n");
return 0;
}
void e()
{
t();
eprime();
}
void eprime()
{
if(input[i]=='+')
{
i++;
t();
eprime();
}
}
void t()
{
f();
tprime();
}
void tprime()
{
if(input[i]=='*')
{
i++;
f();
tprime();
}
}
void f()
{
if(isalnum(input[i]))
i++;
else if(input[i]=='(')
{
i++;
e();
if(input[i]==')')
i++;
else 
error=1;
}
}








#include <stdio.h>
#include <string.h>
#include <ctype.h>

char input[50][10];   
char stack[50][10];   
int top = -1;
int n = 0;            


void printStack() {
    for (int j = 0; j <= top; j++)
        printf("%s", stack[j]);
}


int reduce() {
    int change = 0;

    
    if (top >= 2 && strcmp(stack[top - 2], "E") == 0 &&
        strcmp(stack[top - 1], "+") == 0 &&
        strcmp(stack[top], "E") == 0) {
        strcpy(stack[top - 2], "E");
        top -= 2;
        printf("REDUCE BY E -> E+E\n");
        change = 1;
    }

   
    else if (top >= 2 && strcmp(stack[top - 2], "E") == 0 &&
             strcmp(stack[top - 1], "*") == 0 &&
             strcmp(stack[top], "E") == 0) {
        strcpy(stack[top - 2], "E");
        top -= 2;
        printf("REDUCE BY E -> E*E\n");
        change = 1;
    }

  
    else if (top >= 2 && strcmp(stack[top - 2], "(") == 0 &&
             strcmp(stack[top - 1], "E") == 0 &&
             strcmp(stack[top], ")") == 0) {
        strcpy(stack[top - 2], "E");
        top -= 2;
        printf("REDUCE BY E -> (E)\n");
        change = 1;
    }

   
    else if (top >= 0 && strcmp(stack[top], "id") == 0) {
        strcpy(stack[top], "E");
        printf("REDUCE BY E -> id\n");
        change = 1;
    }

    return change;
}


void tokenize(char* expr) {
    int i = 0;
    while (expr[i] != '\0') {
        if (isspace(expr[i])) {
            i++;
            continue;
        } else if (isalpha(expr[i])) {
            if (expr[i] == 'i' && expr[i + 1] == 'd') {
                strcpy(input[n++], "id");
                i += 2;
            } else {
                printf("Invalid identifier, only 'id' is allowed.\n");
                return;
            }
        } else if (strchr("()+*-/", expr[i])) {
            char temp[2];
            temp[0] = expr[i];
            temp[1] = '\0';
            strcpy(input[n++], temp);
            i++;
        } else {
            printf("Invalid character: %c\n", expr[i]);
            return;
        }
    }
}

int main() {
    char expr[100];
    printf("Enter the input string (use 'id' only): ");
    scanf("%s", expr);

    tokenize(expr);

    printf("\nStack\t\tInput\t\tAction\n");
    printf("--------------------------------------------------\n");

    int i = 0;
    while (i < n) {
       
        strcpy(stack[++top], input[i++]);

        printStack();
        printf("\t\t");
        for (int j = i; j < n; j++)
            printf("%s", input[j]);
        printf("\t\tSHIFT\n");


        while (reduce()) {
            printStack();
            printf("\t\t");
            for (int j = i; j < n; j++)
                printf("%s", input[j]);
            printf("\t\t");
            printf("\n");
        }
    }

    
    while (reduce()) {
        printStack();
        printf("\t\t\t\t\n");
    }

    
    if (top == 0 && strcmp(stack[top], "E") == 0) {
        printf("\nAccepted: Input string is successfully parsed.\n");
    } else {
        printf("\nRejected: Input string could not be parsed.\n");
    }

    return 0;
}
Editor is loading...
Leave a Comment