struct
not struck:-)别这么早罢工啊
如果你对类的概念理解比较好的话
这个问题真的不难
或者,你可以偷懒,把这个struct作为类的成员,呵呵:P
还有,你说的每个函数都在子函数中被调用,没说清楚。

解决方案 »

  1.   

    在struct的定义处改成class,在c++中,struct和class的唯一区别是struct的成员默认是public的,而class中默认是private的
      

  2.   

    另:
    struct不能有成员函数
    所以,你做转换的时候
    要注意成员函数的问题
      

  3.   

    在C++中,struct和class在概念上是一致的,没有本质的不同。唯一的区别在于struct的缺省存取权限是public的,而class是private的。
    因此
    struct A
    {
        int a1;
    private:
        int a2;
    };
    等同于
    class A
    {
        int a2;
    public:
        int a1;
    };在C++中,struct的使用与class也没有本质的区别,比如可以这样定义类:
    #include <iostream.h>struct A
    {
    int i;
    int j;
    };struct B : public A
    {
    virtual void foo(){};
    int m;
    };struct C : B
    {
    void foo() { cout << "C struct" << endl;}
    };int main() {
    B bb; C cc;
    cc.foo(); return 0;
    }
      

  4.   

    #include <stdio.h>
    #include <string.h>
    #include <iostream.h>
    #include <stdlib.h>
    #define STATUS int
    #define TRUE 1
    #define FALSE 0
    #define MAX 3000typedef struct Token_def{
    char Content[100];
    char Type[30];
    char Attr[30];
    }Token; Token t[200];
    int LookHead;STATUS IsDigit(char ch)
    {
    if(ch<='9' && ch>='0') 
    return TRUE;
    else 
    return FALSE;
    }STATUS IsAlpha(char ch)
    {
    if( (ch<='z' && ch>='a') || (ch<='Z' && ch>='A'))
    return TRUE;
    else 
    return FALSE;
    }STATUS Is0To7(char ch)
    {
    if(ch<='7' && ch>='1') 
    return TRUE;
    else 
    return FALSE;
    }STATUS Is1To9(char ch)
    {
    if(ch<='9' && ch>='1') 
    return TRUE;
    else 
    return FALSE;
    }STATUS Is0To9(char ch)
    {
    if(ch<='9' && ch>='0') 
    return TRUE;
    else 
    return FALSE;
    }STATUS Is_a2f(char ch)
    {
    if(ch<='f' && ch>='a')
    return TRUE;
    else 
    return FALSE;
    }STATUS IsKeyword(char *in,char Keyword[][30],int numOfKey)
    {
    for(int i=0;i<=numOfKey-1;i++){
    if (strcmp(in,Keyword[i])==0) return TRUE;
    }
    return FALSE;
    }
      

  5.   

    Token CopyToken(char *content,char *type,char *attr)
    {
    Token t;
    strcpy(t.Content,content);
    strcpy(t.Type,type);
    strcpy(t.Attr,attr);
    return t;
    }Token scan(STATUS &isEnd,char KeyTable[][30],int numOfKey)
    {
    char buf[30];
    char ch;
    int bPos;
    long ul;
    char *stopstr; do{
    ch=getchar();
    }while (ch==' ');

    while(1){
    if((ch == EOF) || (ch == '\n')) {
    isEnd=TRUE;return CopyToken("END","END","END");}
    bPos=0;
    if(IsAlpha(ch)){
    do{
    buf[bPos]=ch;
    ch=getchar();
    bPos++;
    }while (IsAlpha(ch) || IsDigit(ch));

    ungetc( ch, stdin );
    buf[bPos]='\0';
    if(IsKeyword(buf,KeyTable,numOfKey)) 
    return CopyToken(buf,strupr(buf),"_");
    else 
    return CopyToken(buf,"IDN","符号表入口");
    }
    else 
    if (ch=='0') {
    buf[bPos]=ch;
    ch=getchar();
    bPos++; switch(ch){
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    do{
    buf[bPos]=ch;
    ch=getchar();
    bPos++;
    }while(Is0To7(ch)); ungetc(ch, stdin);
    buf[bPos]='\0';

    ul=strtoul(buf,&stopstr,8);
    sprintf(buf,"%d",ul);
    return CopyToken(buf,"OCT",buf); case 'x':
    do{
    buf[bPos]=ch;
    ch=getchar();
    bPos++;
    }while(Is0To9(ch) || Is_a2f(ch)); ungetc( ch, stdin );

    buf[bPos]='\0';
    ul=strtoul(buf,&stopstr,16);
    sprintf(buf,"%d",ul);
    return CopyToken(buf,"HEX",buf);
    default:
    ungetc(ch, stdin );
    return CopyToken("0","DEC","0");
    }//switch
    }//if

    else 
    if (Is1To9(ch)) {
    do{
    buf[bPos]=ch;
    ch=getchar();
    bPos++;
    }while(Is0To9(ch));
    ungetc( ch, stdin);
    buf[bPos]='\0';
    return CopyToken(buf,"DEC",buf);
    }
    else 
    {
    switch(ch){
    case '+': 
    return CopyToken("+","ADD","_");
    case '-': 
    return CopyToken("-","SUB","_");
    case '*':
    return CopyToken("*","MUL","_");
    case '/':
    return CopyToken("/","DIV","_");
    case '=':
    return CopyToken("=","EQ","_");
    case '<':
    buf[bPos]=ch;
    ch=getchar();
    bPos++;
    if(ch=='=') 
    return CopyToken("<=","LE","_");
    else goto Error;
    case '>':
    return CopyToken(">","GT","_");
    case '(':
    return CopyToken("(","LP","_");
    case ')':
    return CopyToken(")","RP","_");
    case ' ':
    break;
    default:
    goto Error;
    }
    }
    ch=getchar();
    }//whileError: cout<<"Meet a syntax error!"<<endl;
    isEnd=TRUE;
    //exit(0);
    return CopyToken("ERROR","ERROR","ERROR");
    }
      

  6.   

    typedef struct sa_tag{
    char code[MAX];
    int  iNext;
    int  iBegin;
    }S_A;typedef struct ea_tag{
    char code[MAX];
    char place[10];
    }E_A,T_A,F_A;typedef struct ca_tag{
    char code[MAX];
    int iTrue;
    int iFalse;
    }C_A;int iPlace,iLabel;
    int newplace()
    {
    return ++iPlace;
    }int  newlabel()
    {
    return ++iLabel;
    }//function declare
    void match(char *);
    void statement(S_A &);
    void condition(C_A &);
    void expression(E_A &);
    void term(T_A &);
    void factor(F_A &);//function implement
    STATUS cmpHead(char *str)
    {
    if(strcmp(t[LookHead].Type,str)==0) return TRUE;
    else return FALSE;
    }
    void ERROR()
    {
    cout<<endl<<"meet a grammar mistake!"<<endl;
    exit(0);
    }void match(char *type)
    {
    if(cmpHead(type)) 
    LookHead++;
    else ERROR();
    }void statement(S_A &sa)
    {
    if(cmpHead("IDN")){
    match(t[LookHead].Type);
    char ss[30];
    strcpy(ss,t[LookHead-1].Content);
    match("EQ");
    E_A ea;
    expression(ea);
    sprintf(sa.code,"L%d:%s\t%s:=%s\n",sa.iBegin,ea.code,ss,ea.place);
    }
    else if(cmpHead("WHILE")){
    C_A ca;
    S_A sa1;

    ca.iFalse=sa.iNext;
    sa1.iNext=sa.iBegin;
    ca.iTrue=sa1.iBegin=newlabel(); match("WHILE");
    condition(ca);
    match("DO");
    statement(sa1); sprintf(sa.code,"L%d:%s%s\tGOTO L%d\n",
    sa.iBegin,ca.code,sa1.code,sa.iBegin);
    }
    else if(cmpHead("IF")){
    C_A ca;
    S_A sa1;
    ca.iTrue=newlabel();
    sa1.iNext=ca.iFalse=sa.iNext; match("IF");
    condition(ca);
    match("THEN");
    sa1.iBegin=ca.iTrue;
    statement(sa1); sprintf(sa.code,"L%d:%s%s",sa.iBegin,ca.code,sa1.code);
    }
    }void condition(C_A &ca)
    {
    E_A ea1;
    expression(ea1);
    if(cmpHead("LE")){
    match("LE");
    E_A ea2;
    expression(ea2);
    sprintf(ca.code,"%s%s\tIF %s<=%s GOTO L%d\n\tGOTO L%d\n",
    ea1.code,ea2.code,ea1.place,ea2.place,ca.iTrue,ca.iFalse);
    }
    else if(cmpHead("GT")){ //">" 
    match("GT");
    E_A ea2;
    expression(ea2);
    sprintf(ca.code,"%s%s\tIF %s>%s GOTO L%d\n\tGOTO L%d\n",
    ea1.code,ea2.code,ea1.place,ea2.place,ca.iTrue,ca.iFalse);
    }
    else ERROR();
    }void expression(E_A &ea)
    {
    T_A ta[40];
    int i=0; term(ta[i]);

    while(1){
    i++;
    if(cmpHead("SUB")) {
    match("SUB");
    term(ta[i]);
    i++;
    sprintf(ta[i].place,"T%d",newplace());
    sprintf(ta[i].code,"%s%s\t%s:=%s SUB %s\n",
    ta[i-2].code,ta[i-1].code,ta[i].place,ta[i-2].place,ta[i-1].place);
    }
    else if(cmpHead("ADD")){
    match("ADD");
    term(ta[i]);
    i++;
    sprintf(ta[i].place,"T%d",newplace());
    sprintf(ta[i].code,"%s%s\t%s:=%s ADD %s\n",
    ta[i-2].code,ta[i-1].code,ta[i].place,ta[i-2].place,ta[i-1].place);
    }
    else break;
    }
    i--;
    strcpy(ea.code,ta[i].code);
    strcpy(ea.place,ta[i].place);
    }
      

  7.   

    to coolfrog(coolfrog):
    c++中struct可以有成员函数,区别是默认是public的
      

  8.   

    void term(T_A &ta)
    {
    F_A fa[40];
    int i=0; factor(fa[i]);

    while(1){
    i++;
    if(cmpHead("MUL")) {
    match("MUL");
    factor(fa[i]);
    i++;
    sprintf(fa[i].place,"T%d",newplace());
    sprintf(fa[i].code,"%s%s\t%s:=%s MUL %s\n",
    fa[i-2].code,fa[i-1].code,fa[i].place,fa[i-2].place,fa[i-1].place);
    }
    else if(cmpHead("DIV")){
    match("DIV");
    factor(fa[i]);
    i++;
    sprintf(fa[i].place,"T%d",newplace());
    sprintf(fa[i].code,"%s%s\t%s:=%s DIV %s\n",
    fa[i-2].code,fa[i-1].code,fa[i].place,fa[i-2].place,fa[i-1].place);
    }
    else break;
    }
    i--;
    strcpy(ta.code,fa[i].code);
    strcpy(ta.place,fa[i].place);
    }void factor(F_A &fa)
    {
    if(cmpHead("LP")) { match("LP");
    E_A ea;
    expression(ea);
    match("RP");
    sprintf(fa.place,"%s",ea.place);
    sprintf(fa.code,"%s",ea.code);
    }
    else if (cmpHead("IDN")) {
    match("IDN");
    strcpy(fa.place,t[LookHead-1].Content);
    strcpy(fa.code,"\0");
    }
    else if (cmpHead("OCT")) {
    match("OCT");
    strcpy(fa.place,t[LookHead-1].Content);
    strcpy(fa.code,"\0");
    }
    else if (cmpHead("HEX")) {
    match("HEX");
    strcpy(fa.place,t[LookHead-1].Content);
    strcpy(fa.code,"\0");
    }
    else if (cmpHead("DEC")) {
    match("DEC");
    strcpy(fa.place,t[LookHead-1].Content);
    strcpy(fa.code,"\0");
    }
    else ERROR();
    }void main()
    {
    int i;
    STATUS end;
    char KeywordTable[][30]={"if","then","while","do"};
    LookHead=0;
    int numOfKey=sizeof(KeywordTable)/30;
    for(i=0,end=FALSE;!end;i++)
    t[i]=scan(end,KeywordTable,numOfKey);
    S_A sa;
    iPlace=iLabel=0;
    sa.iBegin=newlabel();
    sa.iNext=1689;
    statement(sa);
    cout<<endl<<sa.code;
    cout<<"L1689:"<<endl;

      

  9.   

    以上是我的源程序,我本人对oop的了解不多,现在我要将所有的status和struck全部转换为class,大家应该能看明白的,这是一个简单的编译器,词法分析要做成一个类,并为以后的语法和语义分析提供分析结果。