回 帖 发 新 帖 刷新版面

主题:[原创]请问哪位高手能否摆平



               请用高级语言编写要用LR 分析方法输出四元式
       题目:      WHILE循环语句的程序设计(LR方法,输出四元式)


[em9][em9][em9][em9][em9][em9][em9][em9][em9][em9][em9][em9][em9]

回复列表 (共11个回复)

沙发

void CPL0Dlg::OnMenuitemYuyi()
{
    // TODO: Add your command handler code here
    m_cIn.GetWindowText(m_Compiler.GetTable()->SourseBuffer,30000);
    m_Compiler.CiFaFengXi();
    m_Compiler.YuYiFengXi();
    m_cOut.SetWindowText("");
    int i=0;CString str;
    while (m_Compiler.GetTable()->LexicalDuad[i]->type>=0)
    {
        if (m_Compiler.GetTable()->LexicalDuad[i]->type==0)
        {
            str.Format("源程序有词法错误:\r\n\r\n");
            m_cOut.AppendText(str.GetBuffer(0));
            str.Format("错误号:%d %s\r\n",
                m_Compiler.GetTable()->LexicalDuad[i]->value,
                m_Compiler.GetTable()->ErrorMessageTable[m_Compiler.GetTable()->LexicalDuad[i]->value]);
            m_cOut.AppendText(str.GetBuffer(0));
            int se=m_Compiler.GetTable()->LexicalDuad[i]->address;
            int sl=::strlen(m_Compiler.GetTable()->LexicalDuad[i]->text);
            m_cIn.SetSel(se,se+sl);
            return;
        }
        i++;
    }
    str.Format("语义分析结果:\r\n\r\n");
    m_cOut.AppendText(str.GetBuffer(0));
    if (m_Compiler.GetTable()->ErrorNumber!=0)
    {
        str.Format("错误号:%d %s\r\n\r\n",
            m_Compiler.GetTable()->ErrorNumber,
            m_Compiler.GetTable()->ErrorMessageTable[m_Compiler.GetTable()->ErrorNumber]);
        m_cOut.AppendText(str.GetBuffer(0));
        int se=m_Compiler.GetTable()->LexicalDuad[m_Compiler.GetTable()->ErrorAddress]->address;
        int sl=::strlen(m_Compiler.GetTable()->LexicalDuad[m_Compiler.GetTable()->ErrorAddress]->text);
        m_cIn.SetSel(se,se+sl);
        if (AfxMessageBox("源程序有错误,要列出四元式吗?"
            ,MB_YESNO|MB_ICONQUESTION)==IDNO)
            return;
    }
    if (!m_bSysEnable)
    {
        str.Format("输出格式:行号,四元式。\r\n\r\n");
        m_cOut.AppendText(str.GetBuffer(0));
        i=1;
        while (m_Compiler.GetTable()->SemanticQuad[i].op!=0)
        {
            str.Format("%d\t(%s\t,(%d,%d),(%d,%d),(%d,%d))\r\n",i,
                m_Compiler.GetTable()->QuadOpcodeTable[m_Compiler.GetTable()->SemanticQuad[i].op],
                m_Compiler.GetTable()->SemanticQuad[i].arg1.fl,
                m_Compiler.GetTable()->SemanticQuad[i].arg1.flvalue,
                m_Compiler.GetTable()->SemanticQuad[i].arg2.fl,
                m_Compiler.GetTable()->SemanticQuad[i].arg2.flvalue,
                m_Compiler.GetTable()->SemanticQuad[i].result.fl,
                m_Compiler.GetTable()->SemanticQuad[i].result.flvalue);
            m_cOut.AppendText(str.GetBuffer(0));
            i++;
        }
    }
    else
    {
        str.Format("输出格式:行号,四元式(操作,操作数,操作数,结果)。\r\n\r\n");
        m_cOut.AppendText(str.GetBuffer(0));
        i=1;
        while (m_Compiler.GetTable()->SemanticQuad[i].op!=0)
        {
            str.Format("%d\t(%s\t,",i,
                m_Compiler.GetTable()->QuadOpcodeTable[m_Compiler.GetTable()->SemanticQuad[i].op]);
            m_cOut.AppendText(str.GetBuffer(0));
            switch (m_Compiler.GetTable()->SemanticQuad[i].arg1.fl)
            {
            case 0:
                str.Format("__,");break;
            case 1:
                str.Format("%d,",m_Compiler.GetTable()->SemanticQuad[i].arg1.flvalue);break;
            case 2:
                str.Format("I%d,",m_Compiler.GetTable()->SemanticQuad[i].arg1.flvalue);break;
            case 3:
                str.Format("T%d,",m_Compiler.GetTable()->SemanticQuad[i].arg1.flvalue);break;
            case 4:
                str.Format("V%d,",m_Compiler.GetTable()->SemanticQuad[i].arg1.flvalue);break;
            case 5:
                str.Format("VV%d,",m_Compiler.GetTable()->SemanticQuad[i].arg1.flvalue);break;
            default:
                str.Format("P%d_%d,",
                    m_Compiler.GetTable()->SemanticQuad[i].arg1.fl-6,
                    m_Compiler.GetTable()->SemanticQuad[i].arg1.flvalue);
            }
            m_cOut.AppendText(str.GetBuffer(0));
            switch (m_Compiler.GetTable()->SemanticQuad[i].arg2.fl)
            {
            case 0:
                str.Format("__,");break;
            case 1:
                str.Format("%d,",m_Compiler.GetTable()->SemanticQuad[i].arg2.flvalue);break;
            case 2:
                str.Format("I%d,",m_Compiler.GetTable()->SemanticQuad[i].arg2.flvalue);break;
            case 3:
                str.Format("T%d,",m_Compiler.GetTable()->SemanticQuad[i].arg2.flvalue);break;
            case 4:
                str.Format("V%d,",m_Compiler.GetTable()->SemanticQuad[i].arg2.flvalue);break;
            case 5:
                str.Format("VV%d,",m_Compiler.GetTable()->SemanticQuad[i].arg2.flvalue);break;
            default:
                str.Format("P%d_%d,",
                    m_Compiler.GetTable()->SemanticQuad[i].arg2.fl-6,
                    m_Compiler.GetTable()->SemanticQuad[i].arg2.flvalue);
            }
            m_cOut.AppendText(str.GetBuffer(0));
            switch (m_Compiler.GetTable()->SemanticQuad[i].result.fl)
            {
            case 0:
                str.Format("__)\r\n");break;
            case 1:
                str.Format("%d)\r\n",m_Compiler.GetTable()->SemanticQuad[i].result.flvalue);break;
            case 2:
                str.Format("I%d)\r\n",m_Compiler.GetTable()->SemanticQuad[i].result.flvalue);break;
            case 3:
                str.Format("T%d)\r\n",m_Compiler.GetTable()->SemanticQuad[i].result.flvalue);break;
            case 4:
                str.Format("V%d)\r\n",m_Compiler.GetTable()->SemanticQuad[i].result.flvalue);break;
            case 5:
                str.Format("VV%d)\r\n",m_Compiler.GetTable()->SemanticQuad[i].result.flvalue);break;
            default:
                str.Format("P%d_%d)\r\n",
                    m_Compiler.GetTable()->SemanticQuad[i].result.fl-6,
                    m_Compiler.GetTable()->SemanticQuad[i].result.flvalue);
            }
            m_cOut.AppendText(str.GetBuffer(0));
            i++;
        }
        str.Format("\r\n四元式说明:\r\n\r\n");
        m_cOut.AppendText(str.GetBuffer(0));
        str.Format("空:__\r\n常数:###\r\n指令入口:I###\r\n临时变量:T###\r\n直接访问变量:V###\r\n间接访问变量:VV###\r\n第i外层变量:Pi_###\r\n");
        m_cOut.AppendText(str.GetBuffer(0));
        str.Format("(###为数字)\r\n");
        m_cOut.AppendText(str.GetBuffer(0));
    }
}
//have a try

板凳

高手:
请问你用什么语言写的,我实在不知道是什么语言,还望高手指点一下小弟,用什么语言环境调试啊!

3 楼

调试出来的错误:

C:\Documents and Settings\wangtao\123.cpp(1) : error C2653: 'CPL0Dlg' : is not a class or namespace name
C:\Documents and Settings\wangtao\123.cpp(4) : error C2065: 'm_cIn' : undeclared identifier
C:\Documents and Settings\wangtao\123.cpp(4) : error C2228: left of '.GetWindowText' must have class/struct/union type
C:\Documents and Settings\wangtao\123.cpp(4) : error C2065: 'm_Compiler' : undeclared identifier
C:\Documents and Settings\wangtao\123.cpp(4) : error C2228: left of '.GetTable' must have class/struct/union type
C:\Documents and Settings\wangtao\123.cpp(4) : error C2227: left of '->SourseBuffer' must point to class/struct/union
C:\Documents and Settings\wangtao\123.cpp(5) : error C2228: left of '.CiFaFengXi' must have class/struct/union type
C:\Documents and Settings\wangtao\123.cpp(6) : error C2228: left of '.YuYiFengXi' must have class/struct/union type
C:\Documents and Settings\wangtao\123.cpp(7) : error C2065: 'm_cOut' : undeclared identifier
C:\Documents and Settings\wangtao\123.cpp(7) : error C2228: left of '.SetWindowText' must have class/struct/union type
C:\Documents and Settings\wangtao\123.cpp(8) : error C2065: 'CString' : undeclared identifier
C:\Documents and Settings\wangtao\123.cpp(8) : error C2146: syntax error : missing ';' before identifier 'str'
C:\Documents and Settings\wangtao\123.cpp(8) : error C2065: 'str' : undeclared identifier
C:\Documents and Settings\wangtao\123.cpp(9) : error C2228: left of '.GetTable' must have class/struct/union type
C:\Documents and Settings\wangtao\123.cpp(9) : error C2227: left of '->LexicalDuad' must point to class/struct/union
C:\Documents and Settings\wangtao\123.cpp(9) : error C2227: left of '->type' must point to class/struct/union
C:\Documents and Settings\wangtao\123.cpp(9) : fatal error C1903: unable to recover from previous error(s); stopping compilation
执行 cl.exe 时出错.

