CS Exam #1-100 points Spring 2011

Similar documents
CS 415 Midterm Exam Spring SOLUTION

Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer:

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

CS 415 Midterm Exam Spring 2002

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

Semantic Analysis. Lecture 9. February 7, 2018

Why are there so many programming languages?

Introduction to Lexical Analysis

Concepts of Programming Languages

Organization of Programming Languages (CSE452) Why are there so many programming languages? What makes a language successful?

Compilers. Prerequisites

Theory and Compiling COMP360

Part 5 Program Analysis Principles and Techniques

Principles of Programming Languages. Lecture Outline

Chapter 1. Preliminaries

Syntactic Analysis. CS345H: Programming Languages. Lecture 3: Lexical Analysis. Outline. Lexical Analysis. What is a Token? Tokens

Lexical Scanning COMP360

Compilers and Interpreters

Programming Languages, Summary CSC419; Odelia Schwartz

Compiler Design (40-414)

Programming Languages Third Edition. Chapter 7 Basic Semantics

8/27/17. CS-3304 Introduction. What will you learn? Semester Outline. Websites INTRODUCTION TO PROGRAMMING LANGUAGES

CS 3360 Design and Implementation of Programming Languages. Exam 1

Chapter 1. Preliminaries

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

Why are there so many programming languages? Why do we have programming languages? What is a language for? What makes a language successful?

CS 3360 Design and Implementation of Programming Languages. Exam 1

Chapter 1 Preliminaries

Structure of Programming Languages Lecture 3

A simple syntax-directed

CSCI312 Principles of Programming Languages!

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

MIDTERM EXAM (Solutions)

Introduction to Lexical Analysis

General Concepts. Abstraction Computational Paradigms Implementation Application Domains Influence on Success Influences on Design

printf( Please enter another number: ); scanf( %d, &num2);

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

A Pascal program. Input from the file is read to a buffer program buffer. program xyz(input, output) --- begin A := B + C * 2 end.

CS 415 Midterm Exam Fall 2003

CSC 467 Lecture 3: Regular Expressions

CS164: Midterm I. Fall 2003

If you are going to form a group for A2, please do it before tomorrow (Friday) noon GRAMMARS & PARSING. Lecture 8 CS2110 Spring 2014

CD Assignment I. 1. Explain the various phases of the compiler with a simple example.

CS321 Languages and Compiler Design I. Winter 2012 Lecture 4

Syntax. A. Bellaachia Page: 1

CMSC 330: Organization of Programming Languages

Introduction. A. Bellaachia Page: 1

1 Lexical Considerations

Syntactic Analysis. The Big Picture Again. Grammar. ICS312 Machine-Level and Systems Programming

CSE 3302 Programming Languages Lecture 2: Syntax

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

CT32 COMPUTER NETWORKS DEC 2015

Syntax Errors; Static Semantics

MIDTERM EXAMINATION - CS130 - Spring 2005

Lexical analysis. Syntactical analysis. Semantical analysis. Intermediate code generation. Optimization. Code generation. Target specific optimization

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

Introduction to Compiler Construction

The role of semantic analysis in a compiler

programming languages need to be precise a regular expression is one of the following: tokens are the building blocks of programs

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

! Broaden your language horizons! Different programming languages! Different language features and tradeoffs. ! Study how languages are implemented

Early computers (1940s) cost millions of dollars and were programmed in machine language. less error-prone method needed

G Compiler Construction Lecture 4: Lexical Analysis. Mohamed Zahran (aka Z)

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

CS 321 IV. Overview of Compilation

Crafting a Compiler with C (II) Compiler V. S. Interpreter

4. An interpreter is a program that

Compiler Construction D7011E

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Introduction. Interpreters High-level language intermediate code which is interpreted. Translators. e.g.

CSE 12 Abstract Syntax Trees

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

CSCC24 Functional Programming Scheme Part 2

Midterms Save the Dates!

Writing a Lexer. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, February 6, Glenn G.

10/4/18. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntactic Analysis

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

CS415 Compilers. Lexical Analysis

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

Programming Project 4: COOL Code Generation

CS 426 Fall Machine Problem 1. Machine Problem 1. CS 426 Compiler Construction Fall Semester 2017

CSCI312 Principles of Programming Languages!

CPS 506 Comparative Programming Languages. Syntax Specification

Lexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!

DEMO A Language for Practice Implementation Comp 506, Spring 2018

Lecture 16: Static Semantics Overview 1

CSE 341 Section 7. Eric Mullen Spring Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang

Semantics of programming languages

Compiler Design. Computer Science & Information Technology (CS) Rank under AIR 100

Using Scala for building DSL s

CS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution

G Programming Languages - Fall 2012

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Transcription:

