11.07.2015 Views

Encyclopedia of Computer Science and Technology

Encyclopedia of Computer Science and Technology

Encyclopedia of Computer Science and Technology

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

362 PascalLevine, John R., Tony Mason, <strong>and</strong> Doug Brown. lex & yacc. Sebastapol,Calif.: O’Reilly, 1995.Louden, Kenneth C. Compiler Construction: Principles <strong>and</strong> Practice.Boston: PWS Publishing, 1997.Metsker, Steven John. Building Parsers with Java. Boston: Pearson/Addison-Wesley, 2001.PascalBy the early 1960s, computer scientists had become increasinglyconcerned with finding ways to better organize orstructure programs. Indeed, one language (see Algol) hadalready been developed in part to demonstrate <strong>and</strong> encouragesound programming practices, including the proper use<strong>of</strong> control structures (see loop <strong>and</strong> structured programming).However, Algol lacked a full range <strong>of</strong> data types<strong>and</strong> other features needed for practical programming, whilearguably being too complex <strong>and</strong> inconsistent to serve as agood teaching language.Niklaus Wirth at ETH (the Swiss Federal Institute <strong>of</strong><strong>Technology</strong>) worked during the mid-1960s with a committeethat was trying to overcome the problems with Algol<strong>and</strong> make the language more practical <strong>and</strong> attractive tocomputer manufacturers <strong>and</strong> users. However, Wirth graduallybecame disillusioned with the committee’s unwieldyresults, <strong>and</strong> proceeded to develop a new language, Pascal,announcing its specifications in 1970.Pascal both streamlined Algol <strong>and</strong> extended it. Besidesproviding support for character, Boolean, <strong>and</strong> set data types,Pascal allows users to define new data types by combiningthe built-in types. This feature is particularly useful fordefining a “record” type that, for example, might combinean employee’s name <strong>and</strong> job title (characters), ID number (along integer), <strong>and</strong> salary (a floating-point number). The rigoroususe <strong>of</strong> data types also extends to the way proceduresare called <strong>and</strong> defined (see procedures <strong>and</strong> functions).Pascal attracted much interest among computer scientists<strong>and</strong> educators by providing a well-defined languagein which algorithms could be expressed succinctly. Theacceptance <strong>of</strong> Pascal was also aided by its innovative compilerdesign. Unlike the machine-specific compilers <strong>of</strong> thetime, the Pascal compiler did not directly create machinecode. Rather, its output was “P-code,” a sort <strong>of</strong> abstractmachine language (see also pseudocode). A run-time systemwritten for each computer interprets the P-Code <strong>and</strong>executes the appropriate machine instructions. This meantthat Pascal compilers could be “ported” to a particularmodel <strong>of</strong> computer simply by writing a P-Code Interpreterfor that machine. This strategy would be used more thantwo decades later by the creators <strong>of</strong> a popular language forWeb applications (see Java).Structure <strong>of</strong> a Pascal ProgramThe following simple program illustrates the basic structure<strong>of</strong> a program in Pascal. (The words in bold type are keywordsused to structure the program.) The program beginswith a Type section that declares user-defined data types.These can include arrays, sets, <strong>and</strong> records (composite typesthat can include several different basic types <strong>of</strong> data). Herean array <strong>of</strong> up to 10 integers (whole numbers) is defined as atype called IntList.The Var (variable) section then declares specific variablesto be used by the program. Variables can be definedusing either the language’s built-in types (such as integer)or types previously defined in the Type section. An importantcharacteristic <strong>of</strong> Pascal is that user-defined types mustbe defined before they can be used in variable declarations,<strong>and</strong> variables in turn must be declared before they can beused in the program. Some programmers found this strictnessto be confining, but it guards against, for example, atypographical error introducing an undefined variable inplace <strong>of</strong> the one intended. Today most languages enforcethe declaration <strong>of</strong> variables before use.The word begin introduces the executable part <strong>of</strong> theprogram. The variables needed for the loop are first initializedby assigning them a value <strong>of</strong> zero. Note that in Pascal :=(colon <strong>and</strong> equals sign) is used to assign values. The outer ifstatement is used to ensure that the user does not input aninvalid number <strong>of</strong> items. The for loop then reads each inputvalue, assigns it to its place in the array, <strong>and</strong> keeps a runningtotal. That total is then used to compute the average,which is output by the writeln (write line) statement.program FindAvg (input, output);type IntList = array [1 . . 10] <strong>of</strong> integer;varInts: IntList;Items, Count, Total, Average: integer;beginAverage := 0;Total := 0;Readln (Items);If ((Items > 0) <strong>and</strong> (Items

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!