Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 153: Concepts of Compiler Design September 28 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Similar presentations


Presentation on theme: "CS 153: Concepts of Compiler Design September 28 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak."— Presentation transcript:

1 CS 153: Concepts of Compiler Design September 28 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak 1

2 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 2 Type Definition Structures symbol table entry (class SymTabEntryImpl ) type specification (class TypeSpecImpl ) TYPE ATTRIBUTE KEY (enumeration TypeKeyImpl ) symbol table entry (class SymTabEntryImpl ) Each symbol table entry has a pointer to a type specification. Each type specification of a named type has a pointer to the type identifier’s symbol table entry.

3 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 3 Type Definition Structures, cont’d Unnamed type When it is parsing declarations, the parser builds type specification structures. When it is parsing executable statements, the parser builds parse trees. SUBRANGE_BASE_TYPE

4 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 4 ARRAY_ELMT_TYPE Type Definition Structures, cont’d rec = RECORD x,y : integer END Each record type has its own symbol table to contain its record field identifiers.

5 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 5 TypeDefinitionsParser.parse()  Loop to parse each type definition. Parse the type identifier and the = sign. Call the parse() method of TypeSpecificationParser.  Parse the type specification and return a TypeSpec object.  Cross-link the SymTabEntry object of the type identifier with the TypeSpec object. TYPE e = (alpha, beta, gamma); sr = alpha..beta; ar = ARRAY [1..10] OF sr; rec = RECORD x, y : integer END; TypeDefinitionParser.parse() TypeSpecificationParser.parse()

6 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 6 TypeSpecificationParser.parse()  Parse an array type. If there is an ARRAY reserved word. Call the parse() method of ArrayTypeParser  Parse a record type. If there is a RECORD reserved word. Call the parse() method of RecordTypeParser  Parse a simple type. In all other cases. Call the parse() method of SimpleTypeParser

7 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 7 SimpleTypeParser.parse()  Method parse() parses: A previously-defined type identifier.  Including integer, real, etc. An enumeration type specification.  Call the parse() method of EnumerationTypeParser. A subrange type specification.  Call the parse() method of SubrangeTypeParser.

8 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 8 Pascal Subrange Type

9 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 9 SubrangeTypeParser.parse()  Call TypeFactory.createType(SUBRANGE) to create a new subrange type specification.  Parse the minimum constant value. Call ConstantDefinitionsParser.parseConstant()  Get and check the data type of the minimum constant value: Call ConstantDefinitionsParser.getConstantType() Call checkValueType()  The type must be integer, character, or an enumeration.  Consume the.. token.

10 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 10 SubrangeTypeParser.parse()  Parse the maximum constant value.  Check that both minimum and maximum values have the same data type.  Check that the minimum value <= maximum value.  Set attributes of the subrange type specification. SUBRANGE_BASE_TYPE SUBRANGE_MIN_VALUE SUBRANGE_MAX_VALUE

11 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 11 Parsing a Subrange Type  Pascal  PascalParserTD.parse()  BlockParser.parse()  DeclarationsParser.parse()  TypeDefinitionsParser.parse()   TypeSpecificationParser.parse()   SimpleTypeParser.parse()   SubrangeTypeParser.parse() TYPE sr = 1..10; enum = (alpha, beta, gamma); ar = ARRAY [sr, enum] OF integer; rec = RECORD x, y : real END; “sr” TYPE SUBRANGE MIN_VALUE: 1 MAX_VALUE: 10

12 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 12 Pascal Enumeration Type

13 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 13 EnumerationTypeParser.parse()  Call TypeFactory.createType(ENUMERATION) to create a new enumeration type specification.

14 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 14 EnumerationTypeParser.parse() cont’d  Loop to parse each enumeration identifier. Call parseEnumerationIdentifier()  Set the definition of the identifier to ENUMERATION_CONSTANT.  Set the typeSpec field of the identifier to the enumeration type specification.  Set the CONSTANT_VALUE of the identifier to the next integer value (starting with 0). Build an ArrayList of symbol table entries for the enumeration identifiers.

15 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 15 EnumerationTypeParser.parse() cont’d  Set the ENUMERATION_CONSTANTS attribute of the enumeration type specification to the array list.

16 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 16 Parsing an Enumeration Type TypeDefinitionsParser.parse()   TypeSpecificationParser.parse()   SimpleTypeParser.parse()   EnumerationTypeParser.parse()  parseEnumerationIdentifier() “enum” TYPE “alpha” ENUMERATION_CONSTANT “beta” ENUMERATION_CONSTANT “gamma” ENUMERATION_CONSTANT ENUMERATION TYPE sr = 1..10; enum = (alpha, beta, gamma); ar = ARRAY [sr, enum] OF integer; rec = RECORD x, y : real END;

17 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 17 Pascal Record Type rec = RECORD x,y : integer END

18 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 18 RecordTypeParser.parse()  Call TypeFactory.createType(RECORD) to create a new record type specification.  Create and push a new symbol table onto the symbol table stack. Set the RECORD_SYMTAB attribute of the record type specification to the new symbol table. rec = RECORD x,y : integer END

19 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 19 RecordTypeParser.parse() cont’d  Call VariableDeclarationsParser.parse() to parse the field declarations. Set each field’s definition to FIELD. Enter each field into the current symbol table (the one just pushed onto the top of the stack).  Pop the record type’s symbol table off the symbol table stack. rec = RECORD x,y : integer END After the record type’s symbol table has been popped off the symbol table stack, it’s still referenced by the RECORD_SYMTAB attribute.

20 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 20 Parsing a Record Type TypeDefinitionsParser.parse()   TypeSpecificationParser.parse()   RecordTypeParser.parse()  VariableDeclarationsParser.parse() “rec” TYPE “X”“X” FIELD “Y”“Y” RECORD symtab TYPE sr = 1..10; enum = (alpha, beta, gamma); ar = ARRAY [sr, enum] OF integer; rec = RECORD x, y : real END;

21 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 21 Pascal Array Type

22 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 22 Pascal Multidimensional Array  These definitions are all equivalent: dim3 = ARRAY [1..3, 'a'..'z', boolean] OF real; dim3 = ARRAY [1..3] OF ARRAY ['a'..'z'] OF ARRAY [boolean] OF real; dim3 = ARRAY [1..3, 'a'..'z'] OF ARRAY [boolean] OF real; dim3 = ARRAY [1..3] OF ARRAY ['a'..'z, boolean] OF real; Therefore, they must all generate the same type specification structure.

23 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 23 Pascal Multidimensional Array The first two element types are arrays, and the third element type is real scalar. The ARRAY_ ELEMENT_TYPE references form the backbone of this structure (a linked list).

24 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 24 ArrayTypeParser.parse()  Call TypeFactory.createType(ARRAY) to create a new array type specification.

25 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 25 ArrayTypeParser.parse()  Call parseIndexTypeList() to parse the list of index types. Set local variable elementType to the new array type specification. Loop to call parseIndexType() to parse each index type.  Call simpleTypeParser.parse() to parse the index type.  Set the attributes for a subrange or enumeration index type specification.  Set the ARRAY_ELEMENT_COUNT attribute for the current array type spec.

26 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 26 ArrayTypeParser.parse()  Call parseIndexTypeList() to parse the list of index types (cont’d). For each index type in the list after the first:  Call TypeFactory.createType(ARRAY) to create the next elementType value.  Set the ARRAY_ELEMENT_TYPE attribute of the previous elementType value to link to the new elementType value.  Call parseElementType() to parse the final element type. Link the previous element type to the final element type. These elementType references create the backbone.

27 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 27 ENUMERATION “enum” TYPE index type Parsing an Array Type TypeDefinitionsParser.parse()   TypeSpecificationParser.parse()   ArrayTypeParser.parse()  parseIndexTypeList()  parseIndexType()  parseElementType() “ar” TYPE ARRAY SUBRANGE “sr” TYPE index type element type SCALAR “integer” TYPE element type TYPE sr = 1..10; enum = (alpha, beta, gamma); ar = ARRAY [sr, enum] OF integer; rec = RECORD x, y : real END;

28 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 28 Pascal Variable Declarations Compare to:

29 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 29 VariableDeclarationsParser.parse()  Repeatedly call parseIdentifierSublist() to parse the semicolon-separated sublists of variables. Loop to parse the comma-separated list of variable names. VAR i, j, k : integer; x, y, z : real; semicolon-separated sublist comma-separated list

30 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 30 VariableDeclarationsParser.parse()  Repeatedly call parseIdentifierSublist() to parse the semicolon-separated sublists of variables (cont’d). Call parseIdentifier() to parse each variable name.  Enter each identifier into the current symbol table (the one at the top of the symbol table stack).  Set each identifier’s definition to VARIABLE VAR i, j, k : integer; x, y, z : real;

31 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 31 VariableDeclarationsParser.parse()  Repeatedly call parseIdentifierSublist() to parse the semicolon-separated sublists of variables (cont’d). Call parseTypeSpec() to parse the type specification.  Consume the : token.  Call TypeSpecificationParser.parse() to parse the type specification. Assign the type specification to each variable in the list. VAR i, j, k : integer; x, y, z : real;

32 Computer Science Dept. Fall 2015: September 28 CS 153: Concepts of Compiler Design © R. Mak 32 Demo  Pascal Cross-Referencer II Parse declarations Generate a detailed cross-reference listing Syntax check declarations


Download ppt "CS 153: Concepts of Compiler Design September 28 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak."

Similar presentations


Ads by Google