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.

Slides:



Advertisements
Similar presentations
1 Compiler Construction Intermediate Code Generation.
Advertisements

CSE 131B – Compiler Construction II Discussion 4: Project 1 1/31/2007.
CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 152: Programming Language Paradigms April 9 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design August 24 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
CS 153: Concepts of Compiler Design September 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 235: User Interface Design September 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 9 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 14 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 174: Web Programming September 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms April 2 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design August 26 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 16 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 423 Compiler project notes Dept. of Comp. Sci. & Eng. Geunbae Lee.
CS 153: Concepts of Compiler Design September 21 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 10 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design November 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 30 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms April 21 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 153: Concepts of Compiler Design November 18 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 12 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms May 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 154 Formal Languages and Computability March 22 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
CS 432: Compiler Construction Lecture 9
CS 153: Concepts of Compiler Design September 14 Class Meeting
CS 153: Concepts of Compiler Design October 17 Class Meeting
CS 153: Concepts of Compiler Design October 5 Class Meeting
CS 432: Compiler Construction Lecture 10
CS 153: Concepts of Compiler Design September 7 Class Meeting
CS 153: Concepts of Compiler Design October 3 Class Meeting
CMPE 152: Compiler Design December 5 Class Meeting
CMPE 152: Compiler Design February 6 Class Meeting
CMPE 152: Compiler Design September 25 Class Meeting
CMPE 152: Compiler Design September 4 Class Meeting
CMPE 152: Compiler Design August 30 Class Meeting
CMPE 152: Compiler Design September 18 Class Meeting
CMPE 152: Compiler Design September 13 Class Meeting
CMPE 152: Compiler Design October 2 Class Meeting
CMPE 152: Compiler Design October 4 Class Meeting
CMPE 152: Compiler Design September 11/13 Lab
CMPE 152: Compiler Design October 4 Class Meeting
CMPE 152: Compiler Design August 21/23 Lab
CMPE 152: Compiler Design September 20 Class Meeting
CMPE 152: Compiler Design September 27 Class Meeting
CS 153: Concepts of Compiler Design October 30 Class Meeting
CS 153: Concepts of Compiler Design November 6 Class Meeting
CS 432: Compiler Construction Lecture 11
CMPE 152: Compiler Design February 28 Class Meeting
CMPE 152: Compiler Design March 7 Class Meeting
CMPE 152: Compiler Design April 9 Class Meeting
CMPE 152: Compiler Design February 12 Class Meeting
CMPE 152: Compiler Design February 7 Class Meeting
CMPE 152: Compiler Design February 21/26 Lab
CMPE 152: Compiler Design February 28 / March 5 Lab
CMPE 152: Compiler Design February 21 Class Meeting
CMPE 152: Compiler Design February 26 Class Meeting
CMPE 152: Compiler Design March 7 Class Meeting
CMPE 152: Compiler Design March 5 Class Meeting
CMPE 152: Compiler Design April 16 Class Meeting
CMPE 152: Compiler Design March 7/12 Lab
CMPE 152: Compiler Design August 27 Class Meeting
CMPE 152: Compiler Design September 17 Class Meeting
CMPE 152: Compiler Design February 7 Class Meeting
CMPE 152: Compiler Design September 26 Class Meeting
CMPE 152: Compiler Design September 19 Class Meeting
Presentation transcript:

CS 153: Concepts of Compiler Design September 28 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak 1

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.

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

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.

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()

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

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.

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

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.

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

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

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

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.

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.

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.

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;

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

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

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.

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;

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

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.

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).

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.

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.

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.

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;

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

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

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;

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;

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