Appendix A: Syntax Diagrams
Total Page:16
File Type:pdf, Size:1020Kb
Appendix A: Syntax Diagrams Chapter 3 provides a syntax diagram for every language element of Modula-2. These diagrams have been collected in this appendix for easy reference when questions arise concerning the formulation of particular program constructs. The syntax diagrams are listed and numbered in the same order as in Chapter 3. The following alphabetical list is intended to assist in locating individual syntax diagrams. ArrayType19 OctInteger6 Assignment.41 ParamSection53 B lock 12 PointerType25 CaseLabelList24 PrioritY67 CaseStatement.43 ProcedureCa1l55 ConstantDeclaration26 ProcedureDeclarationso ConstExpr27 ProcedureHeadings1 DecInteger5 ProcedureType57 Declaration 13 ProgramModule11 Definition63 Qualldent2 DefinitionModule62 Reals Designator31 RecordType20 Element39 Relation33 Enumeration 15 RepeatStatemen~s ExitStatemen~s RetumStatement56 EXport61 Set3s Expression32 SetType17 Factor37 SimpleExpression34 FieldList21 SimpleType1s FormalParameters52 Statemen~o FormalType54 S tatementSequence 14 FormalTypeList5S String9 ForStatemen~6 Subrange 16 HexInteger7 Term35 Ident1 Type29 IfStatemen42 TypeDeclaration28 ImplementationModule65 TypeDefinition64 Import60 TypeTransfer 66 Integer4 VariableDeclaration3o LoopStatemen~7 Variant23 ModuleDeclarations9 VariantFieldList22 MulOperator36 WhileStatemen44 Number3 WithStatemen~9 OctChar10 Appendix A: Syntax Diagrams 235 Qualldent2 Number3 Integer4 Declnteger 5 1 Octlnteger 6 I Hexlnteger 7 ~ Declnteger5 OctInteger6 236 Appendix A: Syntax Diagrams HexInteger7 ,~4CJ)44?4~1 ~~~~--------------------~-~.~ Rea1s o 0character~ r~Chafficter~ :~ ~ OctChar 10 .1--------'''--... OctCharlO ProgramModulell ~~~~....L""I Block121-~lldent1 1-0-- Block12 Declaration 13 ---""'-------..J---ot"_( BEGIt\D-- StatementSequence 14 Appendix A: Syntax Diagrams 237 Declaration 13 ConstantDeciaration 26 Variable Declaration 30 ProcedureDeclaration 50 _.L-__..... I ModuleDeciaration 59 I-----....::=..~~ StatementSequencel4 ----~o~.. ------- ---'=1 State~ent 40 ~ Enumeration 15 Subrangel6 ~ Qualldent 2 tJ--CD--l ConstExpr 27 ~I ConstExpr 27 ~ SetTypel7 -C@-®--I SimpleType 181- SimpleTypel8 Qualldent 2 19Enumeration 15 1Subrange 161 • 238 Appendix A: Syntax Diagrams ArrayType19 - ( Q •. 1 -(ARRAY~I SimpleType 18 ~~I Type 29 r-- RecordType20 ,-----lQI4.-~ -(RECORD~I FieldList 21 ~~ Fie1dList21 o-lType29~ '----I VariantFieldList 221---,./ VariantFieldList22 .(CASE~lldent1I;--o-l Qualldent2~ OF Variant23 CaseLabelList 24 CaseLabelList24 o· ~ ConstExpr 27f---.rO=:l ConstExpr 271, J• Appendix A: Syntax Diagrams 239 PointerType25 -CPOINTER)-®---I Type 29 ~ ConstantDeclaration26 -ildent 1 ~0-1 ConstExpr 27 ~o- ConstExpr27 -I Expression 32 ~ TypeDeclaration28 _lldent 1 ~G-I Type 29 ~o- Type29 SetType 17 ~-...... Simple Type 18 ArrayType 19 t---..J VariableDeclaration3o ~o--I Type 29 1--0-- 240 Appendix A: Syntax Diagrams Designator31 1....-----1 Ident 1 }+.--..!Io.--1 Expression 32 - Qualldent 21--~-------------L-"" Expression32 - Simple Expression 34 I Relation 33 ~ SimpleExpression 34 Relation33 SimpleExpression34 Term35 MulOperator 36 ---=~--1.~1 Factor 37 I--~'--. MulOperator36 Appendix A: Syntax Diagrams 241 Factor37 Q.-..-----..) ·1 Designator 31 ( 1 Expression 321 CD ~ ., .. a>r Number 3 ·l String 9 Set 38 CD---I Expression 32 ) NOT - 1 Factor371 ~ ~. ) .CD LI Qualldent 2 tr-CD \ JI8Il1int 391 ! Element39 -I Expression 32 ~o---.I Expression 32 tr- 242 Appendix A: Syntax Diagrams Statemenl40 Assignment 41 If Statement 42 Assignmen41 -I Designator 31 ~G--I Expression 32 ~ IfStatemen42 r-------------~~EL~S~IF~~------------ I Expression 32 ~C!B:®--- StatementSequence 14 '----~.. ~ StatementSequence 14 @Q)f------. Appendix A: Syntax Diagrams 243 CaseStatemen43 -------•• @@-I Expression 32~ OF )-------- '"--=-......1 CaseLabelList 24~o--. StatementSequence 141-:r--"~ '----........... ill@--- StatementSequence 14 @@I----_. WhileStatemen44 -CWHILE}--I Expression 32 ~ StatementSequence 14 ~@Q)- RepeatS tatemen45 -(REPEAT}---I StatementSequence 14 ~(UNTIL}---I Expression 32 ~ ForStatemen46 --~lldent 1 1--0-1 Expression 321--CIQ)- Expression 32 ConstExpr 27 ®---I StatementSequence 141--~ LoopS tatemen47 -~I StatementSequence 14 ~~ ExitStatemen4s WithStatemen49 -@ITB)----I Designator 31 ~@--I StatementSequence 14 ~@Q)- ProcedureDeclaration50 -I ProcedureHeading 51 ~I Block 12 ~@Q)---Ildent 1 ~o- 244 Appendix A: Syntax Diagrams ProcedureHeading51 -CPROCEDURE}--Ildent 1 ~I FormalParameters 52 r-0-- FormalParameters52 r---~O"·--""", ~CD \(1 ParamSection 531 J, "CD----x:0--1 Quallden. 2 r- ParamSection53 L@!D, ~0--1 FormaIType"i-- FormalType54 LCARRAY}-@y--I Qualldent 2 ~ ProcedureCa1l55 0,.·---. -I Designator 31 ~,-_CD_(_l_l_I_E_xp_re_S_S_io_n_32_I_J_ !__ ·_T~ RetumStatement56 -CRETURN~I Expression 32 r- ProcedureType57 -CPROCEDURE~I FormalTypeList 58 r- Appendix A: Syntax Diagrams 245 FormaiTypeLists8 ~CD l \' ~:nnaITYpe "I lJ ·<D---t=0-1 Q"IIde", , ry.---- ModuieDeciarationS9 -----... (MODULE}.-Ildent 1 ~I Priority 67 fy- ;J---- ....... I Block12~ Ident, ~o-- Import6o Lill!Q!\I[)--lldenl, Iy---(lMPORT~O-- EXport61 ~(EXPORT>----r-(QUALIFIED)f ~o-- DefinitionModule62 -(DEFINITION)---.(MODULE)--Ildent 1 ~ ; ~"----------"''' Definition 63 '-----~----....L.._@IQ)_Ildent 1 ~Of-----· 246 Appendix A: Syntax Diagrams Definition63 ConstantDeclaration 26 TypeDefinition 64 VariableDeclaration 30 ProcedureHeading 511 • TypeDefinition64 -ildent 1 ~0-1 Type 29 tJo---. ImplementationModule65 -OMPLEMENTATION}--I ProgramModule 11 ~ TypeTransfer66 -I Qualldent 2~CD--1 Expression 32~CD-- PrioritY67 -CD--I ConstExpr 27t-(j)- Appendix B: Predefined Identifiers Modula-2 contains a number of predefined identifiers for constants, elementary data types and standard procedures. The following summary provides an overview of what they mean. A more detailed explanation can be found in Sections 3.4.1.1 ("Predefined Data Types") and 3.7.8 ("Standard Procedures"). Predefined Constants FALSE boolean value "false" NIL POINTER value that points to no object TRUE boolean value "true" Predefined Data Types BITSET set BOOLEAN logical values "true" or "false" CARDINAL natural numbers (~O) CHAR characters INTEGER whole numbers (positive or negative) PROC parameterless procedures REAL real numbers Standard Procedures DEC (x) reduces (decrements) the value of x by 1 DEC(x,n) reduces (decrements) the value of x by n DISPOSE(p) frees the storage to which p points EXCL(s,e) removes (excludes) the element e from the set s HALT terminates program execution INC (x) increases (increments) the value of x by 1 INC(x,n) increases (increments) the value of x by n INCL(s,e) includes the element e in the set s NEW(p) allocates dynamic storage Standard Functions ABS (x) returns the absolute value of x CAP (ch) changes the character ch to a capital letter CHR(c) returns the character whose ordinal number is c FLOAT (c) transforms the CARDINAL number c into a REAL number HIGH (a) returns the highest index of the array a MAX(T) returns the greatest value that can be represented by the data type T MIN(T) returns the smallest value that can be represented by the data type T ODD (x) returns the boolean value of the expression"x is odd" TRUNC(r) transforms the REAL number r to a CARDINAL number VAL(T,c) returns the value of data type T having the ordinal number c Appendix C: Compatibility Rules The strict type concept in Modula-2 forces the programmer to keep in mind which data types are compatible under which conditions. All the respective rules were covered in Chapter 3, but they are spread throughout the entire chapter. Thus we have summarized all the rules governing compatibility in this appendix. Type Identity (see Section 3.4.5) The two objects x I and x2 with the data types t I and t 2 are of the same data type if one of the following conditions is met: t I and t 2 are identified by the same name, e.g.: VAR VAR n: INTEGER; material: Material; val: INTEGER; substance: Material; xl and x2 are declared within the same variable list, e.g.: VAR n, val: INTEGER; PROCEDURE P (x, y: CHAR) ; die,dot: [1. .6]; t I and t 2 are identified by names that are designated as synonyms in the type declaration, e.g.: TYPE TYPE Minute = [0 .. 59]; t = (a,b, c) ; Second = Minute; tl = t; VAR t2 = t; sec: Second; VAR min: Minute; xl: tl; x2: t2; xl and x2 are constants o/the same enumeration type, as low and high or red and yellow in these examples: VAR VAR voltage: (low,high); color: (red,blue,yellow); Type equality is required with V AR parameters The data types of formal and actual parameters must be the same. Exceptions: If the formal parameter is an ARRAY parameter, only the element types must be identical in the formal and actual parameters. ADDRESS is compatible with CARDINAL and with all POINTER types. Appendix C: Compatibility Rules 249 WORD is compatible with every data type that occupies exactly one word of memory. ARRAY OF WORD is compatible with any and all data types. with procedure variables If a procedure P is to be assigned to a procedure variable pv, the parameters of all data types in P and pv must be identical. If P and pv are function procedures, the data types of their function values must also be identical. Expression Compatibility (see Section 3.5.5) Two operands x 1 and x 2 with data types t 1 and t 2 are expression compatible if any one of the following conditions is met: t 1 and t 2 are the same data type. t 1 is a subrange type with base type t2 (or vice versa). t 1 and t 2 are subrange types with the same base types. t 1 is INTEGER or CARDINAL and t 2 is a subrange