450 exam 2 Flashcards

1
Q

static semantics vs dynamic semantics

A

static: syntax
dynamic: meaning

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

attribute grammar is a ? grammar with additions

A

context-free

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what are the three additions to context-free grammars that make an attribute grammar? (“need to understand but not write all down”)

A
  1. each grammar symbol has a set of attributes
  2. each rule has function(s) that define certain attributes of the nonterminals in the rule
  3. each rule has predicate(s) to check for attribute consistency
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

fully attributed parse tree

A

if all the attribute values in a parse tree have been computed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

three approaches to describe dynamic semantics

A

operational, denotational, axiomatic

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

dynamic semantics: operational vs denotational vs axiomatic

A

operational: describe meaning by executing its statements on a machine
denotational: meaning defined by the values of the program’s variables
axiomatic: based on mathematical logic (predicate calculus); axioms (inference rules) defined for each statement type in the language

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

example of operational semantics

A

JUnit Testing

@Test
public void testBinarySearch() {
// do stuff
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

example of denotational semantics

A

grammar rules with a semantic function (? - review this)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

example of axiomatic semantics

A

{x > 5} x = x - 3 {x > 0}

precondition, statement, postcondition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

assertions

A

the logic expressions in axiomatic expressions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

precondition

A

an assertion before a statement that states the relationships and constraints among variables that are true at that point in execution

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

weakest precondition

A

the least restrictive precondition that will guarantee the postcondition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

postcondition

A

an assertion following a statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what is the weakest precondition for x = x- 3 {x > 0} ?

A

{x > 3}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what is the weakest precondition for a = b + 1 {a > 1} ?

A

{b > 0}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

the syntax analysis portion of a language nearly always consists of two parts

A

lexical analysis and syntax analysis

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

lexical analyzer

A

converts characters in the source program into lexical units (lexemes and tokens)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

lexeme

A

the lowest level syntactic unit (such as =)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

token

A

collection of lexemes (such as ASSIGN_OP)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

syntax analyzer (or parser)

A

analyzes the syntactical structure of the given input and checks if the input is in the correct syntax of the language

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

two categories of parsers

A

top-down and bottom-up

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

top-down parser

A

produce the parse tree, beginning at the root (downward to the leaves) – order is that of a leftmost derivation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

bottom-up parser

A

produce the parse tree, beginning at the leaves (upward to the root) – order is that of the REVERSE of a rightmost derivation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

two most common top-down parsing algorithms (LL algorithms)

A

recursive-descent parser and LL parser

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

what does LL stand for in LL algorithms?

A

Left-to-right, Leftmost

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

recursive-descent parser

A

there is a subprogram for each nonterminal in the grammar, which can parse sentences that can be generated by that nonterminal; a coded implementation based on BNF or EBNF

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

recursive-descent parser coding process

A

for each terminal symbol in the RHS, compare it with the next input token
if they match, continue; else, there is an error

for each nonterminal symbol in the RHS, call its associated parsing subprogram

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

LL parser

A

table- (or stack-) driven implementation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

LL parser procedure

A

in each step, the parser reads the next available symbol from the input stream and the top-most symbol from the stack (application of a grammar rule)

if the input symbol and the stack-top symbol match, the parser discards them both, leaving only the unmatched symbols in the input stream and on the stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

purpose of the pairwise disjointness test

A

indicates whether the parser can always choose the correct RHS on the basis of the next token of input

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

pairwise disjointness test

A

for each nonterminal A in the grammar that has more than one RHS, for each pair of rules A -> ai and A -> aj, it must be true that FIRST(ai) ∩ FIRST(aj) = empty set

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

pairwise disjointness test on

A -> a | bB | cAb

A
FIRST(a) = {a}
FIRST(bB) = {b}
FIRST(cAb) = {c}

pass

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

pairwise disjointness test on

A -> a | aB

A
FIRST(a) = {a}
FIRST(aB) = {a}

fail

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

bottom-up parser (LR algorithm)

A

reads input text from left to right and produces a rightmost derivation in reverse (starts with the input and attempts to rewrite it to the start symbol)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

two most common actions for bottom-up parsers

A

shift and reduce

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

shift

A

advances in the input stream by one symbol; that shifted symbol becomes a new node in the parse tree

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

reduce

A

applies a grammar rule to some of the recent parse trees, joining them together as one tree with a new root symbol

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

shift-reduce algorithm

A

if the input has no syntax errors, the parser continues to shift and reduce until all the input has been consumed and all of the parse trees have been reduced to a single tree representing an entire legal (valid) input

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

l-value of a variable

A

address

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

r-value of a variable

A

contents

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

what does x represent in the following statement?

x = 5;

A

the l-value or address of the variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

what does x represent in the following statement?

y = x + z;

A

the r-value or content of the variable y

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

a binding is static if

A

it first occurs before run time and remains unchanged throughout the program execution

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

static binding is also called

A

early binding

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

dynamic binding is also called

A

late binding

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

a binding is dynamic if

A

it first occurs during execution or can change during the execution of the program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

static type binding: explicit declaration definition + example

A

a statement in a program that lists variable names and specifies that they are a particular data type

example: Java, C++, etc. saying int a = 5;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

static type binding: implicit declaration definition

A

a means of associating variables with types through context (type inference) or default conventions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

Example of type inference (static type binding - implicit declaration)

A

In C#, a variable can be declared with var and an initial value, which sets the type of the variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

Example of default conventions (static type binding - implicit declaration)

A

In Perl, any name that begins with $ is a scalar, a name that begins with @ is an array, so $apple and @apple are variables with different types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

for statically typed variables, their types are

A

fixed for the lifetime of the unit in which they are declared (that is, you cannot change the data type)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

dynamic type binding: any variable can be assigned any type value, and a variable’s type (can/cannot) change during execution

A

can

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

example of dynamic type binding (language such as Python, Ruby, PHP, etc. use it)

A

in Python:
a = 17
a = ‘hi’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

lifetime of a variable

A

the time during which it is bound to a particular memory cell (how long it exists in memory)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

allocation

A

getting a memory cell from some pool of available cells

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

deallocation

A

putting a memory cell back into the pool

57
Q

four categories of variables by lifetime

A

static, stack-dynamic, explicit heap-dynamic, implicit heap-dynamic

58
Q

static variables + example

A

variables before the execution begins and stay available through the entire run (declared with the keyword static)

e.g., static variables in Java and C++

59
Q

stack-dynamic variables + example

A

Come into existence when you call a method (or function). Their existence is temporary. They are either in the parameter list or declared inside the method. They disappear when the method ends.

60
Q

explicit heap-dynamic variables + example

A

allocated and deallocated by explicit instructions specified by the programmer

e.g., Objects in Java and C++ (created via new), referenced through pointers or references (new is an explicit instruction used to create the variable)

(come into being via the new operator, and sometimes need to be explicitly deallocated by the delete operator)

61
Q

implicit heap-dynamic variables + example

A

bound to heap storage only when they are assigned values

e.g., list = [74, 84, 86, 90, 71], whether it was previously used, it is now a list of 5 numeric values (Python)

62
Q

scope of a variable

A

the range of statements over which it is visible

63
Q

local variables of a program unit

A

those that are declared in that unit

64
Q

nonlocal variables of a program unit

A

those that are visible in the unit but not declared there

65
Q

global variables

A

a special category (subset) of nonlocal variables

66
Q

static scoping (also called lexical scoping)

A

a variable always refers to its top level environment (static parent) – a property of the program text, not the runtime stack

67
Q

(dynamic/static) scoping is used by most of the major programming languages

A

static

68
Q

dynamic scoping

A

based on calling sequences of program units, not their textual layout; parent is who calls it

69
Q

how does the search process for dynamic scoping work?

A

compiler first searches locally then successively all the calling functions

70
Q

dynamic scoping results in (less/more) reliable programs than static scoping

A

less

71
Q

referencing environment of a statement

A

the collection of all names that are visible in the statement

72
Q

what is the referencing environment in a static-scoped language?

A

the local variables plus all of the visible variables in all of the enclosing scopes

73
Q

what is the referencing environment in a dynamic-scoped language?

A

the local variables plus all visible variables in all active subprograms

74
Q

primitive data types

A

those that are not defined in terms of other data types; provided by almost all programming languages

75
Q

ordinal type

A

one in which the range of possible values can be easily associated with a set of integers

76
Q

examples of primitive ordinal types in Java

A

integer, char, boolean

77
Q

boolean in Java versus C/C++

A

In Java, 1 is true and 0 is false

In C/C++, any nonzero is true and zero is false

78
Q

enumeration type

A

one in which all of the possible values, which are named constants, are provided (or enumerated) in the definition

79
Q

Java example of enumeration type for Days

A

enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}

Day workday = Day.WED;

80
Q

array

A

a homogeneous aggregate of data elements in which an individual element is identified by its position in the aggregate, relative to the first element

81
Q

static array

A

subscript ranges are statically bound and storage allocation is static (before run-time)

82
Q

advantage to static arrays

A

efficiency

83
Q

four categories of arrays

A

static, fixed stack-dynamic, fixed heap-dynamic, heap-dynamic

84
Q

fixed stack-dynamic array

A

subscript ranges are statically bound, but the allocation is done during execution

85
Q

advantage to fixed stack-dynamic arrays

A

space efficiency (just allocate enough memory space)

86
Q

fixed heap-dynamic array

A

similar to fixed stack-dynamic, storage binding is done when requested, but storage is allocated from heap, not stack

87
Q

heap-dynamic array

A

binding of subscript ranges and storage allocation is dynamic and can change any number of times

88
Q

advantage to heap-dynamic arrays

A

flexibility (arrays can grow or shrink at any time during program execution)

89
Q

arrays that include static modifier are

A

static

90
Q

arrays created without static modifier are

A

fixed stack-dynamic

91
Q

fixed heap-dynamic arrays use ? to manage heap storage;; an array is treated as ?

A

new and delete; a pointer to a collection of storage cells

92
Q

in Java, all non-generic arrays are

A

fixed heap-dynamic

93
Q

Perl, JavaScript, Python, and Ruby support

A

heap-dynamic arrays

94
Q

static array example (C++)

A

static int nums[10] = {1, 3, 5, 7};

95
Q

fixed stack-dynamic array example (C++)

A

int nums[] = {1, 3, 5, 7};

96
Q

fixed heap-dynamic array example (C++)

A

int *nums = new int[10];

delete[] nums;

97
Q

heap-dynamic array example (Python)

A
myList = [1, 3, 5]
myList = [2, 4, 6, 8, 10]
98
Q

slice

A

some substructure of an array

99
Q

Python slice example
vector = [2, 4, 6, 8, 10, 12, 14, 16]
what does vector[3:6] return?

A

[8, 10, 12]

gets 3-element array from index 3 (inclusive) to 6 (exclusive)

100
Q

Python 2D slice example
mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
what does mat[0][0:2] return?

A

[1, 2]

gets the first and second element of the first row of mat

101
Q

Ruby slice example
list = [2, 4, 6, 8, 10]
what does list.slice(2, 2) return?

A

[6, 8]

start at index 2, get 2 elements; returns the third and fourth elements of list

102
Q

associative array

A

an unordered collection of data elements that are indexed by an equal number of values called keys

103
Q

associative arrays are also called ? and they contain ?

A

dictionaries, maps, or hashes; key-value pairs

104
Q

record

A

a heterogeneous aggregate of data elements in which the individual elements are identified by names

105
Q

tuple

A

a data type that is similar to a record, except that the elements are not named

106
Q

Python’s tuples are closely related to its lists, but they are

A

immutable (cannot change anything in the tuple); uses parentheses instead of square brackets

107
Q

list

A

also serves as Python’s arrays, mutable, elements can be of any type

108
Q

[x ** 2 for x in range(6) if x % 3 == 0]

A

[0, 9]

range is [0, 5], only 0%3 and 3%3 = 0, so 0^2 and 3^2

109
Q

[y * 3 for y in range(1, 20, 3) if y % 4 == 0]

A

[12, 48]

range [1, 19] with step value 3 yields 1, 4, 7, 10, 13, 16, 19
4, 16 are the only ones where mod 4 = 0
4 * 3 and 16 * 3

110
Q

[y * 3 for y in range (20, 1, -3) if y % 4 == 0]

A

[60, 24]

range [20, 2] with step -3 yields 20, 17, 14, 11, 8, 5, 2
20 and 8 are the only ones where mod 4 = 0
multiply by 3

111
Q

union

A

a type whose variables are allowed to store different type values at different times during execution

112
Q

free union

A

no language support for type checking; unsafe

113
Q

discriminated union

A

support type checking

114
Q

why do Java and C# not support unions? what do they use?

A

growing concerns for safety in programming languages; use classes instead

115
Q

pointer

A

a variable that stores the memory address of another value located in the memory

116
Q

reference types

A

references are references to objects; Java uses reference variables and allows them to replace pointers entirely (b/c pointers are not safe)

117
Q

two fundamental pointer operations

A

assignment and dereferencing

118
Q

assignment

A

used to set a pointer variable’s value to some useful memory address

119
Q

address-of-operator

A

&

120
Q

dereference operator

A

*

121
Q

dereferencing

A

yields the value stored at the location represented by the pointer’s value

122
Q

problems with pointers

A

dangling pointers and lost heap-dynamic variables (and hence memory leakage)

123
Q

solution to dangling pointer problem

A

two algorithms: tombstone and locks-and-keys

124
Q

tombstone algorithm

A

every heap-dynamic variable includes an extra cell, called a tombstone, that the pointer variable points at; tombstone is a marker for “variable is no longer here or dead”

125
Q

locks-and-keys algorithm

A

represent pointers as ordered pairs (key, address) where the key is an integer value; every access to the pointer compares the lock value in the variable’s cell and the key value in the pointer’s cell

126
Q

solution to heap management problem

A

two approaches (algorithms) to reclaim garbage: reference counters (eager) and mark-sweep (lazy)

127
Q

reference counter algorithm

A

this algorithm maintains a counter that stores the number of pointers currently pointing at the cell; if the reference counter reaches zero, it has become garbage

128
Q

mark-sweep algorithm

A

mark phase: mark all unreachable cells (as garbage)

sweep phase: reclaim the heap space used by garbage cells and make the space available to the program again

129
Q

garbage

A

an allocated heap-dynamic variable that is no longer accessible to the user program

130
Q

memory leakage

A

the process of losing heap-dynamic variables

131
Q

coercion

A

automatic implicit conversion to a legal type

132
Q

a programming language is strongly typed if ?

A

type errors are always detected

133
Q

a programming language is weakly (loosely) typed if ?

A

type errors are not always detected

134
Q

type equivalence

A

determining whether two types are considered the same; two standard ways: name type equivalence and structure type equivalence

135
Q

name type equivalence

A

two types are equal if and only if they have the same name (e.g., both integers, both Puppy objects)

136
Q

(name/structure) type equivalence is easier to implement and used more frequently by Java

A

name

137
Q

structure type equivalence

A

two types are equal if and only if they have the same “structure”

138
Q

do most languages have a mixture of name/structure type equivalence? if so, give an example

A

yes; e.g., C++ uses structure equivalence except for structs and classes (where name equivalence is used)

139
Q

which type of variable is only found in a dynamic type binding language?

A

implicit heap-dynamic variables