📄 attributes.h
字号:
////////////////////////////////////////////////////////////////////////////////// attributes.[h,cc] -- routines to manage the attribute stack//// the interface between parsing and semantic analysis//// The compiler does not build a complete parse tree. At any one time, it// maintains space for attributes of all the symbols in all the productions// between the root of the (hypothetical) parse tree and the production in// which the parser is currently parsing. These attributes lie in the// "attribute stack", which is named as for short. An auxilliary stack,// the "production stack" (ps for short) is hidden inside attributes.cc.// It keeps track of indices into the attribute stack for all productions in// progress.//// The parser calls three routines to maintain the attribute stack:// start_production --// Push right hand side onto attribute stack.// Save old values of rhs and lhs indices on production stack.// Initialize new values of rhs and lhs.// end_production --// Pop right hand side off attribute stack.// Pop old values for rhs and lhs off production stack.// init_token --// Fill in synthetic attributes of a terminal.//// The semantic routines in the grammar file, in turn, use the information// in the attribute stack to build an abstract syntax tree using constructors// member functions of concrete subtypes of the abstract type syntax_tree_t.//#ifndef ATTRIBUTES_H#define ATTRIBUTES_H#include "scanner.h"#include "inpbuf.h"#include "symtab.h"#define MAX_ATTR_STACK 500////////// Forward Referencesclass syntax_code_t;class st_token_t;class st_identifier_t;class st_number_t;class st_operator_t;class st_unary_op_t;class st_binary_op_t;class st_statement_t;class st_assignment_t;class st_while_t;class st_if_t;class st_call_t;class st_declaration_t;class st_constant_t;class st_variable_t;class st_procedure_t;class st_block_t;////////////////////////////////////////////////////////////////////////////////// Syntax Tree Classes//// Every node in the attributed syntax tree is a object belonging to one of// the concrete subtypes of the abstract type syntax_tree_t.//// Each subclass of syntax_tree_t must implement the following functions:// * constructor (in attributes.cc)// * check_semantics: (in semantics.cc)// - Checks the semantics of the node and any of its children.// * optimize: (in optimize.cc)// - Perform any code optimizations on a node and its children.// * allocate_registers: (in codegen.cc)// - Allocates any registers used by the node and lets its children also// allocate their needed registers. Returns a pointer to its// syntax_code_t structure which details register usage and where the// node's parent can locate any results generated by the node.// * generate_code: (in codegen.cc)// - Generates any inline assembly code need by the node and its// children and dumps code to the output file using the mips_op// functions.// * generate_lcode: (in codegen.cc)// - Generate any inline code needed to access a node's lvalue.// * dump_object_data: (in attributes.cc)// - Dump all useful debugging information about a syntax tree node// to the display.// * destructor (in attributes.cc)// - Deallocate and destruct the node and its children.//class syntax_tree_t{protected: location_t location; // Source location of node. syntax_code_t * code_gen_info; // Code generation information.public: syntax_tree_t(location_t loc); location_t get_location(void) const; virtual void check_semantics(scope_number_t scope) = 0; virtual syntax_tree_t * optimize(void) = 0; virtual syntax_code_t * allocate_registers(void) = 0; virtual syntax_code_t * generate_code(void) = 0; virtual syntax_code_t * generate_lcode(unsigned reg); virtual void dump_object_data(int recurse, int level) const; virtual ~syntax_tree_t(void) = 0;};////////// Tokens//class st_token_t : public syntax_tree_t{protected: token_attrib_t attributes; // Token and its attributes.public: st_token_t(token_attrib_t attr); virtual void check_semantics(scope_number_t scope); virtual syntax_tree_t * optimize(void); virtual syntax_code_t * allocate_registers(void); virtual syntax_code_t * generate_code(void); virtual void dump_object_data(int recurse, int level) const; token_attrib_t get_attributes(void) const; virtual ~st_token_t(void);};class st_identifier_t : public st_token_t{protected: symbol_entry_t * symbol_entry; // Pointer to the identifier's // symbol table entry.public: st_identifier_t(token_attrib_t attr); virtual void check_semantics(scope_number_t scope); virtual syntax_tree_t * optimize(void); virtual syntax_code_t * allocate_registers(void); virtual syntax_code_t * generate_code(void); virtual syntax_code_t * generate_lcode(unsigned reg); virtual void dump_object_data(int recurse, int level) const; symbol_entry_t * get_symbol(void) const; // the following three routines call the same-named member of the // identifier's symbol table entry void set_assigned(void); void set_used(void); void set_called(void); virtual ~st_identifier_t(void);};class st_number_t : public st_token_t{protected: int value; // Value of this number.public: st_number_t(token_attrib_t attr); // Normal constructor. Sets value to 0. // st_number_t::check_semantics does the string-to-integer // conversion to set value based on attributes.get_text(). // attributes, of course, is initialized by the constructor for // our base class, st_token_t. st_number_t(token_attrib_t attr, int n_value); // Auxilliary constructor used by constant-folding routines in // optimize.cc. virtual void check_semantics(scope_number_t scope); virtual syntax_tree_t * optimize(void); virtual syntax_code_t * allocate_registers(void); virtual syntax_code_t * generate_code(void); virtual void dump_object_data(int recurse, int level) const; int get_value(void) const; virtual ~st_number_t(void);};////////// Operators//class st_operator_t : public syntax_tree_t{protected: st_token_t * op; // Operator.public: st_operator_t(location_t loc, st_token_t *id); virtual void check_semantics(scope_number_t scope); virtual syntax_tree_t * optimize(void); virtual syntax_code_t * allocate_registers(void); virtual syntax_code_t * generate_code(void); virtual void dump_object_data(int recurse, int level) const; virtual ~st_operator_t(void);};class st_unary_op_t : public st_operator_t{protected: syntax_tree_t * operand; // Operand.public: st_unary_op_t(location_t loc, st_token_t *id, syntax_tree_t *opand); virtual void check_semantics(scope_number_t scope); virtual syntax_tree_t * optimize(void); virtual syntax_code_t * allocate_registers(void); virtual syntax_code_t * generate_code(void); virtual void dump_object_data(int recurse, int level) const; virtual ~st_unary_op_t(void);};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -