Kiến trúc máy tính và hợp ngữ - Chapter 7: Semantics
To be complete, an imperative language needs:
Statement sequencing
Conditional statement
Looping statement
37 trang |
Chia sẻ: huyhoang44 | Lượt xem: 744 | Lượt tải: 0
Bạn đang xem trước 20 trang tài liệu Kiến trúc máy tính và hợp ngữ - Chapter 7: Semantics, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Programming Languages2nd editionTucker and NoonanChapter 7SemanticsSurely all this is not without meaning. Ishmael, Moby Dick by Herman MelvilleContents7.1 Motivation7.2 Expression Semantics7.3 Program State7.4 Assignment Semantics7.5 Control Flow Semantics7.6 Input/Output Semantics7.7 Exception Handling Semantics7.1 MotivationTo provide an authoritative definition of the meaning of all language constructs for:ProgrammersCompiler writersStandards developersA programming language is complete only when its syntax, type system, and semantics are well-defined. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program.Ideas of meaning:The meaning attached by compiling using compiler C and executing using machine M. Ex: Fortran on IBM 709.Axiomatize statements -- Chapter 12Statements as state transforming functionsThis chapter uses an informal, operational model.7.2 Expression Semantics (a + b) - (c * d)Polish Prefix: - + a b * c dPolish Postfix: a b + c d * -Cambridge Polish: (- (+ a b) (* c d))Infix uses associativity and precedence to disambiguate.Associativity of OperatorsLanguage + - * / Unary - ** == != next) ... // ???Copy vs. Reference SemanticsCopy: a = b;a, b have same value.Changes to either have no effect on other.Used in imperative languages.Referencea, b point to the same object.A change in object state affects bothUsed by many object-oriented languages. public void add (Object word, Object number) { Vector set = (Vector) dict.get(word); if (set == null) { // not in Concordance set = new Vector( ); dict.put(word, set); } if (allowDupl || !set.contains(number)) set.addElement(number);}7.5 Control Flow SemanticsTo be complete, an imperative language needs:Statement sequencingConditional statementLooping statementSequences1 s2Semantics: in the absence of a branch:First execute s1 Then execute s2Output state of s1 is the input state of s2ConditionalIfStatement if ( Expresion ) Statement [ else Statement ]Example:if (a > b) z = a;else z = b; If the test expression is true,then the output state of the conditional is the output state of the then branch,else the output state of the conditional is the output state of the else branch.LoopsWhileStatement while ( Expression ) StatementThe expression is evaluated.If it is true, first the statement is executed,and then the loop is executed again.Otherwise the loop terminates.7.6 Input/Output SemanticsBinding: open, closeAccess: sequential vs. randomStream vs. fixed length recordsCharacter vs. binaryFormatStandard FilesUnix: stdin, stdout, stderrC: stdin, stdout, stderrC++: cin, cout, cerrJava: System.in, System.out, System.errInput/Output StreamsFortraninteger :: i, a(8)write(8,*) “Enter 8 integers: “read(*,*) awrite(*,*) aJavafile, pipe, memory, urlfilterreader, writerFormatsCCodes: d, e, f, c, s (decimal. float, float, char, string)Specifier: % opt-width codeEx: %s %5d %20s %8.2fFortranCodes: i, f, a (integer, float, string)Specifier: op-repeat code widthEx: 8i4, f8.2, a20
Các file đính kèm theo tài liệu này:
- ch07a_3664.ppt