can i have a sample of a working lexical analyzer??plss...i need it for my project..
Recommended Answers
Jump to PostHmm...show us some work? Lets see how far you get.
Let us know where you get stuck.
Read up on Lexical Analyzers...http://en.wikipedia.org/wiki/Lexical_analysis
Jump to PostCode tags, formatting, and ask a specific question. Your question is way too vague and there's way too much code, even with formatting and tags, to go through without some guidance.
[code=cplusplus] // paste code here
[/code]
All 6 Replies
Freaky_Chris
299
Master Poster
Hmm...show us some work? Lets see how far you get.
Let us know where you get stuck.
Read up on Lexical Analyzers...http://en.wikipedia.org/wiki/Lexical_analysis
paulo_war
-1
Newbie Poster
i have a lexical analyzer here.. but i dont know how to put loops in it..can you help me?
paulo_war
-1
Newbie Poster
/*************************************************************************
HEADRER FILES
*************************************************************************/
#include <iostream.h>
#include<conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include<string.h>
/*************************************************************************
GLOBAL DECLARARIONS FOR THE
HOLE CODE REPRESENT EACH TOKEN
CASE
*************************************************************************/
const int M_Dec=4,E_E=5,N_F_C=6;
const int CS=7,DEC=1,IF=2,SWITCH=3;
const int UK=0,N_F=1,N_F_B=2,N_F_N=3;
/*************************************************************************
USED CLASSES IN THE CODE FOR
THE COMPILER PART SIMULATION
*************************************************************************/
class ddr
{
public:
char* token;
int type;
ddr* next;
};
class Double
{
public:
char* token3;//Exists code
char* token;//for good code
char* token2;//for bad code
int type;
Double* next3;
Double* next;//for good code
Double* next2;//for bad code
};
class analayzer
{
public:
char* f_n;
char stack[10];
int stack_no;
Double* rs_head;
ddr* df_head;
Double* cd_head;
Double* thras;
analayzer(int);
void tok_s();
void create_tokens();
void add_code(char*);
int token_not_added(char*);
void compile();
int dd_detect(char*);
int dec_detect(char*);
int if_dect(char*);
int swch_dect(char*);
int lo_dect(char*);
int dig_dect(char*);
int mult_dect(char*);
void push();
void pop();
void ddrine(char*);
void error(int);
void print();
void print_tokens();
}
/*************************************************************************
MAIN CLASS FUNCTION DEFINITION
*************************************************************************/
analayzer::analayzer(int x)
{
char* f_n;
if(x==2)f_n="code1.cpp";
else f_n="code2.cpp";
this->f_n = new char[strlen(f_n)];
strcpy(this->f_n,f_n);
rs_head=NULL;
tok_s();
df_head = NULL;
cd_head = NULL;
thras = NULL;
stack_no=-1;
create_tokens();
compile();
print_tokens();
}
Double* p;
void analayzer::tok_s()
{
// this will made a link list of the defined code
rs_head=new Double;//asign a pointer from the type Double to it
rs_head->token3=new char[4];//the char * token 3 to a new char
strcpy( rs_head->token3, "int" );//give token3 int word
rs_head->token3[3]='\0';//then close end it by /0
rs_head->type=DEC;//the int part to 1
rs_head->next3=new Double;//continue the link list
p=rs_head->next3;
p->token3=new char[6];
strcpy(p->token3,"float");
p->token3[5]='\0';
p->type=DEC;
p->next3=new Double;
p=p->next3;
p->token3=new char[5];
strcpy(p->token3,"long");
p->token3[4]='\0';
p->type=DEC;
p->next3=new Double;
p=p->next3;
p->token3=new char[7];
strcpy(p->token3,"double");
p->token3[6]='\0';
p->type=DEC;
p->next3=new Double;
p=p->next3;
p->token3=new char[6];
strcpy(p->token3,"short");
p->token3[5]='\0';
p->type=DEC;
p->next3=new Double;
p=p->next3;
p->token3=new char[5];
strcpy(p->token3,"byte");
p->token3[4]='\0';
p->type=DEC;
p->next3=new Double;
p=p->next3;
p->token3=new char[3];
strcpy(p->token3,"if");
p->token3[2]='\0';
p->type=IF;
p->next3=new Double;
p=p->next3;
p->token3=new char[5];
strcpy(p->token3,"else");
p->token3[4]='\0';
p->next3=new Double;
p=p->next3;
p->token3=new char[7];
strcpy(p->token3,"switch");
p->token3[6]='\0';
p->type=SWITCH;
p->next3=new Double;
p=p->next3;
p->token3=new char[5];
strcpy(p->token3,"case");
p->token3[4]='\0';/////*************************
p->next3=new Double;
p=p->next3;
p->token3=new char[8];
strcpy(p->token3,"ddrault");
p->token3[7]='\0';
p->next3=NULL;
}
void analayzer::create_tokens()
{
FILE* file;
char ch;
char* token;
int i=0;
file = fopen(f_n,"r");
if (file == NULL)
{
cout<<"Error:an error occured while reading from file\n";
getch();
exit(EXIT_FAILURE);
}
fseek(file,0,SEEK_SET);
while((ch = fgetc(file)) !=EOF)
{
if (ch == ' ' || ch == '\n')
{
if (i!=0)
token[i]='\0';
if(token_not_added(token))
add_code(token);
i=0;
}
else
if(ch>32&&ch<48 || ch>57&&ch<65 || ch>90&&ch<95 || ch>122&&ch<127)
{
if (i!=0)
token[i]='\0';
if (token_not_added(token))
add_code(token);
token = new char[10];
token[0] = ch;
token[1] = '\0';
i=0;
if(token_not_added(token))
add_code(token);
}
else
{
if(i==0)
token = new char[10];
token[i] = ch;
i++;
}
}
}
void analayzer::add_code(char* token)
{
Double* p;
p = cd_head;
if (cd_head == NULL)
{
cd_head = new Double;
cd_head->token = new char[strlen(token)];
strcpy(cd_head->token,token);
cd_head->next = NULL;
}
else
{
while(p->next!=NULL)
p= p->next;
p->next = new Double;
p = p->next;
p->token = new char[strlen(token)];
strcpy(p->token,token);
p->next = NULL;
}
}
int analayzer::token_not_added(char* token)
{
Double* p;
p=cd_head;
while(p->next!= NULL)
p=p->next;
if (strcmp(p->token,token)==0)
return 0;
return 1;
}
void analayzer::compile()
{
Double* p;
p=cd_head;
int state = 0;
int if_stmt=0;
int flag=1;
while(flag)
{
switch(state)
{
case 0:if(dec_detect(p->token))
state = 1;
else
if (dd_detect(p->token))
{
state=3;
if_stmt=0;
}
else
if(if_dect(p->token))
state=6;
else
if(swch_dect(p->token))
state=8;
else
if(strcmp(p->token,"{")==0)
{
state=0;
push();
}
else
if(strcmp(p->token,"}")==0)
{
pop();
state=0;
}
else
if(strncmp(p->token,"case",4)==0)
state=10;
else
if(strcmp(p->token,"break")==0)
state=5;
else
if(strcmp(p->token,"else")==0)
state=0;
else
{
error(UK);
}
break;
case 1:if(dd_detect(p->token))
error(M_Dec);
else
ddrine(p->token);
state = 2;
break;
case 2:if(strcmp(p->token,",")==0)
state=1;
else
if(strcmp(p->token,";")==0)
{
state=0;
}
else
{
error(N_F);
state=0;
}
break;
case 3:if (strcmp(p->token,"=")==0)
state=4;
else
{
error(E_E);
state=0;
}
break;
case 4:if(dig_dect(p->token) || dd_detect(p->token))
{
if(if_stmt)
state=7;
else
state=5;
}
else
{
error(UK);
}
break;
case 5:if(mult_dect(p->token))
{
state=4;
if_stmt=0;
}
else
if(strcmp(p->token,";")==0)
state=0;
else
{
error(N_F);
state=0;
}
break;
case 6:if(strcmp(p->token,"(")==0)
{
state=4;
if_stmt=1;
}
else
{
error(N_F_B);
state=0;
}
break;
case 7:if(lo_dect(p->token))
{
state=4;
}
else
{
if(strcmp(p->token,")")==0)
{
state=0;
}
else
{error(N_F_B);
state=0;}}
break;
case 8:if(strcmp(p->token,"(")==0)
state=9;
else
{
error(N_F_B);
state=0;
}
break;
case 9:if(dd_detect(p->token))
state=7;
else
{
error(UK);
state=0;
}
break;
case 10:if(dig_dect(p->token))
state=11;
else
{
error(N_F_N);
state=11;
}
break;
case 11:if(strcmp(p->token,":")==0)
state=0;
else
{
error(N_F_C);
state=0;
}
break;
}
p=p->next;
if (p ==NULL)
flag=0;
}
if (stack_no>-1)
{pop();error(CS);}
}
int analayzer::dd_detect(char* token)
{
ddr* p;
p=df_head;
while(p!=NULL)
{
if (strcmp(p->token,token)==0)
return 1;
p=p->next;
}
return 0;
}
int analayzer::dec_detect(char* token)
{
Double* p;
p=rs_head;
while(p!=NULL)
{
if(strcmp(p->token3,token)==0 && p->type == DEC)
return 1;
p=p->next3;
}
return 0;
}
int analayzer::if_dect(char* token)
{
/*Double* p;
p=rs_head;
while(p!= NULL)
{*/
if(strcmp(token,"if")==0)
return 1;
/*p=p->next;
}*/
return 0;
}
int analayzer::swch_dect(char* token)
{
if(strncmp(token,"switch",6)==0)
return 1;
return 0;
}
int analayzer::mult_dect(char* token)
{
if (strcmp(token,"*")==0||strcmp(token,"+")==0||strcmp(token,"/")==0||strcmp(token,"-")==0)
return 1;
return 0;
}
void analayzer::push()
{
stack_no++;
stack[stack_no]='{';
}
void analayzer::pop()
{
if(stack_no>=0)
stack[stack_no]='\0';
// else
stack_no--;
}
int analayzer::dig_dect(char* token)
{
for (int i=0;i<strlen(token);i++)
if(!isdigit(token[i]))
return 0;
return 1;
}
int analayzer::lo_dect(char* token)
{
if (strcmp(token,">")==0||strcmp(token,"<")==0||strcmp(token,"=")==0)
return 1;
return 0;
}
void analayzer::ddrine(char* token)
{
ddr* p;
if(df_head ==NULL)
{
df_head = new ddr;
df_head->token= new char[strlen(token)];
strcpy(df_head->token,token);
df_head->next=NULL;
}
else
{
p=df_head;
while(p->next!=NULL)
p=p->next;
p->next = new ddr;
p=p->next;
p->token=new char[strlen(token)];
strcpy(p->token,token);
p->next=NULL;
}
}
void analayzer::error(int type)
{
char* ErrStr;
Double* p;
ErrStr=new char[30];
switch (type)
{
case UK:
ErrStr="\nSyntax Error:\tundefined variable";
break;
case N_F:
ErrStr="\nSyntax Error:\texpecting;";
break;
case N_F_B:
ErrStr="\nSyntax Error:\tExpecting (,),{,}";
break;
case N_F_N:
ErrStr="\nSyntax Error:\tExpecting Value";
break;
case M_Dec:
ErrStr="\nSyntax Error:\tMultiple Declaration";
break;
case E_E:
ErrStr="\nSyntax Error:\tExpecting =";
break;
case N_F_C:
ErrStr="\nSyntax Error:\tExpecting | :";
break;
case CS:
ErrStr="\nSyntax Error:\tEach { should have }:";
break;
}
if(thras==NULL)
{
thras = new Double;
thras->token2=new char[strlen(ErrStr)];
strcpy(thras->token2,ErrStr);
thras->next2=NULL;
}
else
{
p=thras;
while(p->next2!=NULL)
p=p->next2;
p->next2=new Double;
p=p->next2;
p->token2 = new char[strlen(ErrStr)];
strcpy(p->token2,ErrStr);
p->next2=NULL;
}
}
void analayzer::print()
{
Double* p;
p=thras;
if(thras==NULL)
cout<<"\nRESULT: success\n";
else
while(p!=NULL)
{
cout<<p->token2;
p=p->next2;
}
getch();
}
void analayzer::print_tokens()
{
Double* p;
p=cd_head;
int x;
cout<<"\nTokens are\n";
while(p!=NULL)
{
x++;
if(x<4)
cout<<p->token<<"\t\t";
else {
cout<<"\n\n";
x=0;
}
p=p->next;
}
}
FILE* new_file(char*);
FILE* open_file(char*);
int write_code_to_file(char*);
void main()
{
clrscr();
analayzer* comp;
int x;
char* f_n;
FILE* file;
while(1){
cout<<"1.Good Code\n2.Bad Code\n";
cin>>x;
if(x==2){
comp = new analayzer(2);
comp->print();
exit(1);
}
if(x==1){
comp = new analayzer(1);
comp->print();
exit(1);
}
else exit(1);
}
}
FILE* new_file(char* name)
{
FILE* file;
file = fopen(name,"w");
fclose(file);
return file;
}
FILE* open_file(char* f_n)
{
FILE* file;
file = fopen(f_n,"r");
fclose(file);
return file;
}
int write_code_to_file(char* f_n)
{
int row=4;
char c;
FILE* file;
file=fopen(f_n,"a+");
if (file == NULL)
return 0;
gotoxy(5,row);
c=getchar();
while (c!='~')
{
fputc(c,file);
gotoxy(wherex(),row);
c=getchar();
if (c=='\n')
{
row+=1;
gotoxy(5,row);
}
}
fclose(file);
return 1;
}
William Hemsworth
commented:
Learn to post..
-1
VernonDozier
2,218
Posting Expert
Featured Poster
Code tags, formatting, and ask a specific question. Your question is way too vague and there's way too much code, even with formatting and tags, to go through without some guidance.
[code=cplusplus] // paste code here
[/code]
paulo_war
-1
Newbie Poster
what should i do now?...
paulo_war
-1
Newbie Poster
what should i do with this "
// paste code here
"?
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.