Waiting for answer This question has not been answered yet. You can hire a professional tutor to get the answer.

QUESTION

I am working on a lexical analyzer with lexical error checking, and the compilation listing generator for the compiler. Comments begin with -- and...

I am working on a lexical analyzer with lexical error checking, and the compilation listing generator for the compiler.

1. Comments begin with -- and end with the end of the line.

2. White space between tokens is permitted but not required.

3. Identifiers must begin with a letter, followed by letters, digits or underscores. Consecutive underscores and trailing underscores are not permitted

4. Integer literals consist of a sequence of digits preceded by an optional sign.

5. Real literals consist of a sequence of digits containing a decimal point. At least one digit must be before the decimal point.

6. Boolean literals are true and false.

7. The logical operators are not, and and or. Each logical operator should be a separate token.

8. The relational operators are =, /=, >, >=, <, and <=. All six lexemes should be represented by a single token.

9. The adding operators are the binary + and -. Both lexemes should be represented by a single token.

10. The multiplying operators are *, / and rem. The first two lexemes should be represented by a single token.

11. The exponentiation operator is **.

12. The following punctuation symbols should be accepted: commas, colons, semicolons, and parentheses. In addition the two character arrow symbol => should be a punctuation token.

13. The following are reserved words:

begin, boolean, case, else, end, endcase, endif, function, if, is, integer, real, returns, then, when

The lexical analyzer should be created using flex.

The compiler should produce a listing of the program with lexical error messages included after the line in which they occur. Any character that cannot start any token should be considered a lexical error. An example of compilation listing output is shown below:

  1 -- Program with two lexical errors   2   3 function main a: integer returns integer;   4     b: integer is a * 2;   5 begin   6     if a <= 0 then   7         b + b;   8     else   9         b ^ b $ Lexical Error, Invalid Character ^ Lexical Error, Invalid Character $

10     endif;  11 end;

Lexical Errors 2

It should also generate a file containing the lexeme-token pairs as a means to verify that the lexical analyzer is working correctly. Only token numbers are required, not token names. The token numbers for the punctuation symbols should be the ASCII value of the character. The remaining tokens should be numbered sequentially beginning at 256.

This is what I have so far but need help

#include<string.h>#include "tokens.h"#include<ctype.h>#include<stdio.h>%option noyywrapvoid keyword(char str[10]){ if(<span id="IL_AD8" class="IL_AD">strcmp</span>("for",str)==0||strcmp("while",str)==0||strcmp("do",str)==0||strcmp("int",str)==0||strcmp("float",str)==0||strcmp("char",str)==0||strcmp("<span id="IL_AD10" class="IL_AD">double</span>",str)==0||strcmp("static",str)==0||strcmp("switch",str)==0||strcmp("case",str)==0)printf("n%s is a keyword",str);elseprintf("n%s is an <span id="IL_AD9" class="IL_AD">identifier</span>",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("nEnter <span id="IL_AD1" class="IL_AD">the c program</span>");/*gets(st1);*/f1=fopen("<span id="IL_AD11" class="IL_AD">input</span>","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=='t')printf(" ");else if(c=='n')lineno++;elseputc(c,f3);}fclose(f2);fclose(f3);fclose(f1);printf("nThe no's in the program are");for(j=0;j<i;j++)printf("%d",num[j]);printf("n");f2=fopen("identifier","r");k=0;printf("The <span id="IL_AD6" class="IL_AD">keywords</span> and identifiersare:");while((c=getc(f2))!=EOF){if(c!=' ')str[k++]=c;else{str[k]='';keyword(str);k=0;}}fclose(f2);f3=fopen("specialchar","r");printf("nSpecial characters are");while((c=getc(f3))!=EOF)printf("%c",c);printf("n");fclose(f3);printf("Total no. of lines are:%d",lineno);}
Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question