Home

PL/0 User`s Manual

image

Contents

1. You tried to assign a value to a variable using 2 must be followed by a number Syntax error near constant declarations or in a conditional expression 3 Identifier must be followed by Syntax error near constant declarations 4 const int procedure must be followed by identifier Syntax error near constant variable or procedure declarations 5 Semicolon or comma missing You missed a semicolon or a comma somewhere Also check that you aren t adding extra semicolons to if then else and while do s 6 Incorrect symbol after procedure declaration Not currently used 7 Statement expected Not currently used 8 Incorrect symbol after statement part in block Not currently used 9 Period expected Missed a period at the end of the program 10 Semicolon between statements missing Except for the last one in a block every statement needs to end with a semicolon 11 Undeclared identifier You tried to use an undeclared constant variable or procedure or you tried to access something that is outside of your scope 12 Assignment to constant or procedure is not allowed You tried to assign a value to a constant or a procedure Check your variable names 13 Assignment operator expected You began a statement with an identifier but it wasn t followed by an assignment operator 14 call must be followed by an identifier You used call but you didn t include the procedure name 15 Call of a constant or variable
2. constant you may use them throughout your program as you would an ordinary number The compiler converts constants to their respective values upon compilation so the following example foo FOO BAR is equivalent to this 1 1 2 Integers Integers are mutable meaning you can define and reassign their values later on Integers are declared at the top of the file after any constants if there are any and you may declare more than one at a time Integers are defined with values as constants are Instead you must assign integers values using the assignment operator as seen above Here is an example of how to declare integers in PL 0 int foo bar baz You may assign variables values of constants or other variables number literals or expressions see section 1 2 2 1 1 3 Procedures Procedures can be thought of as subroutines or sub programs They contain nearly everything that a program could contain Please be aware that this version of PL O currently supports up to 10 levels of nested procedures 1 Main 10 Procedures Figure 2 shows a procedure that prints the numbers 1 through 10 Bere aie yet A ake Ty I AR eee EAR AE yee ie uty ee Ae CY ke AR ey ae et A A ey Se RE ey Se procedure count10 int a begin a 1 while a lt 10 do begin out a a a 1 end end E A n Gate OS PE E n E a oo ee E ee A LRA E P See As S aA ott 65 RRR nee Ry N a r Sed Figure 2 Procedure that counts 1 to 10 2 1 0 Pr
3. the same set of l o E l instructions until some condition is false if op ADD then c li add From the same four function calculator else if op SUB then call sub program used above Figure 3 shows an else if op MULT then call mult example of a while loop else if op DIV then call div else done 1 end K Jee SN Ee es TR LA E A A a a Mr es B R ee L HT LIT A JO a 1 ee eS ak D B Figure 3 while loop example 4 1 0 Programming in PL 0 1 3 6 Calling Procedures To invoke a procedure use the call keyword There is no way to explicitly pass arguments to a procedure However procedures have access to any variables and procedures that are declared within their scope For instance Figure 4 right shows the complete source code for the calculator program Notice that the procedures are able to use the x y and done variables because they were declared within their scope Using the Basic Calculator Program First the program will ask the user to input the first value x Next the program will ask the user to input an operation Op This can be any one of 1 2 3 or 4 add subtract multiply or divide respectively Inputting anything else will cause the program to exit Then the program will ask the user to input the second value y Finally the program will call the procedure corresponding to op This will assign a new value to x and will also output this value The program returns to asking the user t
4. PL O User s Manual Author Adam Dunson Last updated 14 April 2012 PL O User s Manual Table of Contents DURAN STT TE DHT 1 TED DALAT AIE AA A EA A T E AA S 1 S LMSC Ta E 1 EET T A E E E E E S 2 DIZ INUC A E E EE EE E A E E 2 PES POCO OE CS asi daca caus e EE TEE EE ER a aoa e AAA E aA AEE 2 ded Da2 EEO a N A AA E E 3 1 3 Statements Linssasvsssussnsiite case donvaeeniwivastsadssasveceesbuentnevasiepaiegnaysdaadaesniSeseseund sass iawn esducatebtesenesdvaussauddeccomsababiaevsvedscsaveests 3 BAA Minuit OU Ube TT 3 Dee US CNS TTT 3 E R e CA at aT AAEE EAEE EE AEE EE 3 Re ST E 3 L329 LOOPS vesscissseasassvsvenstetesvanessstocsssausvosesustetoasavarsvsensecacepoeasdedessudesersvdeiebepucscseounsdedadeosvabedosvessr sdocasedesbersevesetebesbeas 4 1 3 6 Calling Pro edures innnan seks vati ateees a an E aaa aAA 5 tA Adyanced Examples nnne a iaaa a aaiae aeeiiaii 6 RRR RECURSIVE Proc dures irnn aaeeea EEE aiae ietie eiia EEE r aaa E aiaee 6 TAZ Nested DOG CCS re a A a E a a Aaaa RE 6 2 0 Compiling and Executing Programs Written in PL 0 csssssssssscsssssesssssssesssssssecssscsssssssssssesssesssesssscssscsescassees 8 ZA Bustle OSC OMI TTT 8 2 2 Running PL 0 Programs with pl0 compiler cssssssesscsssssssessssssssssssssssssssssssssesssssssssssssesssesssesssssssssseseseees 8 BO ROTC LE aTe a E EEE EE E E Mecca Nas 88 Ea 9 Bs DIE OLE INI eT 9 3 2 Complete List of Reserved Words and Tokene sssssscssscecesssssssecs
5. add else if op SUB then call sub else if op MULT then call mult else if op DIV then call div else done 1 if done then out x end begin done 0 in x while done do begin in op if op lt 1 then done 1 else if op gt 4 then done 1 if done then begin in y call calculate end end 1 0 Programming in PL 0 calculator program using nested procedures PL O User s Manual 2 0 Compiling and Executing Programs Written in PL O The p10 compiler program is both a compiler and a virtual machine for PM 0 the machine for which the PL 0 ISA was designed 2 1 Building p10 compiler These instructions assume you have experience using a terminal You will need GCC and GNU Make prior to building p10 compiler To build the compiler s executable file do the following 1 Obtain a copy of the source code for p10 compiler 2 Open a terminal or command prompt and navigate to the project directory You should see a file called README text and another called Makefile 3 Runmake e This will output a file called p10 compiler or p10 compiler exe into the bin directory Once you have an executable p10 compiler you are ready to run PL O programs 2 2 Running PL 0 Programs with p10 compiler To begin open a terminal or command prompt and navigate to wherever your p10 compiler is located The default mode is to display only in out calls from the PL O program To do this run the followin
6. g p10 compiler path to your file If you want to see more output there are three command line flags available that you can use e The 1 flag instructs p10 compiler to display the internal representation of the PL 0 program That is it displays the token file including both a raw and a pretty format The a flag instructs p10 compiler to display the generated assembly code in both a raw and pretty format e The v flag instructs p10 compiler to display a stack trace while the virtual machine executes your program These flags may be used in any combination for more or less output To use more than one flag you can run something like this pl10 compiler l a v path to your file or else like this pl0 compiler lav path to your file The only restriction is that the filename of your PL 0 program must be the last argument 8 2 0 Compiling and Executing Programs Written in PL 0 PL O User s Manual 3 0 Reference 3 1 PL O EBNF Grammar S E INS ER SIAR St MS ep ee LH Nai es Se Nah Se Se St ep IR Ok Ge EE LRAT ey EE St He wt ge dete BAS Sg ae Ae wh Fp NA T opt Si ast ete itd IE Ese ee Sent Sot T program block block const declaration var declaration procedure declaration statement const declaration const ident number ident number var declaration int ident ident procedure declaration procedure ident block statement ident e
7. is meaningless You tried to call a constant or a variable which is meaningless 16 then expected if condition must be followed by then statement 17 Semicolon or end expected Not currently used 3 0 Reference 11 18 19 20 21 22 23 24 25 26 27 28 29 do expected PL O User s Manual while condition must be followed by do statement Incorrect symbol following statement Not currently used Relational operator expected In a conditional expression you are missing a relational operator Expression must not contain a procedure identifier You cannot use procedures in expressions since they do not return or represent values Right parenthesis missing Missing the right parenthesis at the end of a factor The preceding factor cannot begin with this symbol There is something wrong with a factor used in an expression An expression cannot begin with this symbol Not currently used This number is too large Code generator exceeded the maximum number of lines of code out must be followed by an expression You used out but didn t specify anything to output in must be followed by an identifier You used in but you didn t specify what variable to assign it to Cannot reuse this symbol here Not currently used Cannot redefine constants Constants cannot be redefined Table 2 Error codes 3 4 PL O Instruction Set Architecture Al
8. l PL O instructions are of the form OP L M where OP is the op code L is the lexicographical level and M is an address data or an ALU operation Op Code 1 2 12 Syntax LIT 0 M OPR 0 M OPR 0 0 OPR 0 1 OPR 0 2 OPR 0 3 OPR 0 4 OPR 0 5 Description Push constant value literal M onto the stack Operation to be performed on the data at the top of the stack Return used to return to the caller from a procedure Negation pop the stack and return the negative of the value Addition pop two values from the stack add and push the sum Subtraction pop two values from the stack subtract second from first and push the difference Multiplication pop two values from the stack multiply and push the product Division pop two values from the stack divide second by first and push the quotient 3 0 Reference 10 OPR 0 6 OPR 0 7 OPR 0 8 OPR 0 9 OPR 0 10 OPR 0 11 OPR 0 12 OPR 0 13 LOD L M STO L M CAL L M INC 0 M JMP 0 M JPC 0 M SIO 0 1 SIO 0 2 PL 0 User s Manual Is odd divisible by two pop the stack and push 1 if odd 0 if even Modulus pop two values from the stack divide second by first and push the remainder Equality pop two values from the stack and push 1 if equal 0 if not Inequality pop two values from the stack and push 0 if equal 1 if not Less than pop two values from the stack and push 1 if first is less than second 0 if no
9. limiters e g and will be ignored Finally PL O programs must end with a period Figure 1 shows an example of a simple PL 0 program with comments RR E ee ee ee AE int foo begin foo here is a comment 1 comments can span multiple lines out foo Figure 1 Program with comments 1 1 Datatypes Currently this implementation of PL 0 supports the following datatypes e constants const e integers int e procedures procedure An identifier is used to refer to specific instances of each datatype Identifiers must be no more than 11 characters in length must begin with a character may contain uppercase and lowercase letters as well as numbers and must not be any of the reserved keywords listed in Appendix A In addition to identifiers number literals are used through the program for arithmetic and other operations Number literals must be integers and must be no more than 5 digits long We do not currently support negative number literals 1 0 Programming in PL 0 1 PL O User s Manual 1 1 1 Constants Constants are integer types They may only be defined once per program Constants are immutable that is you may not assign values to them after they have been defined You may define more than one constant at a time by separating the identifiers by commas Constant definitions must end with a semicolon Constants are defined using the following syntax const FOO 1 BAR 2 After you define a
10. n lt then f 1 else if n then f 1 else call fact f f ans1 end begin in n call fact out f end Figure 5 Recursive procedure example 1 4 2 Nested Procedures Nested procedures give the programmer more control over where procedures may be called from Figure 6 on the next page is a modified version of the basic four function calculator program example from before Notice that the main block now calls calculate which in turn uses op to determine which of the nested procedures to call Be aware that you cannot call add sub mult or div from inside the main block as you could before Finally notice that mult now calls add this is to demonstrate scope The variable c cannot be accessed outside of mult due to scope 6 1 0 Programming in PL 0 PL O User s Manual TSI Oe GPE ey ee ety TE E NSS Sy Oe ig ee CE T ae eh ae Mere eat Seah Se ey RTE ae NE EY em EP A ny TS aS Sp My GLY T Ge a aE Figure 6 Modified const ADD 1 SUB 2 MULT 3 DIV 4 int op x y done procedure calculate procedure add begin x i X y end procedure sub begin x LS X y end procedure mult int c begin c y 1 y Xx resets y argument for calling add while c gt 0 do begin call add c c 1 end old method x x y end procedure div begin check for divide by zero errors if y lt gt 0 then begin X1 xX y end else done 1 end begin if op ADD then call
11. o input an operation It will continue this process until the user inputs any value other than 1 2 3 or 4 for the op PL O User s Manual EE aT E A A ER LEI AER RAE E E AE U eR N const ADD 1 SUB 2 MULT 3 DIV 4 int op x y done procedure add begin x i X y out x end procedure sub begin x i X y out x end procedure mult begin X 1 X y out x end procedure div begin check for divide by zero errors if y lt gt 0 then begin X1 xX y out x end else done 1 end begin done 0 in x while done 0 do begin in op if op lt 1 then done 1 else if op gt 4 then done 1 if done then begin in y if op ADD then call add else if op SUB then call sub else if op MULT then call mult else if op DIV then call div else done 1 end end Figure 4 Complete source code for basic four function calculator program in PL 0 1 0 Programming in PL 0 PL O User s Manual 1 4 Advanced Examples PL O supports recursive and nested procedures Nested procedures introduce some nuances into the concept of scope that might be less than obvious 1 4 1 Recursive Procedures Figure 5 shows a program that uses a recursive procedure to calculate the factorial of a user input integer K R A wa R See R IRE GIS EM nr EN i MEN SW Baye TR LM M ET A 0 TT T 9 LRE E 9 LTR EY ee TA 0 int f n procedure fact int ans1 begin ansi n n n 1 if
12. ogramming in PL 0 PL O User s Manual 1 2 Expressions Expressions get their name from mathematical expressions which represent or return a value Expressions can be composed of constant or variable identifiers number literals or the arithmetic symbols and PL 0 follows the standard order of operations when calculating the value of an expression 1 3 Statements Statements are how the program gets things done Except for the last statement in a block statements must end with a semicolon 1 3 1 Input Output Input is handled by using the in keyword followed by a variable identifier you cannot use a constant or procedure identifier e g in foo This will assign whatever value the user inputs to foo Output is handled by using the out keyword followed by a constant or variable identifier a number literal or an expression Here are a few examples out foo out 42 out 1 2 3 4 1 3 2 Blocks Blocks are collections of statements each of which are separated by a semicolon Blocks are denoted by the begin and end keywords See section 1 1 3 Procedures for how a block can be nested inside of other statements 1 3 3 Assignment As mentioned before variables can be assigned values by using constant or variable identifiers number literals or expressions The assignment operator only works for variables inside of a statement Currently you are not able to assign variables initial values at
13. ssssssssscssscecscssecesesssesecesssssssssssescscscscececess 9 BS BUY Ot COC T 11 3 4 PL 0 Instruction Set Architecture esse seene 12 Table of Contents iii PL O User s Manual Index of Figures Figure 1 Program with comments sss esse eee eeee eee eee ereenn enen 1 Figure 2 Procedure that counts 1 0 10 iscsccscccscecatsnsusaciavivssasesscuescdvcecceatstievididasstuaviecuasssavecavecedsinedlenaavtainannan anaes 2 Figtibe Se while loop TT 4 Figure 4 Complete source code for basic four function calculator program in PLG sese 5 Figure 5 Recursive procedure example ss seesseeee eers ereenn nenen 6 Figure 6 Modified calculator program using nested procedures sss sss sees sees 7 Fig re 7 PL OEBNE Grama P 9 Index of Tables Table 1 Complete List of Reserved Words and TokKens sss sss 10 Table TT eSa aa a a a a a a a a oaae ee aiea a aai i adai 12 Table 3 PL 0 Instruction Set Architecture sss sees seene 13 iv Tables and Figures PL O User s Manual 1 0 Programming in PL 0 PL 0 is a fairly simple language that supports constants integers and procedures PL 0 programs have the following structure 1 constant definitions 2 variable declarations 3 procedure declarations a subroutine definition same as program structure 4 statements For the most part whitespace is ignored except in certain cases in order to differentiate between reserved keywords and identifiers Additionally anything between comment de
14. sym callsym constsym intsym procsym outsym insym elsesym PL O User s Manual 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 checking that two expressions are not equal checking that the left expression is less than the right expression checking that the left expression is less than or equal to the right expression checking that the left expression is greater than the right expression checking that the left expression is greater than or equal to the right expression begin a factor end a factor separate constant variable identifiers in their respective declarations end statements end of program variable assignments begin a block of statements end a block of statements begin an if then statement followed by a condition part of if then followed by a statement begin while loop followed by a condition part of while loop followed by a statement calls a procedure begin constant declarations begin integer declarations begin a procedure declaration output the value of an expression ask the user to input a value and assign it to a variable optionally follows if then statements 10 Table 1 Complete List of Reserved Words and Tokens 3 0 Reference PL O User s Manual 3 3 Error Codes Error Error Message Comments Suggestions Number 0 No errors program is syntactically correct N A 1 Use instead of
15. t Less than or equal to pop two values from the stack and push 1 if first is less than or equal second 0 if not Greater than pop two values from the stack and push 1 if first is greater than second 0 if not Greater than or equal to pop two values from the stack and push 1 if first is greater than or equal second 0 if not Load value to top of stack from the stack location at offset M from L lexicographical levels down Store value at top of stack in the stack location at offset M from L lexicographical levels down Call procedure at code index M Increment the stack pointer by M allocate M locals by convention this is used as the first instruction of a procedure and will allocate space for the Static Link SL Dynamic Link DL and Return Address RA of an activation record Jump to instruction M Pop the top of the stack and jump to instruction M if it is equal to zero Start I O pop the top of the stack and output the value Start I O read input and push it onto the stack Table 3 PL O Instruction Set Architecture 3 0 Reference 13
16. the time of declaration 1 3 4 Conditionals To conditionally execute code use the conditional keywords if then and else These allow you to check a condition and if true execute some code or else execute some other code A conditional expression can either be two expressions separated by a relational operator e g expression rel op expression or else using the unary odd keyword odd x y The odd keyword will return true if the expression evaluates to an odd number or else it will return false if the expression evaluates to an even number 1 0 Programming in PL 0 3 PL O User s Manual Valid relational operations are as follows e equal e lt gt not equal e lt less than e lt less than or equal e gt greater than e gt greater than or equal to Here is an example of an if then without an else if 1 1 then out 1 And with an else if 1 lt gt 1 then out 1 else out 0 Conditionals can be nested in one another One such use is checking multiple conditions before executing code For example here s a snippet from a basic four function calculator program if op ADD then call add else if op SUB then call sub else if op MULT then call mult else if op DIV then call div else done 1 1 3 5 Loops Loops are another useful construct delimited Oe tee ee TT by the while and do keywords Often you while done do will need to iterate over a range ofnumbers Begin or perhaps to perform
17. xpression call ident begin statement statement end if condition then statement else statement while condition do statement read ident write expression e v condition odd expression expression rel op expression rel op S D u West Well Weal wu Wa F expression term term term factor factor factor ident number expression number digit digit ident letter letter digit digit z Q Wan WOM Wen mq 5 6 Zu vou No letter Wa pl ee y Ww aN B Pree uym uz Figure 7 PL O EBNF Grammar 3 2 Complete List of Reserved Words and Tokens Symbol Internal Name Internal Value Usage nulsym 1 reserved identsym 2 constant variable and procedure identifiers numbersym 3 number literals plussym 4 addition in expressions minussym 5 subtraction in expressions x multsym 6 multiplication in expressions slashsym 7 division in expressions odd oddsym 8 determining if an expression is odd eqlsym 9 constant definitions checking the equality of two expressions 3 0 Reference lt gt begin end if then while do call const int procedure out in else neqsym lessym leqsym gtrsym geqsym lparentsym rparentsym commasym semicolomsym periodsym becomessym beginsym endsym ifsym thensym whilesym do

Download Pdf Manuals

image

Related Search

Related Contents

Lexmark 41G0100 User's Manual  同意する - メルセデス・ベンツ  EDEN : INCIDENCE ET PRÉVALENCE DES AFFECTIONS  User Manual and Technical Description  Mounting and Operating Instructions for the Electronic  型名 GZ-HD40/GZ  Réparation d`appareils de lecture et d`enregistrement  User guide Guide de l'utilisateur    CLUNY - Lacanche  

Copyright © All rights reserved.
Failed to retrieve file