CSC3339 test2 review View Miciosoft Word Product Activation
Solution
Q1. Primary tasks of a lexical analyzer are as follows:
Q2. Two goals of syntax analysis:
1. Check input for correct syntax.
2. Produce complete parse tree of input.
Q3. A machine as number of states, when these states are represent in the form graph
from which you can understand that what events should occur and what effect on object.
Q4. Differences between top-down and bottom-up parsers.
Top-down parsers
Bottom-up parsers
Q5. Recursive-descent parsing subprogram is written for a rule with a single RHS. For each terminal symbol in the RHS, compared with nextToken terminal symbol. If the camparision is false then, it is a syntax error. If the comparision match, the lexical analyzer is called to get the next input token. For each nonterminal, parsing subprogram to be called nonterminal.
Q10. When type of the object is determined at compiled time(by the compiler), it is known as static binding.
If there is any private, final or static method in a class, there is static binding.
class func{
private void funcCall(){System.out.println(\"func is executing...\");}
public static void main(String args[]){
func f1=new func();
f1.funcCall();
}
}
Q11. Scope & Lifetime
The scope of a declaration is the part of the program for which the declaration is in effect.
C/C++ use lexical scoping.
The lifetime of a variable or object is the time period in which the variable/object has valid memory.
Lifetime is also called \"allocation method\" or \"storage duration.\"
The relationships and differences among them are as follows:
Lifetime
Static: A static variable is stored in the data segment of the \"object file\" of a program. Its lifetime is the entire duration of the program\'s execution.
Automatic: An automatic variable has a lifetime that begins when program execution enters the function or statement block or compound and ends when execution leaves the block. Automatic variables are stored in a \"function call stack\".
Dynamic: The lifetime of a dynamic object begins when memory is allocated for the object (e.g., by a call to malloc() or using new) and ends when memory is deallocated (e.g., by a call to free() or using delete). Dynamic objects are stored in \"the heap\".
Scope
Local scope: \"visible\" within function or statement block from point of declaration until the end of the block.
Class scope: \"seen\" by class members.
Namespace scope: visible within namespace block.
File scope: visible within current text file.
Global scope: visible everywhere unless \"hidden\".
Q12. Variables can be placed in one of four categories depending on their lifetimes:
1. Static variables
2. Stack-dynamic variables
3. Explicit heap-dynamic variables
4. Implicit heap-dynamic variables
1. static variables
lifetime : entire program execution
examples:
global variables
local variables declared static in C and C++
maintain their values between function executions (history sensitive)
this requires statically allocated storage
2. stack-dynamic variables
• lifetime : they are declared in execution of the block
• examples: nonstatic local variables in functions and blocks
elaboration of a declaration refers to allocating storage and binding it to the variable in a declaration.
In languages that allow variable declarations after the beginning of a block (Java, C++), elaboration of such variables may occur at the beginning of the nearest enclosing block, or at the point where the variable is declared (implementation dependent). The lifetime of a stack-dynamic variable is from elaboration time until the end of the execution of the block. Hence, the lifetime of such a variable maybe less than the execution of the block.
3. explicit heap-dynamic variables
• explicit heap-dynamic variables are memory cells explicitly allocated from the heap
• examples: memory cells allocated using new in Java and C++
• often anonymous (unnamed)
• lifetime : allocation time until explicit deallocation or garbage collection
• example: in C++, the lifetime of such a variable is from the time it is allocated (using new) untilit is deallocated (using delete).
4. implicit heap-dynamic variables
• implicit heap-dynamic variables are variables for which storage isn’t allocated until the variable is assigned to, and the storage associated with the variable can change with every assignment to it.
• lifetime : from one such assignment to the next.
• examples: strings and arrays (hashes) in Perl, arrays in JavaScript.
• implicit heap-dynamic variables are extremely flexible, but also expensive to use.
Q13. In programming, a constant is a value that never changes throughout the program. Other type of values that programs use is variables, symbols that can represent different values.
A constant can be a number, like 25 or 3.6.
A constant can be a character, like a or $.
A constant can be a character string, like \"this is a string\".

