310 likes | 579 Views
Grammer conclusion, Variables, Reading, and Quiz returning. Reading assignment. For a week from today, read Chapter 5 in your textbook and answer the questions online The questions will ask you to investigate new languages that you haven't gotten experience with yet
E N D
Reading assignment • For a week from today, read Chapter 5 in your textbook and answer the questions online • The questions will ask you to investigate new languages that you haven't gotten experience with yet • You will need to do some investigation of your own online • Sources you use should be cited. Please list the URLs of websites that you use. There's no need to go full MLA.
Left factoring • If a nonterminal has a number of rules that all have the same prefix, we can separate that prefix out, putting the suffix into a new nonterminal
<lhs> → ID | ID LBRACKET INT RBRACKET <lhs> → ID <lhstail> <lhstail> → LBRACKET INT RBRACKET | empty
<assign> → <lhs> EQUALS <expr> <lhs> → ID | ID LBRACKET INT RBRACKET <expr> → ID PLUS ID | ID MINUS ID | ID
Variables, names, bindings, and scopes • A name in a program is a sequence of characters that identify something in a program • In C-style languages, names match the following regexp: • [a-zA-Z_][a-zA-Z0-9_]* • Names can be either case-sensitive or case-insensitive • Names can refer to many different things
Name uses • Names can refer to many things, including: • Variables • Functions • Classes • Keywords • Reserved words
Keywords vs. reserved words • Both keywords and reserved words are names that have special meaning to a programming language • Difference: • Keywords can be redefined • Reserved words can't be redefined
ABSENT ABSTRACT ACCEPT ACCESS ACQUIRE ACTIVE-CLASS ACTUAL ADD ADDRESS ADVANCING AFP-5A AFTER ALL ALLOCATE ALLOW ALPHABET ALPHABETIC ALPHABETIC-LOWER ALPHABETIC-UPPER ALPHANUMERIC ALPHANUMERIC-EDITED ALSO ALTER ALTERNATE AND ANY APPLY ARE AREA AREAS AS ASCENDING ASSIGN AT AUTHOR AUTO-HYPHEN-SKIP AUTO-SKIP AUTOMATIC B-AND B-EXOR B-LEFT B-NOT B-OR B-RIGHT B-XOR BACKWARD BASED BASIS BEEP BEFORE BEGINNING BINARY BINARY-CHAR BINARY-DOUBLE BINARY-LONG BINARY-SHORT BIT BLANK BLOCK BOOLEAN BOTTOM BROWSING BY C01 C02 C03 C04 C05 C06 C07 C08 C09 C10 C11 C12 CALL CALL-CONVENTION CALLED CANCEL CBL CBL-CTR CD CF CH CHAIN CHAINING CHANGED CHARACTER CHARACTERS CHECKING CLASS CLASS-CONTROL CLASS-ID CLASS-OBJECT CLOCK-UNITS CLOSE COBOL CODE CODE-SET COERCION COL COLLATING COLS COLUMN COLUMNS COM-REG COMMA COMMIT COMMITMENT COMMON COMMUNICATION COMP COMP-0 COMP-1 COMP-2 COMP-3 COMP-4 COMP-5 COMP-6 COMP-X COMPUTATIONAL COMPUTATIONAL-0 COMPUTATIONAL-1 COMPUTATIONAL-2 COMPUTATIONAL-3 COMPUTATIONAL-4 COMPUTATIONAL-5 COMPUTATIONAL-6 COMPUTATIONAL-X COMPUTE CONFIGURATION CONSOLE CONSTANT CONTAINS CONTENT CONTINUE CONTROL CONTROL-AREA CONTROLS CONVERT CONVERTING COPY CORE-INDEX CORR CORRESPONDING COUNT CREATING CRT CRT-UNDER CSP CURRENCY CURRENT-DATE CURSOR CYL-INDEX CYL-OVERFLOW DATA DATABASE-KEY DATABASE-KEY-LONG DATE DATE-COMPILED DATE-WRITTEN DAY DAY-OF-WEEK DBCS DE DEBUG DEBUG-CONTENTS DEBUG-ITEM DEBUG-LINE DEBUG-NAME DEBUG-SUB-1 DEBUG-SUB-2 DEBUG-SUB-3 DEBUGGING DECIMAL-POINT DECLARATIVES DEFAULT DEFINITION DELETE DELIMITED DELIMITER DEPENDING DESCENDING DESTINATION DETAIL DISABLE DISC DISK DISP DISPLAY DISPLAY-1 DISPLAY-ST DIVIDE DIVISION DOWN DROP DUPLICATES DYNAMIC EBCDIC ECHO EGCS EGI EJECT ELSE EMI EMPTY-CHECK ENABLE END END-ACCEPT END-ADD END-CALL END-CHAIN END-COMPUTE END-DELETE END-DISPLAY END-DIVIDE END-EVALUATE END-IF END-INVOKE END-MULTIPLY END-OF-PAGE END-PERFORM END-READ END-RECEIVE END-RETURN END-REWRITE END-SEARCH END-START END-STRING END-SUBTRACT END-UNSTRING END-WAIT END-WRITE ENDING ENTER ENTRY ENVIRONMENT EOP EQUAL EQUALS ERROR ESCAPE ESI EVALUATE EVENT-POINTER EVERY EXAMINE EXCEEDS EXCEPTION EXCEPTION-OBJECT EXCESS-3 EXCLUSIVE EXEC EXECUTE EXHIBIT EXIT EXTEND EXTENDED EXTENDED-SEARCH EXTERNAL EXTERNALLY- DESCRIBED-KEY FACTORY FALSE FD FH--FCD FH--KEYDEF FILE FILE-CONTROL FILE-ID FILE-LIMIT FILE-LIMITS FILLER FINAL FIRST FIXED FLOAT-EXTENDED FLOAT-LONG FLOAT-SHORT FOOTING FOR FREE FROM FUNCTION FUNCTION-ID GENERATE GIVING GLOBAL GO GOBACK GREATER GRID GROUP HEADING HIGH-VALUE HIGH-VALUES I-O I-O-CONTROL ID IDENTIFICATION IDENTIFIED IF IGNORE IN INDEX INDEXED INDIC INDICATE INDICATOR INDICATORS INHERITING INHERITS INITIAL INITIALIZE INITIATE INPUT INPUT-OUTPUT INSERT INSPECT INSTALLATION INTEGER INTERFACE INTERFACE-ID INTO INVALID INVOKE INVOKED IS JAPANESE JUST JUSTIFIED KANJI KEPT KEY KEYBOARD LABEL LAST LEADING LEAVE LEFT LEFT-JUSTIFY LEFTLINE LENGTH LENGTH-CHECK LESS LIMIT LIMITS LIN LINAGE LINAGE-COUNTER LINE LINE-COUNTER LINES LINKAGE LOCAL-STORAGE LOCK LOCKING LOW-VALUE LOW-VALUES LOWER MASTER-INDEX MEMORY MERGE MESSAGE METACLASS METHOD METHOD-ID MODE MODIFIED MODULES MONITOR-POINTER MORE-LABELS MOVE MULTIPLE MULTIPLY MUTEX-POINTER NAME NAMED NATIONAL NATIONAL-EDITED NATIVE NCHAR NEGATIVE NESTED NEXT NO NO-ECHO NOMINAL NOT NOTE NSTD-REELS NULL NULLS NUMBER NUMBERS NUMERIC NUMERIC-EDITED O-FILL OBJECT OBJECT-COMPUTER OBJECT-ID OBJECT-STORAGE OCCURS OF OFF OMITTED ON OOSTACKPTR OPEN OPTIONAL OPTIONS OR ORDER ORGANIZATION OTHER OTHERWISE OUTPUT OVERFLOW OVERLINE OVERRIDE PACKED-DECIMAL PADDING PAGE PAGE-COUNTER PASSWORD PERFORM PF PH PIC PICTURE PLUS POINTER POS POSITION POSITIONING POSITIVE PRESENT PRINT PRINT-SWITCH PRINTER PRINTER-1 PRINTING PRIOR PRIVATE PROCEDURE PROCEDURE-POINTER PROCEDURES PROCEED PROCESS PROCESSING PROGRAM PROGRAM-ID PROGRAM-POINTER PROMPT PROPERTY PROTECTED PUBLIC PURGE QUEUE QUOTE QUOTES RAISE RAISING RANDOM RANGE RD READ READING READY RECEIVE RECORD RECORD-OVERFLOW RECORDING RECORDS RECURSIVE REDEFINES REDEFINITION REEL REFERENCE REFERENCES RELATIVE RELEASE RELOAD REMAINDER REMARKS REMOVAL RENAMES REORG-CRITERIA REPEATED REPLACE REPLACING REPORT REPORTING REPORTS REPOSITORY REREAD RERUN RESERVE RESERVED RESET RESTRICTED RESUME RETRY RETURN RETURN-CODE RETURNING REVERSED REWIND REWRITE RF RH RIGHT RIGHT-JUSTIFY ROLLBACK ROLLING ROUNDED RUN S01 S02 S03 S04 S05 SAME SCREEN SD SEARCH SECTION SECURITY SEEK SEGMENT SEGMENT-LIMIT SELECT SELECTIVE SELF SELFCLASS SEMAPHORE-POINTER SEND SENTENCE SEPARATE SEQUENCE SEQUENTIAL SERVICE SET SHARING SHIFT-IN SHIFT-OUT SIGN SIZE SKIP1 SKIP2 SKIP3 SORT SORT-CONTROL SORT-CORE-SIZE SORT-FILE-SIZE SORT-MERGE SORT-MESSAGE SORT-MODE-SIZE SORT-OPTION SORT-RETURN SORT-TAPE SORT-TAPES SOURCE SOURCE-COMPUTER SOURCES SPACE SPACE-FILL SPACES SPECIAL-NAMES STANDARD STANDARD-1 STANDARD-2 STANDARD-3 START STARTING STATUS STOP STRING SUB-QUEUE-1 SUB-QUEUE-2 SUB-QUEUE-3 SUBFILE SUBTRACT SUM SUPER SUPPRESS SYMBOLIC SYNC SYNCHRONIZED SYSIN SYSIPT SYSLST SYSOUT SYSPCH SYSPUNCH SYSTEM-DEFAULT TAB TABLE TALLY TALLYING TAPE TAPES TERMINAL TERMINATE TEST TEXT THAN THEN THREAD-LOCAL THREAD-LOCAL-STORAGE THREAD-POINTER THROUGH THRU TIME TIME-OF-DAY TIME-OUT TIMEOUT TIMES TITLE TO TOP TOTALED TOTALING TRACE TRACK-AREA TRACK-LIMIT TRACKS TRAILING TRAILING-SIGN TRANSACTION TRANSFORM TRUE TRY TYPE TYPEDEF UNEQUAL UNIT UNITS UNIVERSAL UNLOCK UNSTRING UNTIL UP UPDATE UPON UPPER UPSI-0 UPSI-1 UPSI-2 UPSI-3 UPSI-4 UPSI-5 UPSI-6 UPSI-7 USAGE USE USER USER-DEFAULT USING VALID VALIDATE VALUE VALUES VARIABLE VARYING WAIT WHEN WHEN-COMPILED WITH WORDS WORKING-STORAGE WRITE WRITE-ONLY WRITE-VERIFY WRITING ZERO ZERO-FILL ZEROES ZEROS
$ SET SOURCEFORMAT"FREE" IDENTIFICATION DIVISION. PROGRAM-ID. Multiplier. AUTHOR. Michael Coughlan. DATA DIVISION. WORKING-STORAGE SECTION. 01 Num1 PIC 9 VALUE ZEROS. 01 Num2 PIC 9 VALUE ZEROS. 01 Result PIC 99 VALUE ZEROS. PROCEDURE DIVISION. DISPLAY "Enter first number (1 digit) : " WITH NO ADVANCING. ACCEPT Num1. DISPLAY "Enter second number (1 digit) : " WITH NO ADVANCING. ACCEPT Num2. MULTIPLY Num1 BY Num2 GIVING Result. DISPLAY "Result is = ", Result. STOP RUN.
Variables • We write our code for a von Neumann architecture • Almost all variables will have the following information: • Name • Address • Type • Value • We'll see what these mean, and then talk about when that information is determined
Variable names • Typically, variable names follow the same rules as any other names in the language • Some restrictions may apply by convention: • first_thing • someThing • AnotherThing • YET_ANOTHER_THING • Some restrictions may apply by the language: • In Haskell, variables must begin with a lowercase letter, and types must begin with a capital letter
Variable addresses • A variable represents information that is stored in a certain place in computer memory • The address of a variable can be referred to as the l-value* • Multiple variables may have the same address (aliasing) • A variable may correspond to multiple addresses
union IntFloat { long i; float f; }; structVariableInfo { IntFloat data; bool isInt; string name; }
union IntFloat { long i; float f; }; structVariableInfo { IntFloat data; bool isInt; string name; } VariableInfovf; vf.data.i = 12;
union IntFloat { long i; float f; }; float FastInvSqrt(float x) { float xhalf = 0.5f * x; IntFloat u; u.f = x; u.i = 0x5f3759df – (u.i >> 1); x = u.i; x = x * (1.5f - (xhalf * x * x)); return x; }
Aliasing • Two important different kinds of aliasing: • Aliasing where all aliases have the same type (and therefore same value) • Aliasing where all aliases may have differing types
Variable Type • A variable's type indicates: • What information can be stored in the variable • How many bits/bytes of storage are used for this variable • How the bits in the storage address represent the values that can be represented • What operations can be performed on the variable
Example: C/C++/Java "double" • What does "double" imply about: • What information can be stored in the variable? • How many bits/bytes of storage are used for this variable? • How the bits in the storage address represent the values that can be represented? • What operations can be performed on the variable?
Variable value • The value of a variable represents the bits stored in the address of the variable as well as the connotation of those bits based on the type of the data • The value of a variable can also be referred to as the r-value
Bindings • A binding is when an answer is given to one of the previous aspects about a variable • Answers generally are bound at one of two times: • Compile time, never later changing: statically bound • Runtime, or compile time but can change later: dynamically bound
Type bindings • One of the most commonly addressed questions is how variable types are bound • Static type binding: Once a variable is associated with a type, it will never change • Dynamic type binding: A variable's type can change throughout the execution of a program
Static type binding • Explicit type declaration: variables have their types explicitly stated • Implicit type declaration: based on various conventions, types are associated with variables when first mentioned • Type inference: The programming language automagically figures out what type a variable is based on how its used
Explicit type declaration • Any examples? • Advantages? • Disadvantages?
SalesProductOptionWorkOrdersalesProductOptionWorkOrder = new SalesProductOptionWorkOrder();
Implicit type declaration • Based on the variable name, the type of the variable is deduced • Examples: • Perl • Fortran
Perl and sigils • Perl variable names have a sigil (a symbol before the name) to indicate the type • $ indicates scalar • @ indicates array • % indicates hash (key-value container) • & indicates subroutines/functions • Thoughts?
Fortran • Fortran does let you explicitly state the type of a variable • If not, Fortran will deduce the type of a variable based on the following rules: • If the first character is i, j, k, l, m, n, I, J, K, L, M, N then the variable is INTEGER • If the first character is anything else, then the variable is REAL (floating-point) • "God is real, unless declared integer"
Type inference • Based on how a variable is used or what is stored in it, deduce what the type of the variable must be • Examples? • Advantages? • Disadvantages?
Dynamic type binding • In dynamic type binding, the type associated with a variable can change at any time • Examples? • Advantages? • Disadvantages?