Chapter 6 Programming Languages 2 Introduction to CS
























- Slides: 24
Chapter 6 Programming Languages (2) Introduction to CS 1 st Semester, 2011 Sanghyun Park
Outline ± ± ± ± Historical Perspective Traditional Programming Concepts Procedural Units Language Implementation (covered) Object-Oriented Programming Concurrent Activities Declarative Programming (skip)
Procedures ± A ____ is a set of instructions for performing a task that can be used as an abstract tool by other program units ± ____ is transferred to the procedure at the time its services are required ± Control is returned to the original program unit after the procedure ____ ± A procedure can be regarded as a _____ program
Flow of Control
Variables and Parameters ± Local variables ® Variables declared ______ a procedure ® ______ to that procedure only ± Global variables ® Variables not ____ to a particular part of a program ® Accessible _____ the program ± Parameters ® Variables that are passed to ______ ® Formal parameters and ______ parameters
Procedures: Example
Parameter Passing ± Pass by ________ ± There are pros and cons with each
Pass By Value ± A ____ of the data represented by the actual parameters is produced and given to the procedure ± Any alterations to the data made by the procedure are reflected only in the _____ --the data in the calling program unit are ______ changed ± Therefore, this model is ____ ± If the parameters represent large blocks of data, this can be ______
Pass By Value: Example
Pass By Reference ± The ____ of the actual parameters is given to the procedure ± The procedure has ____ access of the data represented by the actual parameters ± This is more ____ ± This is however more _____
Pass By Reference: Example
Functions ± There are two types of procedures ® Those that DO NOT return a value (sometimes called _____) ® Those that DO return a value (usually called _____)
Language Implementation ± ± ± The process of converting a program from one language to another is called _____ The program in its original form is the _______ program; the translated version is the ____ program Translation process
Lexical Analyzer ± Reads the source program symbol by symbol, ____ which groups of symbols represent single units, and _____ those units (numeric values, words, …) ± As each unit is classified, the lexical analyzer generates a bit pattern known as a ______ ± Therefore, the lexical analyzer converts the program (a stream of _______) into a stream of _______
Parser ± Identifies the ______ structure of the program and recognizes the ____ of each component ± Based on a set of syntax rules (= _____) that define the syntax of the programming language ± A ______ tree is used to show that a stream of tokens conforms to the grammar
Syntax Diagram: Example 1
Syntax Diagram: Example 2
Parse Tree for x + y x z
Ambiguity ± A parse tree represents the parser’s _______ of the program’s grammatical composition ± The grammar is _____ if it is possible to have more than one parse tree for one string ± Note that the following statement leads to ambiguity: if B 1 then if B 2 then S 1 else S 2 (____ Else)
Parse Tree 1 for Dangling Else
Parse Tree 2 for Dangling Else
Dangling Else Resolution ± _____ close every if statement, or ± Match every else with the ____ if
Code Generation ± Constructs the ____-language instructions to implement the statements recognized by the parser ± This process involves numerous issue, one being that of producing efficient code (code _____) ± Some optimizations include ® Reduce number of _____ ® Remove _____ and/or useless instructions ® Reduce number of ____
Linking and Loading ± When all steps of translation have been finished, we have a piece of ______ code ± Object code may contain _____ to other object code ± Thus, generally several pieces of object code need to be ______ together to form an executable program ± In order for an executable program to be run, it needs to be loaded into memory by a program called a ______