130242014058+吴伟铭+第2次实验

Posted 吴伟铭

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了130242014058+吴伟铭+第2次实验相关的知识,希望对你有一定的参考价值。

一、实验目的

  1.熟悉体系结构的风格的概念

  2.理解和应用管道过滤器型的风格。

  3、理解解释器的原理

  4、理解编译器模型

二、实验环境

  硬件: 

  软件:Python或任何一种自己喜欢的语言

三、实验内容

  1、实现“四则运算”的简易翻译器。

  结果要求:

    1)实现加减乘除四则运算,允许同时又多个操作数,如:2+3*5-6 结果是11

    2)被操作数为整数,整数可以有多位

    3)处理空格

    4)输入错误显示错误提示,并返回命令状态“CALC”

     

      图1    实验结果示例

  加强练习:

    1、有能力的同学,可以尝试实现赋值语句,例如x=2+3*5-6,返回x=11。(注意:要实现解释器的功能,而不是只是显示)

    2、尝试实现自增和自减符号,例如x++ 

    3、采用管道-过滤器(Pipes and Filters)风格实现解释器

 

                        图2  管道-过滤器风格

 

 

                     图 3  编译器模型示意图

  本实验,实现的是词法分析和语法分析两个部分。

import wx  
  
# begin wxGlade: extracode  
# end wxGlade  
  
  
class MyFrame(wx.Frame):  
    def __init__(self, *args, **kwds):  
        # begin wxGlade: MyFrame.__init__  
        kwds["style"] = wx.DEFAULT_FRAME_STYLE  
        wx.Frame.__init__(self, *args, **kwds)  
        self.text_ctrl_1 = wx.TextCtrl(self, -1, "",style=wx.TE_READONLY)  
        self.button_37 = wx.Button(self, -1, "1")  
        self.button_38 = wx.Button(self, -1, "2")  
        self.button_39 = wx.Button(self, -1, "3")  
        self.button_40 = wx.Button(self, -1, "+")  
        self.button_41 = wx.Button(self, -1, "4")  
        self.button_42 = wx.Button(self, -1, "5")  
        self.button_43 = wx.Button(self, -1, "6")  
        self.button_44 = wx.Button(self, -1, "-")  
        self.button_46 = wx.Button(self, -1, "7")  
        self.button_45 = wx.Button(self, -1, "8")  
        self.button_47 = wx.Button(self, -1, "9")  
        self.button_48 = wx.Button(self, -1, "x")  
        self.button_49 = wx.Button(self, -1, "C")  
        self.button_50 = wx.Button(self, -1, "0")  
        self.button_51 = wx.Button(self, -1, "=")  
        self.button_52 = wx.Button(self, -1, "/")  
  
        self.__set_properties()  
        self.__do_layout()  
  
        self.Bind(wx.EVT_BUTTON, self.bu1, self.button_37)  
        self.Bind(wx.EVT_BUTTON, self.bu2, self.button_38)  
        self.Bind(wx.EVT_BUTTON, self.bu3, self.button_39)  
        self.Bind(wx.EVT_BUTTON, self.bu_plus, self.button_40)  
        self.Bind(wx.EVT_BUTTON, self.bu4, self.button_41)  
        self.Bind(wx.EVT_BUTTON, self.bu5, self.button_42)  
        self.Bind(wx.EVT_BUTTON, self.bu6, self.button_43)  
        self.Bind(wx.EVT_BUTTON, self.bu_min, self.button_44)  
        self.Bind(wx.EVT_BUTTON, self.bu7, self.button_46)  
        self.Bind(wx.EVT_BUTTON, self.bu8, self.button_45)  
        self.Bind(wx.EVT_BUTTON, self.bu9, self.button_47)  
        self.Bind(wx.EVT_BUTTON, self.bu_mul, self.button_48)  
        self.Bind(wx.EVT_BUTTON, self.bu_clear, self.button_49)  
        self.Bind(wx.EVT_BUTTON, self.bu0, self.button_50)  
        self.Bind(wx.EVT_BUTTON, self.bu_result, self.button_51)  
        self.Bind(wx.EVT_BUTTON, self.bu_chu, self.button_52)  
        # end wxGlade  
        self.Show(True)  
  
    def __set_properties(self):  
        # begin wxGlade: MyFrame.__set_properties  
        self.SetTitle("Python Calculater by CYG")  
        self.text_ctrl_1.SetMinSize((400, 50))  
        self.button_37.SetMinSize((100, 50))  
        self.button_38.SetMinSize((100, 50))  
        self.button_39.SetMinSize((100, 50))  
        self.button_40.SetMinSize((100, 50))  
        self.button_41.SetMinSize((100, 50))  
        self.button_42.SetMinSize((100, 50))  
        self.button_43.SetMinSize((100, 50))  
        self.button_44.SetMinSize((100, 50))  
        self.button_46.SetMinSize((100, 50))  
        self.button_45.SetMinSize((100, 50))  
        self.button_47.SetMinSize((100, 50))  
        self.button_48.SetMinSize((100, 50))  
        self.button_49.SetMinSize((100, 50))  
        self.button_50.SetMinSize((100, 50))  
        self.button_51.SetMinSize((100, 50))  
        self.button_52.SetMinSize((100, 50))  
        # end wxGlade  
  
    def __do_layout(self):  
        # begin wxGlade: MyFrame.__do_layout  
        sizer_2 = wx.BoxSizer(wx.VERTICAL)  
        sizer_3 = wx.BoxSizer(wx.VERTICAL)  
        grid_sizer_1 = wx.GridSizer(4, 4, 0, 0)  
        sizer_3.Add(self.text_ctrl_1, 0, 0, 0)  
        grid_sizer_1.Add(self.button_37, 0, 0, 0)  
        grid_sizer_1.Add(self.button_38, 0, 0, 0)  
        grid_sizer_1.Add(self.button_39, 0, 0, 0)  
        grid_sizer_1.Add(self.button_40, 0, 0, 0)  
        grid_sizer_1.Add(self.button_41, 0, 0, 0)  
        grid_sizer_1.Add(self.button_42, 0, 0, 0)  
        grid_sizer_1.Add(self.button_43, 0, 0, 0)  
        grid_sizer_1.Add(self.button_44, 0, 0, 0)  
        grid_sizer_1.Add(self.button_46, 0, 0, 0)  
        grid_sizer_1.Add(self.button_45, 0, 0, 0)  
        grid_sizer_1.Add(self.button_47, 0, 0, 0)  
        grid_sizer_1.Add(self.button_48, 0, 0, 0)  
        grid_sizer_1.Add(self.button_49, 0, 0, 0)  
        grid_sizer_1.Add(self.button_50, 0, 0, 0)  
        grid_sizer_1.Add(self.button_51, 0, 0, 0)  
        grid_sizer_1.Add(self.button_52, 0, 0, 0)  
        sizer_3.Add(grid_sizer_1, 1, wx.EXPAND, 0)  
        sizer_2.Add(sizer_3, 1, wx.EXPAND, 0)  
        self.SetSizer(sizer_2)  
        sizer_2.Fit(self)  
        self.Layout()  
        # end wxGlade  
  
    def bu1(self, event):  # wxGlade: MyFrame.<event_handler>  
        self.text_ctrl_1.AppendText("1")  
        event.Skip()  
  
    def bu2(self, event):  # wxGlade: MyFrame.<event_handler>  
        self.text_ctrl_1.AppendText("2")  
        event.Skip()  
  
    def bu3(self, event):  # wxGlade: MyFrame.<event_handler>  
        self.text_ctrl_1.AppendText("3")  
        event.Skip()  
  
    def bu_plus(self, event):  # wxGlade: MyFrame.<event_handler>  
        self.num1=self.text_ctrl_1.GetValue()  
        self.op="+"  
        self.text_ctrl_1.Clear()  
        event.Skip()  
  
    def bu4(self, event):  # wxGlade: MyFrame.<event_handler>  
        self.text_ctrl_1.AppendText("4")  
        event.Skip()  
  
    def bu5(self, event):  # wxGlade: MyFrame.<event_handler>  
        self.text_ctrl_1.AppendText("5")  
        event.Skip()  
  
    def bu6(self, event):  # wxGlade: MyFrame.<event_handler>  
        self.text_ctrl_1.AppendText("6")  
        event.Skip()  
  
    def bu_min(self, event):  # wxGlade: MyFrame.<event_handler>  
        self.num1=self.text_ctrl_1.GetValue()  
        self.op="-"  
        self.text_ctrl_1.Clear()  
        event.Skip()  
  
    def bu7(self, event):  # wxGlade: MyFrame.<event_handler>  
        self.text_ctrl_1.AppendText("7")  
        event.Skip()  
  
    def bu8(self, event):  # wxGlade: MyFrame.<event_handler>  
        self.text_ctrl_1.AppendText("8")  
        event.Skip()  
  
    def bu9(self, event):  # wxGlade: MyFrame.<event_handler>  
        self.text_ctrl_1.AppendText("9")  
        event.Skip()  
  
    def bu_mul(self, event):  # wxGlade: MyFrame.<event_handler>  
        self.num1=self.text_ctrl_1.GetValue()  
        self.op="x"  
        self.text_ctrl_1.Clear()  
        event.Skip()  
  
    def bu_clear(self, event):  # wxGlade: MyFrame.<event_handler>  
        self.text_ctrl_1.Clear()  
        event.Skip()  
  
    def bu0(self, event):  # wxGlade: MyFrame.<event_handler>  
        self.text_ctrl_1.AppendText("0")  
        event.Skip()  
  
    def bu_chu(self, event):  # wxGlade: MyFrame.<event_handler>  
        self.num1=self.text_ctrl_1.GetValue()  
        self.op="/"  
        self.text_ctrl_1.Clear()  
        event.Skip()  
  
    def bu_result(self, event):  # wxGlade: MyFrame.<event_handler>  
        self.num2=self.text_ctrl_1.GetValue()  
        num1=int(self.num1)  
        num2=int(self.num2)  
        if self.op=="+":  
            self.text_ctrl_1.SetValue(str(num1+num2))  
        elif self.op=="-":  
            self.text_ctrl_1.SetValue(str(num1-num2))  
        elif self.op=="x":  
            self.text_ctrl_1.SetValue(str(num1*num2))  
        elif self.op=="/":  
            self.text_ctrl_1.SetValue(str(num1/num2))  
        event.Skip()  
  
  
# end of class MyFrame  
if __name__ == "__main__":  
  
    app=wx.App(False)  
    myframe=MyFrame(None)  
    app.MainLoop()  

 

以上是关于130242014058+吴伟铭+第2次实验的主要内容,如果未能解决你的问题,请参考以下文章

第二次实验报告:使用Packet Tracer分析应用层协议

第四次实验报告:使用 Packet Tracer理解RIP路由协议

130242014057 周陈清 第2次实验

OS第2次实验报告:创建进程

操作系统第2次实验报告:创建进程

操作系统第2次实验报告:创建进程