Sunteți pe pagina 1din 4

Project Title : Lexical Analyzer & Symbol Table Implementation

Group Members : Mohsin Amin :: Reg# : MCS172037


Inzamam Jafar:: Reg# : MCS172106
//Lexical Analyzer
#include<stdio.h>
int main()
{

char expr[100],operators[20]; //Arrays for storing expression and operator


char variables[20][20]; // 2d array for storing variable or identifier
int constants[20],ascii[100]={0}; //for storing constants, ascii values of
expression
int cnt,i,number=0; //cnt to store string length
int j=-1,k=-1,l=-1,n=0; //counters for filling arrays
int pr[10]={0},m=0; //counters for printing identifier arrays
printf("\n���-LEXICAL ANALYZER ���-\n\n");
printf("Enter the String\n\n"); //Accepts expression from user
scanf ("%[^\n]%*c", expr); // �%[^\n]%*c� allows user to include spaces in entered
string
cnt = strlen(expr);
printf("\nString length is: %d\n", cnt); //(OPTIONAL)To display string length

for (i = 0; i < cnt; ++i) //Loop to store ascii values in array ascii
{
ascii[i] = (int)expr[i];
}

for(i=0;i<cnt;++i)

{
if( isdigit(expr[i]) ) // Condition for current element to be digit
{
while(isdigit(expr[i])) //Run loop until successive elements are digits
{
number=10*number+ ascii[i] -'0'; // Needed to convert for eg. �3� �4� to 34
i++;
}
j++;
constants[j]=number;
number=0;
}

if( isalpha(expr[i]) ) //Condition for current element to be a variable


{
while( isalpha(expr[i]) || isdigit(expr[i]) || expr[i]=='_' ) //Run loop till next
element is a letter or digit.
{
k++;
variables[m][k]=expr[i];
i++;
}
m++;
pr[n]=k;
n++;
k=-1;
}

if(expr[i]=='+'||expr[i]=='-'||expr[i]=='/'|| expr[i]=='*'|| expr[i]=='='||


expr[i]=='^') // Conditions to check for operators
{
l++;
operators[l]= expr[i];
}

//�������Printing the Literals����������-//


printf("\nThe literals are: \n\n");
for(i=0;i<=j;i++)
{
printf("\tlit%d\t%d \n",i+1,constants[i]);
}

//�������Printing the Operators����������-//


printf("\nThe operators are: \n\n");
for(i=0;i<=l;i++)
{
printf("\top%d\t%c\n",i+1,operators[i]);
}

//�������Printing the Varibles����������-//


printf("\nThe variables are: \n\n");
for(i=0;i<m;i++)
{
printf("\n\tid%d\t",i+1);
for(j=0;j<=pr[i];j++)
{
printf("%c",variables[i][j]);
}

printf("\n����� END OF LEXICAL ANALYZER ����\n\n");

return 0;
}

//Symbol Table

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdbool.h>

#define BUFFER_SIZE 1024


#define TOKEN_SIZE 32

typedef struct symbol


{
char* identifierName;
char* datatype;
int offset;
struct symbol* next;
}symbol;

void insert(symbol** headRef,char* identifier,char* type)


{
symbol* newnode = (symbol*) malloc(sizeof(symbol));
static symbol* prevnode;
newnode->identifierName = (char*) malloc((strlen(identifier)+1)*sizeof(char));
newnode->datatype = (char*) malloc((strlen(type)+1)*sizeof(char));

if(*headRef==NULL)
{
newnode->offset=0;
prevnode = newnode;
}

strcpy(newnode->identifierName,identifier);
strcpy(newnode->datatype,type);

if(strcmp(newnode->datatype,"int") == 0)
(newnode->offset) = (prevnode->offset) + sizeof(int);
else if(strcmp("float",newnode->datatype) == 0)
(newnode->offset) = (prevnode->offset) + sizeof(float);
else if(strcmp("char",newnode->datatype) == 0)
(newnode->offset) = (prevnode->offset) + sizeof(char);
else if(strcmp("double",newnode->datatype) == 0)
(newnode->offset) = (prevnode->offset) + sizeof(double);

prevnode = newnode;
newnode->next = (*headRef);
(*headRef) = newnode;
}

void display(symbol* node)


{
while(node!=NULL)
{
printf("%s %s %d \n",node->identifierName,node->datatype,node->offset);
node = node->next;
}
}

bool isIdentifier(const char* token)


{
if(isalpha(token[0]) || token[0]=='_')
{
for(int i=1;token[i]!=(char)NULL;i++)
{
if(!isalnum(token[i]))
return false;
}
return true;
}
return false;
}

bool isDatatype(const char* token)


{
const char *datatype[]={"char","double","float","int"};
for(size_t i=0;i<(sizeof(datatype)/sizeof(char*));i++)
{
if(strcmp(token,datatype[i])==0)
return true;
}
return false;
}

void isDeclaration(char* statement,symbol** head)


{
char *delimiters = " ,;\t";
char *token = strtok(statement,delimiters);
if(!isDatatype(token))
return;
char *datatype = token;
while((token=strtok(NULL,delimiters)))
if(isIdentifier(token))
insert(head,token,datatype);
}

FILE* openFile(int param1,char* param2[])


{
if(param1<2)
{
fprintf(stderr,"No file specified to read. \n");
exit( EXIT_FAILURE );
}

if(param1>2)
{
fprintf(stderr,"Too many arguments for read to perform. \n");
exit( EXIT_FAILURE );
}

FILE *fp = fopen(param2[1],"r");

if(fp==NULL)
{
fprintf(stderr,"File access denied on read. \n");
exit( EXIT_FAILURE );
}
return fp;
}

int main(int argc, char *argv[])


{
FILE *fp = openFile(argc,argv);

char string[BUFFER_SIZE];
symbol* head = NULL;

while(fgets(string,sizeof string, fp))


isDeclaration(string,&head);

display(head);
return 0;
}

S-ar putea să vă placă și