PROGRAM CODE FOR LEXICAL ANALYZER - Implement scanner for the lexical rules The
ID: 673855 • Letter: P
Question
PROGRAM CODE FOR LEXICAL ANALYZER
- Implement scanner for the lexical rules
The scanner is embedded and thus it will return one token every time it is called as a subroutine by a parser-directed translator.
Keywords are reserved with postprocessing lookup table
The scanner should be DFA implemented a table with a driver for 100%. It can also be implemented for 50% as plain string reader assuming
all tokens must be separated by spaces without the table/driver
no comments
lines not counted
in this case you must have the README file and state on your first line that you are using this option without table/driver. Also if you read strings make sure to properly generate the EOF_token when file exhausted
Implement a token as a tripple (tokenID, tokenInstance, line#). TokenID is I suggest enumeration or integer, tokenInstance can be a string without tables or can be some reference to a symbol table
Dont forget EOFtk token
Implement the scanner in the file scanner.c (or cpp) and its prototypes in scanner.h. Have a main test driver in a different file driveScanner.c, opening the file, and then calling the scanner repeatedly until it gets the eof token (not eof itself) from the scanner. Every token received by the main driver should be displayed to the screen, one token per line, listing line#, some name identifying the token, and the instance string of the token if any.
Define the token in token.h
Invocation:
testScanner [file]
to scan from stdin or file file.lan. Note that the extension is implicit. That is if the source is inproject.lan (sources must have extension .lan), then the correct invocation is testScannerproject and not testScanner project.lan.
reading from the keyboard should work with redirection from a file
Suggestions for the FA option
have the scanner read the file through a filter. The filter will
skip over #...
count lines
construct the string
return column number instead of the actual character read
represent the DFA as 2-d array of type integer
0, 1, etc are numbers corresponding to the row for the next state
-1, -2, etc are different error cases
1000, 1001, etc are final states for different tokens (e.g. 1000 is for ID, etc)
keyword
Explanation / Answer
INCLUDE files for :Lexical Analyzer in C
#include<stdio.h>
#include<ctype.h>
#include<string.h>
void keyw(char *p);
int i=0,id=0,kw=0,num=0,op=0;
char keys[32][10]={"auto","break","case","char","const","continue","default",
"do","double","else","enum","extern","float","for","goto",
"if","int","long","register","return","short","signed",
"sizeof","static","struct","switch","typedef","union",
"unsigned","void","volatile","while"};
main()
{
char ch,str[25],seps[15]=" ,;(){}[]#"<>",oper[]="!%^&*-+=~|.<>/?";
int j;
char fname[50];
FILE *f1;
//clrscr();
printf("enter file path (drive:\fold\filename) ");
scanf("%s",fname);
f1 = fopen(fname,"r");
//f1 = fopen("Input","r");
if(f1==NULL)
{
printf("file not found");
exit(0);
}
while((ch=fgetc(f1))!=EOF)
{
for(j=0;j<=14;j++)
{
if(ch==oper[j])
{
printf("%c is an operator ",ch);
op++;
str[i]='';
keyw(str);
}
}
for(j=0;j<=14;j++)
{
if(i==-1)
break;
if(ch==seps[j])
{
if(ch=='#')
{
while(ch!='>')
{
printf("%c",ch);
ch=fgetc(f1);
}
printf("%c is a header file ",ch);
i=-1;
break;
}
if(ch=='"'){
do
{
ch=fgetc(f1);
printf("%c",ch);
}while(ch!='"');
printf(" is an argument ");
i=-1;
break;
}
str[i]='';
keyw(str);
}
}
if(i!=-1){
str[i]=ch;
i++;
}
else
i=0;
}
printf("Keywords: %d Identifiers: %d Operators: %d Numbers: %d ",kw,id,op,num);
//getch();
}
void keyw(char *p)
{
int k,flag=0;
for(k=0;k<=31;k++)
{
if(strcmp(keys[k],p)==0)
{
printf("%s is a keyword ",p);
kw++;
flag=1;
break;
}
}
if(flag==0)
{
if(isdigit(p[0]))
{
printf("%s is a number ",p);
num++;
}
else
{
//if(p[0]!=13&&p[0]!=10)
if(p[0]!='')
{
printf("%s is an identifier ",p);
id++;
}
}
}
i=-1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.