Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The first project involves writing the lexical analyzer with lexical error check

ID: 3584472 • Letter: T

Question

The first project involves writing the lexical analyzer with lexical error checking, and the compilation listing generator for the compiler. Comments begin with { and end with }. Comments may not be nested. White space between tokens permitted but not required. Identifiers must begin with a letter, followed by letters or digits. Integer literals consist of a sequence of digits. Real literals consist of a sequence of digits containing a decimal point. At least one digit must be before and after the decimal point. The relational operators are =, <>, >, >=, <, and <=. The adding operators are +, - and or The multiplying operators are *, /, div, mod and and. The following are reserved words: array, begin, do, else, end, function, if, integer, of, procedure, program, real, then, while, var The lexical analyzer should be created using lex. Identifiers should be entered into the symbol table as they are encountered. The compiler should produce a listing of the program with lexical error messages included after the line in which they occur. A provision should be included to optionally dump the contents of the symbol table to verify its correctness. If you plan to implement you project in C, you may find this hash function useful in constructing your symbol table.

Explanation / Answer

/* C Program to Design Lexical Analyzer*/ /* Download more programs at http://sourcecode4u.com/ */ /*************************************************************************************/ #include #include #include void keyword(char str[10]) { if(strcmp("for",str)==0||strcmp("while",str)==0||strcmp("do",str)==0|| strcmp("int",str)==0||strcmp("float",str)==0||strcmp("char",str)==0|| strcmp("double",str)==0||strcmp("static",str)==0||strcmp("switch",str)==0|| strcmp("case",str)==0) printf(" %s is a keyword",str); else printf(" %s is an identifier",str); } main() { FILE *f1,*f2,*f3; char c,str[10],st1[10]; int num[100],lineno=0,tokenvalue=0,i=0,j=0,k=0; printf(" Enter the c program");/*gets(st1);*/ f1=fopen("input","w"); while((c=getchar())!=EOF) putc(c,f1); fclose(f1); f1=fopen("input","r"); f2=fopen("identifier","w"); f3=fopen("specialchar","w"); while((c=getc(f1))!=EOF) { if(isdigit(c)) { tokenvalue=c-'0'; c=getc(f1); while(isdigit(c)) { tokenvalue*=10+c-'0'; c=getc(f1); } num[i++]=tokenvalue; ungetc(c,f1); } else if(isalpha(c)) { putc(c,f2); c=getc(f1); while(isdigit(c)||isalpha(c)||c=='_'||c=='$') { putc(c,f2); c=getc(f1); } putc(' ',f2); ungetc(c,f1); } else if(c==' '||c==' ') printf(" "); else if(c==' ') lineno++; else putc(c,f3); } fclose(f2); fclose(f3); fclose(f1); printf(" The no's in the program are"); for(j=0;j
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote