4

I'm not much of a C programmer, but I was under the assumption that C macros were almost sort of a find and replace feature where the pre-processor takes the macro definition and puts it wherever it sees the macro name.

This is the Dragon Book's example of dynamic scope rules and how they apply to macros:

#define a (x + 1)

int x = 2;
void b () { int x = 1; printf("%d\n", a); }
void c () { printf("%d\n", a); }
void main () { b(); c(); }

They also discuss how dynamic scope rules apply to the name x within macro a. I was under the assumption that it would basically replace a with (x + 1) and then compile the program and so the scope rules would be exactly the same as if you had written (x + 1) instead of a (which would be static scope rules).

Could anyone clarify this?

Edit: Book referred to is Compilers: Principles, Techniques & Tools Second Edition. The example quoted is from pages 31-32.

0

3 Answers 3

3

Your understanding is correct: find every use of the identifier a and substitute that a with (x + 1). That is exactly what the preprocessor does.

The only "scope" that we can discuss with respect to an object-like macro (like a) is the scope of the macro itself: The scope of a macro is from the line on which it is defined (with a #define directive) until the line on which it is undefined (with a #undef directive) or until the end of the translation unit (the .cpp and all of the headers it includes), if it is never undefined.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm confused why it was talking about the dynamic scope rules and how they apply to x...
This answer fails to address the question about clarifying Aho et. al.'s example and misses the whole point of it. See happydave's response or my succinct answer.
2

Your understanding of the #define behaviour is correct.

What I think the book means when it says "dynamic scoping" is that the name x is resolved based on the environment where the macro is called, not where it is defined. So if you've set a global variable x=3 just before your #define, that's irrelevant to the value of x in the #define - it will just use the value of x wherever you use the macro - if there is some other local variable x in the function where you use the macro, then the local value will be used.

This is in contrast to lexical scoping (which is what is actually used in the C language, and in almost all modern languages), in which a name refers to its local lexical environment. For example, if you replaced the #define in your example by the simple statement a = x+1, then the value of a in the function would be one more than whatever x happened to be at the point where a = x+1 appears in the code. It wouldn't matter if some other local variable named x happened to exist at the point where you use the value a. Similarly, if you defined a function int f() { return x + 1; }, x would refer to the global variable x, not some other local variable named x that happens to exist where f() is called. If this seems blindingly obvious, it's because, as I said, pretty much all languages use lexical scope (although Perl, for example, also allows dynamic scope using the local function).

See http://en.wikipedia.org/wiki/Scope_(computer_science)#Lexical_scoping_and_dynamic_scoping for a bit more of an explanation of the concept.

4 Comments

I guess I understand what dynamic scoping is vs. lexical scoping - I'm mainly confused why they used c macros to explain it, since when the program is actually being compiled the macro is long gone and the statement x+1 is in it's place. This was a good explanation, though.
I agree, I've never really heard the term dynamic scoping applied to macros. But I guess the idea is that conceptually, the preprocessing step is an implementation detail - the fact that the preprocessor gives you the ability to create dynamically scoped names is where (some of) the real power of the preprocessor comes from.
True, I guess I just took the example too literally. As long as I'm not missing some weird detail. Thanks!
@user12345613 They used C macros to explain it because it's a simple and obvious example of dynamic scoping in a familiar language. And you're making assumptions about how C compilers work that aren't necessarily true and aren't relevant. Many C compilers do not do macro replacement throughout the source and then compile it, they expand macros when encountered and pass them to the grammar parser on demand. How it is compiled doesn't affect the abstraction of x being dynamically scoped that they are addressing.
1

Their point is that the x that a refers to is the local in b() but the global in c() -- it's dynamically scoped. The #define could be interpreted as "use dynamic scope to resolve x when evaluating a".

5 Comments

but this is really where my confusion comes in, because there is no resolving x if the pre processor replaces a with x + 1. Right?
You're engaging in a basic programmer's error, "implementation on the brain". You should think more abstractly. Imagine a subset of C in which there was no textual replacement of macros but the given example was valid -- only names can be #defined, and only with well-formed expressions. The value of a is defined in terms of x. Which x? It's dynamically scoped.
P.S. I think you may be making another mistake that might be called "sticking to your guns". You say you're confused, but you seem to be implying that one should be confused ... that Aho et. al.'s example is bad or wrong. Consider that they may have a much better understanding of the subject than you do, in which case you should try much harder to see their example as valid rather than seeking objections to it.
@ Jim good points. I don't really think I'm "sticking to my guns", however. I made a post about it because I really was confused. I'm on chapter one of my first reading of any compiler book and figured I would try and understand why they chose the example. The answers here claimed my understanding of the macros was correct - which made me even more confused by the example. I see now they weren't. I know that the writers of the book know more than I do, or I wouldn't have paid money for it. I get your point about the implementation details be irrelevant and I see what they are going for now.
@user12345613 Excellent! You're way ahead of the game.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.