My Profile
Active Members
TodayLast 7 Days
more...
Awards & Gifts
Online Exams
Fresher Jobs
Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian
cities including Bangalore, Chennai, Hyderabad, Pune or Kochi
Resources
Find educational articles, blogs, discussion threads and other resources.
Colleges
Find details about any college in India or search for courses.
|
Resources » Articles/Knowledge Sharing » Education »
Compiler Design Lab Program -2
|
PROGRAM FOR LEXICAL ANALYSIS
#include #include #include #include #include #define SIZE 128 #define NONE -1 #define EOS '\0' #define NUM 256 #define KEYWORD 257 #define PAREN 258 #define ID 259 #define ASSIGN 260 #define REL_OP 261 #define DONE 262 #define MAX 999 char lexemes[MAX]; char buffer[SIZE]; int lastchar=-1; int lastentry=0; int tokenval=NONE; int lineno=1; struct entry { char *lexptr; int token; } symtable[100]; struct entry keywords[]={"if",KEYWORD,"else",KEYWORD,"for",KEYWORD,"int",KEYWORD, "float",KEYWORD,"double",KEYWORD,"char",KEYWORD, "struct",KEYWORD,"return",KEYWORD,0,0}; void Error_Message(char *m) { fprintf(stderr,"line %d: %s\n",lineno,m); exit(1); } int look_up(char s[]) { int k; for(k=lastentry;k>0;k=k-1) if(strcmp(symtable[k].lexptr,s)==0) return k; return 0; } int insert(char s[],int tok) { int len; len = strlen(s); if(lastentry+1>=MAX) Error_Message("Symbol Table Is Full"); if(lastchar +len+1>=MAX) Error_Message("Lexemes Array Is Full"); lastentry=lastentry+1; symtable[lastentry].token=tok; symtable[lastentry].lexptr=&lexemes[lastchar+1]; lastchar=lastchar + len + 1; strcpy(symtable[lastentry].lexptr,s); return lastentry; } void initialize() { struct entry *ptr; for(ptr=keywords;ptr->token;ptr++) insert(ptr->lexptr,ptr->token); } int lexer() { int t; int val,i=0; while(1) { t=getchar(); if(t==' '||t=='\t'); else if(t=='\n') lineno=lineno+1; else if(t=='('||t==')') return PAREN; else if(t=='<'||t=='>'||t=='<='||t=='>='||t=='!=') return REL_OP; else if(t=='=') return ASSIGN; else if(isdigit(t)) { ungetc(t,stdin); scanf("%d",&tokenval); return NUM; } else if(isalpha(t)) { while(isalnum(t)) { buffer[i]=t; t=getchar(); i=i+1; if(i>=SIZE) Error_Message("Compiler Error"); } buffer[i]=EOS; if(t!=EOF) ungetc(t,stdin); val=look_up(buffer); if(val==0) val=insert(buffer,ID); tokenval=val; return symtable[val].token; } else if(t==EOF) return DONE; else { tokenval=NONE; return t; }}} void main() { int lookahead; char ans; clrscr(); printf("\n\t\t program for lexical Analysis\n"); initialize(); printf("\n Enter the expression and put the ; at the end"); printf("\n Press control z to terminate"); lookahead=lexer(); while(lookahead!=DONE) { if(lookahead==NUM) { printf("\n Number:"); printf("%d",tokenval); } if(lookahead=='+'||lookahead=='-'||lookahead=='*'||lookahead=='/') printf("\n Operator"); if(lookahead==PAREN) printf("\n Parenthesis"); if(lookahead==ID) { printf("\n Identifiers"); printf("%s",symtable[tokenval].lexptr); } if(lookahead==KEYWORD) printf("\n Keyword"); if(lookahead==ASSIGN) printf("\n Assignment operator"); if(lookahead==REL_OP) printf("\n Relational Operator"); lookahead=lexer(); } }
|
|
Responses to the resource: "Compiler Design Lab Program -2"
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|
|