PLS Quiz 2 Lexical and Syntax Analysis & Names, Binding, and Scope
What are the primary tasks of lexical analysis?
"The main task of lexical analysis is to to read input characters in the code and produce tokens. "Get next token" is a command which is sent from the parser to the lexical analyzer. On receiving this command, the lexical analyzer scans the input until it finds the next token. It returns the token to Parser."
What are aliases?
- If two variable names can be used to access the same memory location, they are called aliases - Aliases are created via pointers, reference variables, C and C++ unions - Aliases are harmful to readability (program readers must remember all of them)
Advantages of LR parsers
-They will work for nearly all grammars that describe programming languages. -They work on a larger class of grammars than other bottom-up algorithms, but are as efficient as any other bottom-up parser. -They can detect syntax errors as soon as it is possible. -The LR class of grammars is a superset of the class parsable by LL parsers.
Describe briefly the three approaches to building a lexical analyzer.
1. Write a formal description of the token patterns of the language using a descriptive language related to regular expressions. These descriptions are used as input to a software tool that automatically generates a lexical analyzer. There are many such tools available for this. The oldest of these, named lex, is commonly included as part of UNIX systems. 2. Design a state transition diagram that describes the token patterns of the language and write a program that implements the diagram. 3. Design a state transition diagram that describes the token patterns of the language and hand-construct a table-driven implementation of the state diagram.
Binding time in ruby for: General meaning +
?
Binding time in ruby for: Keywords
?
Binding time in ruby for: Memory allocation for a variable
?
Binding time in ruby for: Name of a variable
?
Binding time in ruby for: Value of a variable
?
What are the differences between finite automata and pushdown automata?
A Pushdown automata (PDA) is a finite state machine with an added stack storage. Additional stack is used in making the decision for transitions apart from input symbols and current state. A Finite Automata is a mathematical model of any machine by which we can calculate the transition of states on every input symbol. Each transition in finite automata depends on the input symbols and current transition state.
What is binding?
A binding is an association between an entity and an attribute, such as between a variable and its type or value, or between an operation and a symbol
Advantages and disadvantages of dynamic binding
Advantage: • flexibility (generic program units) Disadvantages: • High cost (dynamic type checking and interpretation) • Type error detection by the compiler is difficult
Advantages and disadvantages of static binding
Advantages: • efficiency (direct addressing), history-sensitive subprogram support Disadvantage: • lack of flexibility (no recursion), cannot be shared among variables
What are the differences between nondeterministic and deterministic finite automata?
DFA: DFA refers to Deterministic Finite Automaton. A Finite Automata(FA) is said to be deterministic if corresponding to an input symbol, there is a single resultant state i.e. there is only one transition. NFA refers to Nondeterministic Finite Automaton. A Finite Automata(FA) is said to be non-deterministic if there is more than one possible transition from one state on the same input symbol.
What are dangling pointers?
Dangling pointers and wild pointers that do not point to a valid object of the appropriate type.
What are the advantages and disadvantages of dynamic scoping?
Disadvantages: Inability to statically check for references to nonlocal variables. Dynamics scoping also makes program difficult to read because the calling sequence of subprograms must be known to determine the meaning of references to nonlocal variables. There's no way to protect local variables from being accessed to by subprograms because local variables of subprogram are all visible to all other executing subprograms, regardless of textual proximity. Accessing to nonlocal variables in dynamic scoping takes far longer than accesses to nonlocal variables when static scoping is used. Advantages: On the other hand, the only advantage of dynamic scoping is writability. It is more convenient and it provides more flexibility than static scoping. For example, in some cases, some parameters passed from one subprogram to another are variables that are defined in the caller. None of these need to be passed in a dynamic scoped language, because they are implicitly visible in the called subprogram.
What is dynamic binding
Dynamic Type Binding (JavaScript, Python, Ruby, PHP, and C# (limited)) Specified through an assignment statement e.g., JavaScript Dynamic binding takes place during runtime
What is the different times binding can happen?
Execution time or Translation time: Language Design Time Language implementation time compile time link time load time runtime
What are the primary goals of syntax analysis?
First, syntax analyzer must check the input program to determine whether it is syntactically correct. The second goal is to produce a complete parse tree, or at least trace the structure of the complete parse tree, for syntactically correct input.
Dynamic type binding is closely related to implicit heap-dynamic variables. Explain this relationship.
Implicit heap-dynamic variables acquire types only when assigned values, which must be at runtime. Therefore, these variables are always dynamically bound to types.
What are the relative advantages and disadvantages of LL and LR parsers?
LR parsers are more general: all LL languages are LR languages, but there are more LR languages than LL languages (a language is an LL language if it can be parsed with an LL parser, and a language is an LR language if it can be parsed with an LR parser). compiler errors and error recovery: LL wins here. In an LL parse, it's usually pretty easy to emit useful compiler errors, in particular in hand-written parsers. You know what you're expecting next, so if it doesn't turn up, you usually know what went wrong and what the most sensible error would be.
Binding time for: General meaning of operators, e.g. + (add)
Language design time
Binding time for: Keywords, e.g. class in C++ and Java
Language design time
Binding time for: Primitive types, e.g. float
Language design time
Binding time for: Reserved words, e.g. main in C++
Language design time
Binding time for: Syntax and semantics, e.g. if (a>0) b=a;
Language design time
Binding time for: Internal representation of literals, e.g. 56 and "foo bar" The data type is bound to a range of possible values
Language implementation time
Binding time for: Linking calls to static library routines, e.g. printf in C++
Link time
Binding time for: Merging multiple object codes into one executable
Link time
Binding time for: Loading executable in memory and adjusting absolute addresses
Load time
What are memory leaks?
Memory is not properly de-allocated when it is done being used or out of scope
What are the six attributes of a variable?
Name: identifier Address: memory location(s) Value: particular value at a moment Type: range of possible values Lifetime: when the variable is accessible Scope: where in the program it can be accessed
Which automata is used to model/recognize which grammar?
Pushdown automata: Context-free grammar Finite Automata: Regular grammar
Binding time for: Non-static allocation of space for variable
Run time
Binding time in ruby for: Type of a variable
Run time
Scope vs lifetime
Scope is the region or section of code where a variable can be accessed. Lifetime is the time duration where an object/variable is in a valid state.
What is a static ancestor of a subprogram? What is a dynamic ancestor of a subprogram?
Static ancestors are those that strictly contain the subprogram in question. Dynamic ancestors of a subprogram sub() are all the procedures called before sub() during the execution of a program, that have not yet finished executing.
What is static binding
Static--bound to memory cells before execution begins and remains bound to the same memory cell throughout execution, e.g., C and C++ static variables in functions
Some programming languages are typeless. What are the obvious advantages and disadvantages of having no types in a language?
The advantage of a typeless language is flexibility; any variable can be used for any type values. The disadvantage is poor reliability due to the ease with which type errors can be made, coupled with the impossibility of type checking detecting them.
Load time ex.
bind a C or C++ static variable to a memory cell)
Link time ex.
bind a function from a library
Runtime ex.
bind a nonstatic local variable to a memory cell
Compile time ex.
bind a variable to a type in C or Java
Language implementation time ex.
bind floating point type to a representation
Language design time ex.
bind operator symbols to operations
What is l-value and r-value?
l-value: variable address r-value: variable value
Explicit heap-dynamic variables
• Explicit heap-dynamic -- Allocated and deallocated by explicit directives, specified by the programmer, which take effect during execution • Referenced only through pointers or references, e.g. dynamic objects in C++ (via new and delete), all objects in Java • Advantage: provides for dynamic storage management • Disadvantage: inefficient and unreliable
implicit heap-dynamic
• Implicit heap-dynamic--Allocation and deallocation caused by assignment statements - all variables in APL; all strings and arrays in Perl, JavaScript, and PHP Advantage: - flexibility (generic code) Disadvantages: - Inefficient, because all attributes are dynamic - Loss of error detection
Stack-dynamic
• Stack-dynamic--Storage bindings are created for variables when their declaration statements are elaborated. (A declaration is elaborated when the executable code associated with it is executed) • If scalar, all attributes except address are statically bound - local variables in C subprograms (not declared static) and Java methods Advantage: allows recursion; conserves storage Disadvantages: - Overhead of allocation and deallocation - Subprograms cannot be history sensitive