0% found this document useful (0 votes)
3 views5 pages

22bce0698

The document contains two programming tasks related to compiler design. The first task involves implementing a parser for production rules using C, while the second task checks if a given expression adheres to operator precedence grammar using C++. Both tasks include code snippets and describe their functionality.

Uploaded by

debanshuprusty
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)
3 views5 pages

22bce0698

The document contains two programming tasks related to compiler design. The first task involves implementing a parser for production rules using C, while the second task checks if a given expression adheres to operator precedence grammar using C++. Both tasks include code snippets and describe their functionality.

Uploaded by

debanshuprusty
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/ 5

COMPILER LAB

NAME : SOUHARDYA KUNDU


REG NO : 22BCE0698

Q1.

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

struct ProductionRule
{
char left[10];
char right[10];
};

int main()
{
char input[20], stack[50], temp[50], ch[2], *token1, *token2, *substring;
int i, j, stack_length, substring_length, stack_top, rule_count = 0;
struct ProductionRule rules[10];

stack[0] = '\0';

printf("SOUHARDYA KUNDU 22BCE0698\n");


printf("\nEnter the number of production rules: ");
scanf("%d", &rule_count);

printf("\nEnter the production rules (in the form 'left->right'): \n");


for (i = 0; i < rule_count; i++)
{
scanf("%s", temp);
token1 = strtok(temp, "->");
token2 = strtok(NULL, "->");
strcpy(rules[i].left, token1);
strcpy(rules[i].right, token2);
}

printf("\nEnter the input string: ");


scanf("%s", input);

i = 0;
printf("\n%-15s%-15s%-15s\n", "Stack", "Input", "Action");

while (1)
{
if (i < strlen(input))
{
ch[0] = input[i];
ch[1] = '\0';
i++;
strcat(stack, ch);

printf("%-15s", stack);
for (int k = i; k < strlen(input); k++)
{
printf("%c", input[k]);
}
if (i == strlen(input))
{
printf("ε");
}
printf("%-15sShift %s\n", "", ch);
}

for (j = 0; j < rule_count; j++)


{
substring = strstr(stack, rules[j].right);
if (substring != NULL)
{
stack_length = strlen(stack);
substring_length = strlen(substring);
stack_top = stack_length - substring_length;
stack[stack_top] = '\0';
strcat(stack, rules[j].left);

printf("%-15s", stack);
for (int k = i; k < strlen(input); k++)
{
printf("%c", input[k]);
}
if (i == strlen(input))
{
printf("ε");
}
printf("%-15sReduce %s->%s\n", "", rules[j].left, rules[j].right);

j = -1;
}
}
if (strcmp(stack, rules[0].left) == 0 && i == strlen(input))
{
printf("Accepted");
break;
}
if (i == strlen(input))
{
printf("Not accepted\n");
break;
}
}

return 0;
}

OUTPUT

Q2.
CODE:
#include <iostream>
#include <stack>
#include <string>
using namespace std;
bool isOperator(char c) {
return (c == '+' || c == '*' || c == '(' || c == ')');
}
bool isOperand(char c) {
return (c == 'i' || c == 'd');
}
bool checkPrecedenceTable(char a, char b) {
if (a == '+' && b == '*') return true;
if (a == '+' && b == '+') return false;
if (a == '*' && b == '*') return false;
if (a == '(' && b == ')') return true;
return false;
}
bool isOperatorPrecedenceGrammar(const string& expression) {
stack<char> s;
for (int i = 0; i < expression.length(); i++) {
char current = expression[i];
if (isOperator(current)) {
if (!s.empty() && isOperator(s.top())) {
if (!checkPrecedenceTable(s.top(), current)) {
return false;
}
}
} else if (isOperand(current)) {
continue;
} else if (current == ')') {
while (!s.empty() && s.top() != '(') {
s.pop();
}
if (!s.empty() && s.top() == '(') {
s.pop();
}
}
s.push(current);
}
return true;
}
int main() {
string expression;

cin >> expression;


if (isOperatorPrecedenceGrammar(expression)) {
cout << "The grammar is an operator precedence grammar." << endl;
} else {
cout << "The grammar is not an operator precedence grammar." << endl;
}
return 0;
}
OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy