0% found this document useful (0 votes)
98 views12 pages

Cdlab T.K.R

The document describes several programs written for a compiler design and distributed databases lab. The programs demonstrate a lexical analyzer, breaking a sentence into lines at spaces, removing comments from code, a recursive descent parser, and a shift-reduce parser.

Uploaded by

swetha_tkrecit
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views12 pages

Cdlab T.K.R

The document describes several programs written for a compiler design and distributed databases lab. The programs demonstrate a lexical analyzer, breaking a sentence into lines at spaces, removing comments from code, a recursive descent parser, and a shift-reduce parser.

Uploaded by

swetha_tkrecit
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 12

Distributed Databases and Compiler Design Lab

Roll No: 11K91D5810

Compiler Design Programs:


Program No:1 This program demonstrates a simulator for lexical analyzer The program identifies the presence of: main() keywords: void, char, int, float operators: +, -, *, /, %, = delimiters: ';', ',' line count variable count parenthesis count ********************************************************************** * #include <stdio.h> #include <conio.h> #include <string.h> #define MAX_TOKEN_LENGTH 32 /*******function declaration begins*****/ int lookAhead(); void analyseWord(char a[]); void printDetails(); void clearBuffer(); /*******function declaration ends******/ /********global variable declaration begins*****/ FILE *srcPointer; int operatorCount; int paranthesisCount; int delimiterCount; int keywordCount; int variableCount; int lineCount = 1; int mainFound; char buffer[MAX_TOKEN_LENGTH]; int i; /********global variable declaration ends*****/ /**********main function begins***********/ void main(int argc, char *argv[]) { int curChar; int space_occured = 0; if(argc != 2) { printf("Wrong Usage: lex <filename>"); exit(1); 1

Distributed Databases and Compiler Design Lab } srcPointer = fopen(argv[1], "r"); if(srcPointer == NULL) { printf("Error opening file..."); exit(1); } printf("\nLexical Analysis of %s .... \n", argv[1]); while( (curChar=lookAhead()) != EOF ) {

Roll No: 11K91D5810

if(curChar == '\n') /*check for newline*/ { lineCount++; continue; } if( ((curChar == '\t') || (curChar == ' ')) && (space_occured) ) /*skip more than one space*/ continue; else if((curChar == '\t') || (curChar == ' ')) /*space encountered*/ { if( buffer[0] != '\0') /*if buffer filled check word*/ { analyseWord(buffer); clearBuffer(); } space_occured = 1; continue; } space_occured = 0; /*indicate no space encountered*/ if( curChar == ',' ) /*on comma we increment delimiter*/ { variableCount++; /*comma also indicates a variable occurence*/ delimiterCount++; clearBuffer(); continue; } if( curChar == ';' ) { delimiterCount++; clearBuffer(); continue; } if( (curChar == '(') || (curChar == ')') || (curChar == '{') || (curChar == '}')) { paranthesisCount++; continue; } 2

Distributed Databases and Compiler Design Lab Roll No: 11K91D5810 if( (curChar == '+') || (curChar == '-') || (curChar == '/') || (curChar == '*') || (curChar == '=') || (curChar == '%')) { operatorCount++; clearBuffer(); continue; } else /*add charactes to buffer*/ { buffer[i++] = (char)curChar; buffer[i] = '\0'; continue; } } fclose(srcPointer); printDetails(); } /***************************************/ int lookAhead() { return fgetc(srcPointer); } void analyseWord(char *buf) { if(!strcmp("void", buf) ) { keywordCount++; return; } if(!strcmp("char" , buf) || !strcmp("int", buf) || !strcmp("float", buf) ) { keywordCount++; variableCount++; return; } if( !strcmp("main", buf) ) { mainFound = 1; return; } }

void printDetails()

printf("\n Line count: %d", lineCount ); 3

Distributed Databases and Compiler Design Lab printf("\n main() %s ", (mainFound ? "found" : "not found")); printf("\n Variable count: %d", variableCount ); printf("\n Keyword count: %d", keywordCount ); printf("\n Operator count: %d", operatorCount ); printf("\n Delimiter count: %d", delimiterCount ); printf("\n Paranthesis count: %d", paranthesisCount ); }

Roll No: 11K91D5810

void clearBuffer() { buffer[0] = '\0'; i = 0; } Output: D:\TC\BIN>lex lex.c Lexical Analysis of lex.c .... Line count: 167 main() found Variable count: 40 Keyword count: 18 Operator count: 322 Delimiter count: 92 Paranthesis count: 184

Distributed Databases and Compiler Design Lab Roll No: 11K91D5810 Program No: 2 Program for Break the Sentence ************************************************************************ #include<stdio.h> #include<conio.h> void main() { FILE *f1,*f2; char ch,str[40]; clrscr(); printf("\n enter text into v1:"); gets(str); f1=fopen("v1.c","w"); fputs(str,f1); fclose(f1); f1=fopen("v1.c","r"); f2=fopen("v2.c","w"); while((ch=fgetc(f1))!=EOF) { if(ch==' ') fputc('\n',f2); else fputc(ch,f2); } fcloseall(); } Output: Enter text into v1: raj is in sri indu V1.c raj is in sri indu V2.c Raj Is In Sri indu

