Skip to content

Latest commit

 

History

History
64 lines (52 loc) · 1.08 KB

.md

File metadata and controls

64 lines (52 loc) · 1.08 KB

11. Demonstrate a C program to implement operator precedence parsing.

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

void main()
{
    char grammar[20][20], c;
    int i, n, j = 2, flag = 0;

    printf("Enter the no of productions: ");
    scanf("%d", &n);

    printf("Enter the productions: \n");
    for (i = 0; i < n; i++)
        scanf("%s", grammar[i]);

    for (i = 0; i < n; i++)
    {
        c = grammar[i][2];

        while (c != '\0')
        {
            if (grammar[i][3] == '+' || grammar[i][3] == '-' || grammar[i][3] == '*' || grammar[i][3] == '/')
                flag = 1;
            else
                flag = 0;

            if (c == '$')
                flag = 0;

            c = grammar[i][++j];
        }
    }

    if (flag == 1)
        printf("Operator grammar\n");
    else
        printf("Not operator grammar\n");
}

Output :-
Enter the no of productions: 3
Enter the productions: 
A=A*A
B=AA
A=$
Not operator grammar

Enter the no of productions: 2
Enter the productions: 
A=A/A 
B=A+A
Operator grammar