Lyt
posts - 16,comments - 61,trackbacks - 0

 

今天使用智能指针LytPtr初步完成了表达式树,未调试完全,头文件代码如下:

 1class Interpret
 2{
 3public:
 4  Error ErrorInfomation;
 5
 6  bool IsError();
 7  LytPtr<Program> Run(const LytWString Content);
 8
 9private:
10  LytPtr<Program> GetProgram(Node<Token*>*& Current);
11  LytPtr<Function> GetFunction(Node<Token*>*& Current);
12  LytPtr<Var> GetVar(Node<Token*>*& Current);
13  LytPtr<Block> GetBlock(Node<Token*>*& Current);
14  LytPtr<FunHead> GetFunHead(Node<Token*>*& Current);
15  LytPtr<FunHead> GetFunctionHead(Node<Token*>*& Current);
16  LytPtr<FunHead> GetProcedureHead(Node<Token*>*& Current);
17  LytPtr<Statement> GetStatement(Node<Token*>*& Current);
18  LytPtr<Statement> GetAssignStatementOrSimpleStatement(Node<Token*>*& Current);
19  LytPtr<Statement> GetIfStatement(Node<Token*>*& Current);
20  LytPtr<Statement> GetWhileDoStatement(Node<Token*>*& Current);
21  LytPtr<Statement> GetDoWhileStatement(Node<Token*>*& Current);
22  LytPtr<Statement> GetControlStatement(Node<Token*>*& Current);
23  LytPtr<Statement> GetReturnStatement(Node<Token*>*& Current);
24  LytPtr<Statement> GetCompoundStatement(Node<Token*>*& Current);
25  LytPtr<Expression> GetExpression(Node<Token*>*& Current);
26  LytPtr<Expression> GetRelationExpression(Node<Token*>*& Current);
27  LytPtr<Expression> GetXorExpression(Node<Token*>*& Current);
28  LytPtr<Expression> GetOrExpression(Node<Token*>*& Current);
29  LytPtr<Expression> GetAddSubExpression(Node<Token*>*& Current);
30  LytPtr<Expression> GetAndExpression(Node<Token*>*& Current);
31  LytPtr<Expression> GetMulDivExpression(Node<Token*>*& Current);
32  LytPtr<Expression> GetPositiveNegativeExpression(Node<Token*>*& Current);
33  LytPtr<Expression> GetNotExpression(Node<Token*>*& Current);
34  LytPtr<Expression> GetSimpleExpression(Node<Token*>*& Current);
35  LytPtr<Expression> GetCallFunctionExpression(Node<Token*>*& Current);
36}
;

其中,关于优先级的设计有点迷惑,参考了网上关于Pascal操作符优先级后,文法如下:

Expression = RelationExpression;
RelationExpression = RelationExpression ("<" | "<=" | "=" | "!=" | ">" | ">=") XorExpression | XorExpression
XorExpression = XorExpression "xor" OrExpression | OrExpression
OrExpression = OrExpression "or" AddExpression | AddSubExpression
AddSubExpression = AddSubExpression ("+" | "-") AndExpression | AndExpression
AndExpression = AndExpression "and" MulDivExpression | MulDivModExpression
MulDivModExpression = MulDivModExpression ("*" | "div" | "mod") UnaryExpression | UnaryExpression
PositiveNegativeExpression = ("+" | "-") NotExpression | NotExpression
NotExpression = "not" SimpleExpression | SimpleExpression;
SimpleExpression = Number | bool | CallFunctionExpression

代码举例:

 1LytPtr<Expression> GetAddSubExpression(Node<Token*>*& Current)
 2{
 3    if (!Current) return 0;
 4    else
 5    {
 6        LytPtr<Expression> Result=GetAndExpression(Current);
 7        if (IsError()) return 0;
 8        else
 9        {
10            while (Current && (Current->Data->Content==L"+" || Current->Data->Content==L"-"))
11            {
12                LytPtr<Expression> Left=Result;
13                Result=new BinaryExpression;
14                BinaryExpression* Temp=(BinaryExpression*)Result.Buffer();
15                Temp->Left=Left;
16                Temp->Operator=*(Current->Data);
17                Current=Current->Next;
18                if (!Current)
19                {
20                    ErrorInfomation.SetMessage(L"加减表达式缺少右操作数 =>"+ErrorInfomation.GetMessage());
21                    return 0;
22                }

23                LytPtr<Expression> Right=GetAndExpression(Current);
24                if (IsError())
25                {
26                    ErrorInfomation.SetMessage(L"加减表达式右操作数出错 =>"+ErrorInfomation.GetMessage());
27                    return 0;
28                }

29                else Temp->Right=Right;
30            }

31            return Result;
32        }

33    }

34}

35
36LytPtr<Expression> GetMulDivExpression(Node<Token*>*& Current)
37{
38    if (!Current) return 0;
39    else
40    {
41        LytPtr<Expression> Result=GetPositiveNegativeExpression(Current);
42        if (IsError()) return 0;
43        else
44        {
45            while (Current && (Current->Data->Content==L"*" || Current->Data->Content==L"div"))
46            {
47                LytPtr<Expression> Left=Result;
48                Result=new BinaryExpression;
49                BinaryExpression* Temp=(BinaryExpression*)Result.Buffer();
50                Temp->Left=Left;
51                Temp->Operator=*(Current->Data);
52                Current=Current->Next;
53                if (!Current)
54                {
55                    ErrorInfomation.SetMessage(L"*/div表达式缺少右操作数 =>"+ErrorInfomation.GetMessage());
56                    return 0;
57                }

58                LytPtr<Expression> Right=GetPositiveNegativeExpression(Current);
59                if (IsError())
60                {
61                    ErrorInfomation.SetMessage(L"*/div表达式右操作数出错 =>"+ErrorInfomation.GetMessage());
62                    return 0;
63                }

64                else Temp->Right=Right;
65            }

66            return Result;
67        }

68    }

69}

 

不难发现,含有二元操作符的表达式文法接近(一元也一样),函数实现方法也类似,为了让代码好看点,我动了几个念头,但最终没实现:

1.函数模板

2.函数指针:利用typedef,后来发现定义过程中有递归,遂写不出来,暂时放弃

3.宏:记得括号来括号去那里要加得很清楚,脑袋晕乎,暂时放弃

晚上睡觉能想到啥好办法明天再解决。

 

在构造表达式树的时候还出现了几个问题:

1.先前由于我没有充分测试,LytPtr有bug,自食其果了

结论:写出啥数据结构在使用前一定要充分测试

2.犹豫到底该用返回什么类型的指针,暂时还不明白返回LytPtr<Expression>与返回LytPtr<Base>的区别,其中Expression是继承Base的

3.LytPtr不支持类型转换,于是在需要类型转换的时候愣了几下,忘记可以转成指针来用了

结论:该用指针的时候还是用指针

 

希望明天完成测试表达式树与类型检查。

posted on 2009-03-22 00:55 Lyt 阅读(535) 评论(1)  编辑 收藏 引用 所属分类: Pascal简化版

FeedBack:
# re: 初步完成表达式树
2009-03-22 01:29 | 陈梓瀚(vczh)
4:将操作符的优先级放在一个数组里面,然后设计一个通用算法解决。  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理