📄 casl.y
字号:
return 0;
}
void showErrMsg(char *s)
{
printf("Line %d:%s", g_iLineNumber - 1, s);
}
/*
#ifndef YY_INPUT
#define YY_INPUT(buf,result,max_size) \
if ( yy_current_buffer->yy_is_interactive ) \
{ \
int c = '*', n; \
for ( n = 0; n < max_size && \
(c = getc( yyin )) != EOF && c != '\n'; ++n ) \
buf[n] = (char) c; \
if ( c == '\n' ) \
buf[n++] = (char) c; \
if ( c == EOF && ferror( yyin ) ) \
YY_FATAL_ERROR( "input in flex scanner failed" ); \
result = n; \
} \
else if ( ((result = fread( buf, 1, max_size, yyin )) == 0) \
&& ferror( yyin ) ) \
YY_FATAL_ERROR( "input in flex scanner failed" ); \
for (int i = 0; i < result; i++) \
{ \
if (buf[i] >= 'a' && buf[i] <= 'z') \
{ \
buf[i] = buf[i] - ('a' - 'A'); \
} \
} \
cout << "Get It" << endl;
#endif
*/
int main(int argc, char * argv[])
{
//---------variables definitions begins------------------------
CFileMapping mapFile;
string strFile;
string strTemp;
FILE * fp;
int * lpData = NULL;
unsigned int uiIndex = 0;
size_t i = 0;
size_t uiSize = 0;
//---------variables definitions ends ------------------------
//进行编译器初始化工作
if (CaslUtil::CASL_UTIL_OP_OK != CaslUtil::CaslCompilerInitialize())
{
cerr << "error during the initialization of the Casl compiler!!!" << endl;
return COMPILING_FAILED;
}
//命令行参数是否大于1
if (argc > 1)
{
//检查待编译的文件是否为编译器支持的类型
strFile = argv[1];
uiIndex = strFile.rfind(".");
//文件没有扩展名,视为出错
if (uiIndex >= strFile.npos)
{
cout << "Only Support .csl File";
return COMPILING_FAILED;
}
//文件扩展名不是支择的类型,也视为出错
strTemp = strFile.substr(uiIndex);
CaslUtil::MakeLowerCase(strTemp);
if (strTemp.compare(".csl"))
{
cout << "Only Support .csl File";
return COMPILING_FAILED;
}
fp = fopen(argv[1], "r");
if (NULL == fp)
{
cerr << "File " << argv[1] << "Failed!" << endl;
return COMPILING_FAILED;
}
}
else
{
cout << "Usage: Casl_Tab " << " SourceFileName " << endl;
return COMPILING_WITHOUT_PARAM;
}
if (NULL != fp)
{
yyin = fp;
}
else
{
yyin = stdin;
}
yyparse();
//如果编译的源程序中没有一条可执行指令的放,则视为源程序出错
if (g_vecIntermediateCode.size() == 0)
{
g_vecErrMsg.push_back(CompilingErrMsg(0, NO_EXEC_STATEMENT));
}
//如果编译过程中出错,就不需要再进行后面的代码生成工作了!
if (g_vecErrMsg.size() > 0 || g_bCompilingErr)
{
//完成错误信息有合并
CaslUtil::MergeErrMsg(g_vecErrMsg, g_verUnknownErrMsg);
cout << "Compiling Failed! " << endl;
//将错误信息写入共享内存文件中,以便于IDE处理
if (CFileMapping::FILE_MAP_OK != mapFile.CreateSharedMapFile(g_szMapNameForCompilingErrMsg, g_uiSizeOfMapOfCompilingErrMsg))
{
return COMPILING_FAILED;
}
lpData = (int*)mapFile.GetMapFileBase();
if (lpData != NULL)
{
//将错误个数先写入内存文件中
*((unsigned int*)lpData) = g_vecErrMsg.size();
lpData++;
uiSize = g_vecErrMsg.size();
//依次将所有的错误信息写入内存映射文件中
cout << "****************************Compiling Error Begins*****************************************" << endl;
for (i = 0;i < uiSize; i++)
{
*lpData = g_vecErrMsg[i].iLine;
lpData++;
(*(unsigned int *)lpData) = g_vecErrMsg[i].uiMsgID;
lpData++;
//向控制台输出错误信息
cout << "Line " << g_vecErrMsg[i].iLine << " : ";
cout << "Message ID:" << g_vecErrMsg[i].uiMsgID << ", Content:" << g_szCompilingErrMsg[g_vecErrMsg[i].uiMsgID] << endl;
}
cout << "Write Errmsg " << uiSize << endl;
cout << "****************************Compiling Error Ends" << endl;
}
else
{
mapFile.CloseSharedMapFile();
return COMPILING_FAILED;
}
mapFile.CloseSharedMapFile();
//返回2表示编译过程中发现源程序出错
return COMPILING_WITH_ERROR;
}
#ifdef DEBUG_MODE
//--------------输出生成的中间代码表 begins------------------------------------
cout << "//--------------输出中间代码表-----------------------------" << endl;
uiSize = g_vecIntermediateCode.size();
for (i = 0; i < uiSize; i++)
{
cout << "指令: " << g_szInstructionName[g_vecIntermediateCode[i].type] << " ";
//-----------输入操作数1-------------------------------------
cout << "操作数1: " << g_szOperandTypeName[g_vecIntermediateCode[i].operand1.type] << "--";
if (REG_OPERAND == g_vecIntermediateCode[i].operand1.type)
{
cout << g_vecIntermediateCode[i].operand1.OperandVal.regOperand.regNumber;
}
else if (EA_OPERAND == g_vecIntermediateCode[i].operand1.type)
{
cout << g_vecIntermediateCode[i].operand1.OperandVal.eaOperand.regNumber << "+" ;
if (LABEL_EA_ADDRESS == g_vecIntermediateCode[i].operand1.OperandVal.eaOperand.address.type)
{
cout << g_vecIntermediateCode[i].operand1.OperandVal.eaOperand.address.addressVal.labelAddress.strLabelName;
}
else if (DEC_EA_ADDRESS == g_vecIntermediateCode[i].operand1.OperandVal.eaOperand.address.type)
{
cout << g_vecIntermediateCode[i].operand1.OperandVal.eaOperand.address.addressVal.decAddress;
}
}
else if (EMPTY_OPERAND == g_vecIntermediateCode[i].operand1.type)
{
cout << "empty Operand";
}
else
{
cout << "unknown operand type" ;
}
cout << " ";
//-----------输入操作数2-------------------------------------
cout << "操作数2: " << g_szOperandTypeName[g_vecIntermediateCode[i].operand2.type] << " ";
if (REG_OPERAND == g_vecIntermediateCode[i].operand2.type)
{
cout << g_vecIntermediateCode[i].operand2.OperandVal.regOperand.regNumber;
}
else if (EA_OPERAND == g_vecIntermediateCode[i].operand2.type)
{
cout << g_vecIntermediateCode[i].operand2.OperandVal.eaOperand.regNumber << "+" ;
if (LABEL_EA_ADDRESS == g_vecIntermediateCode[i].operand2.OperandVal.eaOperand.address.type)
{
cout << g_vecIntermediateCode[i].operand2.OperandVal.eaOperand.address.addressVal.labelAddress.strLabelName;
}
else if (DEC_EA_ADDRESS == g_vecIntermediateCode[i].operand2.OperandVal.eaOperand.address.type)
{
cout << g_vecIntermediateCode[i].operand2.OperandVal.eaOperand.address.addressVal.decAddress;
}
}
else if (EMPTY_OPERAND == g_vecIntermediateCode[i].operand2.type)
{
cout << "empty Operand";
}
else
{
cout << "unknown operand type" ;
}
cout << endl;
}
//--------------输出生成的中间代码表 ends------------------------------------
//--------------输出变量表 begins-------------------------------------------
cout << "//--------------输出变量表-----------------------------" << endl;
uiSize = g_vecVariable.size();
for (i = 0; i < uiSize; i++)
{
cout << g_vecVariable[i].strLabelName << " " ;
cout << g_vecVariable[i].iSpaceCnt;
cout << endl;
}
//--------------输出变量表 ends-------------------------------------------
//--------------输出标号表 begins----------------------------------------
cout << "//--------------输出标号表-----------------------------" << endl;
LabelMap::iterator itMap = g_mapLabel.begin();
for (; itMap != g_mapLabel.end(); itMap++)
{
cout << itMap->second << endl;
}
//--------------输出标号表 ends-------------------------------------------
//输出常量表
cout << "//--------------输出常量表-----------------------------" << endl;
uiSize = g_vecConst.size();
for (i = 0; i < uiSize; i++)
{
cout << "Const Type---" << g_szConstTypeName[g_vecConst[i].type] << " ";
cout << " Const Name--" << g_vecConst[i].strLabelName << " ";
switch(g_vecConst[i].type)
{
case DEC_CONST:
cout << g_vecConst[i].constData.decVal;
break;
case HEX_CONST:
cout << g_vecConst[i].constData.hexVal;
break;
case LABEL_CONST:
cout << g_vecConst[i].constData.labelVal;
break;
case STR_CONST:
cout << g_vecConst[i].constData.strVal;
break;
}
cout << endl;
}
#endif
//生成变量标号表,常量标号表,指令标号表
if (CaslUtil::CASL_UTIL_OP_OK != CaslUtil::MakeVariableLabelTable(g_vecVariable, g_vecVariableLabel))
{
cerr << "Make Variable Lable Table Failed!" << endl;
return COMPILING_FAILED;
}
if (CaslUtil::CASL_UTIL_OP_OK != CaslUtil::MakeConstLabelTable(g_vecConst, g_vecConstLabel))
{
cerr << "Make Const Lable Table Failed!" << endl;
return COMPILING_FAILED;
}
if (CaslUtil::CASL_UTIL_OP_OK != CaslUtil::MakeInstructionLabelTable(g_mapLabel, g_vecInstructionLabel))
{
cerr << "Make Instruction Lable Table Failed!" << endl;
return COMPILING_FAILED;
}
//生成目标代码
if (argc <= 1)
{
strFile = "test.cse";
}
else
{
strFile = argv[1];
uiIndex = strFile.rfind(".");
strFile = strFile.substr(0, uiIndex);
strFile = strFile + ".cse";
}
if (CaslUtil::CASL_UTIL_OP_OK == CaslUtil::MakeObjectCode())
{
CaslUtil::WriteExeFile(strFile);
}
else
{
cout << "Make Object Code Failed!!!" << endl;
return COMPILING_FAILED;
}
#ifdef DEBUG_MODE
cout << "Totally " << g_iLineNumber << endl;
#endif
cout << "Compiling " << argv[1] << " Successfully!" << endl;
return COMPILING_OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -