Programming Language Basics in Compiler Design
Programming Language Basics
To design an efficient compiler, we should know some language basics.
Programming Language Basics
Static and Dynamic Distinction
- Static - Events occur at compile time.
- Dynamic - Events occur at run time.
Example
- The scope of a declaration of x is the region of the program in which uses of x refer to this declaration.
- A language uses static scope or lexical scope if it is possible to determine the scope of a declaration by looking only at the program.
- Otherwise, the language uses dynamic scope. With dynamic scope, as the program runs, the same use of x could refer to any of several different declarations of x.
Environment and States
- The environment is mapping from names to locations in the store. Since variables refer to locations, we could alternatively define an environment as a mapping from names to variables.
- The state is a mapping from locations in store to their values.
Environment and States
Static Scope and Block Structure
- The scope rules for C are based on program structure. The scope of a declaration is determined implicitly by where the declaration appears in the program.
- Programming languages such as C++, Java, and C#, also provide explicit control over scopes through the use of keywords like public, private, and protected.
- A block is a grouping of declarations and statements. C uses braces { and } to delimit a block, the alternative use of begin and end in some languages.
Static Scope and Block Structure
Read Also
Explict Access Control
- Classes and structures introduce a new scope for their members.
- If p is an object of a class with a field (member) x, then the use of x in p.x refers to field x in the class definition.
- Through the use of keywords like public, private, and protected, object oriented languages such as C++ or Java provide explicit control over access to member names in a super class. These keywords support encapsulation by restricting access.
- Public - Public names are accessible from outside the class
- Private - Private names include method declarations and definitions associated with that class and any "friend" classes.
- Protected - Protected names are accessible to subclasses.
Dynamic Scope
- The term dynamic scope, however, usually refers to the following policy: a use of a name x refers to the declaration of x in the most recently called procedure with such a declaration.
- Dynamic scoping of this type appears only in special situations. The two dynamic policies are:
- Macro expansion in the C preprocessor
- Method resolution in object-oriented programming.
Since dynamic scoping is very uncommon in the familiar languages, we consider the following code as our example.
Sample Code
Output
20
Parameter Passing Mechanisms
- Every language has some method for passing parameters to functions and procedures.
- Formal Parameters: The identifier used in a method to stand for the value that is passed into the method by a caller.
- Actual Parameters: The actual value that is passed into the method by a caller.
- Call by Value - The actual parameter is evaluated (if it is an expression) or copied (if it is a variable) in a formal parameter.
- Call by Reference - The address of the actual parameter is passed as value of the corresponding formal parameter.
- Call by Name - The Called object execute as if the actual parameter were substituted literally for the formal parameter.
Formal and Actual Parameter
Aliasing
- When two names refer to the same location in memory.
- There is an interesting consequence of call-by-reference parameter passing or its simulation, as in Java, where references to objects are passed by value.
- It is possible that two formal parameters can refer to the same location; such variables are said to be aliases of one another.
- As a result, any two variables, which may appear to take their values from two distinct formal parameters, can become aliases of each other.
Aliasing