Question: Macro processors, such as cpp and m4, do a form of program manipulation before the program is further compiled. You can think of this as



Macro processors, such as cpp and m4, do a form of program manipulation before the program is further compiled. You can think of this as symbolic program "pre-evaluation." For example, if a program contains the macro \#define square (x)((x)(x)) macro expansion of a statement containing square (y+3) will replace square (y+3) with (y+3)(y+3). One way to build a macro processor is to associate a lambda expression with each macro definition, and then do -reduction wherever the macro is used. For example, we can represent square as the lambda expression x.(xx) and -reduce: (x.(xx))(y+3)=(y+3)(y+3) if square (y+3) appears in the program. Some problems may arise, however, when variables that appear in macros also appear in the program. 1. [2pts] Suppose a program contains three macros: #definef(x)#defineg(y)#defineh(z)(x+x)(y2)(f(g(z))) Macro h can be written as the following lambda expression: zf(xx+x)(g(yy2)z) Simplify the expression h (3) by using -reduction. Do not simplify the arithmetic. Only reduce one step at a time. Use as many or as few lines as you need. (z(xx+x)((yy2)z))3==== 2. [2pts] A problem arises if a local variable inside a macro has the same name as an argument the macro is invoked with. Assuming that typeof is supported, the following macro works as long as neither of the parameters is named temp. \#define swap (a,b){ typeof (a) temp =a;a=b;b= temp; } Show what happens if you macro-expand swap ( x, temp) without doing anything special to recognize that temp is bound in the body of the macro. You do not need to convert this macro to lambda notation. 3. [2pts] Explain what the problem is and how you can solve it. 4. [2pts] Show how your solution would expand swap ( x, temp) properly
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
