Question:
C++ simplified lexical analyzer?
Username
2010-09-06 18:28:34 UTC
It's just supposed to pull the data from a txt file and display it. I can't compile it yet because I still have to wait for the .txt input file. I think the program won't work this way. Especially with the arrays. Does anybody know how to fix this?
-------------------------
#include
#include
#include
#include

/****************************************************************
Functions prototype.
*****************************************************************/

void Open_File();
void Demage_Lexeme();
int Search(char[256],int);
void analyze();
void Skip_Comment();
void Read_String();
void Is_Keyword_Or_Not();
void Is_Identifier_Or_Not();
void Is_Operator_Or_Not();
void Read_Number();
void Is_Special_Or_Not();
void Is_Comparison_Or_Not();
void Add_To_Lexical (char[256],int,char[256]);
void Print_ST();
void Print_TOKEN();
void Token_Attribute();

/****************************************************************
Data structure used in program.
*****************************************************************/
struct lexical
{
char data[256]; //Value of token.
int line[256]; //Line # which token appear in input
file.
int times; //# of times that token appear in input
file.
char type[256]; //Type of each token.
struct lexical *next;
};

typedef struct lexical Lex;
typedef Lex *lex;

/****************************************************************
File pointer for accessing the file.
*****************************************************************/

FILE *fp;
FILE *st;
FILE *token;
char lexeme[256],ch;
int f,flag,line=1,i=1;
lex head=NULL,tail=NULL;

/****************************************************************
Array
*****************************************************************/

char words[maxwordcount][maxwordlength];

int wordcount = 0;
while(inputstream >> words[wordcount ++]);

**************
*MAIN PROGRAM*
**************
void main()
{
Open_File();
analyze();
fclose(fp);
Print_ST();
Print_TOKEN();
}

/****************************************************************
This function open input sourse file.
*****************************************************************/
void Open_File()
{

fp=fopen("source.txt","r"); //provide path for source.txt here
if(fp==NULL)
{
printf("!!!Can't open input file - source.txt!!!");
getch();
exit(0);
}
}

/****************************************************************
Function to add item to structure of array to store data and
information of lexical items.
*****************************************************************/

void Add_To_Lexical (char value[256],int line,char type[256])
{
lex new_lex;

if (!Search(value,line)) //When return 1 the token not found.
{

new_lex=malloc(sizeof(Lex));

if (new_lex!=NULL)
{
strcpy(new_lex->data,value);
new_lex->line[0]=line;
new_lex->times=1;
strcpy(new_lex->type,type);
new_lex->next=NULL;

if (head==NULL)
head=new_lex;
else
tail->next=new_lex;

tail=new_lex;
}
}
}
/****************************************************************
Function to print the ST.TXT .
*****************************************************************/
void Print_ST()
{
lex x=head;
int j;

if ((st=fopen("ST.TXT","w"))==NULL)
printf("The file ST.TXT cat not open.
");

else

{
fprintf(st," %s %s %s
","Line#","Lexeme","Type");
fprintf(st," ---- ------ ----
");

while (x!=NULL)
{
if ((strcmp(x->type,"num")==0) ||
(strcmp(x->type,"keyword")==0) ||
(strcmp(x->type,"identifier")==0))
{
fprintf(st," ");

for (j=0;jtimes;j++)
{
fprintf(st,"%d",x->line[j]);
if (j!=x->times-1) //This condition to prevent the comma
fprintf(st,",",x->line[j]); //"," to not print after last line #.
}

fprintf(st," %-6s %-6s
",x->data,x->type);
}
x=x->next;
}

fclose(st);
}
}
/****************************************************************
This function read all character of strings.
*****************************************************************/

void Read_String()
{
int j=0;

do
{
lexeme[j++]=ch;
ch=fgetc(fp);
} while(isalpha(ch));

fseek(fp,-1,1);
lexeme[j]='
Four answers:
Cubbi
2010-09-07 06:43:45 UTC
This is a C program, it has nothing to do with C++ except for one lonely stream extraction operator which won't compile since it's written outside the body of any function. If you do intend to write this in C++, begin with learning how to use strings. At http://www.cplusplus.com/reference/string/string/ for example.



Whatever language it's written in, try not to write so much code without unit-testing each function and block of code first: Write a test program that implements Open_File(); and open a test file with whatever contents. Write a test program that implements Add_To_Lexical() with some hard-coded input fed to it from the the test program's main(), (hopefully you'll switch to C++ and it will take const string& value instead of the char[256] and will add a new lex object to a list instead of using manual memory allocation). Write a test program that implements Print_ST(), etc.
The Phlebob
2010-09-06 18:33:20 UTC
Experienced programmer advice: Do NOT wait for the official data file. Compiling doesn't need one, and you can use the time to clean up compilation problems. Once that's done, start debugging with a dummy file. Any old .txt file can be used. Or, if the file has to be in a particular format, write one yourself.



But don't waste time waiting for the data!
2016-04-21 02:49:11 UTC
The Lexical Analyzer reads the source code text and organizes it into elements that can be passed to a logical analyzer. For instance, it normally picks out programming language keywords and converts them to "tokens", possibly with associated values.
?
2010-09-06 18:39:31 UTC
You should Give the path of the file to the C++ Compiler from which you want to give the input..

Without the path the program would'nt run and would randomly take a garbage value..


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...