pcd lab manual.pdf - GKM College of engineering and technology ht tp cs et ub e.c o nr Allapakam-mappedu road gkm nagar Chennai Principles of compiler

pcd lab manual.pdf - GKM College of engineering and...

This preview shows page 1 - 9 out of 47 pages.

GKM College of engineering and technology Allapakam-mappedu road, gkm nagar, Chennai. Principles of compiler and design Lab Manual
Image of page 1
IMPLEMENTATION OF A LEXICAL ANALYZER IN C AIM: To write and execute a C program to implement the lexical analyzer. ALGORITHM: Start the program. Declare the file pointer and necessary variables. Open the input file in the read mode. Use the analyze function to analyze the input program and store the identifiers, keywords and operator on idhd, keyhd, ophd respectively. Stores the tokens in data structure linked lists. Increment the line number of each token and its occurrences. Using the show function print the linked lists in a tabular format. Stop the program.
Image of page 2
PROGRAM #include<stdio.h> #include<ctype.h> #include<string.h> #include<malloc.h> struct lineno { int ln; struct lineno *next; }; struct lx { char str[20]; struct lineno *next; struct lx *down; }; typedef struct lineno node; typedef struct lx lex; lex *keyhd,*ophd,*idhd; FILE *fp; char temp[20]; int line=1; lex *getkeynode(); node *getlnode(); void analyze(); void add(lex *); void show(lex *); int main() { keyhd=getkeynode(); ophd=getkeynode(); idhd=getkeynode(); fp=fopen("inp.c","rt"); if(fp==NULL) { printf("No input file!!"); /*exit(1);*/ } analyze(); printf("\n\t\tKeywords\n\n"); show(keyhd); printf("\n\t\tIDENTIFIER\n\n"); show(idhd); printf("\n\t\tOPERATORS\n\n");
Image of page 3
show(ophd); free(ophd); free(idhd); free(keyhd); return 0; } lex *getkeynode() { lex *temp; temp=(lex*)malloc(sizeof(struct lx)); temp->next=NULL; temp->down=NULL; return temp; } node *getlnode() { node *temp; temp=(node*)malloc(sizeof(struct lineno)); temp->next=NULL; return temp; } void analyze() { char ch; int i,flag; ch=fgetc(fp); while(ch!=EOF) { if(ch=='\n') { ch=fgetc(fp); line++; continue; } if(isspace(ch)) { ch=fgetc(fp); continue; } if(ch== '/') while((ch=fgetc(fp))!='/'); if(isalpha(ch)) { i=0; do {
Image of page 4
temp[i++]=ch; ch=fgetc(fp); } while(isalpha(ch)||ch=='.'); fseek(fp,-1,1); temp[i]='\0'; i=0; flag=0; while(strlen(key[i])) { if(strcmp(temp,key[i])==0) { add(keyhd); flag=1; break; } i++; } if(flag==0) add(idhd); } else { temp[0]=ch; temp[1]='\0'; add(ophd); } ch=fgetc(fp); } } void add(lex *hd) { node *l,*m; lex *q,*p=hd->down; while(p) { if(strcmp(p->str,temp)==0) break; p=p->down; } if(p==NULL) { p=hd; while(p->down)p=p->down; q=getkeynode(); p->down=q;
Image of page 5
strcpy(q->str,temp); l=getlnode(); l->ln=line; q->next=l; } else { m=getlnode(); l=p->next; m->ln=line; if(l==NULL) p->next=m; else { while(l->next) l=l->next; l->next=m; } } } void show(lex *hd) { lex *p=hd->down,*q; node *l,*m; int i=1,occ=0; printf("\tTokens\t\tLineno\t\tOccurences\n\n"); while(p) { occ=1; l=p->next; printf("\t%s\t\t",p->str); printf("%d",l->ln); l=l->next; while(l) { printf("%d",l->ln); l=l->next; occ++; } printf("\t\t%d",occ); p=p->down; printf("\n"); } }
Image of page 6
INPUTFILE: #include<stdio.h> void main() { int a,b,c; printf("Enter the value of a& b"); scanf("%d%d",&a,&b); c=a+b; printf("%d",c); } OUTPUT: [[email protected] ~]$ cc exp1.c [[email protected] ~]$ ./a.out KEYWORDS Tokens Lineno Occurences include 1 1 stdio.h 1 1 void 2 1 main 2 1 int 4 1 printf 5,8 2 scanf 6 1 IDENTIFIER Tokens Lineno Occurences a 4,5,6,7 4 b 4,5,6,7 4 c 4,7,8 3 Enter 5 1 the 5 1 value 5 1 of 5 1 d 6,6,8 3
Image of page 7
Image of page 8
Image of page 9

You've reached the end of your free preview.

Want to read all 47 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes