CMPE 152: Compiler Design September 26 Class Meeting

Slides:



Advertisements
Similar presentations
CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Advertisements

CS 153: Concepts of Compiler Design September 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 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 153: Concepts of Compiler Design September 16 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
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 September 30 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 153: Concepts of Compiler Design September 28 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Lecture 9 Symbol Table and Attributed Grammars
CS 432: Compiler Construction Lecture 9
CS 153: Concepts of Compiler Design September 14 Class Meeting
CS 153: Concepts of Compiler Design August 29 Class Meeting
CS 153: Concepts of Compiler Design September 12 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 September 6 Class Meeting
CMPE 152: Compiler Design August 30 Class Meeting
CMPE 152: Compiler Design September 11 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 August 28 Class Meeting
CMPE 152: Compiler Design October 4 Class Meeting
CMPE 152: Compiler Design August 23 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
CMPE 152: Compiler Design January 31 Class Meeting
CS 153: Concepts of Compiler Design November 6 Class Meeting
CS 432: Compiler Construction Lecture 11
CMPE 152: Compiler Design February 14 Class Meeting
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 January 29 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 February 19 Class Meeting
CMPE 152: Compiler Design March 7 Class Meeting
CMPE 152: Compiler Design April 18 – 30 Labs
CMPE 152: Compiler Design March 5 Class Meeting
CMPE 152: Compiler Design April 16 Class Meeting
CMPE 152: Compiler Design March 19 Class Meeting
CMPE 152: Compiler Design March 7/12 Lab
CMPE 152: Compiler Design September 3 Class Meeting
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 19 Class Meeting
Presentation transcript:

CMPE 152: Compiler Design September 26 Class Meeting Department of Computer Engineering San Jose State University Fall 2019 Instructor: Ron Mak www.cs.sjsu.edu/~mak

Tutoring Hours for Compiler Design Eddie Broeder is your instructional student assistant (ISA). He is a grad student who took compiler design from me last year, so he’s familiar with the material. He grades your assignments. He will also provide two hours/week of tutoring for compiler design students: Mondays and Wednesdays, 5:00 – 6:00 PM Room ENG 325

Parsing Pascal Declarations Chapter 9 Parsing Pascal Declarations

Function PascalParserTD::parse() wci::frontend::pascal::PascalParserTD.cpp icode = ICodeFactory::create_icode(); Predefined::initialize(symtab_stack); routine_id = symtab_stack->enter_local("dummyprogramname"); routine_id->set_definition((Definition) DefinitionImpl::PROGRAM); symtab_stack->set_program_id(routine_id); routine_id->set_routine_symtab(symtab_stack->push()); routine_id->set_routine_icode(icode); Token *token = next_token(nullptr); BlockParser block_parser(this); ICodeNode *root_node = block_parser.parse_block(token, routine_id); icode->set_root(root_node); symtab_stack->pop(); Predefined types  global symbol table. Push the new level 1 symbol table. The symbol table entry of the program id keeps a pointer to the level 1 symbol table. Parse the program’s block. When done, pop off the level 1 symbol table.

Function PascalParserTD::parse() wci::frontend::pascal::PascalParserTD.cpp token = current_token(); if (token->get_type() != (TokenType) PT_DOT) {     error_handler.flag(token, MISSING_PERIOD, this); } int last_line_number = token->get_line_number(); // Set the parse tree root node. if (root_node != nullptr) icode->set_root(root_node); // Send the parser summary message. steady_clock::time_point end_time = steady_clock::now(); double elapsed_time =         duration_cast<duration<double>>(end_time - start_time).count(); Message message(PARSER_SUMMARY); message.content.parser_summary.line_count = line_count; message.content.parser_summary.error_count = get_error_count(); message.content.parser_summary.elapsed_time = elapsed_time; send_message(message); Parse the final period.

Function BlockParser::parse() DeclarationsParser declarations_parser(this); StatementParser statement_parser(this); declarations_parser.parse_declaration(token); token = synchronize(StatementParser::STMT_START_SET); TokenType token_type = token->get_type(); ICodeNode *root_node = nullptr; if (token_type == (TokenType) PT_BEGIN) {     root_node = statement_parser.parse_statement(token); } ... return root_node; Parse declarations. Synchronize to the start of a statement. Look for BEGIN. Parse the compound statement. wci.frontend.pascal.parsers.BlockParser

