📄 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 + -