📂
CS-NoteBook
  • Introduction
  • CS144
    • concise introduction to Internet
      • 1.1 Networked Applications
      • 1.2 The 4 Layer Internet
      • 1.3 IP
      • 1.4 A Day in the Life of a Packet
      • 1.5 Principle: Packet Switching
      • 1.6 Principle:Layering
      • 1.7 Principle: Encapsulation
      • 1.8 Byte order and packet formats
      • 1.9 name and addresses:IPv4
      • 1.10 Longest Prefix Match for Link Layer
      • 1.11 Address Resolution Protocol(ARP)
      • 1.12 Summary
    • Transport Layer
      • 2.1 The TCP Service Model
      • 2.2 UDP service model
      • 2.3 ICMP(Internet Control Message Protocol 互联网报文控制协议)
      • 2.4 The End-to-End Principle
      • 2.5 Error Detection:3 schemes (Checksum,CRC and MAC)
      • 2.6 Finite State Machines(有限状态机)
      • 2.7 Flow Control
      • 2.8 Sliding window
      • 2.9 Retransmission Strategies
      • 2.10 TCP Header
      • 2.11 TCP Setup and Teardown
      • 2.12 Recap
    • Package Switching
      • 3.1 The history of Internet
      • 3.2 What is packet switching
      • [3.3 End-to-end delay and Queueing delay
      • 3.4 Playback Buffer(回放缓存区)
  • CS 61C
    • 1.4 C Memory Mangement, Usage
    • 1.5 Intro to Assembly Language, MIPS Intro
    • 1.5 extra bits operation
  • CS 61B
  • CS 61A
    • Function
    • Names
    • The Art of the Function
    • Control
    • Higher-Order Function
    • Recursive Function
    • List
    • Non-Local Assignment
    • Iterators
    • Objects
    • Data Abstraction
    • OOP
    • Inheritance
    • Representations
    • Decomposition
    • Scheme
    • Exceptions
    • Calculator
    • Interpreters
    • Declarative_Programming
    • Table
    • Aggregation
      • More_recursion
    • Databases
    • Distributed_Data
    • Tail Recursion
    • Exercises
      • lab00
      • lab01
      • hw01
      • tree Recursion example -- give Change
  • The Web DevelopMent Bootcamp
    • html5
    • css
    • bootstrap3
    • bootstrap4
    • javascript expression
    • javascript function
Powered by GitBook
On this page
  • Scheme syntax calculator
  • eval function
  • Interactive Interpreters

Was this helpful?

  1. CS 61A

Calculator

  • Parsing (解析)

    Text -> Lexical analysis (词法分析) -> Token (标记) -> Syntactic analysis (句法分析) -> Expression

  • scheme 可用递归进行句法分析

    • Base case:符号和数字

    • Recursive call:scheme_read子表达式,并将其组合起来

    • Syntactic analysis:确定了一个表达的层次结构,可以是嵌套

    • 每次调用scheme_read都会消耗一个表达式的输入标记。

Scheme syntax calculator

  • 计算器表达式的值是递归定义的。

    • Primitive。一个数字对其自身进行评估。

    • Call。调用表达式对其参数值进行运算。

      • +: 参数值之和

      • *: 各项参数的乘积

      • -: 如果只有一个参数,取负。如果有多个参数,则从第一个参数中减去其余参数。

      • /: 如果有一个参数,就把它反过来。如果超过一个参数,则从第一个参数中除以其余部分。

eval function

  • eval函数计算一个表达式的值,它总是一个数字

  • 它是一个通用函数,根据表达式的类型(primitive 或 call)进行分配。

def calc_eval(exp):
    if type(exp) in (int, float): # A number evaluates to itself
        return exp

    # A call expression evaluates to its argument values combined by an operator 
    # (调用表达式对其参数值进行评估,由一个运算符组合而成。)
    elif isinstance(exp, Pair):  

        # calc_eval: Recursive call returns a number for each operand 
        # (递归调用为每个操作数返回一个数字。)
        arguments = exp.second.map(calc_eval)  

        # exp.first: '+', '-', '*', '/'
        # arguments: A Scheme list of numbers
        return calc_apply(exp.first, arguments)

    else:
        raise TypeError

def calc_apply(operator, args):
    if operator == '+':
        return reduce(add, args, 0)
    elif operator == '-':
        ...
    elif operator == '*':
        ...
    elif operator == '/':
        ...
    else:
        raise TypeError

Interactive Interpreters

  • 读取 —— 执行 — 打印循环

    • 许多编程语言的用户界面是一个交互式解释器。

      1. 打印一个提示

      2. 读取用户输入的文本

      3. 将输入的文本解析为一个表达式

      4. 评价这个表达式

      5. 如果发生任何错误,请报告这些错误。

      6. 否则打印表达式的值,并重复

def read_print_loop():
    """Run a read-print loop for Scheme expressions."""
    while True:
        try:
            src = buffer_input()
            while src.more_on_line:
                expression = scheme_read(src)
                print(repr(expression))
        except (SyntaxError, ValueError) as err:  # ValueError: 2.3.4; SyntaxError: 额外的‘)’
            print(type(err).__name__ + ':', err)
        except (KeyboardInterrupt, EOFError):  # <Control>-D, etc. 真正的停止方式
            return
PreviousExceptionsNextInterpreters

Last updated 4 years ago

Was this helpful?