Ch3&5 Flashcards

(56 cards)

1
Q

Define syntax and semantics.

A

Syntax: Form of its expressions, statements, and program units.
Semantics: Meaning of these expressions, statements, and program units.

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

What is the difference between a sentence and a sentential form?

A

Sentence: Strings of symbols belonging to a language; a sentential form containing only terminal symbols.
Sentential Form: Any string of symbols that appear in a derivation. May contain non-terminal symbols.

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

Define a left-recursive grammar rule.

A

A grammar is left-recursive if some non-terminal A will eventually derive a sentential form with itself (A) as the left-most symbol.

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

What three extensions are common to most EBNFs?

A

Optional parts: [ ]
Repetitions: { }
Alternatives: |

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

Distinguish between static and dynamic semantics.

A

Static semantics: Can be determined at compile time (e.g., type checking).
Dynamic semantics: Can only be determined at run time (the program’s behavior during execution).

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

What purpose do predicates serve in an attribute grammar?

A

Specify conditions that must be true for a rule to be valid (static semantic constraints).

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

What is the difference between a synthesized and an inherited attribute?

A

Synthesized attribute: Its value is computed from its children in the parse tree (information flows up).
Inherited attribute: Its value is passed down from its parent or siblings (information flows down).

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

How is the order of evaluation of attributes determined for the trees of a given attribute grammar?

A

By the dependencies between attributes. An attribute is evaluated after its dependencies are evaluated.

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

What is the primary use of attribute grammars?

A

To define the static semantics of programming languages.

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

Explain the primary uses of a methodology and notation for describing the semantics of programming languages.

A

Programmer understanding.
Compiler construction.
Program verification.
Language design.

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

Why can machine languages not be used to define statements in operational semantics?

A

They are too low-level and machine-dependent. Operational semantics aims for abstraction.

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

Describe the two levels of uses of operational semantics.

A

Natural operational semantics: Describes the overall effect of program execution.
Structural operational semantics: Describes the step-by-step execution.

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

In denotational semantics, what are the syntactic and semantic domains?

A

Syntactic domains: Sets of syntactic objects (programs, statements).
Semantic domains: Sets of meanings (numbers, functions).

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

What is stored in the state of a program for denotational semantics?

A

The values of variables and other program data.

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

Which semantics approach is most widely known?

A

Operational semantics.

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

What two things must be defined for each language entity in order to construct a denotational description of the language?

A

A semantic domain.
A semantic function.

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

Which part of an inference rule is the antecedent?

A

The part above the horizontal line (the premises).

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

What is a predicate transformer function?

A

Maps a postcondition to a precondition.

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

What does partial correctness mean for a loop construct?

A

If the loop terminates, the postcondition will be true.

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

On what branch of mathematics is axiomatic semantics based?

A

Mathematical logic (predicate calculus).

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

On what branch of mathematics is denotational semantics based?

A

Domain theory.

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

What is the problem with using a software pure interpreter for operational semantics?

A

They can be complex, slow, and may not be sufficiently abstract.

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

Explain what the preconditions and postconditions of a given statement mean in axiomatic semantics.

A

Precondition: What must be true before execution.
Postcondition: What will be true after execution.

24
Q

Describe the approach of using axiomatic semantics to prove the correctness of a given program

A

Derive preconditions from postconditions using inference rules. If the program’s initial state matches the derived precondition, it’s correct.

25
Describe the basic concept of denotational semantics.
Maps syntactic components to mathematical objects (denotations).
26
In what fundamental way do operational semantics and denotational semantics differ?
Operational: Describes execution. Denotational: Maps to mathematical meanings.
27
Who are language descriptions for?
Programmers Compiler writers Language designers Researchers
28
Describe the operation of a general language generator.
Takes a grammar as input. Produces valid sentences (programs) that adhere to that grammar. Starts with a start symbol and applies grammar rules to derive strings.
29
Describe the operation of a general language recognizer.
Takes a grammar and a string as input. Determines if the string is a valid sentence in the language defined by the grammar (parses the string).
30
The two mathematical models of language descriptions are generation and recognition. Describe how each can define the syntax of a programming language.
Generation: is a device that generates sentences of a language. Recognition: is able to read a sentence/string from a given alphabet.
31
What are the design issues for names?
Case sensitive, and; Reserved words or keywords.
32
What is the potential danger of case-sensitive names?
Reduced readability: Names that differ only in case can be easily confused. Portability issues: Code that works on one platform may not work on another if the platforms handle case sensitivity differently.
33
In what way are reserved words better than keywords?
Reserved words: Cannot be used as identifiers, preventing ambiguity. Keywords: Can be used as identifiers if context allows, which can lead to confusion. Therefore reserved words provide greater language clarity.
34
What is an alias?
An alias is when two or more names refer to the same memory location.
35
Which category of C++ reference variables is always aliases?
C++ reference variables passed as parameters to functions are always aliases.
36
What is the l-value of a variable? What is the r-value?
l-value: The address of a variable (its location in memory). r-value: The value stored in the variable's memory location.
37
Define binding and binding time.
Binding: An association between an attribute and an entity (e.g., a variable and its type, a variable and its value). Binding time: The time at which a binding takes place.
38
After language design and implementation [what are the four times bindings can take place in a program?]
Compile time: Bindings made by the compiler (e.g., type bindings in static languages). Load time: Bindings made by the loader (e.g., linking external libraries). Link time: Binding of external subprograms. Run time: Bindings made during program execution (e.g., dynamic type binding).
39
Define static binding and dynamic binding.
Static binding: Occurs before run time and remains unchanged throughout program execution. Dynamic binding: Occurs 1 during run time and can change during program execution.
40
What are the advantages and disadvantages of implicit declarations?
Advantages: Convenience, shorter code. Disadvantages: Reduced readability, increased risk of errors (e.g., misspelled variable names).
41
What are the advantages and disadvantages of dynamic type binding?
Advantages: Flexibility, generic programming. Disadvantages: Reduced reliability, increased run-time overhead.
42
Define static, stack-dynamic, explicit heap-dynamic, and implicit heap-dynamic variables. What are their advantages and disadvantages?
Static variables: Bound to memory locations before run time, remain bound throughout execution. Advantages: Efficiency. Disadvantages: Lack of flexibility. Stack-dynamic variables: Bound to memory locations when their declaration statements are elaborated (executed). Advantages: Flexibility (e.g., local variables in subprograms). Disadvantages: Run-time overhead of allocation and deallocation. Explicit heap-dynamic variables: Allocated and deallocated by explicit instructions (e.g., new and delete in C++). Advantages: Flexibility, dynamic data structures. Disadvantages: Programmer responsibility (memory leaks, dangling pointers). Implicit heap-dynamic variables: Bound to heap storage when they are assigned values. Advantages: very high flexibility. Disadvantages: Highest amount of run time overhead.
43
Define lifetime, scope, static scope, and dynamic scope.
Lifetime: The time during which a variable is bound to a memory location. Scope: The region of a program within which a variable is visible. Static scope: The scope of a variable is determined by its position in the source code. Dynamic scope: The scope of a variable is determined by the calling sequence of subprograms.
44
How is a reference to a nonlocal variable in a static-scoped program connected to its definition?
The compiler searches enclosing scopes, starting with the innermost scope, until the variable's declaration is found.
45
What is the general problem with static scoping?
It can lead to unexpected behavior if a variable's declaration is hidden by another declaration in an inner scope.
46
What is the referencing environment of a statement?
The collection of all names that are visible at that statement.
47
What is a static ancestor of a subprogram? What is a dynamic ancestor of a subprogram?
Static ancestor: An enclosing subprogram in the static scope sense. Dynamic ancestor: A calling subprogram in the dynamic scope sense.
48
What is a block?
A section of code with its own local variables and scope. A section of code with its own local variables and scope.
49
What is the purpose of the let constructs in functional languages?
To introduce local names and scopes.
50
What is the difference between the names defined in an ML let construct from the variables declared in a C block?
ML let constructs define immutable names (bindings cannot be changed), while C block variables are mutable.
51
Describe the encapsulation of an F# let inside a function and outside all functions.
Inside a function: the let creates a local binding, only usable inside of that function. Outside all functions: The let creates a binding that is available to all code within that file.
52
What are the advantages and disadvantages of dynamic scoping?
Advantages: Flexibility, code reuse. Disadvantages: Reduced readability, increased risk of errors, difficult to reason about.
53
What are the advantages of named constants?
Improved readability, increased reliability, easier maintenance.
54
Some programming languages are typeless. What are the obvious advantages and disadvantages of having no types in a language?
A programming language without types would likely exhibit characteristics similar to languages with dynamic type binding. Advantages: Increased programming flexibility: Programs can be written generically to handle data of any type, as variables can be bound to different types at different times. Simpler syntax (potentially): The need for explicit type declarations is eliminated. Disadvantages: Reduced reliability: The compiler cannot detect type errors, leading to potential run-time errors that might be difficult to diagnose. Increased run-time overhead: Type checking must be performed at run time, and each variable might need a descriptor to maintain its current type. Storage might also need to be of varying size.
55
Dynamic type binding is closely related to implicit heap-dynamic variables. Explain this relationship.
The relationship lies in the fact that with dynamic type binding, the storage requirements of a variable can change as its type changes. The heap is a region of memory that allows for dynamic allocation and deallocation of storage at run time. Therefore, when a variable's type is dynamically bound, the system often needs to allocate or reallocate storage on the heap to accommodate the value of the new type being assigned.
56
Describe a situation when a history-sensitive variable in a subprogram is useful.
A history-sensitive variable in a subprogram is one whose value is retained between different calls to the subprogram. This is useful in situations where a subprogram needs to maintain state across invocations.