Programming Languages Review Questions

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Outline the steps carried out by the driver in a shift-reduce parser. (2)

1. Shift tokens from scanner onto stack while keeping track of state until 2. A right-hand side is recognized and is then 3. reduced to a left-hand side by popping off the stack and pushing left-hand side back onto right

How can a variable be live but not in scope? Give 2 examples. (3)

1.) Global variable x is still live but not in scope if a local variable x is declared in the scope. 2.) C++: the private member variables of an object of class C are live but not in scope when execution is not inside a member function of C.

What formal rules are needed to define a regular grammar, and how can a regular grammar be extended to a context-free grammar? (2)

3 rules needed: Concatenation, alternation, Kleene closure (repetition) Regular expression to context-free grammar -> add recursion

Explain the difference between context-sensitive and context-free grammars. (2)

A CS Grammar mean some rules only apply under certain conditions (aXb -> aYb, where Y may be terminal or nonterminal, and either Y, a, or b are null/missing) but CF grammar has only a terminal on the left side of a production (X -> aYb).

What is the handle of an expression, and what is its significance (why is it important)? (2)

A handle is the left-most simple phrase of a parse tree. Significant because it's the next node to be processed

What is a simple phrase in a parse tree? (2)

A leaf one step away from the root

What is a predictive parser? (2)

A parser that is top down, table driven and LL

What is a frame pointer? (3)

A register that points to a known location within the frame of the current subroutine

What is a free list? (3)

A singly linked-list of heap blocks not currently in use

What is a stack frame? (3)

A structure stored on the stack where all information associated with a function call is recorded.

What is the difference between aliasing and overloading? (3)

Aliases - 2 or more names referring to the same object Overloading - 2 or more entities sharing the same name

Define and explain the role of assertions. (4)

An assertion is a statement that a specified condition is expected to be true when execution reaches a certain pint in the code. Become runtime dynamic checks

What is a forward reference? (3)

An attempt to use a variable before it's declared; It's a static semantic error

What is an epsilon production? (2)

An empty string on the right hand side of a production

What is a regular expression? (2)

An object that describes a pattern of characters Any one of the following: 1. A character 2. Empty string (epsilon) 3. Two regular expression concatenated with each other 4. Two regular expression separated with a vertical bar 5. An expression followed by a Kleene star *

What is sentential form? (2)

Any string derivable from the start symbol

What is run time? (3)

Anything from program startup, object creation, procedure entry, block entry, expression evaluation, etc.

What is a semantic function? (4)

Arbitrarily complex functions defined by the language designer; example (sum)

Classify each of the following languages as to type: Haskell, Lisp, Prolog, C, C++, Perl, Ada, PHP, Python, Smalltalk, Eiffel, APL, Javascript. (1)

C++, Eiffel, Smalltalk, Python- Object Oriented Javascript, perl, PHP - Scripting C, Ada - von Neumann prolog - logical Haskell, Lisp, APL - Functional

Describe the phases of the compilation process. (1)

Char stream -> scanner (lexical analysis) -> token stream Token stream -> parser (syntax analysis) -> parse tree Parse tree -> Semantic analyzer (semantic analysis and intermediate code generation) -> AST or Other intermediate form AST or Intermediate Form -> Code Analyzer -> Improved intermediate form Improved intermediate form -> Code generator -> Assembly code Assembly code -> code optimizer -> improved code for target platform

What is semantic analysis? (1)

Checks syntactically correct statements to see if they are logically correct and work. (meaning)

What is the difference between conservative and optimistic compilers? (4)

Conservative optimization->only applies when optimization is guaranteed safe and efficient Optimistic compiler->always uses speculative optimization

What is a compile-time constant? (3)

Constants required to be declared and have known values at compile time

How does a top-down parser proceed through the source code? (2)

Constructs parse tree from the root down; predicts which production will be used to expand the current node, based on the next available token of input

Within the declarative and imperative families, there are several subclasses, which are usually used to classify languages. Name 5 such categories (1)

Declarative - Functional, Dataflow, Logic/Constraint-Based Languages Imperative - von Neumann, Object Oriented, and Scripting languages

Explain the difference between declarative and imperative languages. (1)

Declarative languages focus on what the computer should do. Imperative languages focus on how the computer should do it.

What is an attribute grammar? (4)

Grammar that provides a formal framework/grammar for decoration of a tree

What is an S-attributed grammar? (4)

Grammar where all attributes are synthesized

What is displacement addresssing? (3)

Implicitly providing a predetermined offset to the value in the frame pointer as a part of ordinary load or store instructions

How does type coercion reduce the amount of function overloading necessary? What are the main arguments for and against it? (3)

Instead of overloading functions the compiler will automatically convert the value into the required type. This improves code reuse and reduces need for explicit casts, but it reduces the number of type-errors that can be caught at compile time

What is heap fragmentation? (3)