Function DeclarationsParser::parse() Parse constant definitions. If there is the CONST reserved word. Call the parse() method of ConstantDefinitionsParser Parse type definitions. If there is the TYPE reserved word. Call the parse() method of TypeDefinitionsParser Parse variable declarations. If there is the VAR reserved word. Call the parse() method of VariableDeclarationsParser

ConstantDefinitionsParser epsilon = 1.0e-6; pi = 3.1415926; delta = epsilon; parse() parseConstant() parseIdentifierConstant() getConstantType()

Review: Type Definitions Sample type definitions: TYPE e = (alpha, beta, gamma); sr = alpha..beta; ar = ARRAY [1..10] OF sr; rec = RECORD x, y : integer END; How can we represent type specification information in our symbol table and associate the correct type specification with an identifier?

Type Definition Structures type specification (class TypeSpecImpl) symbol table entry (class SymTabEntryImpl) symbol table entry (class SymTabEntryImpl) vector<SymTabEntry *> *constants; 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.

Type Definition Structures, cont’d SUBRANGE_BASE_TYPE When it is parsing declarations, the parser builds type specification structures. When it is parsing executable statements, the parser builds parse trees. Unnamed type Is there an unnamed type?

Type Definition Structures, cont’d ARRAY_ELMT_TYPE rec = RECORD x,y : integer END Each record type has its own symbol table to contain its record field identifiers.

TypeDefinitionsParser::parse() e = (alpha, beta, gamma); sr = alpha..beta; ar = ARRAY [1..10] OF sr; rec = RECORD x, y : integer END; TypeDefinitionParser.parse() TypeSpecificationParser.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.

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

SimpleTypeParser::parse() Function 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.

Pascal Subrange Type

SubrangeTypeParser.parse() Call TypeFactory::create_type(SUBRANGE) to create a new subrange type specification. Parse the minimum constant value. Call ConstantDefinitionsParser::parse_constant() Get and check the data type of the minimum constant value: Call ConstantDefinitionsParser ::get_constant_type() The type must be integer, character, or an enumeration. Consume the .. token.

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. typedef union { ...     struct     {         TypeSpec *base_type;         int min_value;         int max_value;     } subrange; } TypeContent; TypeSpecImpl.h

Parsing a Subrange Type sr = 1..10; enum = (alpha, beta, gamma); ar = ARRAY [sr, enum] OF integer; rec = RECORD x, y : real END; SUBRANGE MIN_VALUE: 1 MAX_VALUE: 10 “sr” TYPE Pascal  PascalParserTD.parse()  BlockParser.parse()  DeclarationsParser.parse()  TypeDefinitionsParser.parse()  TypeSpecificationParser.parse()  SimpleTypeParser.parse()  SubrangeTypeParser.parse()

Pascal Enumeration Type

EnumerationTypeParser::parse() Call TypeFactory::create_type(ENUMERATION) to create a new enumeration type specification.

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 data value of the identifier to the next integer value (starting with 0). Build a vector<SymTabEntry *> of symbol table entries for the enumeration identifiers.

EnumerationTypeParser::parse() cont’d Set the constants field of the enumeration type specification to the vector. TypeSpecImpl.h typedef union { ...     struct     {         vector<SymTabEntry *> *constants;     } enumeration; } TypeContent;

Parsing an Enumeration Type sr = 1..10; enum = (alpha, beta, gamma); ar = ARRAY [sr, enum] OF integer; rec = RECORD x, y : real END; “enum” TYPE ENUMERATION “alpha” ENUMERATION_CONSTANT “beta” ENUMERATION_CONSTANT TypeDefinitionsParser.parse()  TypeSpecificationParser.parse()  SimpleTypeParser.parse()  EnumerationTypeParser.parse()  parseEnumerationIdentifier() “gamma” ENUMERATION_CONSTANT

Lab #4, cont’d Due Friday, September 27 at 11:59 PM. LoopTest2.txt BEGIN i := 0; LOOP i := i + 1; WHEN i > 10 ==>; i2 := i*i; WHEN i2 > 50 ==>; AGAIN END Due Friday, September 27 at 11:59 PM.