⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exam5_2view.cpp

📁 编译原理的词法分析程序的实现
💻 CPP
📖 第 1 页 / 共 2 页
字号:
						"黑体");

				   pDC->TextOut(x,y,outstr[10]);
				  CSize strSize1 =pDC->GetTextExtent(outstr[10],outstr[10].GetLength());
				  x=x+strSize1.cx;
		
                   pDC->SelectObject(&NewFont2);


                


				  pDC->SetTextColor (RGB(30,144,255));
				 pDC->TextOut(x,y,outstr1[i]);
				 pDC->SelectObject(pOldFont);

				 CSize strSize2 =pDC->GetTextExtent(outstr1[i],outstr1[i].GetLength());
				  x=x+strSize2.cx;
				  pDC->SetTextColor (RGB(155,155,155));
				 pDC->TextOut(x,y,outstr[11]);
				  }
				}
				

                break;
            case BLOCK:            //    代码块   浅灰蓝
				{ x=0;
					if (i==0)
				{	pDC->SetTextColor (RGB(176,224,230));
				pDC->TextOut(x,y,outstr1[0]);}
				else{

				if (strcmp(outstr1[i-1],")")==0 || strcmp(outstr1[i-1],"}")==0 || strcmp(outstr1[i-1],";")==0 )
					{	y=y+tm.tmHeight+100*tm.tmExternalLeading;
				        x=0;
						floor[f]=i;
						f=f+1;
						for (int k=i-1;k>=0;k--)
						{  strSize2[k] =pDC->GetTextExtent(outstr1[k],outstr1[k].GetLength());
						    flength[f]=flength[f]+strSize2[k].cx;

						}

					}	


				  for (int j=0;j<=i-1;j++)
				  {	 
					 
				   pDC->GetTextMetrics (&tm);
				  strSize[j] =pDC->GetTextExtent(outstr1[j],outstr1[j].GetLength());
				 x=x+strSize[j].cx;
				  } 
				  
				  x=x+i*5;

				  
				  
				   if (f>=1)
					{   
						for (int k=f-1;k>=0;k--)
						{
							x=x-flength[k];
						}
						x=x-LENGTH;
					}
				 
				   
				  pDC->SetTextColor (RGB(176,224,230));
				 pDC->TextOut(x,y,outstr1[i]);
                 
				}
				}

                break;
            }
		

				
		}
        
    }
    catch (InterpExc exc)
    {
        sntx_err(exc.get_err());
    
    }
    
   
   
    
 
    
 
}

/////////////////////////////////////////////////////////////////////////////
// CExam5_2View printing

BOOL CExam5_2View::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CExam5_2View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CExam5_2View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CExam5_2View diagnostics

#ifdef _DEBUG
void CExam5_2View::AssertValid() const
{
	CView::AssertValid();
}

