Home > Back-end >  Ask god help me a lexical compiler can file input and output
Ask god help me a lexical compiler can file input and output

Time:09-26

#include
#include
#include
# include & lt; Stdlib. H>

Char prog [80], token [8].
char ch;
Int the syn, p, m, n, sum;
Int count, government;//=1 -, 2=+, 0=wu
Int xiaoshu, zhishu, zhishumi, fuhao;
Int temp2;
Double temp, shixiaoshu;
Int repeat;
Char * rwtab [6]={" function ", "if", "then", and "while" and "do" and "endfunc"};
FILE *fp;



Void printToFile ()
{
If ((fp=fopen (" E: \ \ jieguo TXT ", "a"))==NULL)
{
Printf (" the file save failed! \n");
exit(0);
}
Do
{
Scaner ();
The switch (syn)
{
Case 11:
Fputc (' (', fp);
Fprintf (fp, "% d, % s", the syn, token);
Fputc ('), fp);
Fputc (' \ n 'fp);
break;
Case 1:
Fprintf (fp, "% s", "error");
break;
////////////////processing blank characters
Case: 100
break;
//////////////////////*
Default:
Fputc (' (', fp);
Fprintf (fp, "% d, % s", the syn, token);
Fputc ('), fp);
Fputc (' \ n 'fp);
}
} while (syn!=0);
}


Void redToFile ()
{
If ((fp=fopen (" E: \ \ yuanma TXT ", "r"))==NULL)
{
Printf (" the file read failure! \n");
exit(0);
}
While ((ch=fgetc (fp))!=(EOF)
{
Prog [+ + p]=ch;
Printf (" % c ", ch);////////////the file output to the screen.
}
fclose(fp);
}

Void main () {
Int choose1, choose2;
P=0;
count=0;
Shixiaoshu=0;
Zhishu=0;
Repeat=0;


Printf (" input 1 from the file into a string (yuanma. TXT) \ n ");
Printf (" input 2 said from the keyboard input string \ n ");
Printf (" please enter your choice: \ n ");
The scanf (" % d ", & amp; Choose1);

Printf (" input 1 says it will result in file (jieguo. TXT) \ n ");
Printf (" input 2 said will according to the results to the screen \ n ");
Printf (" please enter your choice: \ n ");
The scanf (" % d ", & amp; Choose2);
If (choose1==2)//////////input from the keyboard
{
P=0;
Printf (" do input string: \ n ");
Do
{
The scanf (" % c ", & amp; Ch);
Prog [+ + p]=ch;
} while (ch!='#');
}
The else///////////////read from the file
{
RedToFile ();
}
P=0;
If (choose2==2)
{
If ((fp=fopen (" E: \ \ yuanma TXT ", "r"))==NULL)
{
Printf (" the file read failure! \n");
exit(0);
}
Do {
The scanf (" % c ", & amp; Ch);
Prog [p++]=ch;
}
While (ch!='#');
P=0;
Do {
Scaner ();
The switch (syn)
{
Case 11:
If (xiaoshu==0) {
Printf (" \ n (% d, % d) ", the syn, sum); break;
}
Else if (zhishu==1) {
Printf (" \ n \ n (% d, % 10.5 e) ", the syn, sum);
Zhishu=0; Xiaoshu=0;
break;
}
Else if (xiaoshu==1) {
Printf (" % d, % (8.4 f) \ n ", the syn, sum);
Xiaoshu=0;
break;
}
Case 1: printf (" \ n erron ");
break;

Default: printf (" \ n (% d, % s) ", the syn, token);
}
} while (syn!=0);
The else
{printToFile (); }
}
}
Void scaner ()
{
Sum=0;
Shixiaoshu=0;
For (n=0; n<8; N++)
Token [n]=NULL;
Ch=prog [p++];
While (ch=='| | ch==' \ n ')
Ch=prog [p++];
If ((ch>='a' & amp; & Ch<='z') | | (ch>='A' & amp; & Ch<='Z'))
{m=0;
While ((ch>='a' & amp; & Ch<='z') | | (ch>='0' & amp; & Ch<='9') | | (ch>='A' & amp; & Ch<='Z'))
{
Token [m++]=ch;
Ch=prog [p++];
}
Token [m]='\ 0';
Ch=prog [p -];
The syn=10;
For (n=0; n<6; N++)
If (STRCMP (token, rwtab [n])==0)
{
The syn=n + 1;
break;
}
}
The else
If (ch>='0' & amp; & Ch<='9')
{
IsNum:
If (government==1) {
Token [m++]='-';
}///////////

While (ch>='0' & amp; & Ch<='9')
{
Sum=sum * 10 + ch - '0';
Ch=prog [p++];
}
If (ch==') {
Xiaoshu=1;
Ch=prog [p++];
count=0;
While (ch>='0' & amp; & Ch<='9') {
Temp=(ch - '0') * pow (0.1 + + count);
Shixiaoshu=shixiaoshu + temp;
Ch=prog [p++];
}
Sum=sum + shixiaoshu;
}
If (ch=='e' | | ch=='e') {
Zhishu=1;
Ch=prog [p++];
If (ch=='-') {
Fuhao=1;
Ch=prog [p++];
}
While (ch>='0' & amp; & Ch<='9')
{
Zhishu=zhishu * 10 + ch - '0';
Ch=prog [p++];
}
If (fuhao)
Sum=sum * pow (0.1, fuhao);
The else
Sum=sum * pow (10, fuhao);
}
If (government==1)
{
Sum=- sum;
Government=0;
}

Ch=prog [p -];
The syn=11;
}
The else
The switch (ch)
{
Case '& lt; ': m=0; Token [m++]=ch;
Ch=prog [p++];
If (ch=='& gt; ')
{
The syn=21;
Token [m++]=ch;
}
If (ch=='=')
{
The syn=22;
Token [m + 1]=ch;
}
The else
{
The syn=20;
Ch=prog [p -];
}
break;
Case '& gt; ': m=0; Token [m++]=ch;
Ch=prog [p++];
If (ch=='=')
{
The syn=24;
Token [m++]=ch;
}
The else
{
The syn=23;
Ch=prog [p++];
}
break;
Case: '=' m=0; Token [m++]=ch;
Ch=prog [p++];
If (ch=='=')
{
The syn=25;
Token [m++]=ch;
}
The else
{
The syn=18;
Ch=prog [p -];
}
break;
Case '! ': m=0; Token [m++]=ch;
Ch=prog [p++];
If (ch=='=')
{
The syn=22;
Token [m++]=ch;
}
The else
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related