123.exe - 1 error(s), 0 warning(s)

4 楼

快改啊 我同学跟你题目一样 !靠你了![em8]

5 楼

你是王涛 ?

6 楼

我用vc++以m_开头的变量是对话框上的文本编辑框的ctrol类型变量

7 楼

哪位大虾能帮我用C语言解决这个词法分析器问题1.  给出一简单语言单词符号的种别编码单词符号    种别编码
begin         1
if            2
then          3
while         4
do           5
end          6
l ( l | d ) *     10
dd *          11
+            13
–           14
*            15
/             16
:             17
:=            18
<             20
<>            21
<=           22
>             23
>=            24
=             25
;              26
(              27
)              28
#               0
要求:输入是源程序字符串,以‘#’ 结束。输出是单词符号的二元组
( 单词种别码, 单词自身字符串 )
即      ( syn , token 或 sum )
已给出伪代码如下:
# include  <stdio.h> 定义 I / O库所用的某些宏和变量
# include  <string.h> 定义 字符串库函数
char   prog[80], token[8] ;
char   ch ;
int  syn, p, m, n ,sum ;
char   *rwtab[6]={ “begin”, “if ”, “then”, “while”,  “do” ,“end” };
main( )
    { p=0 ;
       printf( “\n please input  string : \n” ) ;
       do  { ch=getchar( );
                prog[p++]=ch ;
              } while ( ch!=‘#’);
P=0;
do
  {  scaner();
      switch(syn)
          { case 11:  printf (“(%2d,%8d)\n”,syn,sum); break;
             case –1:  printf(“input error\n”);  break;     
             default:   printf (“(%2d,%8s)\n”,syn,token);
   } while (syn!=0);
}
scaner( )
    {  for ( n=0; n<8; n++ ) token[n]=NULL;
        ch=prog[p++] ;
        while (ch==‘ ‘)  ch=prog[p++] ;
if  (  ch 是字母字符 )
      { while ch 为字母字符或数字字符
             { ch => token ;
                读下一个字符 ;   
              }
         token[m++]=‘\0’ ;   p-- ;   syn=10 ;
         for (n=0 ;   n<6 ;  n++ )
            if  ( strcmp( token, rwtab[n])= =0)
               {  给出 syn 值;
                   break ;
                }
       }
   else   
if  (  ch 是数字字符 )
{  while ch 为数字字符
      { sum=sum*10+ch-’0’;
         读下一个字符 ;   
      }
      p-- ;   syn=11;
}
else
   switch(ch)
      {
case '<':
    m=0; token[m++]=ch;
    ch=prog[p++];
    if (ch=='>')
    {  syn=21;
        token[m++]=ch;
    }
    else if (ch=='=')
           {  syn=22;
               token[m++]=ch;
                 }
    else { syn=20;
              p--;
          }
         break;
case ‘>':
    ch => token ;
    读下一个字符 ;
    if (ch=='=')
    {将>=的种别码=>syn;
            ch => token ;
    }
    else
    { 将>的种别码=>syn ;
           p--;
     }
        break;
case ':':
    ch => token ;
    读下一个字符 ;
    if (ch=='=')
    { 将:=的种别码=>syn;
              ch => token ;
    }
    else
    { 将:的种别码=>syn ;
            p--;
     }
        break;
case ‘+':    syn=13; token[0]=ch; break;
case ‘-':    syn=14; token[0]=ch; break;
case ‘*':    syn=15; token[0]=ch; break;
case ‘/':    syn=16; token[0]=ch; break;
……
case '#':    syn=0;  token[0]=ch; break;
default:
    syn= -1;  

8 楼

最近,我们要做“。txt文档的生成”这个课程设计,因为我对编程不懂,所以求教各位大哥 。为了不耽误大哥们的时间只希望大哥们帮我划个什么模块图,数据流图之类的啊 。急用啊,希望大哥们帮帮忙啊  

9 楼

#include "stdio.h"
#include "ctype.h"
#include "math.h"
#include  "iostream.h"
#include"string.h"
#define LETTER  0
#define DIGIT   1
#define POINT   2
#define OTHER    3
#define POWER   4
#define PLUS    5
#define MINUS    6
#define ClassNo   100
#define ClassOther   200
#define EndState   -1
char num[100];
int w,n,p,e,d;
int Class,i=0;
int ICON;
float FCON;
static int CurrentState;

int GetChar(void);
int EXCUTE(int,int);
int LEX(void);

int HandleOtherWord(void)
{
    return ClassOther;
}

int HandleError(void)
{
    printf("ERROR!\n");
    return 0;
}

int GetChar(void)
{
    int c;

    c=num[i++];

    if(isdigit(c))
    {
        d=c-'0';return DIGIT;
    }
    if(c=='.')return POINT;
    if(c=='E'||c=='e')return POWER;
    if(c=='+') return PLUS;
    if(c=='-')return MINUS;
    return OTHER;
}
//============================================

int EXCUTE(int state,int symbol)
{
    switch (state)
    {
    case 0:switch(symbol)
           {
            case DIGIT:n=0;p=0;e=1;w=d;CurrentState=1;Class=ClassNo;break;
            case POINT:w=0;n=0;p=0;e=1;CurrentState=3;Class=ClassNo;break;
            default:HandleOtherWord();Class=ClassOther;
                CurrentState=EndState;
           }
        break;
    case 1:switch(symbol)
           {
        case DIGIT:w=w*10+d;break;
        case POINT:CurrentState=2;break;
        case POWER:CurrentState=4;break;
        default:FCON=w;CurrentState=EndState;
           }
        break;
    case 2:switch(symbol)
           {
            case DIGIT:n++;w=w*10+d;break;
            case POWER:CurrentState=4;break;
            default:FCON=w*pow(10,e*p-n);CurrentState=EndState;
           }
        break;
    case 3:switch(symbol)
           {
            case DIGIT:n++;w=w*10+d;CurrentState=2;break;
            default:HandleError();CurrentState=EndState;
           }
        break;
    case 4:switch(symbol)
           {
            case DIGIT:p=p*10+d;CurrentState=6;break;
            case MINUS:e=-1;CurrentState=5;break;
            case PLUS:CurrentState=5;break;
            default:HandleError();CurrentState=EndState;
           }
        break;
    case 5:switch(symbol)
           {
               case DIGIT:p=p*10+d;CurrentState=6;break;
               default:HandleError();CurrentState=EndState;

           }
        break;
    case 6:switch(symbol)
           {
            case DIGIT:p=p*10+d;break;
            default:FCON=w*pow(10,e*p-n);CurrentState=EndState;

           }
        break;

    }
return CurrentState;
}


int LEX(void)
{
    int ch;
    CurrentState=0;
        while(CurrentState!=EndState)        
        {
            ch=GetChar();
            EXCUTE(CurrentState,ch);
        }
        return Class;
}
void main()
{ printf("please input\n");
scanf("%s",num);

LEX();
// cout<<ICON<<endl;
cout<<FCON<<endl;

}
//参考一下

10 楼


能否解释下你的程序,好多看不懂,运行时有一个错误:
C:\Documents and Settings\Administrator\c.cpp(5) : error C2447: missing function header (old-style formal list?)
Error executing cl.exe.

c.obj - 1 error(s), 0 warning(s)
请改下,谢谢~!!

我来回复

您尚未登录,请登录后再回复。点此登录或注册