Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

EXPERIMENT NO.

9
PART A

A.1 AIM: - To implement Operator Precedence parsers

A.2 Prerequisite
Basic syntax and semantics of programming languages, object oriented programming
A.3 Outcome
After successful completion of this experiment students will be able to
1. Implement Operator Precedence parsers
A.4 Theory
Theory:

Operator precedence determines which operator is performed first in an expression with more
than one operators with different precedence. For example 10 + 20 * 30 is calculated as 10 +
(20 * 30) and not as (10 + 20) * 30.

Associativity is used when two operators of same precedence appear in an expression.


Associativity can be either Left to Right or Right to Left. For example ‘*’ and ‘/’ have same
precedence and their associativity is Left to Right, so the expression “100 / 10 * 10” is treated
as “(100 / 10) * 10”.
Precedence and Associativity are two characteristics of operators that determine the evaluation
order of subexpressions in absence of brackets.
1) Associativity is only used when there are two or more operators of same precedence.`
2) All operators with same precedence have same associativity
3) Precedence and associativity of postfix ++ and prefix ++ are different
4) Comma has the least precedence among all operators and should be used carefully
5) There is no chaining of comparison operators in C
PART B
(PART B: TO BE COMPLETED BY STUDENTS)
(Students must submit the soft copy as per following segments within two hours of the
practical. The soft copy must be uploaded on the portal or emailed to the concerned lab
in charge faculties at the end of the practical in case the there is no Black board /portal
access available)
Roll No.44 Name:Aaryak Shandilya
Program:BTech Data Science Division:J
Semester:IV Batch :J3
Date of Experiment: Date of Submission:
Grade :

B.1 Software Code written by student:


// Operator_Parser.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<string.h>
#include<stdio.h>
#include<conio.h>
void main(){
char array[20],ip[20],opt[10][10][1],ter[10];
int i,j,k,n,top=0,col,row;
clrscr();
for(i=0;i<10;i++)
{
array[i]=NULL;
ip[i]=NULL;
for(j=0;j<10;j++)
{
opt[i][j][1]=NULL;
}
}
printf("Enter the no.of terminals :\n");
scanf("%d",&n);
printf("\nEnter the terminals :\n");
scanf("%s",&ter);
printf("\nEnter the table values :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Enter the value for %c %c:",ter[i],ter[j]);
scanf("%s",opt[i][j]);
}
}
printf("\n**** Operator Precedence Table ****\n");
for(i=0;i<n;i++)
{
printf("\t%c",ter[i]);
}
printf("\n");
for(i=0;i<n;i++){printf("\n%c",ter[i]);
for(j=0;j<n;j++){printf("\t%c",opt[i][j][0]);}}
array[top]='$';
printf("\nEnter the input string:");
scanf("%s",ip);
i=0;
printf("\nStack\t\t\tInput String\t\t\tAction\n");
printf("\n%s\t\t\t%s\t\t\t",stack,ip);
while(i<=strlen(ip))
{
for(k=0;k<n;k++)
{
if(array[top]==ter[k])
col=k;
if(ip[i]==ter[k])
row=k;
}
if((array[top]=='$')&&(ip[i]=='$')){
printf("String is accepted\n");
break;}
else if((opt[col][row][0]=='<') ||(opt[col][row][0]=='='))
{ array[++top]=opt[col][row][0];
array[++top]=ip[i];
printf("Shift %c",ip[i]);
i++;
}
else{
if(opt[col][row][0]=='>')
{
while(array[top]!='<'){--top;}
top=top-1;
printf("Reduce");
}
else
{
printf("\nString is not accepted");
break;
}
}
printf("\n");
for(k=0;k<=top;k++)
{
printf("%c",array[k]);
}
printf("\t\t\t");
for(k=i;k<strlen(ip);k++){
printf("%c",ip[k]);
}
printf("\t\t\t");
}
getch();
}

B.2 Input and Output:


B.3 Conclusion:
Hence we learn how to implement operator precedence in C.

You might also like