Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
47 views3 pages

Meenakshi Sundararajan Engineering College: Program: In.L

This document contains code for a compiler program that parses expressions and stores them in a table. It includes code for lexing tokens, defining a grammar with Bison, and routines for adding expressions to a table and printing the table. The program allows users to enter an expression, parses it, stores it in a global array called "arr", and prints out the contents of the array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views3 pages

Meenakshi Sundararajan Engineering College: Program: In.L

This document contains code for a compiler program that parses expressions and stores them in a table. It includes code for lexing tokens, defining a grammar with Bison, and routines for adding expressions to a table and printing the table. The program allows users to enter an expression, parses it, stores it in a global array called "arr", and prints out the contents of the array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

MEENAKSHI SUNDARARAJAN ENGINEERING COLLEGE

#363, Arcot Road, Kodambakkam, Chennai – 600024, Tamil Nadu, India

Department: Computer Science & Engineering Register No.: 311517104005

PROGRAM:

IN.L

%{
#include"y.tab.h"
%}
%%
[0-9]+ {yylval.symbol=(char)(yytext[0]);return NUMBER;}
[a-z] {yylval.symbol= (char)(yytext[0]);return LETTER;}
. {return yytext[0];}
\n {return 0;}
%%
IN.Y

%{
#include"y.tab.h"
#include<stdio.h>
char addtotable(char,char,char);
int index1=0;
char temp = 'A'-1;
struct expr{
char operand1;
char operand2;
char operator;
char result;
};
%}
%union{
char symbol;
}
%left '+' '-'
%left '/' '*'
%token <symbol> LETTER NUMBER
%type <symbol> exp

Page No.:
MEENAKSHI SUNDARARAJAN ENGINEERING COLLEGE
#363, Arcot Road, Kodambakkam, Chennai – 600024, Tamil Nadu, India

Department: Computer Science & Engineering Register No.: 311517104005

%%
statement: LETTER '=' exp ';' {addtotable((char)$1,(char)$3,'=');};
exp: exp '+' exp {$$ = addtotable((char)$1,(char)$3,'+');}
|exp '-' exp {$$ = addtotable((char)$1,(char)$3,'-');}
|exp '/' exp {$$ = addtotable((char)$1,(char)$3,'/');}
|exp '*' exp {$$ = addtotable((char)$1,(char)$3,'*');}
|'(' exp ')' {$$= (char)$2;}
|NUMBER {$$ = (char)$1;}
|LETTER {(char)$1;};
%%
struct expr arr[20];
int yyerror(char *s){
printf("Errror %s",s);
exit(1);
}
char addtotable(char a, char b, char o){
temp++;
arr[index1].operand1 =a;
arr[index1].operand2 = b;
arr[index1].operator = o;
arr[index1].result=temp;
index1++;
return temp;
}
void threeAdd(){
int i=0;
char temp='A';
while(i<index1-1){
printf("%c:=\t",arr[i].result);
printf("%c\t",arr[i].operand1);
printf("%c\t",arr[i].operator);
printf("%c\t",arr[i].operand2);

Page No.:
MEENAKSHI SUNDARARAJAN ENGINEERING COLLEGE
#363, Arcot Road, Kodambakkam, Chennai – 600024, Tamil Nadu, India

Department: Computer Science & Engineering Register No.: 311517104005

i++;
temp++;
printf("\n");
}
//printf("%c:=\t",arr[i].result);
printf("%c:=\t",arr[i].operand1);
// printf("%c\t",'');
printf("%c\t",arr[i].operand2);
}
int yywrap(){
return 1;
}
int main(){
printf("Enter the expression: ");
yyparse();
threeAdd();
return 0;
}
OUTPUT:

RESULT:

Page No.:

You might also like