📄 ci.txt
字号:
#include<math.h>
#include <fstream.h>
#include<string>
using std::string;
////////////////////////////////////////////
const int norw=9; // {保留字个数}
const int txmax=100; // {标识符表的长度}
const int nmax=14; //{数中数字的最大个数}
const int a1=10; //{标识符的长度}
const int amax=20471;//{最大地址}
const int levmax=3; // {程序体嵌套的最大深度}
const int cxmax=200; //{代码数组的大小}
const int writex=20;
const stacksize=100;
///////////////////////////////////////////
class symset{
public:
string syms[15];
int s;
symset(){
s=0;
}
symset operator +(symset a){
symset temp;
for(int i=0;i<s;i++)
//for(int j=16;j>=0;j--)
{
temp.syms[i]=syms[i];
}
for(i=0;i<a.s;i++)
//for(int j=16;j>=0;j--)
{
temp.syms[i+s]=a.syms[i];
}
temp.s=s+a.s;
return temp;
}
symset operator +(string ch){
symset temp;
for(int i=0;i<s;i++)
//for(int j=16;j>=0;j--)
{
temp.syms[i]=syms[i];
}
temp.syms[i+1]=ch;
temp.s=s+1;
return temp;
}
};
//////////////////////////////////////////////////
class yy{
private:
enum fct{lit,opr,lod,sto,cal,int1,jmp,jpc};
enum object{constant,variable};
struct instruction{
fct f; // {功能码}
int l;//{层}
int a; //{相对地址}
};
struct varibl{
int level; // //{层}
int dr;
};
union value{
int val;
varibl vari;
};
struct tabl{
string name;
object kind;
value vv;
};
tabl table[txmax];
int b;
int s[stacksize];
ifstream fin,fi;
ofstream fout;
int dx; // {数据分配下标}
int cx0; // {起始代码的下标}
char ch; // {最近读到的字符}
// symbol sym;
string sym; // {最近读到的符号}
string id; // {最近读到的标识符}
int num; // {最近读到的数}
int cc; // {字符计数}
int ll; // {行长}
int wx;
int kk,err,cw;
int cx; //{代码分配下标}
string line;//:array[1..81]of char;[81]
string a;
int tx;
int lev;//层次
symset declbegsys,statbegsys,facbegsys,aaa,bbb;
string chwr[writex]; //chwr:array[1..writex] of alfa;
instruction code[cxmax];//code:array[0..cxmax] of instruction;
string mnemonic[8];
string word[norw];//word:array of alfa;
string wsym[norw];//:array[1..norw] of symbol;
void error(int n);
void getch();
void getsym(); // {读单词子程序}
void gen(fct x,int y,int z); // {代码生成子程序}
void test(symset s1,symset s2,int n);
void enter(object k); // {把object填入标识符表中}
int position(string id); // { 在标识符表中查标识符id }
void constdeclaration();// {常量说明处理子程序}
void vardeclaration(); //{变量说明处理子程序}
void listcode(); // {列出本程序体生成的代码子程序}
void factor(symset fsys); //{因子处理子程序
void term(symset fsys); // {项处理子程序}
void expression(symset fsys); //{表达式处理子程序}
void condition(symset fsys); // {条件表达式处理子程序开始}
void statement(symset fsys); //{语句处理子程序}
void block(int lev,int tx,symset fsys); //{分程序处理模块}
void interpret(); // {执行目录代码子程序}
int incl(symset fsys);
int base(int l);
public:
yy();
};
void yy::error(int n){
fout<<"**** "<<n;
err=err+1;
}
int yy::incl(symset fsys){
int i=0;
while(i<fsys.s){
if(fsys.syms[i]==sym) return 1;
i++;
}
return 0;
}
void yy::getch(){
if(cc==ll){
if(fin.eof()){
fout<<"program incompletet";
exit(0);
}
ll=0;cc=0; fout<<cw<<' ';
cw=cw+1;
// std::getline(fin,line);
fin.get(ch);
while(ch!='\n')//!eoln(ff)!
{
ll=ll+1;
fin.get(ch);
fout<<ch;
line[ll]=ch;
}
// writeln(fw2);
ll=ll+1;
//fin.read(line[ll]);
}
cc=cc+1;
ch=line[cc];
}
void yy::getsym(){
int i,j,k;
// {读单词子程序开始}
while(ch==' ')getch();
if('a'<=ch&&ch<='z'){ //in ['a'..'z']
k=0;
do{
if(k<a1){
k=k+1;
a[k]=ch;
}
getch();
}while('a'<=ch&&ch<='z');// in['a'..'z','0'..'9']
if(k>=kk)kk=k;
else{
do{ a[kk]=' ' ;
kk=kk-1;
}while(kk!=k);
}
id=a;
i=1;
j=norw;
do{
k=(i+j)/2;
if(id<=word[k]) j=k-1;
if(id>=word[k]) i=k+1;//重载
}while(i<=j);
if (i-1>j) sym=wsym[k];
else sym="ident";
} // {标识符或保留字处理结束}
else if('0'<=ch&&ch<='9' ){ // ch in ['0'..'9']
// {数处理}
k=0;num=0;sym="number";
do{
num=10*num+(ch-'0');
k=k+1;getch();
}while('0'<=ch&&ch<='9');
if (k>nmax) error(30);//数太大
} // {数处理结束}
else if(ch=='=' ){
sym="becomes";
getch();
if(ch=='='){sym="eql";getch();}
}//== =
else {
switch(ch){
case '+': {
sym="plus";
getch();}
break;
case '-': {
sym="minus";
getch();}
break;
case '*': {
sym="times";
getch();}
break;
case ',': {
sym="comma";
getch();}
break;
case '/': {
sym="slash";
getch();}
break;
case '(': {
sym="lparen";
getch();}
break;
case ')': {
sym="rparen";
getch();}
break;
case ';': {
sym="semicolon";
getch();}
break;
case '{': {
sym="beginsym";
getch();}
break;
case '}': {
sym="endsym";
getch();}
break;
case '>':{getch();
if(ch=='='){
sym="geq";
getch();
}
else sym="gtr";}
break;
case '<':{getch();
if(ch=='='){
sym="leq";
getch();}
else sym="lss";}
break;
case '!':{
getch();
if(ch=='='){sym="neq";
getch();
}
else sym="nul";
}
break;
}// {case }
}//{读单词子程序结束
} // {读单词子程序}
void yy::gen(fct x,int y,int z){
if (cx>cxmax){
fout<<"program too long";
exit(0);
}
code[cx].f=x;
code[cx].l=y;
code[cx].a=z;
cx=cx+1;
} // {代码生成子程序}
void yy::test(symset s1,symset s2,int n){
if(!incl(s1)){//!(sym in s1)
error(n);
s1=s1+s2;//重载+
while(!(incl(s1))) getsym();
}
}
void yy::enter(object k){
tx=tx+1;
table[tx].name=id;
table[tx].kind=k;
if(k==constant){
if(num>amax)
{
error(30);//数太大
num=0;
}
table[tx].vv.val=num;
}
else if(k==variable){
table[tx].vv.vari.level=lev;
table[tx].vv.vari.dr=dx;
dx=dx+1;
}
} // {把object填入标识符表中}
int yy::position(string id){
int i;
table[0].name=id;
i=tx;
while(table[i].name!=id) i=i-1;
return i;// position=i;
} // { 在标识符表中查标识符id }
void yy::constdeclaration(){
if(sym=="ident")
{
getsym();
if(sym!="becomes") error(3);//常量后赋值
//改造error设置等号
getsym();
if(sym=="number"){
enter(constant);
getsym();
}
else error(2); //常量后为数字
}
else error(4);//const后为标识符
}// {常量说明处理子程序}
void yy::vardeclaration(){
if(sym=="ident")
{
enter(variable);
getsym();
}
else error(4);//int 后为标识符
} //{变量说明处理子程序}
void yy::listcode(){
int i;
for(i=cx0; i<=cx-1;i++);//局部
fout<<i<<" "<<" "<<code[i].l<<" "<<code[i].a;
} // {列出本程序体生成的代码子程序}
void yy::factor(symset fsys){
int i;
test(facbegsys,fsys,24);
while(incl(facbegsys))
{
if(sym=="ident") {
i=position(id);
if(i==0) error(11); //标识符未说明
else {
switch(table[i].kind){
case constant:gen(lit,0,table[i].vv.val);
break;
case variable:gen(lod,lev-table[i].vv.vari.level,table[i].vv.vari.dr);
break;
}
}
getsym();
}
else if(sym=="number")
{
if(num>amax)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -