1 / 31

The Cool Language

sc4312. Lecture 3b. 2. Introduction. Cool (Classroom Object-Oriented Language)Cool is a small language designed for the purpose of teaching a compiler construction process.The original Cool uses flex (scanner generator) and bison (parser generator). The original Cool compiler generates a MIPS ass

freira
Download Presentation

The Cool Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. sc4312 Lecture 3b 1 The Cool Language SC4312 – Compiler Construction Kwankamol Nongpong

    2. sc4312 Lecture 3b 2 Introduction Cool (Classroom Object-Oriented Language) Cool is a small language designed for the purpose of teaching a compiler construction process. The original Cool uses flex (scanner generator) and bison (parser generator). The original Cool compiler generates a MIPS assembly code. Cool programs are run on a MIPS simulator called spim.

    3. sc4312 Lecture 3b 3 We use Coco/R in this class. C-.Net supports simple classes and one-dimensional array. It does not support inheritance, polymorphism, overloading, multi-dimensional arrays, delegates, index, enumerations, properties, and events.

    4. sc4312 Lecture 3b 4 About Cool A Cool program is a set of classes. Every class defines a type. Inheritance allows new types to extend the behavior of existing types. Most Cool constructs are expressions. Every expression has a value and a type.

    5. sc4312 Lecture 3b 5 Classes class type [ : type ] is feature_list end; All code in cool is organized into classes. Each class definition must be contained in a single source file, but multiple classes may be defined in the same file.

    6. sc4312 Lecture 3b 6 Features (1) The body of a class definition consists of a list of feature definitions. A feature is either an attribute or a method.

    7. sc4312 Lecture 3b 7 Features (2) Attribute id : type; Method id ( id : type, … , id : type ) : type := expr ;

    8. sc4312 Lecture 3b 8 Inheritance If a class definition has the form class A : B is … end; Then class A inherits the features of B. B is the parent class of A. A is a child class of B. Cool only supports “single inheritance” i.e., a class may inherit only from a single class.

    9. sc4312 Lecture 3b 9 Types Every class name is also a type. A type declaration has the form, x : C where x is a variable and C is a type. Every variable must have a type declaration at the point it is introduced. The types of all attributes must be declared.

    10. sc4312 Lecture 3b 10 Constructors type ( id : type, … , id : type ) [ : type := expr ] [ begin expr end ]; A constructor has the same name as the class it is in. Each class may include only one constructor. A constructor acts like a special method that is called on a newly created object.

    11. sc4312 Lecture 3b 11 Expressions Constants Identifiers Assignment Dispatch Blocks Conditionals Loops Let Case New Arithmetic and Comparison Operations

    12. sc4312 Lecture 3b 12 Expressions (1) Assignment id := expr Dispatch expr.id(expr, …, expr) Block begin block end

    13. sc4312 Lecture 3b 13 Expression (2) Conditionals if expr then block (elsif expr then)* else block fi Loops while expr loop block pool Lets let ( id :type [:= expr ]; )+ in block end

    14. sc4312 Lecture 3b 14 Expression (3) Case case expr of ( when test => block )+ esac where test is of the form id : type New new type (expr, …,expr)

    15. sc4312 Lecture 3b 15 Arithmetic Operations Arithmetic and Comparison Operations expr1 op expr2 Cool has four binary arithmetic operations: +, -, *, div. Cool has four comparison operations: <, <=, =, ==.

    16. sc4312 Lecture 3b 16 Lexical Units Integers Special notation Type identifiers Object identifiers Strings Comments Keywords White space

    17. sc4312 Lecture 3b 17 Integers and Special Notations Integers are non-empty strings of digits 0-9. Special syntactic symbols are parentheses, assignment operator

    18. sc4312 Lecture 3b 18 Identifiers Identifiers are strings (other than keywords) consisting of letters, digits, and the underscore characters. Type identifiers begin with a capital letter. Object identifiers begin with a lowercase letter.

    19. sc4312 Lecture 3b 19 Strings String literals are enclosed in double quotes “…”. A non-escaped newline character may not appear in a string literal.

    20. sc4312 Lecture 3b 20 Comments Any characters after two dashes -- and before the next newline are ignored.

    21. sc4312 Lecture 3b 21 Keywords The keywords of Cool are: begin, case, class, div, else, elsif, end, esac, false, fi, if, in, is, let, loop, native, new, not, of, pool, super, then, true, void, when, while. Cool keywords are case insensitive except for the constants true, false and void (as they must be lowercase).

    22. sc4312 Lecture 3b 22 White space White space consists of any sequence of the ASCII characters: BS, HT These characters are represented by the following literals respectively: ‘\b’, ‘\t’, ‘\n’, ‘\v’, ‘\f’, ‘\r’ and ‘ ’.

    23. sc4312 Lecture 3b 23 Symbols and Representations ASCII characters are used to represent symbols in terms of characters. Symbols are identifiers, number literals, string literals, character literals, operators, delimiters, keywords, and comments.

    24. sc4312 Lecture 3b 24 Lexical Rules Blanks and line breaks may appear between symbols but must not occur within symbols except: Line breaks in comments Blanks within strings. They are ignored unless they are important for separating two consecutive symbols. Cool is a case-sensitive language.

    25. sc4312 Lecture 3b 25 Comments Comments may be inserted between any two symbols in a program. They are either enclosed by /* and */, or opened by // and closed at the end of line. Example: /* Here is a comment. */ // Here is another comment.

    26. sc4312 Lecture 3b 26 Identifiers Cool identifiers are sequences of letters, underscore, and digits. The first character must be a letter. Example: x Sci cMinus A123 student_id

    27. sc4312 Lecture 3b 27 Numbers There are two number literals in Cool. Integers are sequences of digits. Real numbers have their usual decimal representation such as 1.0, 5.39, and 2.481.

    28. sc4312 Lecture 3b 28 Strings Strings are sequences of zero or more graphic characters enclosed in the quote symbols. String literals may not extend over a line break in the source program.

    29. sc4312 Lecture 3b 29 Characters Characters are denoted by a single graphic character or a single escape sequence, enclosed in single quote marks. Within a string or character literal the following escape sequences denote non-graphic characters: \0 = NUL character, \b = bkspace, \t = tab, \n = lf, \r = cr, \” = quote, \’ = apostrophe.

    30. sc4312 Lecture 3b 30 Operators Operators in C-.Net are !, &&, ||, +, -, *, /, %, ++, --, <, <=, ==, >, >=, =, .(dot), ,(comma), ; (semicolon), (, ), {, }, [, ], and []. Precedence and associativity of C-.Net operators are exactly the same as those of C#.

    31. sc4312 Lecture 3b 31 Keywords Keywords are bool, boolean, char, class, do, else, false, if, int, new, null, private, public, ReadBool, ReadChar, ReadInt, ReadLine, ReadString, return, static, string, String, this, true, void, while, Write. Some keywords are synonyms (e.g., bool and boolean, String and string).

More Related