2013-03-15 58 views
1

我嘗試轉換的C#和C++/CLI簡單的代碼,但是當我嘗試建立轉換後的代碼,我得到這個錯誤回報:錯誤代碼轉換,從C#和C++/CLI

1>------ Build started: Project: Compiler2, Configuration: Debug Win32 ------ 
1> Source.cpp 
1>Source.cpp(21): error C2143: syntax error : missing ';' before '^' 
1>Source.cpp(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
1>Source.cpp(28): error C2143: syntax error : missing ';' before '^' 
1>Source.cpp(28): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
1>Source.cpp(36): error C2143: syntax error : missing ';' before '^' 
1>Source.cpp(36): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
1>Source.cpp(44): error C2143: syntax error : missing ';' before '^' 
1>Source.cpp(44): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
1>Source.cpp(45): error C2143: syntax error : missing ';' before '^' 
1>Source.cpp(45): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
1>Source.cpp(101): error C2146: syntax error : missing ';' before identifier 'Op' 
1>Source.cpp(101): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

和,我的代碼是:

// AST.cs 

using namespace System; 

/* <stmt> := var <ident> = <expr> 
    | <ident> = <expr> 
    | for <ident> = <expr> to <expr> do <stmt> end 
    | read_int <ident> 
    | print <expr> 
    | <stmt> ; <stmt> 
    */ 
public ref class Stmt abstract 
{ 
}; 

// var <ident> = <expr> 
public ref class DeclareVar : Stmt 
{ 
public: 
    String^ Ident; 
    Expr^ expr; 
}; 

// print <expr> 
public ref class Print : Stmt 
{ 
public: 
    Expr^ Expr; 
}; 

// <ident> = <expr> 
public ref class Assign : Stmt 
{ 
public: 
    String^ Ident; 
    Expr^ Expr; 
}; 

// for <ident> = <expr> to <expr> do <stmt> end 
public ref class ForLoop : Stmt 
{ 
public: 
    String^ Ident; 
    Expr^ From; 
    Expr^ To; 
    Stmt^ Body; 
}; 

// read_int <ident> 
public ref class ReadInt : Stmt 
{ 
public: 
    String^ Ident; 
}; 

// <stmt> ; <stmt> 
public ref class Sequence : Stmt 
{ 
public: 
    Stmt^ First; 
    Stmt^ Second; 
}; 

/* <expr> := <string> 
* | <int> 
* | <arith_expr> 
* | <ident> 
*/ 
public ref class Expr abstract 
{ 
}; 

// <string> := " <string_elem>* " 
public ref class StringLiteral : Expr 
{ 
public: 
    String^ Value; 
}; 

// <int> := <digit>+ 
public ref class IntLiteral : Expr 
{ 
public: 
    int Value; 
}; 

// <ident> := <char> <ident_rest>* 
// <ident_rest> := <char> | <digit> 
public ref class Variable : Expr 
{ 
public: 
    String^ Ident; 
}; 

// <bin_expr> := <expr> <bin_op> <expr> 
public ref class BinExpr : Expr 
{ 
public: 
    Expr^ Left; 
    Expr^ Right; 
    BinOp Op; 
}; 

// <bin_op> := + | - | * |/
public enum class BinOp 
{ 
    Add, 
    Sub, 
    Mul, 
    Div 
}; 

這個代碼是從這個代碼轉換:

// AST.cs 

/* <stmt> := var <ident> = <expr> 
    | <ident> = <expr> 
    | for <ident> = <expr> to <expr> do <stmt> end 
    | read_int <ident> 
    | print <expr> 
    | <stmt> ; <stmt> 
    */ 
public abstract class Stmt 
{ 
} 

// var <ident> = <expr> 
public class DeclareVar : Stmt 
{ 
    public string Ident; 
    public Expr Expr; 
} 

// print <expr> 
public class Print : Stmt 
{ 
    public Expr Expr; 
} 

// <ident> = <expr> 
public class Assign : Stmt 
{ 
    public string Ident; 
    public Expr Expr; 
} 

// for <ident> = <expr> to <expr> do <stmt> end 
public class ForLoop : Stmt 
{ 
    public string Ident; 
    public Expr From; 
    public Expr To; 
    public Stmt Body; 
} 

// read_int <ident> 
public class ReadInt : Stmt 
{ 
    public string Ident; 
} 

// <stmt> ; <stmt> 
public class Sequence : Stmt 
{ 
    public Stmt First; 
    public Stmt Second; 
} 

/* <expr> := <string> 
* | <int> 
* | <arith_expr> 
* | <ident> 
*/ 
public abstract class Expr 
{ 
} 

// <string> := " <string_elem>* " 
public class StringLiteral : Expr 
{ 
    public string Value; 
} 

// <int> := <digit>+ 
public class IntLiteral : Expr 
{ 
    public int Value; 
} 

// <ident> := <char> <ident_rest>* 
// <ident_rest> := <char> | <digit> 
public class Variable : Expr 
{ 
    public string Ident; 
} 

// <bin_expr> := <expr> <bin_op> <expr> 
public class BinExpr : Expr 
{ 
    public Expr Left; 
    public Expr Right; 
    public BinOp Op; 
} 

// <bin_op> := + | - | * |/
public enum BinOp 
{ 
    Add, 
    Sub, 
    Mul, 
    Div 
} 

好吧,我怎麼能解決這個錯誤? (因爲對我來說,代碼是正確的...)

+0

你確定你有'/ CLR'開關? – 2013-03-15 12:59:12

+4

我在猜測它的聲明順序;它在C#中並不重要,但它在C++中有效;至少這就是「缺少類型說明符」對我說的。即您在聲明之前使用Expr。 – 2013-03-15 13:02:15

回答

4

ExprBinOp正在使用之前它們被宣佈;不是在C#中的問題,而是一個用C++

試試這個

using namespace System; 

/* <stmt> := var <ident> = <expr> 
    | <ident> = <expr> 
    | for <ident> = <expr> to <expr> do <stmt> end 
    | read_int <ident> 
    | print <expr> 
    | <stmt> ; <stmt> 
    */ 
public ref class Stmt abstract 
{ 
}; 

/* <expr> := <string> 
* | <int> 
* | <arith_expr> 
* | <ident> 
*/ 
public ref class Expr abstract 
{ 
}; 

// <bin_op> := + | - | * |/
public enum class BinOp 
{ 
    Add, 
    Sub, 
    Mul, 
    Div 
}; 

// var <ident> = <expr> 
public ref class DeclareVar : Stmt 
{ 
public: 
    String^ Ident; 
    Expr^ expr; 
}; 

// print <expr> 
public ref class Print : Stmt 
{ 
public: 
    Expr^ Expr; 
}; 

// <ident> = <expr> 
public ref class Assign : Stmt 
{ 
public: 
    String^ Ident; 
    Expr^ Expr; 
}; 

// for <ident> = <expr> to <expr> do <stmt> end 
public ref class ForLoop : Stmt 
{ 
public: 
    String^ Ident; 
    Expr^ From; 
    Expr^ To; 
    Stmt^ Body; 
}; 

// read_int <ident> 
public ref class ReadInt : Stmt 
{ 
public: 
    String^ Ident; 
}; 

// <stmt> ; <stmt> 
public ref class Sequence : Stmt 
{ 
public: 
    Stmt^ First; 
    Stmt^ Second; 
}; 



// <string> := " <string_elem>* " 
public ref class StringLiteral : Expr 
{ 
public: 
    String^ Value; 
}; 

// <int> := <digit>+ 
public ref class IntLiteral : Expr 
{ 
public: 
    int Value; 
}; 

// <ident> := <char> <ident_rest>* 
// <ident_rest> := <char> | <digit> 
public ref class Variable : Expr 
{ 
public: 
    String^ Ident; 
}; 

// <bin_expr> := <expr> <bin_op> <expr> 
public ref class BinExpr : Expr 
{ 
public: 
    Expr^ Left; 
    Expr^ Right; 
    BinOp Op; 
};