Вы находитесь на странице: 1из 7

#include<stdio.h> #include<string.h> #include<conio.h> #include<ctype.

h>

void void int void void void void void void void void void void void void void

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

struct lexical { char data[256]; int line[256]; file. int times; file. char type[256]; struct lexical *next; }; typedef struct lexical Lex; typedef Lex *lex;

//Value of token. //Line # which token appear in input //# of times that token appear in input //Type of each token.

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

char *keywords[]={"procedure","is","begin","end","var","cin","cout","if", "then","else","and","or","not","loop","exit","when", "while","until"}; char arithmetic_operator[]={'+','-','*','/'}; char *comparison_operator[]={"<",">","=","<=","<>",">="};

Array holding all special for checking. char special[]={'%','!','@','~','$'}; { Open_File(); analyze(); fclose(fp); Print_ST(); Print_TOKEN(); } 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); } }

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; } } }

int Search (char value[256],int line) {

lex x=head; int flag=0; while (x->next!=NULL && !flag) { if (strcmp(x->data,value)==0) { x->line[x->times]=line; x->times++; flag=1; } x=x->next; } return flag; } 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 ","Line#","Lexeme","Type"); fprintf(st," ---"); %s %s ---------

while (x!=NULL) { if ((strcmp(x->type,"num")==0) || (strcmp(x->type,"keyword")==0) || (strcmp(x->type,"identifier")==0)) { fprintf(st," "); for (j=0;j<x->times;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," ",x->data,x->type); } x=x->next; } fclose(st); } } void Print_TOKEN() %-6s %-6s

{ int flag=0; fp=fopen("source.txt","r"); if(fp==NULL) { printf("!!!Can't open input file - source.txt!!!"); getch(); exit(0); } else { if ((token=fopen("TOKENS.TXT","w"))==NULL) printf("The file ST.TXT cat not open. "); else { ch=fgetc(fp); while (!(feof(fp))) { if (ch==' ' && !flag) { do ch=fgetc(fp); while (ch==' '); fseek(fp,-2,1); ch=fgetc(fp); flag=1; } if (ch!=' ' && ch!=' ') fprintf(token,"%c",ch); if (ch==' ') { fprintf(token," "); Token_Attribute(); i++; flag=0; } ch=fgetc(fp); } } } fclose(fp); fclose(token); }

void Token_Attribute() { lex x=head; int j; while (x!=NULL) { if (x->line[0]==i) { fprintf(token,"token : %-4s

",x->type);

if ((strcmp(x->type,"num")==0) || (strcmp(x->type,"keyword")==0) || (strcmp(x->type,"identifier")==0)) { fprintf(token,"attribute : line#=%-4d ",i); } else { fprintf(token,"attribute : %-4s ",x->data); } } x=x->next; } fprintf(token," "); }

void analyze() { ch=fgetc(fp); while(!feof(fp)) { if(ch==' ') . { line++; ch=fgetc(fp); } if(isspace(ch) && ch==' ' ) { line++; ch=fgetc(fp); } //Compute # of lines in source.txt //Read character. //While the file is not end.

' )

if(isspace(ch) && ch!=' //The character is space. ch=fgetc(fp); if(ch=='/' || ch=='"') //Function for skipping comments in the //and '"' with display statements.

file Skip_Comment();

if(isalpha(ch)) //The character is leter. { Read_String(); Is_Keyword_Or_Not(); Is_Operator_Or_Not(); Is_Identifier_Or_Not(); } if(isdigit(ch)) Read_Number(); //The character is digit.

if (ch==';') //The character is semicolon. Add_To_Lexical(";",line,"semicolon"); if (ch==':') //The character is colon. Add_To_Lexical(":",line,"colon"); if (ch==',') //The character is comma. Add_To_Lexical(",",line,"comma"); if (ch=='(') //The character is parenthesis. Add_To_Lexical("(",line,"parenthesis"); if (ch==')') //The character is parenthesis. Add_To_Lexical(")",line,"parenthesis"); //The character is comparison_operator if (ch=='<' || ch=='=' || ch=='>') Is_Comparison_Or_Not(); Is_Special_Or_Not(); Demage_Lexeme(); if(isspace(ch) && ch==' ' ) { line++; ch=fgetc(fp); } else ch=fgetc(fp); } //After failed scaning in before cases //check the character is special or not.

void Read_String() { int j=0; do { lexeme[j++]=ch; ch=fgetc(fp); } while(isalpha(ch)); fseek(fp,-1,1); lexeme[j]='

Вам также может понравиться