Distributed Databases and Compiler Design Lab Program No: 3 Program for eliminating the comments #include<stdio.h> #include<conio.h> void main() { FILE *fp,*f; char str[100],ch; int i=0,k,n,l; clrscr(); fp=fopen("a.c","r"); while(1) { ch=getc(fp); if(ch==EOF) break; else str[i++]=ch; } str[i++]='\0'; for(l=0;str[l]!='\0';l++) { if(str[l]=='/'&&str[l+1]=='/') { k=l; while(str[k]!='\n') k++; n=l; while(str[n]!='\0') str[n++]=str[k++]; } } puts(str); f=fopen("b.c","w"); fputs(str,f); getch(); } Output: Enter the text in a.c as void main() { //Chaithanya in sri //indu } The output in b.c is void main() { } 6

Roll No: 11K91D5810

Distributed Databases and Compiler Design Lab Roll No: 11K91D5810 Program No: 4 Recursive Descent parse for the grammar E-TE E->TE/e T->FT T->FT/e F->(E)/id ************************************************************************** #include<stdio.h> #include<conio.h> #include<string.h> int i,flag1=0,flag2=0; void E(char []); void T(char []); void EPRIME(char []); void TPRIME(char []); void F(char []); void main() { char istr[20]; i=0; printf("\n enter the string"); scanf("%s",istr); E(istr); if(flag1==1&&flag2==0) printf("parsing successful"); else if(flag1==0&&flag2==0) printf("parsing unsucessful"); else if(flag2==1&&flag2==1) printf("parsing is unsucessful"); getch(); } void ADVANCE(char istr[]) { i++; if(istr[i]=='\0') flag1=1; } void ERROR() { flag2=1; } void E(char istr[]) { T(istr); EPRIME(istr); return; } void EPRIME(char istr[]) { if(istr[i]=='+') { ADVANCE(istr); T(istr); 7

Distributed Databases and Compiler Design Lab EPRIME(istr); } return; } void T(char istr[]) { F(istr); TPRIME(istr); return; } void TPRIME(char istr[]) { if(istr[i]=='*') { ADVANCE(istr); F(istr); TPRIME(istr); } return; } void F(char istr[]) { if(istr[i]=='i') { ADVANCE(istr); } else if(istr[i]=='(') { ADVANCE(istr); E(istr); if(istr[i]==')') { ADVANCE(istr); } else ERROR(); } else ERROR(); return; } Output: Enter the string: i+i*i Parsing successful

Roll No: 11K91D5810

Distributed Databases and Compiler Design Lab Roll No: 11K91D5810 Program No: 5 Shift reduce parser E->E-E/E*E/(E)/I*/ *************************************************************************** #include<stdio.h> #include<conio.h> #include<string.h> void push(char *,int *,char); char pop(char *,int *); char red(char); void red1(char *); char *name[]={"e*e","e+e"}; int top=-1; void main() { char input[20],*s,d,a[20],b,c; clrscr(); printf("enter the input"); scanf("%s",input); s=input; printf("\n"); while(*s) { d=red(*s); push(a,&top,d); if(top>=2) { red1(a); s++; } else s++; } b=pop(a,&top); c=pop(a,&top); if(b=='e'&&c=='\0') printf("accept the input"); else printf("dont aCCEPT THE INPUT"); getch(); } void push(char *a,int *sp,char item) { if(*sp==20) printf("stack is full"); else { *sp=*sp+1; a[*sp]=item; } 9

Distributed Databases and Compiler Design Lab } char red(char item) { if(item=='i') return('e'); else return(item); } void red1(char *a) { char t[4],j=0,d='e'; while(j<=2) { t[j]=pop(a,&top); j++; } t[j]='\0'; if(strcmp(t,*name)==0) push(a,&top,d); else { if(strcmp(t,*(name+1))==0) push(a,&top,d); else { j--; while(j>=0) { push(a,&top,t[j]); j--; } } }} char pop(char *a,int *sp) { char item; if(*sp==-1) return('\0'); else { item=a[*sp]; *sp=*sp-1; return(item); } } Output: Enter the input i+i*i Accept the input

Roll No: 11K91D5810

10

Distributed Databases and Compiler Design Lab Roll No: 11K91D5810 Program No: 6 Lexical analyzer to identify keywords, relational operators. *************************************************************************** #include<stdio.h> #include<conio.h> #include<string.h> #include<ctype.h> void main() { int i,j; char keywords[5][5]={"begin","if","then","else","end"}; char operators[5][5]={"<","<=","=","!=",">"}; char string[10]; int ch,l,found=0; clrscr(); printf("enter the data of choice"); scanf("%s",string); i=strlen(string); for(i=0;i<5;i++) { if(strcmp(&keywords[i][0],string)==0) { found=1; ch=1; break; } } for(i=0;i<5;i++) { if(strcmp(&operators[i][0],string)==0) { found=2; ch=2; break; } } switch(ch) { case 1:if(found==1) printf("\n you have entered keyword"); break; case 2: if(found==2) printf("\n you have entered relational operator"); break; default: printf("\n you have entered default choice"); } getch(); } 11

Distributed Databases and Compiler Design Lab Output: Enter the data of choice if You have entered keyword Enter the data of choice < You have entered operator

Roll No: 11K91D5810

12

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