Report 3 ¶
Specification ¶
~cpp C supports two kinds of parameter passing mode, pass-by-value and pass-byreference, but not pass-by-name. A technique for implementing pass-by-name is to treat the actual parameters as thunks. Whenever a formal parameter is referenced in a subprogram, the corresponding thunk compiled for that parameter is executed. Even though it is difficult to avoid run-time overhead in the execution of thunks, sometimes it provides a very powerful feature for some applications like a generic summation routine. Suppose that whenever a programmer wants to pass a parameter by name in C, it is required to prefix the keyword name just in front of both the actual and formal parameters as below: Sum (name i + j, 1, n, name V[i]); Of course, if a C program with the call statement above is to be compiled by a conventional compiler, syntax errors should occur since it violates the syntactical rules of C. You are required to implement C preprocessor that supports the pass-by-name parameter-passing method. The preprocessor should transform C programs with name parameters into pure C programs so that the transformed programs manage to handle parameters prefixed with name as if they were called by name without causing any syntax errors.
μμ μ μ΄ν΄λ₯Ό μν΄μ μ°μ thunks λΌλ λ¨μ΄μ μλ―Έλ₯Ό μμμΌν©λλ€.
thunks λ λκ°μ§ μλ―Έλ‘ ν΄μλ©λλ€. 첫λ²μ§Έ, ν¨μν μΈμ΄μμ λ§νλ μ§μ°ν κ³μ° λλ²μ§Έ, νΈνμ±μ μ΄μ λ‘ μλ‘λ€λ₯Έ λ°μ΄ν°ν κ°μ λ§€νμ ννλ νμ
μ΄λ² μμ μμ ꡬννλ €λ κ²μ 첫λ²μ§Έμ μλ―Έλ‘ μ§μ°ν κ³μ°(delayed computation)μ μλ―Έν©λλ€. call-by-name, call-by-needλ₯Ό ν΅ν΄ ν¨μμκ² λμ΄μ€λ μΌλ ¨μ λ§€κ°λ³μλ₯Ό thunkλΌλ μ΄λ¦μΌλ‘ λΆλ¦ λλ€. κ°λ¨ν λ§ν΄ thunkλΌλ κ²μ μ€νμμ κ³μ°λμ΄ λ³μμ κ°μ΄ μ»μ΄μ§λ€λ μλ―Έμ λλ€. (μ΄λ κΈ°μ‘΄μ ν¨μμμ νλΌλ©ν° ν¨μ±μμ Call μμ λ³μμ λ°μΈλ©λλ κ²κ³Όλ λ€λ¦ λλ€.) μ΄λ° κΈ°λ₯μ΄ μ΅μ΄λ‘ ꡬνλ Algol60μ λλ€.
Requirement
μμ€κ° μ μ΄ν΄κ° μλμ λ€λ©΄... -_-; κ³μ 보μλΌκ³ λ°μ;;;
cf) Jensen's Device, Man Boy Test λ Compiler Theory μ ν νλͺ©λ€μ λλ€.
Hand-In
thunks λ λκ°μ§ μλ―Έλ‘ ν΄μλ©λλ€. 첫λ²μ§Έ, ν¨μν μΈμ΄μμ λ§νλ μ§μ°ν κ³μ° λλ²μ§Έ, νΈνμ±μ μ΄μ λ‘ μλ‘λ€λ₯Έ λ°μ΄ν°ν κ°μ λ§€νμ ννλ νμ
μ΄λ² μμ μμ ꡬννλ €λ κ²μ 첫λ²μ§Έμ μλ―Έλ‘ μ§μ°ν κ³μ°(delayed computation)μ μλ―Έν©λλ€. call-by-name, call-by-needλ₯Ό ν΅ν΄ ν¨μμκ² λμ΄μ€λ μΌλ ¨μ λ§€κ°λ³μλ₯Ό thunkλΌλ μ΄λ¦μΌλ‘ λΆλ¦ λλ€. κ°λ¨ν λ§ν΄ thunkλΌλ κ²μ μ€νμμ κ³μ°λμ΄ λ³μμ κ°μ΄ μ»μ΄μ§λ€λ μλ―Έμ λλ€. (μ΄λ κΈ°μ‘΄μ ν¨μμμ νλΌλ©ν° ν¨μ±μμ Call μμ λ³μμ λ°μΈλ©λλ κ²κ³Όλ λ€λ¦ λλ€.) μ΄λ° κΈ°λ₯μ΄ μ΅μ΄λ‘ ꡬνλ Algol60μ λλ€.
~cpp x = sigma(i=1, n) {V} 1) Your program should handle Jensenβs device; show that your program works properly by testing it for various summation below. 2) You are also recommended to check if Swap (x, y) does work properly or not by passing parameters by name. 3) You have to show off the robustness of your program by checking various error conditions.Jensen's Machine μ JΓΈrn JensenλΌλ μ¬λμ΄ Algol 60μ μ μνλ λ³΄κ³ μμμ μ μν νλ‘κ·Έλλ° ν ν¬λμ λ§ν©λλ€.
~cpp begin integer i; real procedure sum(i, lo, hi, term); value lo, hi; integer i, lo, hi; real term; comment term is passed by-name; begin real temp; temp := 0; for i := lo step 1 until hi do temp := temp + term; sum := temp end; print( sum(i, 1, 100, 1/i)) endμ μλ λ¬Έμ λ μκΈ°μ κ°μΌλ©°, μ΄λ 100λ²μ§Έμ μ‘°νμλ₯Ό ꡬνλ λ¬Έμ λ₯Ό ν΄κ²°νλ λ°©λ²μ λλ€. μ°¨νμ κ·Έ μ΄λ¦λ μ λͺ ν Knuth λκ»μ Man Boy Testλ‘ λ°μ μμΌ°μ΅λλ€.
μμ€κ° μ μ΄ν΄κ° μλμ λ€λ©΄... -_-; κ³μ 보μλΌκ³ λ°μ;;;
cf) Jensen's Device, Man Boy Test λ Compiler Theory μ ν νλͺ©λ€μ λλ€.
~cpp You should follow the guideline suggested later. To be specific, your report is supposed to include an external documentation as well as an internal documentation. The external documentation should explain your program in detail so that we can understand what kind of data structures have been used, the characteristics and general as well as unique features of your program, etc. An internal documentation means the comment attached in your source. You are supposed to upload your source program and executable file. You are also required to submit a listing of your program and a set of test data of your own choice, and its output, when you show up for demo. Be sure to design carefully your test data to exercise your program completely.
brief note ¶
μ μ κΉ νλ©΄μ μκ°ν건λ°.... μ΄ μμ μ λ§λ‘ ꡬ리λ€.
λ΄κ° μ΄λ κ² μ¬λ―Έμλ μμ λ₯Ό νκ²λ건 νμΌκ΅¬μ‘°λ binary κ°μ§κ³ μ₯λμΉ μ΄ν μ²μμΈλ―νλ€. λ¬Έμμ΄ μ₯λν κΊΌλ©΄ νλ‘νκ² ν΄μ£Όλμ§... C λ¬Έμμ΄ ν¨μ κ°μ§κ³ λλ €λ... μ λ§λ‘ ꡬ리λ€λΌλ μκ°λΏ~ - eternalbleu
