COMPILER CONSTRUCTIONS LAB RECORD INDEX S.NO LIST OF PROGRAMS PAGE NO. 1 Program to implement a standalone scanner. 2 Write a program in lex to check if a no is octal, Hexadecimal 3 Program in lex to capitalize all comments in a c program 4 Program to implement a lex scanner 5 Write a program to find first and follow elements 6 Write a Program to find canonical LR(0) collections. i.e. a). closure 7 Write a Program to construct SLR parsing table using program 6 8 Write a program to implement a calculator using yacc. a). using lex b). without using lex 9 Program to generate code 10 Program to optimize the code
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
COMPILER CONSTRUCTIONS LAB RECORD INDEXS.NO LIST OF PROGRAMS PAGE NO.
1 Program to implement a standalone scanner.
2 Write a program in lex to check if a no is octal, Hexadecimal
3 Program in lex to capitalize all comments in a c program
4 Program to implement a lex scanner
5 Write a program to find first and follow elements
6 Write a Program to find canonical LR(0) collections. i.e. a). closure
7 Write a Program to construct SLR parsing table using program 6
8 Write a program to implement a calculator using yacc. a). using lex b). without using lex
In.txtvoid main(){int a = 10 , b = 20 , c ;c = a + b;printf("the sum is %d",c);}Out1.txtvoid keywordmain keyword) specialsymbol { specialsymbol int keyworda identifier= is operator 10 is Number, specialsymbol b identifier
= is operator 20 is Number, specialsymbol c identifier; specialsymbol c identifier= is operator a identifier+ is operator b identifierprintf keyword" specialsymbol the identifiersum identifieris identifier% is operator d identifier, specialsymbol c identifier; specialsymbol } specialsymbol
2. Write a program in lex to check if a no is octal, Hexadecimal etc.
Octal.l
%%[0][0-7]+ printf("%s\tIt is octal\n",yytext);[0][Xx][0-9A-Fa-f]+ printf("%s\tIt ishexadecimel\n",yytext);%%
Execution:
Lex octal.lCc lex.yy.c –ll./a.out
Output:0xA10xA1 It ishexadecimel
03210321 It is octal
3. Program in lex to capitalize all comments in a c program.
case 1: fprintf(yyout,"\n%d %s keyword",l,yytext); break;
case 2: fprintf(yyout,"\n%d %s operator",l,yytext);
break;
case 3: fprintf(yyout,"\n%d %s identifier",l,yytext); break;
default: fprintf(yyout,"\n%d %s special symbol",l,yytext); break;
}
}In.txt
#include<stdio.h>int main(){int a,b,c;c=a+b;}Out.txt1 # special symbol1 include keyword1 < special symbol1 stdio.h keyword1 > special symbol3 int keyword3 special symbol3 main keyword3 ( special symbol3 ) special symbol4 { special symbol5 int keyword5 special symbol5 a identifier5 , special symbol5 b identifier5 , special symbol5 c identifier5 ; special symbol6 c identifier6 = operator6 a identifier6 + operator6 b identifier6 ; special symbol7 } special symbol
5. Write a program to find first and follow elements.
First.c
#include<stdio.h>#include<ctype.h>
int main(){
int i,n,j,k; char str[10][10],f; printf("Enter the number of productions\n"); scanf("%d",&n); printf("Enter grammar\n"); for(i=0;i<n;i++) scanf("%s",&str[i]); for(i=0;i<n;i++) {
int np,i,j,k;char prods[10][10],follow[10][10],Imad[10][10];printf("enter no. of productions\n");scanf("%d",&np);printf("enter grammar\n");for(i=0;i<np;i++){
6. Write a Program to find canonical LR(0) collections. i.e. a). closure
closure.c
#include<stdio.h>#include<string.h>main(){ char A[10][10],items[20][20]={},closure[10][10]={},I[20][20],inputs[20][20]; int i,j,k,l=0,m=0,p=3,n=0,ilen[10],numi; int B[10][10]; char c,ch; int flag=0,numprods,temp,temp1,q;
printf("enter the number of productions\n"); scanf("%d",&numprods);
char str[25][50], forLoopParam[90], rightHandParam[10][40], leftHandParam[90];int j=0,k=0,i=0,m=0,n=0,q=0,s=0;int flag[10]={0},count[10]={0};printf("\n Input the loop to be optimized:\n");