⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pas.y.old

📁 this is a lp0 compilator new
💻 OLD
📖 第 1 页 / 共 3 页
字号:
%{/********************************************************************** *                 Bison Grammar for ISO Pascal                       * *   --------------------------------------------------------------   * *                                                                    * *   Gerald Carter                                                    * *   cartegw@eng.auburn.edu                                           * *   January 21, 1996                                                 * *                                                                    * *   The basic grammar defintion file was downloaded from the         *  *   comp.compilers ftp site.  The grammar has not been modified.     * *   Any changes have dealt with the semantic meaning of the          * *   grammar definitions.                                             * *                                                                    * *********************************************************************//* HEADER FILES */#include <stdio.h>              //  printf()#include <iostream.h>           //  cout#include "pas.h"                //  things needed by pas.l & pas.y#include "syntax.h"             //  syntaxNode class declaration#include "labels.h"             //  macros for syntax tree node labels#include "symbol.h"		//  symbol existence (symbolNode class)/* EXTERNAL FUNCTION DECLARATIONS AND VARIABLES*/extern  int     yylineno;extern  char    yytext[];extern  char    numbertext[80];extern  char    *laststring;extern  char    lastident[];extern  char    *create_string();/* MACROS */#define pluralsuffix(num,suffix)        ((num)==1?"":(suffix))#define plural(num)                     pluralsuffix(num,"s")#define YYSTYPE                         syntaxNode*/* #define YYDEBUG                         1 *//* GLOBAL VARIABLES */int             lexical_level = 0;int      	param_level = 0;int      	its_a = ITS_A_UNDEFINED;syntaxNode*     orphans[5] = { 0, 0, 0, 0, 0 };char            indent[128] = "";int             index = 0;/* FUNCTION DELCLARATIONS */int yyerror (char*);%}%start program%token UNSIGNED_INT%token UNSIGNED_REAL%token STRING%token IDENTIFIER%token FORWARD%token NE%token LE%token GE%token BECOMES%token DIV%token MOD%token NIL%token IN%token OR_TOK%token AND_TOK%token NOT_TOK%token DOTDOT%token IF%token THEN%token ELSE%token CASE%token OF%token REPEAT%token UNTIL%token WHILE%token DO%token FOR%token TO%token DOWNTO%token SBEGIN%token END%token WITH%token GOTO%token CONST%token VAR%token TYPE%token ARRAY%token RECORD%token SET%token SFILE%token FUNCTION%token PROCEDURE%token LABEL%token PACKED%token PROGRAM/*------------------------------------------------------------------------* *                              BISON RULES                               * *------------------------------------------------------------------------*/%%   /*start of rules*/program         : PROGRAM { its_a = ITS_A_PROGRAM; } newident external_files ';' block '.'		   { $$ = new syntaxNode (PROGRAM_LABEL);		     orphans[0] == $3;		     orphans[1] == $6;		     $$->AdoptChildren (2, BLOCK_LABEL, orphans);		     sourceTree = $$;                   }		;external_files  : /*empty*/		| '(' { its_a = ITS_A_EXTERNAL_FILE; } newident_list ')' 		   { /*nothing*/ }		;block           : opt_declarations statement_part		   {  $$ = new syntaxNode (BLOCK_LABEL);		      orphans[0] = $1;		      orphans[1] = $2;		      $$->AdoptChildren (2, BLOCK_PART_LABEL, orphans); }		;opt_declarations: /*empty*/ 		   { $$ = new syntaxNode (OPT_DECLARATIONS_LABEL);		     $$->AdoptChildren (0, EMPTY_OPT_DECLARATIONS, orphans); }		| declarations		   { $$ = new syntaxNode (OPT_DECLARATIONS_LABEL);		     orphans[0] = $1;		     $$->AdoptChildren (1, DECLARATIONS_LABEL, orphans); }		;declarations    : declarations declaration		   { $$ = new syntaxNode (DECLARATIONS_LABEL);		     orphans[0] = $1;		     orphans[1] = $2;		     $$->AdoptChildren (2, DECLS_DECL_LABEL, orphans); }		| declaration		   { $$ = new syntaxNode (DECLARATIONS_LABEL);		     orphans[0] = $1;		     $$->AdoptChildren (1, DECLARATION_LABEL, orphans); }		;declaration     : label_dcl_part		   { $$ = new syntaxNode (DECLARATION_LABEL);		     orphans[0] = $1;		     $$->AdoptChildren (1, LABEL_DCL_PART_LABEL, orphans); }		| const_dcl_part		   { $$ = new syntaxNode (DECLARATION_LABEL);		     orphans[0] = $1;		     $$->AdoptChildren (1, CONST_DCL_PART_LABEL, orphans); }		| type_dcl_part		   { $$ = new syntaxNode (DECLARATION_LABEL);		     orphans[0] = $1;		     $$->AdoptChildren (1, TYPE_DCL_PART_LABEL, orphans); }		| var_dcl_part		   { $$ = new syntaxNode (DECLARATION_LABEL);		     orphans[0] = $1;		     $$->AdoptChildren (1, VAR_DCL_PART_LABEL, orphans); }		| proc_dcl_part		   { $$ = new syntaxNode (DECLARATION_LABEL);		     orphans[0] = $1;		     $$->AdoptChildren (1, PROC_DCL_PART_LABEL, orphans); }		;label_dcl_part  : LABEL labels ';'		   { $$ = new syntaxNode (LABEL_DCL_PART_LABEL);		     orphans[0] = $2;		     $$->AdoptChildren (1, LABELS_LABEL, orphans); }		;labels          : labels ',' label 		   { $$ = new syntaxNode (LABELS_LABEL);		     orphans[0] = $1;		     orphans[1] = $3;		     $$->AdoptChildren (2, LABELS_LABEL_LABEL, orphans); }		| label 		   { $$ = new syntaxNode (LABELS_LABEL);		     orphans[0] = $1;		     $$->AdoptChildren (1, LABEL_LABEL, orphans); }		;label           : UNSIGNED_INT /* 0 <= value <= 9999 [6.1.6] */		   { $$ = new syntaxNode (LABEL_LABEL);		     $$->AdoptChildren (0, UNSIGNED_INT, orphans); }		;const_dcl_part  : CONST const_defs ';'		   { $$ = new syntaxNode (CONST_DCL_PART_LABEL);		     orphans[0] = $2;		     $$->AdoptChildren (1, CONST_DEFS_LABEL, orphans); }		;const_defs      : const_defs ';' const_def 		   { $$ = new syntaxNode (CONST_DEFS_LABEL);		     orphans[0] = $1;		     orphans[1] = $3;		     $$->AdoptChildren (2, CONSTS_CONST_LABEL, orphans); }		| const_def		   { $$ = new syntaxNode (CONST_DEFS_LABEL);		     orphans[0] = $1;		     $$->AdoptChildren (1, CONST_DEF_LABEL, orphans); }		;const_def       : { its_a = ITS_A_CONSTANT; } newident '=' constant		   { $$ = new syntaxNode (CONST_DEF_LABEL);		     orphans[0] = $2;		     orphans[1] = $4;		     $$->AdoptChildren (2, CONST_DEF_EQUAL_LABEL, orphans); }		;constant        : unsigned_num 		   { $$ = new syntaxNode (CONSTANT_LABEL);		     orphans[0] = $1;		     $$->AdoptChildren (1, UNSIGNED_NUM_LABEL, orphans); }		| '+' unsigned_num		   { $$ = new syntaxNode (CONSTANT_LABEL);		     orphans[0] = $2;		     $$->AdoptChildren (1, UNSIGNED_NUM_LABEL, orphans); }		| '-' unsigned_num 		   { $$ = new syntaxNode (CONSTANT_LABEL);		     orphans[0] = $2;		     $$->AdoptChildren (1, UNSIGNED_NUM_LABEL, orphans); }		| ident   		   { $$ = new syntaxNode (CONSTANT_LABEL);		     orphans[0] = $1;		     $$->AdoptChildren (1, IDENT_LABEL, orphans); }		| '+' ident 		   { $$ = new syntaxNode (CONSTANT_LABEL);		     orphans[0] = $2;		     $$->AdoptChildren (1, IDENT_LABEL, orphans); }		| '-' ident		   { $$ = new syntaxNode (CONSTANT_LABEL);		     orphans[0] = $2;		     $$->AdoptChildren (1, IDENT_LABEL, orphans); }		| STRING  /*type is char if len=1*/		   { $$ = new syntaxNode (CONSTANT_LABEL);		     $$->AdoptChildren (0, STRING, orphans); }		;unsigned_num    : UNSIGNED_INT		   { $$ = new syntaxNode (UNSIGNED_NUM_LABEL);		     $$->AdoptChildren (0, UNSIGNED_INT, orphans); }		| UNSIGNED_REAL		   { $$ = new syntaxNode (UNSIGNED_NUM_LABEL);		     $$->AdoptChildren (0, UNSIGNED_REAL, orphans); }		;type_dcl_part   : TYPE type_defs ';'		   { $$ = new syntaxNode (TYPE_DCL_PART_LABEL);		     orphans[0] = $2;		     $$->AdoptChildren (1, TYPE_DEFS_LABEL, orphans); }		;type_defs       : type_defs ';' type_def		   { $$ = new syntaxNode (TYPE_DEFS_LABEL);		     orphans[0] = $1;		     orphans[1] = $3;		     $$->AdoptChildren (2, TYPES_TYPE_LABEL, orphans); }		| type_def		   { $$ = new syntaxNode (TYPE_DEFS_LABEL);		     orphans[0] = $1;		     $$->AdoptChildren (1, TYPE_DEF_LABEL, orphans); }		;type_def        : { its_a = ITS_A_TYPE; } newident '=' type		   { $$ = new syntaxNode (TYPE_DEF_LABEL);		     orphans[0] = $2;		     orphans[1] = $4;		     $$->AdoptChildren (2, TYPE_DEF_EQUAL_LABEL, orphans); }		;type            : simple_type		   { $$ = new syntaxNode (TYPE_LABEL);		     orphans[0] = $1;		     $$->AdoptChildren (1, SIMPLE_TYPE_LABEL, orphans); }		| PACKED struct_type 		   { $$ = new syntaxNode (TYPE_LABEL);		     orphans[0] = $2;		     $$->AdoptChildren (1, PACKED_STRUCT_TYPE_LABEL, orphans); }		| struct_type		   { $$ = new syntaxNode (TYPE_LABEL);		     orphans[0] = $1;		     $$->AdoptChildren (1, STRUCT_TYPE_LABEL, orphans); }		| '^' IDENTIFIER  /*check forward reference semantics*/		   { $$ = new syntaxNode (TYPE_LABEL);		     $$->AdoptChildren (0, IDENTIFIER, orphans); 		     $$->SetLexeme (last_id_token); }		;simple_type     : '(' { its_a = ITS_A_ENUM_LITERAL; } newident_list ')'		   { $$ = new syntaxNode (SIMPLE_TYPE_LABEL);		     orphans[0] = $3;		     $$->AdoptChildren (1, ENUMERATED_LIST_LABEL, orphans); }		| constant DOTDOT constant		   { $$ = new syntaxNode (SIMPLE_TYPE_LABEL);		     orphans[0] = $1;		     orphans[1] = $3;		     $$->AdoptChildren (2, CONSTANT_DOTDOT_CONSTANT_LABEL, orphans); }		| ident		   { $$ = new syntaxNode (SIMPLE_TYPE_LABEL);		     orphans[0] = $1;		     $$->AdoptChildren (1, IDENT_LABEL, orphans); }		;struct_type     : ARRAY '[' index_t_list ']' OF type		   { $$ = new syntaxNode (STRUCT_TYPE_LABEL);		     orphans[0] = $3;		     orphans[1] = $6;		     $$->AdoptChildren (2, ARRAY_OF_TYPE_LABEL, orphans); }		| RECORD /*consider this a scope*/ field_list END		   { $$ = new syntaxNode (STRUCT_TYPE_LABEL);		     orphans[0] = $2;		     $$->AdoptChildren (1, FIELD_LIST_LABEL, orphans); }		| SET OF simple_type 		   { $$ = new syntaxNode (STRUCT_TYPE_LABEL);		     orphans[0] = $3;		     $$->AdoptChildren (1, SET_LABEL, orphans); }		| SFILE OF type 		   { $$ = new syntaxNode (STRUCT_TYPE_LABEL);		     orphans[0] = $3;		     $$->AdoptChildren (1, SFILE_OF_TYPE_LABEL, orphans); }		;index_t_list    : index_t_list ',' simple_type 		   { $$ = new syntaxNode (INDEX_T_LIST_LABEL);		     orphans[0] = $1;		     orphans[1] = $3;		     $$->AdoptChildren (2, SIMPLES_SIMPLE_LABEL, orphans); }		| simple_type 		   { $$ = new syntaxNode (INDEX_T_LIST_LABEL);		     orphans[0] = $1;		     $$->AdoptChildren (1, SIMPLES_SIMPLE_LABEL, orphans); }		;field_list      : fixed_part 		   { $$ = new syntaxNode (FIELD_LIST_LABEL);		     orphans[0] = $1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -