?? main.cpp
字號:
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<stdlib.h>
#include <ctype.h>
#include "pl0.h"
//------------------getch()---get char----------------------------------------
void getch(void){
if (cc == ll) //when first char
{
if (feof(infile))
{
printf("\nPROGRAM INCOMPLETE\n");
exit(1);
}//end(if NULL)
ll = cc = 0;
while ( (!feof(infile)) //not NULL infile
&& ((ch = getc(infile)) != '\n'))
{
printf("%c", ch); //output the char to output one line.
line[++ll] = ch; //store in line[80] to store one line.
} // while
printf("\n"); //go to next line after outputting one line.
line[++ll] = ' ';
}//end(if cc==ll)
ch = line[++cc]; //get the next char
}
//------getsym()---gets a symbol from input stream.----------------------------
void getsym(void)
{
int i, k;
char a[MAXIDLEN + 1];
while (ch == ' '|| ch == '\t'){ //ignore all space or tab
getch();
}
if (isalpha(ch))
{ // symbol is a reserved word or an identifier.
k = 0;
do{
if (k < MAXIDLEN){
cout<<ch; //output the reserved word or identifier
}
a[k++] = ch;
getch();
}while (isalpha(ch) || isdigit(ch));
a[k] = 0;
strcpy(id, a);
word[0] = id;
i = NRW;
while (strcmp(id, word[i--]));
if (++i){ // symbol is a reserved word
cout<<" ->reserved word 保留字"<<endl;
}else{ // symbol is an identifier
cout<<" ->identifier標識符"<<endl; }
}
else if (isdigit(ch))
{ // symbol is a number.
do{
cout<<ch;
getch();
}while (isdigit(ch));
cout<<" ->number 數字"<<endl;
}
else if (ch == ':')
{
cout<<ch;
getch();
if (ch == '='){
cout<<ch; // :=
cout<<" ->avaluate symbol賦值符號"<<endl;
getch();
}
}
else if (ch == '>')
{ cout<<ch; // >
getch();
if (ch == '=')
{ cout<<ch; // >=
getch();
}
cout<<" ->symbol符號"<<endl;
}
else if (ch == '<'){
cout<<ch; // <
getch();
if (ch == '=')
{ cout<<ch; // <=
getch();
}
else if (ch == '>')
{ cout<<ch; // <>
getch();
}
cout<<" ->symbol符號"<<endl;
}else{ // other tokens--symbols--'+','-','{','}',...
i = NSYM;
csym[0] = ch;
cout<<ch;
while (csym[i--] != ch);
if (++i){
cout<<" ->symbol符號"<<endl;
getch();
}else{
exit(1);
}
}
} // getsym
////////////////////////////////////////////////
int main(){
char s[80];
printf("Please input source file name: "); // get file name to be compiled
scanf("%s", s);
if ((infile = fopen(s, "r")) == NULL)
{
printf("File %s can't be opened.\n", s);
exit(1);
}
cc = cx = ll = 0; // initialize global variables
ch = ' ';
for(int m=0;m<MAXIDLEN;m++){
cout<<endl;
getsym();
while(cc<ll) getsym();
}//(for m)
fclose(infile);
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -