0% found this document useful (0 votes)
15 views4 pages

Lab4 D

operator presence compiler design lab

Uploaded by

Tushar Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Lab4 D

operator presence compiler design lab

Uploaded by

Tushar Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

OPERATOR PRECEDENCE

Code in C -
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

// Operator precedence table


char precedenceTable[9][9] = {
{'>', '>', '<', '<', '<', '<', '>', '<', '>'},
{'>', '>', '<', '<', '<', '<', '>', '<', '>'},
{'>', '>', '>', '>', '<', '<', '>', '<', '>'},
{'>', '>', '>', '>', '<', '<', '>', '<', '>'},
{'>', '>', '<', '<', '<', '<', '>', '<', '>'},
{'<', '<', '<', '<', '<', '<', '=', '<', 'E'},
{'>', '>', '>', '>', '>', 'E', '>', 'E', '>'},
{'>', '>', '>', '>', '>', 'E', '>', 'E', '>'},
{'<', '<', '<', '<', '<', '<', 'E', '<', 'A'}
};

char stack[30], tempStack[30];


int stackTop = -1, inputPointer = 0, tempTop = -1;

// Push element onto the stack


void pushToStack(char symbol) {
stack[++stackTop] = symbol;
}

// Pop element from the stack


char popFromStack() {
return stack[stackTop--];
}
// Function to determine index in the precedence table
int getPrecedenceIndex(char symbol) {
switch (symbol) {
case '+': return 0;
case '-': return 1;
case '*': return 2;
case '/': return 3;
case '^': return 4;
case '(': return 5;
case ')': return 6;
case 'a': return 7; // assuming variables are 'a'
case '$': return 8;
default: return -1;
}
}

// Displaying shift operation


void showShift(char symbol) {
printf("\nShift %c", symbol);
}

// Displaying reduce operation


void showReduce(char symbol) {
if (isalpha(symbol)) {
printf("\nReduce E->%c", symbol);
} else if (strchr("+-*/^", symbol)) {
printf("\nReduce E->E%cE", symbol);
} else if (symbol == ')') {
printf("\nReduce E->(E)");
}
}

// Check precedence relations


int checkPrecedence(char topOfStack, char inputSymbol, char relation) {
if (isalpha(topOfStack)) topOfStack = 'a';
if (isalpha(inputSymbol)) inputSymbol = 'a';

return precedenceTable[getPrecedenceIndex(topOfStack)][getPrecedenceIndex(inputSymbol)]
== relation;
}

// Function to validate if the grammar is an operator grammar


int validateOperatorGrammar(int rulesCount, char grammar[20][20]) {
for (int i = 0; i < rulesCount; i++) {
if (grammar[i][2] == '$') {
return 0; // No null productions allowed
}
for (int j = 3; j < strlen(grammar[i]); j++) {
if (isupper(grammar[i][j]) && isupper(grammar[i][j-1])) {
return 0; // No adjacent non-terminals allowed
}
}
}
return 1; // Valid operator grammar
}

int main() {
char grammar[20][20];
int numOfRules;

// Reading number of rules in the grammar


printf("Enter the number of productions: ");
scanf("%d", &numOfRules);

for (int i = 0; i < numOfRules; i++) {


scanf("%s", grammar[i]);
}

// Validate if it's an operator grammar


if (!validateOperatorGrammar(numOfRules, grammar)) {
printf("Not an operator grammar\n");
return 0;
} else {
printf("Operator grammar\n");
}

char expression[100];
printf("Enter the Arithmetic Expression End with $: ");
scanf("%s", expression);

// Push initial symbol onto the stack


pushToStack('$');

// Start parsing the expression


while (1) {
if (expression[inputPointer] == '$' && stack[stackTop] == '$') {
printf("\n\nAccepted");
break;
} else if (checkPrecedence(stack[stackTop], expression[inputPointer], '<') ||
checkPrecedence(stack[stackTop], expression[inputPointer], '=')) {

showShift(expression[inputPointer]);
pushToStack(expression[inputPointer]);
inputPointer++;
} else if (checkPrecedence(stack[stackTop], expression[inputPointer], '>')) {
do {
tempStack[++tempTop] = popFromStack();
showReduce(tempStack[tempTop]);
} while (!checkPrecedence(stack[stackTop], tempStack[tempTop], '<'));
}
}
return 0;
}

You might also like