CS 4700 - Exam #1-100 points Spring 2011 Fill in the blanks (1 point each) 1. syntactic sugar is a term coined for additions to the syntax of a computer language that do not affect its expressiveness but make it nicer for humans to use. 2. abstraction means complicated structures can be stated in simple ways by ignoring many of the details. 3. In the pre-parsing phase termed lexical analysis, characters are grouped into logical chunks (keywords, constants, etc). The actual strings that are matched are termed lexemes. 4. A metalanguage is a language used to describe another language. 5. Generality refers to combining closely related constructs into a single more general one. 6. Orthogonality means features can be combined in any combination. 7. When similar things look the same, and different things look different we term it Uniformity 8. With understatement independent, a user need only know about subset explicitly used to write the program. 9. Preciseness means there exists a language definition that can answer programmers and implementers questions. 10. A glue language used for connected software components is considered to be part of the scripting paradigm. Multiple Choice (2 points each) Circle the single best answer. 1. When a language feature is independent of the context in which it is used, we term it a. portable b. **orthogonal c. readable d. unified 2. Why do we care if a grammar is ambiguous? a. We don t really. While ambiguity is interesting, it is not important. b. It takes longer to parse. c. **A different semantic meaning is associated with a different parse tree a. none of the above 3. Lexical analysis is separated from syntax analysis because: a. **lexical analysis is less complex. b. optimizing the lexical analyzer provides more benefits than optimizing the syntax analyzer. c. lexical analysis has to perform input/output operations and, therefore, is platform dependent. d. all of the above. 0

4. The C++ programming language is very popular because it is: a. backward compatible with C. b. object-oriented. c. widely available. d. **all of the above 5. An alternate term for lexical analyzer is: a. **scanner b. parser c. pushdown automaton d. all of the above 6. A concept of means that a small number of primitive constructs can be combined in a small number of ways to build the control and data structures of the language. a. **simplicity b. parsimony c. relativity d. orthogonality 7. The most common programming languages, dating back to the 1940s are called: a. functional. b. object-oriented. c. rule-based. d.** imperative. 8. Which of the following represent a static semantic error in C++? a. **type mismatch b. missing semi-colon c. illegal character d. division by zero e. all of the above 9. Which of the following represent a dynamic semantic error? a. type mismatch b. missing semi-colon c. illegal character d. **division by zero e. all of the above 10. Ruby has the advantage that we can return multiple values by a. Using an asterisk before the variable name b. **Listing multiple values on the left hand side c. Returning a structure d. All of the above 11. In Ruby, when a code block is passed to a method, the scope of the non-local variables in the block are a. **the scope from which the block was called b. the scope where the block is executed c. non-local variables cannot be used in a block 1

Short Answer 1. (7 points) Write a regular expression for strings of 0 s and 1 s in which the string 101 never occurs as a substring (but all other combinations of 0 and 1 are allowed). For example, 0000, 11001, and 1100 are legal. 10011011 is illegal. 0*1*( 00+1*)*0* way. Some of you thought you could say something like [^101] but the not doesn't work that 2. (8 points) What does the following Ruby code do? def generate (length=16) chars = 'abcdefghjkmnpqrstuvwxyzabcdefghjklmnopqrstuvwxyz23456789' word = ' ' length.downto(1) { i word << chars[rand(chars.length - 1)] } word end returns a random string of length 16 (or whatever is specified) 3. (6 points) Describe differences between the type checking and the evaluation of an expression which is dynamically typed. Carefully explain the roles of static and dynamic types. static: known at compile time dynamic: not known until compile time. Possibly may change at run time. To do type checking at run time, I have to look up what the types are and see if they are legal. At compile time, I can tell from the declared types. Similarly, at run time, I need to determine and call the appropriate methods. If the types are known at compile time, it is a compile time determination. 2

4. (10 points) Write the Ruby code to do the following: Given an array of string words, build an array of only those words in the array that are anagrams of the first word in the array. Recall, two words are anagrams if they contain exactly the same letters. mylist = [posh hops are purchased at the food shop] newlist = [posh, hops, shop] def match(a,b) aa = a.split('').sort bb = b.split('').sort aa==bb end mylist = ["posh", "hops", "are", "purchased", "at", "the", "food", "shop"] p mylist newlist = [] first = mylist[0]; mylist.each{ w newlist << w if match(first,w) } p newlist 3

5. (10 points) in Ruby, define a method to count the number of times a vowel occurs in the given string. def vowel_count(mystring) mystring = "Now is the time for all good men" p mystring.count('aeiou') 4

6. (12 points) Consider the Ruby programming language as defined in this class. You are charged with making design changes to Ruby so that it can be used by professional developers to rapidly produce correct, efficient, and maintainable code. Identify four problems with Ruby that prevent it from attaining those goals. Explain each problem, explain how you would change Ruby, and justify your change. Be concrete. Choose problems and solutions to show off your knowledge. The aspects you change can occur at any level (e.g., lexical analysis, parsing, semantic analysis, runtime, language features, etc.). Possible problems include: 1. dynamic type makes it hard to maintain 2. inserting into middle of an array makes it less efficient 3. All the special variables makes it hard to learn 4. The multiple ways to do the same thing makes it hard to maintain How to fix them varies 5

7. (6 points) Algol family languages are typically compiled, while Lisp family languages, where many issues cannot be settled until run time, are typically interpreted. Is interpretation simply what one has to do when compilation is infeasible, or are there actually some advantages to interpreting a language, even when a compiler is available? Explain. Coding in an interpreted environment can be easier to develop with as the errors are shown immediately. 8. (8 points) Discuss the reasons why languages such as Fortran, Algol, and PL/I designed in 1950s and 1960s are less widely used than languages designed in the last 20 years. Actually, there are a lot of reasons. Algol didn't have standardized I/O. Pl/1 was too big. Fortran didn't have recursion or objects. The old languages don't take advantage of new machines or needs of new applications. 6