CS 326 - Review Exam 1

Ace your homework & exams now with Quizwiz!

Managing References Leads to the Dangling Pointer Problem What is a Tombstone Solution?

-Every heap-dynamic variable contains a special pointer (tombstone) ( i.e. a special marker when memory is allocated) -Pointers point to the tombstone, not the variable -When a variable is de-allocated, the tombstone is set to null -Costs extra memory and a level of indirection - i.e. just like when we allocate a pointer we have to deallocate when we are done with the pointer to avoid dangling pointers. Note: In which is is a way to deallocate and set to null (aka Tombstone) this is so the computer knows not to use it to prevent errors.

Managing References leads to the Dangling Pointer Problem What is a Lock and Keys Solution?

-Pointers are ordered pairs (key, address) -Variables contain a "lock," which is a value indicating that the memory is legal -If a pointer has the same lock, it's a match and legal. On dispose, null. -Again, this checking is not free ie. when you allocate memory (ie. create a variable) you also get a unique key for that piece of memory. Every time you access/use that memory you provide the key, if the key is still valid the program allows you to access the memory. Once it is deallocated or "freed" the 'key" is now deactivated/marked invalid. So the program knows not to use it.

Robert Sebesta defines a series of categories, for example, which are? List at least 3 and define them ♣

-Simplicity : overall it affects its readability. A language with small number of basic constructs is easier to learn than a larger one. **--reference --** feature multiplicity -- where there is more than one way to accomplish an operation. (ie. count = count + 1, count += 1, count++, ++count) **--reference --** operator overloading -- where a single operator symbol has more than one meaning. -Orthogonality : This concept refers to symmetry in language design. -Data Types : defines a set of values and the operations that can be performed on them. -Syntax Design: is elements of a language that has a significant effect on the readability of programs **--reference --** special/reserved words (while, class, for etc) **--reference --**Form and meaning (designing statements so that their appearance at least partially indicates their purpose). -Support for Abstraction: refers to the ability of a system or language to hide complexities/details allowing users to focus on higher-level concepts. -Expressivity: means that a language can a relatively convenient way to specify computations. (i.e. count++, count = count + 1) **--reference --** Type Checking - simply testing for type errors in a given program, by the compiler or during execution. -Exception Handling: The ability of a program to intercept runtime errors, take corrective measures, and continue. -Restricted Aliasing: Aliasing is defined as having two or more distinct names in a program that can be used to access the same memory cell. Restricting aliasing generally increases the reliability of the language. Acronym to memorize: SODS SEER

Define the following -Static Scoping -Dynamic Scoping -Global Scoping

-Static Scoping - the scope of a variable is determined at compile time based on the structure of the code -Dynamic Scoping - the scope of a variable is determined at runtime based on the calling sequence of the functions -Global Scoping - refers to variables that are declared outside any function, block, or module and are accessible from any part of the code after their declaration, barring any restrictions placed by the language or modules

What are the 3 reasons why lexical analysis is separated from syntax analysis? REF CH4

-simplicity -efficiency -portability

Draw a parse tree to get to the below string with the given below grammar Consider the following Grammar: <assign> → <id> = <expr> <id> → A | B | C <expr> → <id> + <expr> | <id> * <expr> | ( <expr>) | <id> And the String: A = B * ( A + C )

<assign> / | \ <id> = <expr> | | / | \ A = <id> * <expr> | | | A = B * (<expr>) | | | | / | \ A = B * (<id> + <expr>) | | | | ( | | | ) A = B * ( A + C )

Create a parse tree to get to the below string <assign> → <id> = <expr> <id> → A | B | C <expr> → <id> + <expr> | <id> * <expr> | ( <expr>) | <id> A = B x C + (A x C)

<assign> / | \ <id> = <expr> | | / | \ A = <id> * <expr> | | | | / | \ A = B * <id> + <expr> | | | | | | / | \ A = B * C + ( <expr> ) | | | | | | / | \ A = B * C + (<id> * <expr>) | | | | | | | | | A = B * C + (A * <id> ) | | | | | | | | | A = B * C + (A * C)

Create a parse tree with the following possible string output <factor> → <expr> ** <factor> | <expr> <expr> → ( <factor> ) | <id> <id> → A | B | C A ** B

<factor> / | \ <expr> ** <factor> | | | <id> ** <expr> | | | A ** <id> | B

Create a grammar with the following possible string output A ** B

<factor> → <expr> ** <factor> | <expr> <expr> → ( <factor> ) | <id> <id> → A | B | C

Note: Terminals are in bold. Non-terminals are not. Example Derviation: <program> → begin <stmt_list> end <stmt_list> → <stmt> | <stmt> ; <stmt_list> <stmt> →<var>=<expression> <var>→ A | B | C <expression> → <var> + <var> | <var> - <var> | <var> List the program from top to bottom of its possible execution.

<program> → begin <stmt_list> end → begin <stmt> ; <stmt_list> end → begin A = <var> = <expression> ; <stmt_list> end → begin A = <var> + <var> ; <stmt_list> end → begin A = B + <var> ; <stmt_list> end → begin A = B + C ; <stmt_list> end → begin A = B + C ; <var> = <expression> end → begin A = B + C ; B= <expression> end → begin A = B + C ; B = <var> end → begin A = B + C ; B = C end

Derive the below string: begin A = C - B end

<program> ->begin <stmt_list> end <stmt_list> -> <stmt> | <stmt>; <stmt_list> <stmt> -> <var> = <expression> <var> = A | B | C <expression> -> <var> + <var> | <var> - <var> | <var>

What is a axiom?

A axiom is a logical statement that is assumed to be true. Therefore, an axiom is an inference rule without an antecedent.

What is a control structure? ref ch 8

A control structure is a control statement and the collection of statements whose execution it controls.

Can Grammars be ambiguous? Explain.

A grammar is said to be ambiguous if, for the same input string, two or more parse trees can be generated

What is a preprocessor? ref ch 1

A preprocessor is a program that processes a program just before the program is compiled. (ie. #include myheader.h)

What is a reference type? ref ch 6

A reference type variable is similar to a pointer with one important and fundamental difference: A pointer refers to an address in memory, while a reference refers to an object or value in memory. ie. so don't do arithmetic on references. OK on addresses.

What is a token? ref ch 3

A token represents a lexeme group. So a token of a language is a category of its lexemes. ie index = 2 * count + 17; Lexemes | Tokens index______|__identifier = ___________|__ equal_sign 2 ___________|__ int_literal * ___________|__ mult_op count____|___ identifier +____________|__ plus_op 17__________|__ int_literal ; ___________|__ semicolon

What is an "L" value?

An L-Value is the address of the variable (it maybe used in other ways also)

What is the term "R" value?

An R-Value is the value of the variable

What are enumeration types?

An enumeration type is one in which all of the possible values, which are named constants, are provided, or enumerated in the definition. It provides a way of defining and grouping collections of named constants which are called enumeration constants. ie. enum days {mon, tue, weds, thurs, fri, sat, sun}; enum days {mon=1, tue=2, weds=3, thur=4, fri=5, sat=6, sun=7};

what is expected type? ref ch3

An inherited attribute associated with the nonterminal <expr>. it is used to store the type, either int, or real that is expected for the expression as determined by the type of variable on the left side of the assignment statement.

What is an iterative statement? ref ch 8

An iterative statement is one that causes a statement or collection of statements to be executed 0, 1, 2,...or more times. It is often called a loop.

What is an unconditional branch statement? ref ch 8

An unconditional branch statement transfers execution control to a specified location in the program.

What does Antlr use?

Anltr uses finite automata and context free grammars behind the scenes

What does Antlr NOT use?

Antlr does not use the "visitor" concept often seen in language processors. Instead it uses listeners.

What are Axiomatic Semantics? ref CH3

Axiomatic Semantics are based on mathematical logic, is the most abstract approach to semantics specification. Rather than directly specifying the meaning of a program, the axiomatic semantics specifies what can be proven about it.

How do we describe lists in BNF? Ref CH 3

BNF does not have ellipsis "..." so the alternative method for describing lists of syntactic elements in programming languages - For BNF it is recursion. A Rule is RECURSIVE if its LHS appears on its RHS ie <ident_list> -> identifier ________________ | identifier, <ident_list>

What is BNF and define it ref ch 3

Backus-Naur Form BNF is a natural notation for describing syntax.

List the general Binding Times. What is the binding times of the following statement? integer value = 10

Binding Times: -Compile Time -Language Design Time -Execution time integer value = 10 language design time integer is a data type definition compile time integer = 10 //type checking is done at compile time to make sure the right hand side of assignment is compatible with left hand side Memory allocation for the variable "value" is done during compile time execution time initializing the variable when the program is run, the actual memory reserved for value will be set to 10.

Suppose that long-living objects rarely need to be deallocated. How can we improve Mark and Sweep?

By focusing clean-up efforts mainly on new objects (which are often discarded quickly) and less on older, long-living objects (objects that survive multiple garbage collections), the system can manage memory more efficiently and quickly

What are the "options" when checking types? Define them.

Compatible - Legal for computation Implicit Conversion - Compiler can convert without informing the user Explicit Conversion - User can type cast explicitly to convert Type Error - Conversion of any kind is not allowed.

Consider the following grammar <ident_list> -> identifier | identifer, <ident_list> What is the first portion called? <ident list> -> What is the portion after the arrow called? <ident_list> -> identifier | identifer, <ident_list> What does it mean?

Consider the following grammar <ident_list> -> identifier | identifer, <ident_list> ^The Rule ^a symbol, ^like variable name or letter obviously the " | " between identifier and identifier, <ident_list> is just an OR Means a "list of identifiers" can be either a single identifier or an identifier followed by a "list of identifiers". Note: Grammars like these are naturally recursive, on purpose

This allows indefinite repetition <ident_list> →<identifier> {, <identifier> } Explain what exactly makes it allow for indefinite repetition?

Curly Braces { }: These often denote zero or more repetitions of the enclosed sequence. They indicate that the enclosed sequence can repeat any number of times, including zero times (i.e., not at all). With one identifier followed by a comma with another identifier then followed by another comma with another identifier etc. i.e. a a, b a, b, c a, b, c, .... etc.

What are primitive datatypes?

Datatypes that are not defined in terms of other types.

(CHAPTER 6 Material) Define: Primitives Enums Unions Pointers

Define: Primitives are basic data types. Enums allow for named integer constants. Unions let different data types share the same memory space. Pointers store the memory addresses of variables. MORE DETAIL: Primitives - are the basic data types provided by a programming language (i.e. INT, FLOAT, CHAR, BOOL, DOUBLE) Enums - are a user-defined data type in many languages that allow for defining a set of named integer constants. An enumerated type declares an optional type name and a set of zero or more identifiers that can be used as values of the type.The values of an enum are, by default, zero-based integers, but you can assign different integer values to them if desired. ie. enum Days { SUNDAY, // 0 MONDAY, // 1 TUESDAY, // 2 // ... and so on }; Days today = MONDAY; Unions - are a data structure that allows you to store different data types in the same memory location. They are similar to structures (struct in C and C++), but in a union, all members share the same memory location, meaning only one of its members can contain a value at any given time. This makes unions useful when you want to work with different types of data using the same memory. ie. union Data { int i; float f; char str[20]; }; //they share the same memory location but only one can be used at a time. Pointers - are variables that store the memory address of another variable. i.e. int x = 10; int *ptr = &x; // ptr is a pointer to an integer and holds the memory address of x

Block scoping "could" be Accomplished in Multiple Ways Should the code on the right be legal or not? integer a = 5 if a = 5 integer a = 10 //should this be legal or not? output a //should this be legal or not? end

Depends if the language allows shadowing, if it does then a = 10. If not it will be a compile error or output 5 depending on the language. ie. Python does not allow shadowing ie. C++/Rust allows shadowing Note: typically allocated on the stack

Automatic Deallocation of Memory Example Explain how this works Mark and Sweep basic algorithm for every_pointer p mark(p) void mark (void * p) if (p != 0) { if (p.marker is not marked) { set p.marker mark(p.left_link) mark(p.right_link) } } }

Different garbage collection algorithms work in different ways, but the common approach is Mark and Sweep Method. Mark: During this phase the GC identifies all the memory blocks that are still in use (ie. marking them in use) Sweep: After the "marking" phase, the GC goes through the heap, looking for the blocks that were not marked. These are the garbage blocks and they are safely deallocated (ie. sweep away). --Sweeping deallocates the non-marked nodes Pros: -Memory safety (reduce memory leak risk) -Developer convenience (dont need to free explicitly every memory block - easier to write and maintain) Cons: -Performance Overhead (GC introduces runtime overhead which can affect performance) -Non-determined Deallocation (deallocation is unpredictable) -Possible increased memory consumption (as it is not immediately deallocated after it becomes garbage) Summary: is that GC tracks and frees memory that is no longer in use, preventing memory leaks and simplifying a program at the cost of some performance overhead.

Dissect an Antlr Grammar (what is the below called) grammar Interpreter; (what is the below called) start : expression EOF ; expression : | INT | expression (PLUS | MINUS) expression ; (what is the below called) PLUS: '+'; MINUS: '-'; INT:'0'..'9'+;

Dissect an Antlr Grammar (what is the below called) ANS: NAME grammar Interpreter; (what is the below called) ANS: PARSER RULES start : expression EOF ; expression : | INT | expression (PLUS | MINUS) expression ; (what is the below called) ANS: LEXER RULES PLUS: '+'; MINUS: '-'; INT:'0'..'9'+;

When does "Dynamic Binding" happen and does the data change?

Dynamic Binding Happens -Some properties might be influenced at compile time, but --it can change at run time note: is saying that the language is flexible in one way or another at run time.

What is Dynamic Scoping? what is the value of sub2()? function big() { function sub1() { var x = 7; sub2(); } function sub2() { var y = x; var z = 3; } var x = 3; sub2(); }

Dynamic scoping, the calling sequence determines the value of the variable. The value of y in sub2 is no longer 3, necessarily, and it depends on where it is called from Notice that sub2 is called from two locations Value is now of y = 7

What is Garbage Collection?

Garbage collection is a term for reclaiming resources that are no longer in use (or appear to be no longer in use). After collection, they can be recycled and used for something else. Thereby preventing memory leaks and making memory available for future allocations. It's a form of automatic memory management.

What are "grammars" ?? ref ch3

Grammars - formal language generation mechanisms that are commonly used to describe syntax of a programming language.

List Categories of Programming Language

Imperative (e.g., C, Java) (von neumann architecture) Functional (e.g., Scheme, Lisp) Logic (e.g., Prolog) Object-oriented (e.g., Smalltalk, Java) Scripting languages (e.g., Ruby, JavaScript) Markup (e.g., HTML, CSS)

Are names case sensitive? ref CH 5

In many languages notably C-based languages, upper and lower case letters in names are distinct, so yes for these languages.

What is "garbage"?

In the context of memory management, "garbage" refers to memory blocks that were previously allocated but are no longer accessible or usable by the program. This can happen, for instance, if you have a pointer pointing to a block of memory and then the pointer gets reassigned to a different memory location or goes out of scope. Now, the original block of memory is inaccessible but still occupies space - it becomes "garbage".

What are intrinsic attributes? ref ch 3

Intrinsic attributes are synthesized attributes of leaf nodes whose values are determined outside the parse tree.

What is Strong/Weak Typing?

Is about whether a language is strict (strong) or flexible (weak) about mixing different data types without explicit conversion.

What are LL algorithms? ref ch 4

LL algorithms are equally powerful, as they work on the same subset of all context-free grammars. The first L in LL specifies the left to right scan of the input, the second L specifies that the left most derivation is generated.

Give an example of left associativity and right associativity. Which one is more common of the two? Which one uses recursion?

Left Associativity <expr> → <expr> + <term> | <term> Right Associativity <expr> → <term> + <expr> | <term> The more common one is Left associativity Both use recursion.

Define the term "Lexing"

Lexing, the process of breaking up symbols (text, in practice) into tokens, or categories of symbols (e.g., an identifier)

Define the term "Tokens"

Lexing, the process of breaking up symbols (text, in practice) into tokens, or categories of symbols (e.g., an identifier)

Lifetime and Scope are different things. Define them.

Lifetime - (refers to what variables are in memory) refers to the duration or period during which a variable exists in memory, from its creation to its destruction. Scope -(refers to what variables we can reference) refers to the regions or portions of the code where a variable can be accessed or referred to.

All algorithms used for syntax analyzers of commercial compilers have a complexity of? ref CH 4

O(n) which means the time they take is linearly related to the length of the string to be parsed.

Define Optimization

Optimization is the name given to the collection of techniques that compliers may use to decrease the size and/or increase the execution speed of the code they produce.

What are parse trees?

Parse trees - a hierarchical tree representation of a grammatical structure of a source code. They represent the syntactical structure of the program.

Complexity of Parsing ref ch4

Parsing for any unambiguous grammar are complicated and inefficient. In fact the complexity of such algorithms is O(n^3), which means the amount of time they take is on the order of the cube of length of the string to be parsed

Define Portability

Portability - the ease with which programs can be moved from one implementation to another.

Every parser for a programming language is a "______________", because it is a recognizer for context-free language. ref ch4

Pushdown automation (PDA)

List Language Evaluation Criteria ♣ Ref (CH1 pg 33 to 39)

Readability - Ease of which programs can be read and understood. Writability - Reliability -

What are the two classes of languages we use in computer science according to Noam Chomsky?

Regular Context-free grammars standard notation for grammars labeled today as BNF

Programming Domains ♣ (list 3 at least) Ref (CH1 pg 32-33 )

Scientific Applications Business Applications Artificial Intelligence (AI) Web Software

Define semantics ref ch 3

Semantics is the meaning of those expressions, statements, and program units.

This is an optional part of a rule <if_stmt> →if (<expression>) <statement> [else <statement>] Explain what exactly makes it optional?

Square Brackets [ ]: These denote an optional part of the rule. The enclosed sequence can appear either once or not at all. This is essentially a shorthand for indicating that there are two possible forms: one with the enclosed sequence and one without.

When does "Static Binding" happen and does the data change?

Static Binding Happens -set at compile time -Do not change during execution

What are Static Semantics? Ref CH3

Static Semantics of a language is only indirectly related to the meaning of programs during execution; rather it has to do with the legal forms of programs (syntax rather than semantics). -They are checked for specifications of type constraints at compile time.

List the Multiple Mechanisms for Binding Variables and define them

Static: Memory allocated before program begins Stack-Dynamic Variables - Variables allocated as program executes on the stack Heap Dynamic Variables : comes in 2 forms -Explicit - Memory allocated at runtime -Implicit - Storage in the heap that occurs during an assignment statement

Give me an examples of classifications in programming language design.

Statically Typed -determined at compile time integer name = 5 Dynamically Typed - determined at runtime name = 5 Type Inference - guessing at compile time combines benefits of static and dynamic typing var name = 5 or name = 5

List and define the data types ch 5 and slides

Statically typed & Dynamically typed Statically typed: can be an explicit declaration - statement in a program that lists variable names and specifies that they are a particular type. They can also be implicit declarations - a means of associating variables with types through default conventions, rather than declaration statements. Dynamically typed: the type of a variable is not specified by a declaration statement, nor can it be determined by the spelling of its name. instead, the variable is bound to a type when it is assigned a value in an assignment statement.

What are the differences between Synthesized attributes vs inherited attributes? Ref CH3

Synthesized attributes are used to pass semantic info up the parse tree. Inherited attributes pass semantic info down the and across the tree (think of a child inheriting their parents genetics)

C/C++ allow redefinition, but Java/C# do not. Discuss the pros and cons from the following perspectives: -Technical -Human Factors integer a = 5 if a = 5 integer a = 10 output a end

TECHNICAL FACTORS: PROS - Flexibility (ability to reuse names in nested loops without affecting outer scope.) PROS - Block Scoping (ie. locals in a loop or conditional, shadowing allows for temp variables that don't interfere with boarder scoped variables.) CONS - Ambiguity - harder to find bugs in larger code CONS - Optimization challenges as compilers may have a hard time with shadowed variables because of their ambiguous use HUMAN FACTORS: PROS - less need for unique names CONS - Confusion if they are not aware of out scope variables CONS - Error Prone - accidental shadowing can introduce hard to find bugs CONS - harder readability and maintenance

Define "lifetime" in relation to a variable. ref ch 5

The lifetime of a variable is the time during which the variable is bound to a specific memory location. the lifetime begins when it is bound to a specific cell and ends when its unbound from the cell.

Define Syntax ref ch 3

The syntax of a programming language is a form of its expressions, statements and program units.

Why do we use Antlr as opposed to just looking at BNF?

The tools help us check our assumptions about that flow (i.e. parse tree)

What is the weakest precondition? REF ch 3

The weakest precondition is the least restrictive precondition that will guarantee the validity of the associated post condition ie {x > 0} //weak precondition

Why are grammars confusing?

They are confusing because you have to reason through how the grammars flow

Given the following string, is this grammar ambiguous? Explain. <assign> → <id> = <expr> <id> → A | B | C <expr> → <expr> + <term> | <term> <term> → <term> * <factor> | <factor> <factor> → ( <expr>) | <id>

This is an assignment statement with properties like -Only three possible variables -Multiplication and addition -Parentheses Notice the way expressions are structured, with intermediary rules like "term" and "factor <assign> / | \ <id> = <expr> | | / | \ A = <expr> + <term> | | / | \ <term> + <term> * <factor> | | | | / | \ <factor> + <factor> * ( <id> ) | | | | / | \ <id> + <id> * ( A ) | | | | | | A = B + C * A

What are the Pros and Cons of Tombstones/Lock and Keys?

Tombstone PROS: -Detection of dangling pointers -Consistent Overhead (memory overhead does not vary based on the number of pointers pointing to a memory block) CONS: -Memory Overhead (every allocated memory block needs an associated tombstone, which increases memory overhead). - Access Time (access to a memory block requires an additional dereference to check the tombstone, which can slow the program) Lock and Key PROS: -Effective detection (for both illegal dereferencing of a dangling pointer and unauthorized accesses as every pointer has a unique key that is checked against the memory blocks lock) -No dereferencing Overhead (there is no need for additional dereference when accessing memory) CONS: -Memory Overhead (every pointer and every memory block needs to store their respective keys and locks. It can be significant when there are many pointers. -Time overhead- Every access requires a key-lock check, which adds to the execution time.

What can "type checking" do?

Type checking can reduce errors by finding them at compile time.

Why do we say "variables have a Binding"

We say variables have a Binding, which is referring to when decisions are made about part of a statement. ie. integer value = 10 integer = type address value = name value = 10

Define the term "BNF"

We think of BNF as a meta-language, meaning that it is a language for describing other languages

Layered interface of virtual computers

We will say it like 0 is the most inner layer working outwards in a circle 0 - bare machine 1 - macroinstruction interpreter 2 - operating system 3 - compiler/interpreter

Assuming dynamic scoping, what is the value of y in function sub2 for both calling sequences? function big() { function sub1() { var x = 7; sub2(); } function sub2() { var y = x; var z = 3; } var x = 3; sub2(); }

When sub2 is called directly from big: y = 3. When sub2 is called from sub1: y = 7.

Given the following string, is this grammar ambiguous? <assign> → <id> = <expr> <id> → A | B | C <expr> → <expr> + <expr> | <expr> * <expr> | ( <expr>) | <id> A = B + C * A <-- String result

Yes, because the same string can be parsed in two different ways with the provided grammar -- demonstrating it can be ambiguous.

Is ambiguity a problem for Grammars? Explain in detail.

Yes, it is a major problem for certain kinds of grammar generators, depending on how the internal algorithm is designed. Some grammars (Antlr) are more flexible than others in terms of what kinds of grammars they can handle. Antlr has "Adaptive LL *" that allows for some ambiguity in certain parts of grammar trying to find a balance between flexibility and processing speed

Does the difference in binding impact programmers in practice (e.g., their productivity)? List what key items they can influence and 2 pros and cons

Yes, there are positive and negatives with the difference of binding. 1. Error Detection Statically Typed: PRO - catch errors at compile time and saves time by preventing bugs reaching runtime Dynamically Typed: CON - they are caught at runtime, which there maybe bugs in later stages causing runtime crashes etc 2. Development Speed Statically Typed: CON - need to declare types explicitly can make coding slower Dynamically Typed: PRO - flexibility can speed up initial development and prototyping. Programmers can write code faster. 3. Documentation & Readability Statically Typed: PRO - they can serve as a type of documentation you can look and generally infer its behavior. Dynamically Typed: CON - programmers may need to have more comments as it maybe harder to understand. 4. Learning Curve for beginners Statically Typed: PRO - it can be helpful for beginners to enforce a certain discipline and understanding of datatypes Dynamically Typed: PRO - flexibility and shorter feedback can be encouraging (ie write and run immediately without compile)

Can we create Derivations from Grammars? Explain.

Yes, we can think of a derivation like a control flow from one non-terminal to another. To do this, we follow the symbols, choosing the correct path along the way.

What is BNF discription? ref Ch 3

a grammar or collection of rules.

what is actual type? ref ch3

a synthesized attribute associated with non-terminals (ie. <var> and <expr>) - it is used to store the actual type, int, or real of a variable or expression

The memory cell to which a variable is bound somehow must be taken from a pool of available memory, what is this process called? ref ch 5

allocation

What is it called when the process of placing a memory cell that has been unbound from a variable back into the pool of available memory? ref ch5

deallocation

What is a datatype? ref ch6

defines a collection of data values and a set of predefined operations on those values.

Create an EBNF Grammar for +, -, *, /, and ^ (exponentiation). Then derive a parse tree. a + (b * c) ^ 5 - 4

grammar <expression> ::= <term> { ('+' | '-') <term>} <term> ::= <factor> { ('*' | '/') <factor>} <factor> ::= <base> { '^' <factor> } <base> ::= '(' <expression> ')' | <letter> <letter> ::= 'a' .. 'z' | ...<digit> <digit> ::= '0'..'9' parse tree < e x p r e s s i o n > / | \ | \ <term> '+' <term> '-' <term> | / \ <factor> <factor> <letter> | / | \ | <base> <base> '^' <factor> <digit> | | \ | <letter> (<expression>) <base> 4 | / | \ \ a + (<factor> '*' <factor>)^ <letter> | | | | | | | a + (<term> * <term>) ^ <digit> | | | | | | | a + ( <letter> * <letter>)^ 5 | | | | | | | a + ( b * c )^ 5 - 4

Assuming static scoping, what is the value of y in function sub2? function big() { function sub1() { var x = 7; sub2(); } function sub2() { var y = x; } var x = 3; sub1();

inside sub2() is 3 Let's break down the code: We're inside the big function. x is declared in the scope of the big function and has a value of 3. The sub1 function is called from within the big function. Inside sub1, a local x is declared with a value of 7, but then the sub2 function is called. Inside sub2, there's a declaration for y, which is set to the value of x. Since sub2 does not have a local variable x and due to static scoping, it looks in the nearest outer scope, which is big (not sub1 because sub2 is defined directly inside big and not inside sub1). So, in the scope of the big function, x is 3. Therefore, inside sub2, y = 3.

Give an example of an "R-Value"

int x = 10; //L value int y = 20; //L value x = y; //here y is an R value //sets x to be 20

Give an example of an "L-Value"

int x; x = 10; or int x = 10;

What are Lexemes? ref ch 3

lowest level of syntactical units.

A variable can be characterized as a sextuple of attributes: (list them)

name address value type lifetime scope

Give an example of a Lexeme ref Ch 3

names of variables methods classes ie. identifiers

what are preconditions and post conditions? ref ch3

preconditions and post conditions are assertions of a statement. For two adjacent statements, the post condition of the first serves as the precondition of the second. Developing an axiomatic description or proof of a given program requires that every statement in the program has both a precondition and post condition a precondition and post condition are assertions presented in braces to distinguish them from parts of the statements. ie. precondition for the below statement sum = 2 * x + 1 {sum > 1} one possible precondition is {sum >1}

To access the r-value, what must be determined first? ref ch5

the L-value (address) r-value is the variables value.


Related study sets

Social Problems: CH14 The Environment

View Set

NUR 343 HESI exam study guide (answer with just letters)

View Set

Chapter 8: Eating and Sleep Disorders

View Set

Chapter 3: Artificial Intelligence (AI)

View Set

Chapter 1 Assessment Human Geography

View Set

D216 Unit 4: Contracts, with a focus on Sales and Leases of Goods (14%)

View Set

Cognitive Psychology Chapter 5 Quiz

View Set