CS4337.504 - Final

¡Supera tus tareas y exámenes ahora con Quizwiz!

Why are named constants used, rather than numbers, for token codes?

"Although tokens are usually represented as integer values, for the sake of readability of lexical and syntax analyzers, they are often referenced through constants" p. 170

Compute the weakest precondition for each of the following sequences of assignment statements and their postconditions: a) a = 2 * b + 1; b = a - 3 {b < 0} b) a = 3 * (2 * b + a); b = 2 * a - 1 {b > 5}

(a) a - 3 < 0 a < 3 (precondition for second assignment) 2b + 1 < 3 2b < 2 b < 1 (answer) b) 2a - 1 > 5 2a > 6 a > 3 (precondition for second assignment) 3 (2b + a) > 3 2b + a > 1 2b > 1 - a b > 1/2 - a/2 (answer)

Compute the weakest precondition given the following assignment statement and its postcondition: Show each step of your work. x = (y + 2) * 3 {x > 6}

(y + 2) * 3 > 6 y + 2 > 2 y > 0

Chapter 3

****************************

Chapter 5

****************************

Chapter 4

**********************************

Chapter 16

********************************************

Midterm exam 1 questions

********************************************

What are the two distinct goals of syntax analysis?

1. Make sure that the program is syntactically correct; the compiler must find as many errors as possible in the code. When an error is found, a message is displayed and the compiler returns to a normal state and continues analyzing the code displaying even more errors if there are any. 2. Produce a complete parse tree or a trace of the structure that is used as the basis for translation. The tree is made for syntactically correct input. (p.178)

Briefly describe three advantages of LR parsers.

1. They can be built for all programming languages 2. Syntax errors can be detected as soon as it is possible in left-to-right scan 3. The LR class of grammars is a proper superset of the class parsable by LL parsers, meaning that many left-recursive grammars are LR, but none of these left recursive grammars are LL.

Match the computer science pioneer to their primary contribution. 1. Designed the hardware architecture used in most modern computers 2. Designed the first widely-used high-level language (Fortran) 3. Wrote the first software program 4. Designed the first general purpose computing device, the "difference engine" a. John von Neuman b. John Backus c. Ada Lovelace d. Charles Babbage

1. a. John von Neuman 2. b. John Backus 3. c. Ada Lovelace 4. d. Charles Babbage

Write EBNF descriptions (i.e. rules) for the a Java class definition header statement. Begin with the start symbol <class_header>. Use the non-terminal <class_name> for the user-defined name of the class. You do not have to include a LHS rule which defines valid naming conventions to expand the <class_name> non-terminal.

<class_head> → {<modifier>} class <class_name> [extends class_name [implements <interface_name> {, <interface_name>}] <modifier> → public | abstract | final

Convert the following BNF rule with three RHSs to an EBNF rule with a single RHS. Note: Conversion to EBNF should remove all explicit recursion and yield a single RHS EBNF rule. A ⟶ B + A | B - A | B

A -> B {( + | - ) B}

Using the algorithm described in Section 4.4.2, remove direct left recursion form the following grammar rules. A → Aa | Abc | bc | d

A = A, α _1 = a, α_2 = bc, β_1 = bc, β_2 = d A -> bcA' | dA' A' -> aA' | bcA' | Ɛ

Define static binding and dynamic binding.

A binding is static if it first occurs before run time and remains unchanged throughout program execution A binding is dynamic if it first occurs during execution of the program

What is a block?

A method of creating static scopes inside program units

Convert the following EBNF grammar rules to BNF. Avoid left recursion. Your answer will be graded on both correctness and simplicity. A → B {(x|y) B} B → w[z]

A → BxA | ByA | B B → w | wz

Match these Storage Binding and Lifetime categories of scalar variables with their definitions. Variables that are bound to memory cells before program execution begins and remain bound to those same memory cells until program execution terminates. Variables that are nameless (abstract) memory cells allocated and deallocated by explicit run-time instructions written by the programmer. Variables whose storage bindings are created when their declaration statements are elaborated, but whose types are statically bound. Variables bound to heap storage only when they are assigned values. OPTIONS A. Static variables B. Stack-dynamic variables C. Explicit heap-dynamic variables D. Implicit heap-dynamic variables

A, C, B, D

Match the terms with their definitions. The process of taking a memory cell from a pool of available memory and binding it to a variable. The time during which the variable is bound to a specific memory location. The process of placing a memory cell that has been unbound from a variable back into the pool of available memory. The binding of more than one variable to the same memory cell. OPTIONS A. Allocation B. Deallocation C. Lifetime D. Aliasing

A, C, B, D in order

For each of the following Racket expressions, write the output that would result from evaluating it.Each is worth 1pt, except L and M which are worth 2pts. Note: expt is the exponent function, e.g. (expt 5 2) evaluates to 25. A. (/ 2 5) B. (pair? '(1 2 3)) C. (or (and (= 3 (+ 1 2)) #t) #f) D. (cdr '(1)) E. (cons 5 7) F.(cons '(1 2) '(3 4)) G. ((λ (x y)(expt x y)) 2 3) H. ((λ (x) ((λ (y z) (z x y)) 2 /)) 1)

A. 2/5 B. #t C. #t D. '() E. '(5 . 7) F. '((1 2) 3 4) G. 8 H. 1/2

What are the advantages and disadvantages of dynamic scoping?

Advantages: Convenience Disadvantage: 1. While a subprogram is executing, its variables are visible to all subprograms it calls 2. Impossible to statically type check 3. Poor readability - it is not possible to statically determine the type of a variable

Show a complete parse, including the parse stack contents, input string, and action for the string (id + id) * id, using the grammar and parse table in Section 4.5.3.

Again, NOTES.

Describe, in English, the language defined by the following grammar: S → ABC A → aA | a B → bB | b C → cC | c

All strings that contain one or more a's followed by one or more b's followed by one or more c's. The following strings are all in the language: abc abbbbbbbbbbc aaaaaaaaaabbcc aabcc

Although the + addition operator may be chained with multiple operands, it is actually a binary operator. Therefore, to evaluate 2+3+5 we must first either (a) add 2+3 and combine the result with 5, or (b) add 3+5 and combine the result with 2. The property that describe which of these two strategies a language uses is called. Commutativity Recursion Reflexivity Associativity

Associativity

What are the design issues for names?

Case sensitivity Whether special words are reserved words or keywords Special characters Length limit

Show a complete parse, including the parse stack contents, input string, and action for the string id * (id + id), using the grammar and parse table in Section 4.5.3.

Check your notes. RIGHT NOW.

Match the term to the definition. Variable Keyword Reserved word Name OPTIONS A. A word of a programming language that is special only in certain contexts. B. A special word of a programming language that can- not be used as a name. C. A string of characters used to identify some entity in a program. D. An abstraction of a computer memory cell or collection of cells.

D, A, B, C

What is a static ancestor of a subprogram? What is a dynamic ancestor of a subprogram?

Enclosing static scopes (to a specific scope) are called its static ancestors Dynamic ancestors are those that are called to reach the subprogram in question

In programming languages, the use of keywords is more restrictive than reversed words. (T/F)

F

Like the if/else statement in Java and C++, the Racket else statement is optional. (T/F)

F

Python data types are declared explicitly. (T/F)

F

A Language is a collection of syntax rules that describe how to generate valid sentences. Selected Answer: True False

False

Using the algorithm described in Section 4.4.2, remove direct left recursion form the following grammar rules. X → Xyy | Xz | Yx | w Y → Yy | Yw | zy | x

For X: A = X, α _1 = yy, α _2 = z, β_1 = Yx, β_2 = w X -> YxX' | wX' X' -> yyX' | zX' | Ɛ For X: A = Y, α _1 = y, α _2 = w, β_1 = zy, β_2 = x Y -> zyY' | xY' Y' -> yY' | wY' | Ɛ

What is an alias?

If two variable names can be used to access the same memory location, they are called aliases

What is the purpose of the 'let' constructs in functional languages?

It binds names to values in order to be used in an expression

Variables cam be characterized as a sextuple (6-tuple) of which six attributes?

Name Address Value Type Lifetime Scope

What is the potential danger of case-sensitive names?

Names that look alike are different

What are the advantages of named constants?

Readability Used to parameterize programs

Convert the following EBNF rules to BNF: S → A{bA} A → a[b]A

S -> A | AB A -> aA -> abA B -> bA | BbA

Write a BNF grammar which generates the language described as: "The set of strings with a single x followed by an odd number of y's" You may NOT use the EBNF empty string symbol (ε) to terminate recursion. Your grammar should begin with the start symbol S and avoid left recursion. Your answer will be graded on both correctness and simplicity.

S → xYY → yyY | y

Modify one or more rules of the following grammar so that the + operator has precedence over the * operator. Note: Do not introduce extra terminal symbols, like parentheses, into the grammar. That would change the language, not the operator precedence. S ⟶ X X ⟶ Y + X | Y Y ⟶ Z * Y | Z Z ⟶ 1 | 2 | 3 | 4

S ⟶ X X ⟶ Y * X | Y Y ⟶ Z + Y | Z Z ⟶ 1 | 2 | 3 | 4

Using the grammar in Example 3.2 [p.117]*, draw a parse tree for each of the three following statements: a. A = A * (B + (C * A)) b. B = C * (A * C + B) c. A = A * (B + (C))

See Notes. Right now, not later.

Using the grammar in Example 3.4 [p.120]*, draw a parse tree for each of the four following statements: a. A = ( A + B ) * C b. A = B + C + A c. A = A * (B + C) d. A = B * (C * (A + B))

See Notes. Right now, not later.

What are three reasons why lexical analysis is separated from syntax analysis? (one word each)

Simplicity Efficiency Portability (Textbook page 169 maybe look at these in more detail)

Define lifetime, scope, and dynamic scope.

The lifetime of a variable is the time during which it is bound to a particular memory cell. The scope of a variable can be statically determined (prior to execution). Dynamic scope is based on the calling sequence of subprograms, not on their spatial relationship to each other. Thus, the scope can be determined only at run time.

Using the grammar in Example 3.2 [p.117]*, show a sentential form leftmost derivation for each of the three following statements. a. A = A * (B + (C * A)) b. B = C * (A * C + B) c. A = A * (B + (C))

a. <assign> => <id> = <expr> => A = <expr> => A = <id> * <expr> => A = A * <expr> => A = A * (<expr>) => A = A * (<id> +<expr>) => A = A * (B + <expr>) => A = A * (B + (<expr>)) => A = A * (B + (<id> * <expr>)) => A = A * (B + (C * <expr>)) => A = A * (B + (C * <id>)) => A = A * (B + (C * A)) b. <assign>=> <id> = <expr> => B = <expr> => B = <id> * <expr> => B = C * <expr> => B = C * (<expr>) => B = C * (<id> * <expr>) => B = C * (A * <expr>) => B = C * (A* <id> + <expr>) => B = C * (A* C + <expr>) => B = C * (A* C + <id>) => B = C * (A* C + B) c. <assign> => <id> = <expr> => A = <expr> => A = <id> * <expr> => A = A * <expr> => A = A * (<expr>) => A = A * (<id> + <expr>) => A = A * (B + <expr>) => A = A * (B + (<expr>)) => A = A * (B + (<id>)) => A = A * (B + (C))

Compute the weakest precondition for each of the following assignment statements and postconditions: a. a = 2 * (b - 1) - 1 {a > 0} b. b = (c + 10) / 3 {b > 6} c. a = a + 2 * b - 1 {a > 1} d. x = 2 * y + x - 1 {x > 11}

a. 2(b-1) -1 > 0 2b - 2 - 1 > -0 2b -3 > 0 2b > 3 b > 3/2 b. (c+10) / 3 > 6 c + 10 > 18 c > 8 c. a + 2b - 1 > 1 a + 2b > 2 2b > 2 - a b > 1 - a/2 d. 2y + x - 1 > 11 2y + x > 12 2y > 12 - x y > 6 - x/2

Using the grammar in Example 3.4 [p.120]*, show a sentential form leftmost derivation for each of the four following statements. a. A = ( A + B ) * C b. A = B + C + A c. A = A * (B + C) d. A = B * (C * (A + B))

a. <assign> => <id> = <expr> => A = <expr> => A = <term> => A = <term> * <factor> => A = <factor> * <facor> => A = (<expr>) * <factor> => A = (<expr> + <term>) * <factor> => A = (<term> + <term>) * <facor> => A = (<factor> + <term>) * <factor> => A = (<id> + <term>) * <factor> => A = (A + <term>) * <factor> => A = (A + <factor>) * <factor> => A = (A + <id>) * <factor> => A = (A + B) * <factor> => A = (A + B) * <id> => A = (A + B) * C b. <assign>=> <id> = <expr> => A = <expr> => A = <expr> + <term> => A = <expr> + <term> + <term> => A = <term> + <term> + <term> => A = <factor> + <term> + <term> => A = <id> + <term> + <term> => A = B + <term> + <term> => A = B + <factor> + <term> => A = B + <id> + <term> => A = B + C + <term> => A = B + C + <factor> => A = B + C + <id> => A = B + C + A c. <assign>=> <id> = <expr> => A = <expr> => A = <term> => A = <term> * <factor> => A = <factor> * <facor> => A = <id> * <factor> => A = A * <factor> => A = A * (<expr>) => A = A * (<expr> + <term>) => A = A * (<term> + <term>) => A = A * (<factor> + <term>) => A = A * (<id> + <term>) => A = A * (B + <term>) => A = A * (B + <factor>) => A = A * (B + <id>) => A = A * (B + C) d. <assign>=> <id> = <expr> => A = <expr> => A = <term> => A = <term> * <factor> => A = <factor> * <factor> => A = <id> * <factor> => A = B * <factor> => A = B * (<expr>) => A = B * (<term>) => A = B * (<term> * <factor>) => A = B * (<factor> * <factor>) => A = B * (<id> * <factor>) => A = B * (C * <factor>) => A = B * (C * (<expr>)) => A = B * (C * (<expr> + <term>)) => A = B * (C * (<term> + <term>)) => A = B * (C * (<factor> + <term>)) => A = B * (C * (<id> + <term>)) => A = B * (C * (A + <term>)) => A = B * (C * (A + <factor>)) => A = B * (C * (A + <id>3)) => A = B * (C * (A + B))

Perform the pairwise disjointness test for each of the following grammar rules. Your answer should show the FIRST() function for each RHS, and then state whether the LHS rule passes or fails pairwise disjointness by examining all pairs of RHS FIRST()'s. a. A → aB | b | cBB b. B → aB | bA | aBb c. C → aaA | b | caB

a. FIRST(aB) = {a} FIRST(b) = {b} FIRST(cBB) = {c} FIRST(aB) ∩ FIRST(b) ∩ FIRST(cBB) = Ø {a} ∩ {b} ∩ {c} = Ø The LHS rule A passes pairwise disjointness. b. FIRST(aB) = {a} FIRST(bA) = {b} FIRST(aBb) = {a} FIRST(aB) ∩ FIRST(bA) ∩ FIRST(aBb) = {a} {a} ∩ {b} ∩ {a} = {a} fails since FIRST(aB) = {a} and FIRST(aBb) = {a} do not have unique symbols c. FIRST(aaA) = {a} FIRST(b) = {b} FIRST(caB) = {c} FIRST(aaA) ∩ FIRST(b) ∩ FIRST(caB) = Ø {a} ∩ {b} ∩ {c} = Ø Passes since every FIRST is unique.

Perform the pairwise disjointness test for each of the following grammar rules. Your answer should show the FIRST() function for each RHS, and then state whether the LHS rule passes or fails pairwise disjointness. a. S → aSb | bAA b. A → b{aB} | a c. B → aB | a

a. FIRST(aSb) = {a} FIRST(bAA) = {b} FIRST(aSb) ∩ FIRST(bAA) = Ø {a} ∩ {b} = Ø Passes b. FIRST(b{aB}) = {b} FIRST(a) = {a} FIRST(b{aB}) ∩ FIRST(a) = Ø {b} ∩ {a} = Ø Passes c. FIRST(aB) = {a} FIRST(a) = {a} FIRST(aB) ∩ FIRST(a) = {a} {a} ∩ {a} = {a} Fails since FIRST(aB) = {a} and FIRST(a) = {a} are not unique .

Which of the following A-rules fail the Pairwise Disjointness Test? a. A → aB | BAb B → aB | b b. A ⟶ aB | bAb | Bb B ⟶ cB | d c. X → xy | Yy Y → yx | zx d. X → xy | Yy Y → yz | xz

a. A → aB | BAb B → aB | b d. X → xy | Yy Y → yz | xz

What are some characteristics of the Functional Programming paradigm? (check all that apply) a. Ability to return a function as a valid return type of a function b. Ability to pass functions as arguments to other functions c. Support for dynamic variables d. Based on first-order logic (FOL) e. Data abstraction f. Elimination (or minimization) of side effects g. The presence of anonymous (unnamed) functions h. Close semantic parallels with assembly language

a. Ability to return a function as a valid return type of a function b. Ability to pass functions as arguments to other functions f. Elimination (or minimization) of side effects g. The presence of anonymous (unnamed) functions

Racket is a dialect of which older language? a. LISP b. Fortran c. Assembly d. COBOL

a. LISP

Which of the following are categories of Dynamic Semantics? a. Operational Semantics b. Compilation Semantics c. Axiomatic Semantics d. Denotational Semantics e. Proportional Semantics f. Syntactical Semantics

a. Operational Semantics c. Axiomatic Semantics d. Denotational Semantics

Select all that are examples of dynamic type binding. a. Python x = int(4.0) b. C++ int x = 4; c. Perl $x = 4; d. Java int x = (int)4.0;

a. Python c. Perl (Notice we don't have to specify variable type)

The Readability of a program is affected by which Characteristics of Language Evaluation? (check all that apply) a. Syntax design b. Restricted aliasing c. Simplicity d. Type checking

a. Syntax design c. Simplicity

At which possible binding time is the Java + operator symbol bound to a memory location? a. Language design time b. Runtime c. Compile time d. Load time e. Language implementation time

a. language design time

At which possible binding time is a C or C++ static variable bound to a memory location? a. Load time b. Compile time c. Runtime d. Language design time e. Language implementation time

a. load time

Consider the following grammar: S → aScB | A | b A → cA | c B → d | A Which of the following sentences are in the language generated/recognized by this grammar? acccbd accc abcd acccbcc acd cd

accc abcd

Which of the following is NOT a category of variable with respect to storage bindings and lifetime? a. Stack-dynamic b. Memory locked c. Implicit heap-dynamic d. Static e. Explicit heap-dynamic

b. Memory locked

What is the scope of a named constant? a. Functional local b. Package c. Declaration to end d. Global

b. Package

What is the primary use of attribute grammars? a. Defining dynamic semantics b. Enforcing type compatibility c. Enforcing BNF syntax rules d. Limiting memory usage

b. enforcing type compatability

Consider the following grammar: <S> → <A> a <B> b <A> → <A> b | b <B> → a <B> | a Which of the following strings are in the language generated by this grammar? baab bbbab bbaab bbaaaaa

baab bbaab

Which of the following is NOT a possible binding time? a. Language design time b. Runtime c. Debug time d. Load time e. Language implementation time f. Compile time

c. Debug time

Which of the following is NOT a Characteristic of Language Evaluation criteria associated with Writability? a. Syntax design b. Simplicity c. Exception handling d. Data types

c. Exception handling

On what branch of mathematics is axiomatic semantics based? a. Combinatorics b. Recursive Function Theory c. Formal Logic d. Linear Algebra

c. Formal Logic

On what branch of mathematics is denotational semantics based? a. Formal logic b. Combinatorics c. Recursive Function Theory d. Linear Algebra

c. Recursive function theory

Which one of the following is NOT a reason why lexical analysis is separated from syntax analysis? a. Efficiency b. Simplicity c. Reliability d. Portability

c. Reliability

The first language designed specifically for text and string processing. a. LISP b. COBOL c. SNOBOL d. Fortran

c. SNOBOL

Consider the following grammar: S → xSzB | A | yA → zA | zB → w | A Which of the following strings are in the language generated by this grammar? a. xzw b. xzzzyw c. xzzzzw d. xzzzyzz e. xyzw f. xzzz

c. xzzzzw e. xyzw f. xzzz

What are some of the features of Functional Programming Languages? (select all that apply) a. Based on first-order logic (FOL) b. Data abstraction c. Close semantic parallels with assembly language d. Ability to pass functions as arguments to other functions e. The presence of anonymous (unnamed) functions f. Support for dynamic variables g. Elimination (or minimization) of side effects h. Ability to return a function as a valid return type of a function

d. Ability to pass functions as arguments to other functions e. The presence of anonymous (unnamed) functions g. Elimination (or minimization) of side effects h. Ability to return a function as a valid return type of a function

Which of the following is NOT an attribute of a variable? a. Lifetime b. Data type c. Name d. Length e. Value f. Scope g. Address

d. Length

The first high-level language, designed by Konrad Zuse. a. C b. Ada c. IBM704 d. Plankalkul

d. Plankalkul

An algorithm was described to eliminate direct left recursion in a BNF grammar by replacing a left recursive LHS rule with two new LHS rules. Given the following set of X-rules (capital symbols are variables, lowercase bold are terminals)... X ⟶ Xx | Xy | XY | zY | WY | Wx (a) List each of the α string vlaues and β beta string values (b) Using the α's andβ's, provide two new sets of rules (X-rules and Xʹ-rules) to replace them that will eliminate direct left recursion.

α1 = x, α2 = y, α3 = Y, β1 = zY. β2 = WY, β3 = Wx X -> zYX' | WYX' | WxX' X' -> xX' | yX' | YX' | ε


Conjuntos de estudio relacionados

Chapter 19: The Industrial Revolution and Nineteenth-Century Society

View Set

Chapter 12 Medical Terminology Word parts

View Set

43, collocation- study and learning 3

View Set

apush: ch 23-the 1920s: coping with change

View Set