Languages and Translationjcsites.juniata.edu/faculty/rhodes/lt/exams/exam1f11key.docx · Web...

8
Languages and Translation CS 362 Exam 1 Fall 2011 9/29/11 (100 points) Name ___________Key_______________ 1. True/false on programming languages basics, etc. [10 pts] __T__ Languages are designed for two audiences: machine and the human programmer. __T__ Computer languages fall into the context-free language realm. __F__ Object-oriented is the latest paradigm brought to us in C++. __T__ Functional language paradigm is also implementable in procedural and OO languages. __T__ Turing complete languages minimally include sequential and iteration structures. __T__ Turing complete languages minimally require integer arithmetic. __F__ The logical language paradigm is included in object-oriented language paradigm. __T__ Church’s thesis states that it is not possible to build a machine inherently more powerful than a Turing machine. __F__ The Java language’s virtual machine is based on specific computer hardware. __F__ All computer languages trace their genealogy back to FORTRAN. 2. /*java*/ { FooObject x = new FooObject(m*2); // instantiate [8 pts] Fill in the blanks for this statement that is both _declarative _ and imperative. Identifier x is bound to its R-value at _run ______ -time, but its type is bound at ___compile _-time, while its L-value is bound at ____ run ____-time. Constant 2 has its value bound at ___ compile _. Identifier FooObject has two purposes as a ____type _______ and a _____constructor _________; both are bound at ___ compile __-time.

Transcript of Languages and Translationjcsites.juniata.edu/faculty/rhodes/lt/exams/exam1f11key.docx · Web...

Page 1: Languages and Translationjcsites.juniata.edu/faculty/rhodes/lt/exams/exam1f11key.docx · Web viewReadability is the ease of reading which usually requires the language to be more

Languages and TranslationCS 362 Exam 1

Fall 2011 9/29/11 (100 points)Name ___________Key_______________

1. True/false on programming languages basics, etc.[10 pts]

__T__ Languages are designed for two audiences: machine and the human programmer.

__T__ Computer languages fall into the context-free language realm.

__F__ Object-oriented is the latest paradigm brought to us in C++.

__T__ Functional language paradigm is also implementable in procedural and OO languages.

__T__ Turing complete languages minimally include sequential and iteration structures.

__T__ Turing complete languages minimally require integer arithmetic.

__F__ The logical language paradigm is included in object-oriented language paradigm.

__T__ Church’s thesis states that it is not possible to build a machine inherently more powerful than a Turing machine.

__F__ The Java language’s virtual machine is based on specific computer hardware.

__F__ All computer languages trace their genealogy back to FORTRAN.

2. /*java*/ { FooObject x = new FooObject(m*2); // instantiate[8 pts]

Fill in the blanks for this statement that is both _declarative_ and imperative. Identifier x is bound to its R-value at _run______ -

time, but its type is bound at ___compile_-time, while its L-value is bound at ____ run ____-time. Constant 2 has its value bound

at ___ compile _. Identifier FooObject has two purposes as a ____type_______ and a _____constructor_________; both are bound

at ___ compile __-time.

3. List all the lexical tokens in the Java statement in question #2.[4 pts]

{ FooObject x = new FooObject ( m * 2 ) ;

4. Given the Pascal two dimensional array declarationVAR M: Array [10..20,1..5] of Real;show an effective address calculation for element M(i,j). Assume M[10,1] is at address 2000 and that row-major storage is used (rightmost subscript varies the fastest; leftmost the slowest).

[6 pts]

EA = 2000 + (i-10)*5*4 + (j-1)*4 = 2000 + i*20 – 540 + j*4 – 4 = 1456 + i*20 + j*4

Page 2: Languages and Translationjcsites.juniata.edu/faculty/rhodes/lt/exams/exam1f11key.docx · Web viewReadability is the ease of reading which usually requires the language to be more

CS 362 Fall 2011 Exam #1 Page 25. Language design criteria.

[10 pts]a. Explain the conflict between the “readability” and “writability” criteria in language design. [5]

Readability is the ease of reading which usually requires the language to be more verbose and close to mathematical symbolism; programs tend to be larger in text size.

Writability is the ease of writing which tends to make languages terse and cryptic using symbols. Programs are much smaller and difficult to read.

b. Explain the “orthogonality” criterion. Give an example in Java that portrays a violation of orthogonality. [5]

Two or more features that are independent, when combined provide more usable features. When the orthogonality criterion is adhered, then the programmer only needs to learn the sum of the independent features providing a product of their interaction. The exceptions to the product list have to added to the items to be learned. We want this sum to be much less than the total interaction of features for it to be worthwhile.

Java primitive types and classes are not treated equally. Relational operators for numbers cannot be equally used on Strings.

6. Draw a diagram that connects these 7 modules of a compiler by the theoretical data flow of data. Label the flow lines with the content of data that are streamed from one stage to the next.

[8 pts]Semantic analysis Optimization Code generationSyntactic analysis (parser) Intermediate code generation Lexical analysis (scanner)

Symbol table

7. Back ends and front ends.[5 pts]

Which modules constitute the front end of a compiler? What is significant about the front end?

Scanner, parser, semantic analysis – dependent on the source language, other modules not so

Which modules constitute the back end? What is significant about the back end?

[optimizer], code generator – dependent on the hardware/target machine, other modules not so

Page 3: Languages and Translationjcsites.juniata.edu/faculty/rhodes/lt/exams/exam1f11key.docx · Web viewReadability is the ease of reading which usually requires the language to be more

CS 362 Fall 2011 Exam #1 Page 38. Types and subtypes.

[15 pts]a. What are the two major components that define a datatype? That is, what must be specified to define a type?

domain or set of legal values and the operations on those values

b. Why is a strongly typed language desirable?

catches more programming errors by requiring variables to be bound to a type. Misuse can be caught by compiler.

c. Give an example of a subtype. You do not need to use a specific language.

May have a subrange of integers, e.g., 1..100

d. What are the issues of assigning values back and forth between a subtype and its base type?

subtype assignment to base type is no problem since subtype is a subset. Base to subtype may be a problem since the value may not be in the subtype

e. How are the issues above similar to assigning integers and floats back and forth?

All integers are representable by float but the converse is not true. What to do with fractional part?

9. Scoping rules.[9 pts]

Consider the following Ada skeletal program:Procedure Main is X : Integer; Procedure Sub3; -- a header declaration Procedure Sub1 is X : Integer; Procedure Sub2 is Begin … End; --of Sub2 Begin -– of Sub1 … End; -- of Sub1 Procedure Sub3 is Begin … End; --of Sub3 Begin –- of Main … End; -- of Main

a. Assuming static scoping, which is the declaration of X that is the correct one for each of the procedure bodies?

Sub1 uses X declared in ___Sub1____

Sub2 uses X declared in ___Sub2____

Sub3 uses X declared in ____Main__

b. Assuming dynamic scoping butMain calls Sub1,Sub1 calls Sub2 andSub2 calls Sub3

Sub1 uses X declared in ___ Sub1____

Sub2 uses X declared in ___ Sub1____

Sub3 uses X declared in ___ Sub1____

Page 4: Languages and Translationjcsites.juniata.edu/faculty/rhodes/lt/exams/exam1f11key.docx · Web viewReadability is the ease of reading which usually requires the language to be more

CS 362 Fall 2011 Exam #1 Page 410. Assume the set of productions representing simple variable declarations in a pseudo-Pascal.

[25 pts]dcl ® VAR list : type ;list ® list , idlist ® idtype ® CHAR | NUM id ® i | j | k

a) The alphabet or set of terminals is ___VAR : , CHAR NUM i j k__ [3]

b) The set of nonterminals is ___dcl list type id_______ [3]

c) The start symbol is ____dcl_____ [1]

d) Show a top down, left-most derivation for var i,k : Char; starting with the start symbol. [5]

e) Draw the corresponding parse tree. [5]Top Down Derivation Parse tree

Dcl -> Var list : type ; Var list , id : type ; Var id , id : type ; Var i , id : type ; Var i , j : type; Var i , j : Char ;

f) Show the sequence of bottom up parser actions (shift and reduce) for the same above input that ultimately leads to the start symbol. It is started for you. [8]

var i,k : Char; Stack Input remaining Shift or

ReduceRule used in reduce

$var i,k : Char; Shift$ var i ,k : Char; Shift$ var id ,k : Char; Reduce id-> i$ var list “ Reduce List -> id$ var list , k : Char ; Shift$ var list , k : Char ; Shift$ var list , id “ Reduce Id ->k$ var list “ Reduce List -> list , id$ var list : Char ; Shift$ var list : Char ; Shift$ var list : type ; Reduce Type -> Char$ var list : type ; $ Shift$ dcl $ Reduce Dcl

Accept Empty input, start symbol only on stack