790 likes | 971 Views
CS 3120 Final Exam Review. 15 short answer questions. Programming Language Concepts. Chapter 1: Preliminaries. Main Topics. Reasons for studying programming languages Programming Domains Language Evaluation Criteria Influences on Language Design Language Categories
E N D
CS 3120 Final Exam Review • 15 short answer questions
Programming Language Concepts Chapter 1: Preliminaries
Main Topics • Reasons for studying programming languages • Programming Domains • Language Evaluation Criteria • Influences on Language Design • Language Categories • Language Design Tradeoffs • Implementation Methods • Programming Environments
Why Study PLC? • Increased capacity to express ideas • Improved background for choosing appropriate languages • Increased ability to learn new languages • Better understanding of the significance of implementation • Increased ability to design new languages • Overall advancement of computing
Increased capacity to express ideas • Programming language constrains • Control structures • Data structures • Abstractions that can be used • Awareness of language features reduces these limitations • Features of one language may be simulated in another • Study of PLC builds appreciation for language features and encourages their use
Language Evaluation Criteria • Readability • Writability • Reliability • Cost
Readability • Overall Simplicity • Control Statements • Data Types and Structures • Syntax Considerations
Writability • Support for abstraction • Process abstraction • Data abstraction • Expressivity • APL has powerful operators that accomplish lots of computation with little coding • for statements for counting loops (instead of while) • and then, or else Boolean operators in Ada
Reliability • Type checking • Subscript ranges: Ada vs. C • Static vs. dynamic type checking • Exception handling • Intercept runtime errors, take action to correct problem, and continue processing • PL/I, C++, Ada, Java • Aliasing • 2 or more ways to reference same memory cell • Possible via pointers, reference parameters, unions
Costs • Training programmers • Writing programs • Compiling programs • Executing programs • Language implementation system • Poor reliability • Maintaining programs
Influences on Language Design • Computer architecture • Imperative languages model von Neumann architecture • Functional programming languages need a non-von Neumann architecture to be implemented efficiently • Programming methodologies • Top-down design, stepwise refinement • Data-oriented vs. process-oriented design • Object-oriented design • Concurrency (process-oriented)
Language Categories • Imperative • Functional • Logic • Object-oriented
Language Design Tradeoffs • Reliability vs. cost of execution • Ada’s runtime type checking adds to execution overhead • Readability vs. writability • C and APL • Flexibility vs. safety • Pascal variant record is a flexible way to view a data object in different ways, but no type checking is done to make it safe
Questions to Ponder • What are language design criteria? • What are some design trade-offs? • What are some language design criteria that are in direct conflict with each other?
Implementation methods • Compilation • Interpretation • Hybrid implementation systems • Java applets are compiled into byte code • Compiled applets are downloaded and interpreted by byte code interpreter
Evolution of Major Programming Languages Chapter 2: Evolution Languages • Good chapter for background language evolution information. • NOT on the final exam!
Lexical and Syntax Analysis Chapter 3 and 4 Describing Syntax and Semantics Lexical and Syntax Analysis
Definitions • Syntax: the form or structure of the expressions, statements, and program units • Semantics: the meaning of the expressions, statements, and program units • Sentence: a string of characters over some alphabet • Language: a set of sentences • Lexeme: the lowest level syntactic unit of a language (e.g., *, sum, begin) • Token: a category of lexemes (e.g., identifier)
Describing Syntax • Recognizers: used by compilers • A grammar is used to describe the syntax of a language. • A context-free grammar can be used to develop language translators.
Context-Free Grammar • A context-free grammar is one whose productions take the form A , where A is a single non-terminal symbol and is any string of terminals and/or non-terminals. • Context-free grammars are used to describe the syntax of modern programming languages.
Derivation • A derivation is a repeated application of rules, starting with the start symbol and ending with a sentence. In each step of a derivation, exactly one non-terminal is expanded. • Every string of symbols in a derivation is a sentential form. A sentential form may contain terminal and non-terminal symbols. • A sentence is a sentential form that has only terminal symbols.
Parse Trees • Grammars describe the hierarchical syntactic structure of sentences in the language they define • These hierarchical structures are called parse trees. • Every internal node is labeled with a non-terminal symbol • Every leaf is labeled with a terminal symbol
Questions to Ponder • What’s syntax and semantics? • What’s syntax analysis? • What’s lexical analysis? • What’s a lexeme and a token? • What are grammars used for? • No graphs or parse trees on the final!
Naming • Naming is the process by which the programmer associates a name with a potentially complicated program fragment • The goal is to hide complexity • Programming languages use name to designate variables, types, classes, methods, operators,… • Naming provides abstraction • E.g. Mathematics is all about the formal notation (i.e. naming) that lets us explore more and more abstract concepts
Variable Names Design issues - What should the maximum length be? - Are connector characters allowed? - Are names case sensitive? - Are special words reserved words or keywords? Length - FORTRAN I: maximum 6 - COBOL: maximum 30 - FORTRAN 90 and ANSI C: maximum 31 - Ada: no limit, and all are significant - C++: no limit, but implementers often impose one
Variable Names • Case sensitivity • Disadvantage: readability (names that look alike are different) • C, C++, Java, and Modula-2 names are case sensitive • The names in other languages are not • Special words • A keywordis a word that is special only in certain contexts • Disadvantage: poor readability • A reserved word is a special word that cannot be used as a user-defined name
Variable Names • A variableis an abstraction of a memory cell • Variables can be characterized as a collection of attributes: • name, address, value, type, lifetime, and scope • Name - not all variables have them • Address - the memory address with which it is associated • A variable may have different addresses at different times during execution • A variable may have different addresses at different places in a program • If two variable names can be used to access the same memory location, they are called aliases
Attributes of variables • Name—not all variables have a name • Address • The memory address with which a variable is associated • A variable may have different addresses at different times • Two names that can be used to access the same memory location are called aliases
Attributes of variables • Type • Determines the range of values of variables and the set of operations defined for those values • Value • The contents of the location with which a variable is associated
Some definitions • Binding—an association, such as • between an attribute and an entity, or • between an operation and a symbol • Binding time—the time at which a binding takes place
Binding times • Compile time • Example: binding of a variable to a type in C • Load time • Example: binding of a C static variable to a memory cell • Run time • Example: binding of a non-static local variable to a memory cell
More on binding • A binding is static if it • occurs before run time and • remains unchanged throughout execution • A binding is dynamic if it • Occurs during execution, or • Can change during execution
Scope • The scope of a variable is the range of statements over which it is visible • The non-local variables of a program unit are those that are visible but not declared there • The scope rules of a language determine how references to names are associated with variables • Scope may be static or dynamic
Static scope • Based on the text of a program • To connect a name reference to a variable, the compiler must find the declaration • Search process: search declarations, first locally, then in increasingly larger enclosing scopes, until one is found for the given name.
Dynamic scope • Based on calling sequences of program units, not their textual layout • References to variables are connected to declarations by searching back through the chain of subprogram calls that forced execution to this point
Scope vs. lifetime • Scope and lifetime are sometimes closely related, but are different concepts. • Consider a static variable in a C or C++ function: void fun() { static int x = 0; int y = 0; …} • Scope of x and y is from point of declaration to end of function block • Lifetime of x is entire execution of program • Lifetime of y is during execution of fun()
Scope and Blocks • Blocks - a method of creating static scopes inside program units. Example: • C and C++: • for (...) { • int index; • ... • }
Variable initialization • The binding of a variable to a value at the time it is bound to storage is called initialization • Initialization is often done on the declaration statement: • C++:float sum = 0.0;
Type Checking Type checking is the activity of ensuring that the operands of an operator are of compatible types A compatible type is one that is either legal for the operator, or is allowed under language rules to be implicitly converted, by compiler-generated code, to a legal type. Atype error is the application of an operator to an operand of an inappropriate type A programming language is strongly typed if type errors are always detected
Strong Typing Allows the detection of the misuses of variables that result in type errors. C and C++ don’t have strong typing: parameter type checking can be avoided; unions are not type checked (Java is similar)
Questions to Ponder • What are some design issues for names? • What is static and dynamic scoping? • What is type checking? • What’s strong typing?
Chapters 6 and 8(no 7 on exam) Data Types Statement-Level Control Structures
Abstract Data Types Abstract Data Type - the use of type is separated from the representation and operations on values of that type
Ordinal Types ( user defined ) An ordinal type is one in which the range of possible values can be easily associated with the set of positive integers. Enumeration Types - one in which the user enumerates all of the possible values, which are symbolic constants • Design Issue: Should a symbolic constant be allowed to be in more than one type definition?
Arrays • Anarrayis an aggregate of homogeneous data elements in which an individual element is identified by its position in the aggregate, relative to the first element. • Specific element of an array is identified by: i) Aggregate name; ii) Index (subscript): position relative to the first element.
Design Issues for Arrays 1. What types are legal for subscripts? 2. Are subscripting expressions in element references range checked? 3. When are subscript ranges bound? 4. When does allocation take place? 5. What is the maximum number of subscripts? 6. Can array objects be initialized?
Pointers Apointer type is a type in which the range of values consists of memory addresses and a special value, null. The value null indicates that a pointer cannot currently be used to reference another object. Design Issues: 1. What is the scope and lifetime of pointer variables? 2. What is the lifetime of heap-dynamic variables? 3. Are pointers restricted to pointing at a particular type? 4. Are pointers used for dynamic storage management, indirect addressing, or both? 5. Should a language support pointer types, reference types, or both?
Fundamental Pointer Operations 1. Assignment: Sets a pointer variable to the address of some object. 2. References: Obtaining the value of the memory cell whose address is in the memory cell to which the pointer variable is bound to. In C and C++, dereferencing is specified by prefixing a identifier of a pointer type by the dereferencing operator (*).
Problems with pointers 1. Dangling pointers (dangerous) - A pointer points to a heap-dynamic variable that has been de-allocated 2. Memory Leak (wasteful) - A heap dynamic variable that is no longer referenced by any program pointer