void CExam5_2View::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CExam5_2Doc* CExam5_2View::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CExam5_2Doc)));
	return (CExam5_2Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CExam5_2View message handlers




/**
获取令牌
*/
tok_types get_token()
{
    char* temp;

    token_type = UNDEFTT;
    tok = myUNDEFTOK;

    temp = token;
    *temp = '\0';

    while (isspace(*prog) && *prog)
        ++prog;

    while (*prog == '\r')
    {
        ++prog;
        ++prog;

        while (isspace(*prog) && *prog)
            ++prog;
    }

    //程序是否结束
    if (*prog == '\0')
    {
        *token ='\0';
        tok = myEND;
        return (token_type = DELIMITER);
    }

    //语句块
    if (strchr("{}", *prog))
    {
        *temp = *prog;
        temp++;
        *temp = '\0';
        prog++;
        return (token_type = BLOCK);
    }

    //是否为注释
    if (*prog == '/')
        if (*(prog + 1) == '*')
        {
            prog += 2;
            do
            {
                while (*prog != '*') prog++;

                prog++;

            }
            while (*prog != '/');
            prog++;
            return (token_type = DELIMITER);
        }
        else if (*(prog + 1) == '/')
        {
            prog += 2;
            while (*prog != '\r' && *prog != '\0') prog++;

            if (*prog == '\r') prog += 2;

            return (token_type = DELIMITER);
        }

    //检查双目操作符
    if (strchr("!<>=+-", *prog))
    {
        switch (*prog)
        {
        case '=':
            if (*(prog + 1) == '=')
            {
                prog++;
                prog++;
                *temp = EQ;
                temp++;
                *temp = EQ;
                temp++;
                *temp = '\0';
            }
            break;
        case '!':
            if (*(prog + 1) == '=')
            {
                prog++;
                prog++;
                *temp = NE;
                temp++;
                *temp = NE;
                temp++;
                *temp = '\0';
            }
            break;
        case '<':
            if (*(prog + 1) == '=')
            {
                prog++;
                prog++;
                *temp = LE;
                temp++;
                *temp = LE;
            }
            else if (*(prog + 1) == '<')
            {
                prog++;
                prog++;
                *temp = LS;
                temp++;
                *temp = LS;
            }
            else
            {
                prog++;
                *temp = LT;
            }
            temp++;
            *temp = '\0';
            break;
        case '>':
            if (*(prog + 1) == '=')
            {
                prog++;
                prog++;
                *temp = GE;
                temp++;
                *temp = GE;
            }
            else if (*(prog + 1) == '>')
            {
                prog++;
                prog++;
                *temp = RS;
                temp++;
                *temp = RS;
            }
            else
            {
                prog++;
                *temp = GT;
            }
            temp++;
            *temp = '\0';
            break;
        case '+':
            if (*(prog + 1) == '+')
            {
                prog++;
                prog++;
                *temp = INC;
                temp++;
                *temp = INC;
                temp++;
                *temp = '\0';
            }
            break;
        case '-':
            if (*(prog + 1) == '-')
            {
                prog++;
                prog++;
                *temp = DEC;
                temp++;
                *temp = DEC;
                temp++;
                *temp = '\0';
            }
            break;
        }
        if (*token)
            return (token_type = DELIMITER);
    }

    //其它标点
/*	char mystring[13]=['+','-','*','^','/','%','=',';',':','(',')',''','"'];
		

		for (int i=0;i<13;i++)
		
		{ 
			ptr=strchr("mystring[i]", *prog);
		    if (ptr!=NULL)
			{ *temp = *prog;
        prog++;
        temp++;
        *temp = '\0';
        return (token_type = DELIMITER);

			}
		}*/
    if (strchr("+-*^/%=;:(),'", *prog))
    {
        *temp = *prog;
        prog++;
        temp++;
        *temp = '\0';
        return (token_type = DELIMITER);
    }

    //读取引用字符串
    if (*prog == '"')
    {
        prog++;
        while (*prog != '"' && *prog != '\r' && *prog)
        {
            //检查\n空白间隔
            if (*prog == '\\')
            {
                if (*(prog + 1) == 'n')
                {
                    prog++;
                    *temp++ = '\n';
                }
            }
            else if ((temp - token) < MAX_T_LEN)
                *temp++ = *prog;
            prog++;
        }
        if (*prog == '\r' || *prog == 0)
            throw InterpExc(SYNTAX);
        prog++;
        *temp = '\0';
        return (token_type = STRING);
    }

    //读取一个整型数字
    if (isdigit(*prog))
    {
        while (!isdelim(*prog))
        {
            if ((temp - token) < MAX_ID_LEN)
                *temp++ = *prog;

            prog++;
        }
        *temp = '\0';
        return (token_type = NUMBER);
    }

    //读取标识符或者关键字
    if (isalpha(*prog))
    {
        while (!isdelim(*prog))
        {
            if ((temp - token) < MAX_ID_LEN)
                *temp++ = *prog;
            prog++;
        }
        token_type = TEMP;
    }
    *temp = '\0';
    //确定是标识符还是关键字
    if (token_type == TEMP)
    {
        tok = look_up(token);
        if (tok) token_type = KEYWORD;
        else
            token_type = IDENTIFIER;
    }

    //检查未定义的字符
    if (token_type == UNDEFTT)
        throw InterpExc(SYNTAX);


    return token_type;
}

/**
返回一个令牌到输入流中
*/
void putback()
{
    char* t;

    t = token;
    for (; *t; t++)
        prog--;
}
/**
判断c是否一个分隔符号
*/
bool isdelim(char c)
{
    if (strchr(" !:;,+-<>'/*%^=()", c) || c == 9 ||
            c == '\r' || c == 0)
        return true;

    return false;
}
/**
在token table中查找一个令牌的内部形式
*/
token_ireps look_up(char* s)
{
    int i;
    char* p;

    p = s;
    while (*p)
    {
        *p = tolower(*p);
        p++;
    }

    for (i = 0; *com_table[i].command; i++)
    {
        if (!strcmp(com_table[i].command, s))
            return com_table[i].tok;
    }

    return myUNDEFTOK;
}
/**
装入一个程序
*/
bool load_program(char* p, char* fname)
{
    int i = 0;

    ifstream in(fname, ios::in | ios::binary);
    if (!in)
    {
        //cout << "Cannot Open file.\n";
        return false;
    }

    do
    {
        *p = in.get();
        p++;
        i++;
    }
    while (!in.eof() && i < PROG_SIZE);

    if (i == PROG_SIZE)
    {
        //cout << "Program  Too Big.\n";
        return false;
    }

    //用null结束程序,
    //跳过文件中的EOF标志
    if (*(p - 2) == 0x1a) *(p - 2) = '\0';
    else
        *(p - 1) = '\0';

    in.close();

    return true;
}
/**
显示一个错误信息
*/
void sntx_err(error_msg error)
{
    char *p, *temp;
    int linecount = 1;

    static char* e[] =
        {
            "语法错误",
            "当前没有表达式",
            "不是一个变量",
            "重复的变量名",
            "重复的函数名",
            "需要分号",
            "花括号不匹配",
            "函数未定义",
            "需要明确的类型",
            "Return without call",
            "圆括号不匹配",
            "需要while",
            "Closing quote expected",
            "除数为零",
            "需要左大花括号(switch语句需要语句块)",
            "需要冒号"
        };

    //cout << "\n" << e[error];
    p = p_buf;
    while (p != prog)
    {
        p++;
        if (*p == '\r')
        {
            linecount++;
        }
    }
    // << " in line " << linecount << endl;

    temp = p;
    while (p > p_buf && *p != '\n')
        p--;

    //while (p <= temp)
     //   cout << *p++;

   // cout << endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -