Download presentation
Presentation is loading. Please wait.
Published byΓιάννη Καραμήτσος Modified over 5 years ago
1
CMPE 152: Compiler Design September 26 Class Meeting
Department of Computer Engineering San Jose State University Fall 2019 Instructor: Ron Mak
2
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
3
Parsing Pascal Declarations
Chapter 9 Parsing Pascal Declarations
4
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.
5
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.
6
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
7
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
8
ConstantDefinitionsParser
epsilon = 1.0e-6; pi = ; delta = epsilon; parse() parseConstant() parseIdentifierConstant() getConstantType()
9
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?
10
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.
11
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?
12
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.
13
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.
14
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
15
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.
16
Pascal Subrange Type
17
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.
18
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
19
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()
20
Pascal Enumeration Type
21
EnumerationTypeParser::parse()
Call TypeFactory::create_type(ENUMERATION) to create a new enumeration type specification.
22
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.
23
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;
24
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
25
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.