Internal fragmentation - Problem with heap-based allocation where there is unused space within an allocated block External Fragmentation - Problem with heap-based allocation where blocks are allocated & deallocated, leaving 'gaps' between remaining blocks

What are Scripting Languages? (1)

Languages that emphasize 'gluing together' components from some surrounding context

What are von Neumann languages? (1)

Languages that rely on statements that influence future computation by changing values in memory

What are Object Oriented Languages? (1)

Languages that work by creating objects where the instructions and data required to run the program are contained within a single object

What is an LL parser? (2)

Left to right, leftmost derivation parser. They are typically simpler and easier to understand and are top down/predictive

What is an LR parser? (2)

Left to right, rightmost derivation parser. They are typically a slightly larger class and are bottom-up.

What language feature prevents top-down parsing? How is this problem resolved (other than switching to bottom-up parsing)? (2)

Left-recursion and common prefixes Use disambiguating rule; mechanically remove

What is a buddy system? (3)

Mechanism for dynamic pool adjustment where block sizes are powers of two. When a block is split, one half is used to satisfy the request and the other half is placed on the kth free list

What is Backus-Naur Form (BNF)? (2)

Metalanguage used to formally describe many context-free grammars

Do automated tools for finding storage bugs weaken the argument for automatic garbage collection? Justify your answer. (3)

No, garbage collection ensures that unused memory will be deallocated eventually, tools for finding storage bugs are not guaranteed to be used.

What is unsafe optimization? (4)

Optimizations that may lead to incorrect code

What is speculative optimization? (4)

Optimizations that usually improve performance but have the chance of not

What is a bottom-up parser? (2)

Parser that is LR and constructs parse tree from leaves up

What is a shift-reduce parser? (2)

Parser that is bottom-up and follows the steps of the driver

What is a recursive-descent parser? (2)

Parser whose subroutines correspond to each nonterminal of the grammar. Subroutines recursively call on one another

What is scope? (3)

Portion of a program over which a binding is valid

What is a prologue/epilogue? (3)

Prologue - The code executed at the beginning of a subroutine Epilogue - The code executed at the end of a subroutine

What is Kleene closure? (2)

Repetition an arbitrary number of times

Describe the difference between a leftmost and rightmost derivation. What can we say about these derivations if the grammar is unambiguous? (2)

Rightmost -> replace rightmost nonterminal with righthand side of some production Leftmost -> above except left Grammar is unambiguous if left and right derivations produce the SAME parse tree

What is a production? (2)

Rules that make up a context-free grammar. Denoted with a nonterminal on the left side and terminal(s) on the right side

What are dynamic semantics? (1)

Semantic rules that can be checked at runtime

What are static semantics? (1)

Semantic rules that can be checked prior to execution

Some languages distinguish between upper and lower-case letters in identifiers (so dog and DOG are distinct in some languages and not others). What are the pros and cons of each? (2)

Sensitivity permits code convention & is easier to check Insensitivity adds flexibility and is forgiving of typos

What is a referencing environment? (3)

Set of all active bindings

What is a Fibonacci heap? (3)

Similar to buddy system, but sizes grow in Fibonacci sequence, which is slower than 2^k and results in lower fragmentation

Describe the steps necessary to run Java code. (1)

Source code -> Java Compiler -> Java bytecode ->bytecode interpreter OR bytecode -> JIT compiler ->machine language

Describe the steps necessary to translate C++ source code into a running program. Assume the compiler produces assembly code as an intermediate step. (1)

Source program fed to compiler Compiler creates assembly language Assembly language goes to assembler The assembler creates the machine language

What are keywords (reserved words)? (2)

Special strings that are in identifier form but reserved for special purposes

Explain the difference between subtype polymorphism and parametric polymorphism. Give an example of where each is used. Explain the difference between explicit and implicit parametric polymorphism. (3)

Subtype polymorphism - the code is designed to work with values of some specific type, and can be extended Parametric Polymorphism - takes type as a parameter; (explicit) appears in statically typed languages and implemented at compile time; (implicit) type parameters are incomplete

What is a nonterminal? (2)

Symbols on the left hand sides of the production (variables)

What is a terminal? (2)

Symbols that are to make up the string derived from the grammar (right side)

What is a metasymbol? (2)

Symbols that convey info about the symbols making up a production * ->

Describe the difference between inherited and synthesized attributes. (4)

Synthesized attributes-> attributes whose values are calculated on the lefthand side of a production Inherited attributes->attributes whose values is calculated on the righthand side of a production

What is a scanner(lexer)? (1)

Takes in char stream, breaks apart into tokens by removing whitespace/comments, and recording line column numbers for error messages

What is a parser? (1)

Takes in tokens and performs syntax analysis to create parse tree

What is a token? (2)

The building blocks of a language. Shortest strings w/ individual meaning

What is elaboration time? (3)

The point at which a declaration is first 'seen'

What is lexical analysis? (1)

The process of dividing program text into words or tokens.

What is syntax analysis? (2)

The process of splitting the stream of tokens into phrases, parsing them and recording the error if they are invalid

What is binding time? (3)

The time when a binding is created; in general, this is the time a decision is made

Define and explain the role of: preconditions, invariants, and postconditions. (4)

They cover specific checks more succinctly than assertions, basically structured assertions Invariant -> true at, before, and after subroutine call Precondition -> true before subroutine call Postcondition -> true after subroutine call

What do Dataflow languages do? (1)

They model computation as the flow of information among primitive functional nodes. Inherently parallel & concurrent

What do Logic/Constraint-Based Languages do? (1)

They view computation as an attempt to find data satisfying certain logical relationships using goal-directed search

What is compile time? (3)

Time when compiler maps high-level constructs to machine addresses

What is Language-design time? (3)

Time when decisions are made about Control structures, primitive data type, etc.

What is Language Implementation time? (3)

Time when decisions are made about underlying hardware types, coupling the program's idea of I/O to the OS's idea of files, etc.

What is Program Writing Time? (3)

Time when programmers choose algorithms, data structures, and names.

What is link time? (3)

Time when the communication between modules is handled by the linker

What is load time? (3)

Time when the final mapping between program offsets, virtual addresses, and physical addresses are being made

What is the role of the semantic analyzer? (4)

To enforce all static semantic rules and annotate the program with information needed by the intermediate code generator

What is a parse tree? (1)

Tree generated by parse showing the structure of the program.

What is a syntax tree? (1)

Tree generated by semantic analyzer that shows the abstract syntactic structure of text written in a formal language

What do functional languages do? (1)

Use a computational model built on recursive definition of functions

What is an elaboration-time constant? (3)

Variables that can't be changed after elaboration time

What is deep binding and how is it implemented? (3)

When the referencing environment is in effect when a subroutine is passed as a parameter

How is precedence of operators established in a grammar, and how does it manifest in the parse tree? (2)

You have to set separate procedural rules in the grammar for each operation and order them such that the first operations to execute are lower in the parse tree.

What is inheritance? (3)

allows new classes to be defined as extensions or refinements of existing classes

Describe the difference between best-fit and first-fit heap allocation and the pros and cons of each. (3)

best-fit->search entire list; find smallest block large enough; efficient at reserving large blocks but slow performance first-fit->find first block large enough; faster performance but poor space conservation both will split sub-block in half if it is significantly smaller than request

Given the code int n = 5; identify 5 different binding times that occur before program execution. (There are more than that, so if you come up with more, that's OK.) (3)

binding times :-language design time -language implementation time -program writing time -compile time -link-time (for separate compilation) -load time -runtime decisions: -static binding before runtime -dynamic binding after runtime

Why do compilers produce more efficient code than interpreters? (3)

compilers make decisions earlier than interpreters and can thus take advantage of more optimizations

Explain the difference between a declaration and a definition. (3)

declaration-> introduces name & scope but may omit implementation details definition-> describes object in enough detail for compiler to determine its implementation

What is the primary argument in favor of dynamic scoping? (3)

easy for an interpreter to lookup meaning of name using only a stack of declarations

What are some pros and cons of using macro expansion? (3)

pro: cut down on repetitive code via textual substitution; hygienic macros encapsulate their arguments cons: naming & binding mechanisms apart from the rest of the language

Name the 8 principal categories of language mechanisms to specify control flow. (6)

sequencing->order statements are executed selection->choice at runtime iteration->repetition procedural abstraction -> subroutines recursion->expression defined in terms of itself concurrency->two or more program fragments executed at once exception handling/speculation->ability to branch to a handler if expected conditions are false nondeterminacy-> ordering/choice unspecified but will lead to correct results

Describe the difference between static and dynamic links in the context of resolving scopes. (3)

static link->a frame pointer that points to parent frame; keeps track of surrounding scopes dynamic link->reference to the stack frame of the caller

Distinguish between static, stack, and heap objects. (3)

static object -> has absolute address retained throughout program execution stack object->allocated/deallocated in LIFO in conjunction w/ subroutine calls & returns heap object->alloc/dealloc at arbitrary times; a more expensive storage management algorithm is required

Explain the difference between static & dynamic semantics. (4)

static semantic-> compiler enforces rules at compile time dynamic semantic->compiler generates code to enforce dynamic semantics at runtime

Sometimes parsers will have numbers after them; a parser may be described as LL(1), LR(2), etc. The number refers to (2)

the number of tokens the parser looks ahead


Set pelajaran terkait

Intro to Supply Chain Mgmt Chapter 5 Rutgers

View Set

Animals - circulatory system long questions

View Set

PrepU Chapter 3: Values, Ethics, and Legal Issues

View Set