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

📄 c-.y

📁 一个编译器的例子,基于c语言,在linux下开发 现在了简单的c语言例子
💻 Y
📖 第 1 页 / 共 3 页
字号:
%{#define YYDEBUG 1#include <stdlib.h>#include "struct.h"#include "stack.h"#include "compiler.c"symnode *curIDNode=(symnode *)0;symnode *curFuncNode=(symnode *)0;symnode *lastIDNode=(symnode *)0;/*symnode * TreeMatrix[TREE_X][TREE_Y];void initializeMatrix();void convertParseTree(symnode *,int);void convertPTVert();void printParseTree(symnode *);int printSymbolTable();symnode *setStrIndex(symnode *, int);symnode *pointSymTable(symnode *, symrec *);*/%}%union {              /* define stack type */  char  *val;  symnode *symptr;}%token <val>  ID INT_VAL CHAR_VAL FLT_VAL HEX_VAL EP_VAL STRING SIZEOF%token <val>  PTR_OP INC_OP DEC_OP LEFT_OP RIGHT_OP LE_OP GE_OP EQ_OP NE_OP PRN_OP%token <val>  AND_OP OR_OP MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN%token <val>  SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN%token <val>  XOR_ASSIGN OR_ASSIGN %token <val> CHAR INT FLOAT DOUBLE VOID%token <val> STRUCT%token <val> CASE DEFAULT IF ELSE SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN PRINTLN%type <symptr> primary_expression  unary_operator cast_expression%type <symptr> program external_declaration postfix_expression%type <symptr> argument_expression_list unary_expression%type <symptr> multiplicative_expression additive_expression%type <symptr> relational_expression equality_expression%type <symptr> exclusive_or_expression%type <symptr> logical_and_expression logical_or_expression and_expression inclusive_or_expression%type <symptr> assignment_expression expression declaration shift_expression assignment_operator%type <symptr> constant_expression conditional_expression%type <symptr> struct_specifier struct_declaration_list struct_declaration struct_declarator_list%type <symptr> init_declarator_list init_declarator declaration_specifiers%type <symptr> type_specifier declarator parameter_list type_name abstract_declarator direct_abstract_declarator%type <symptr> specifier_qualifier_list direct_declarator%type <symptr> parameter_declaration identifier_list initializer initializer_list%type <symptr> statement %type <symptr> compound_statement declaration_list %type <symptr> print_statement print_list%type <symptr> statement_list expression_statement selection_statement labeled_statement%type <symptr> iteration_statement jump_statement function_definition%start program %%primary_expression        : ID                      {$$=newLeaf("ID");   					symrec *id;					id=getsym($1, scope);					if(id==NULL) {						yyerror(); yynerrs++;						printf("---error messege(%d,%d):identifier  \"%s\" not defined, first used here\n", 													currLine, column, $1);					}else {						//$$->symTab=add2Table($1,funcType,scope); 						$$->symTab=id;					}					curIDNode = $$;lastIDNode=$$;				   }        | INT_VAL             {$$=newLeaf("INT_V"); strcpy($$->value,$1);}        | CHAR_VAL         {$$=newLeaf("CHAR_V"); 	                                if($1[1]!='\\') {					   $1[0]=$1[1];					   $1[1]='\0';					}else{						if( $1[2]<'0' || $1[2]>'9'){							$1[0]=$1[2];							$1[1]='\0';         						}else{							int i;							char asc[10];							for(i=2; i<strlen($1); i++){								asc[i-2]=$1[i];							}							asc[i-2]='\0';							i=atoi(asc);							//printf("char i: %d\n", i);							$1[0]=i;							$1[1]='\0';						}					}					sprintf($$->value, "%d",(int)($1[0]));				}        | FLT_VAL             {$$=newLeaf("FLT_V"); 	                                      float f;					      f=atof($1); 	                                      sprintf($$->value,"%d", *( (unsigned int *)(&f)));					      //printf("%s  :  %s\n", $1, $$->value);					   //strcpy($$->value,$1);					    }        | HEX_VAL            {$$=newLeaf("HEX_V"); strcpy($$->value,$1);}        | EP_VAL               {$$=newLeaf("EP_V"); strcpy($$->value,$1);}        | STRING                {strcpy(literals[strCount],$1);$$=setStrIndex(newLeaf("STRING"),strCount);strCount++;}        | '(' expression ')'         {$$=$2;}        ;postfix_expression        : primary_expression                               {$$=$1;}        | postfix_expression '[' expression ']'          { $$=addSibling(addNext(newLeaf("offset"),$1), 	                                                                           addNext(newLeaf("exp"),$3));																			   $$->sibling->symTab=curIDNode->symTab;							                   strcpy($$->sibling->value,lastIDNode->value);									   }        | postfix_expression '(' ')'                          {$$=$1;}        | postfix_expression '(' argument_expression_list ')'        {$$=addSibling($1,$3);}        | postfix_expression '.' ID                         {$$=addSibling(addNext(newLeaf("offset"),$1), newLeaf("member"));	                                                                   strcpy($$->sibling->value, $3); 									   $$->sibling->symTab=curIDNode->symTab;									   lastIDNode=$$->sibling;									   									   }        | postfix_expression PTR_OP ID                {$$=addSibling(addNext(newLeaf($2),$1), newLeaf("ID"));}        | postfix_expression INC_OP                      {$$=addSibling(newLeaf("unaryOp"),$1);	                                                   strcpy($$->value,"O+");	                                                   $$->symTab=curIDNode->symTab;							   }        | postfix_expression DEC_OP                    {$$=addSibling(newLeaf("unaryOp"),$1);		                                           strcpy($$->value,"O-");	                                                   $$->symTab=curIDNode->symTab;							   }        ;argument_expression_list        : assignment_expression                              {$$=$1;}        | argument_expression_list ',' assignment_expression                  {$$=addSibling(addNext(newLeaf("asgnExp"),$3),addNext(newLeaf("argExpLst"),$1));}        ;unary_expression        : postfix_expression                                          {$$=$1;}        | INC_OP unary_expression                       {$$=addSibling(newLeaf("unaryOp"), $2);		                                           strcpy($$->value,"+O");	                                                   $$->symTab=curIDNode->symTab;							   }                                     | DEC_OP unary_expression                      {$$=addSibling(newLeaf("unaryOp"), $2);		                                           strcpy($$->value,"-O");	                                                   $$->symTab=curIDNode->symTab;							   }        | unary_operator cast_expression                     {$$=addSibling($1,$2);}        | SIZEOF unary_expression                    {$$=addSibling(newLeaf("sizeof"), $2);}        | SIZEOF '(' type_name ')'                   {$$=addSibling(newLeaf("sizeof"), $3);}        ;unary_operator        : '&'        {$$=newLeaf("&");}        | '*'        {$$=newLeaf("*");}        | '+'        {$$=newLeaf("+");}        | '-'        {$$=newLeaf("-");}        | '!'        {$$=newLeaf("!");}        ;cast_expression        : unary_expression                                            {$$=$1;}        | '(' type_name ')' cast_expression                        {$$=addSibling(newLeaf("cast"), $4);	                                                                                 strcpy($$->value, $2->name);																				}        ;multiplicative_expression        : cast_expression                                                      {$$=$1;}        | multiplicative_expression '*' cast_expression                                     {$$=addSibling(addNext(newLeaf("OP"),$1),$3); strcpy($$->value, "*");}         | multiplicative_expression '/' cast_expression                                     {$$=addSibling(addNext(newLeaf("OP"),$1),$3); strcpy($$->value, "/");}         | multiplicative_expression '%' cast_expression                                     {$$=addSibling(addNext(newLeaf("OP"),$1),$3); strcpy($$->value, "%");}         ;additive_expression        : multiplicative_expression                           {$$=$1;}        | additive_expression '+' multiplicative_expression                                     {$$=addSibling(addNext(newLeaf("OP"),$1),$3);strcpy($$->value, "+");}        | additive_expression '-' multiplicative_expression                                     {$$=addSibling(addNext(newLeaf("OP"),$1),$3);strcpy($$->value, "-");}        ;shift_expression        : additive_expression                                                       {$$=$1;}        | shift_expression LEFT_OP additive_expression                                     {$$=addSibling(addNext(newLeaf("OP"),$1),$3);strcpy($$->value, "<<");}        | shift_expression RIGHT_OP additive_expression                                     {$$=addSibling(addNext(newLeaf("OP"),$1),$3);strcpy($$->value, ">>");}        ;relational_expression        : shift_expression                                                       {$$=$1;}        | relational_expression '<' shift_expression                                               {$$=addSibling(addNext(newLeaf("CMP"),$1),$3);strcpy($$->value, "<");}        | relational_expression '>' shift_expression                                               {$$=addSibling(addNext(newLeaf("CMP"),$1),$3);strcpy($$->value, ">");}        | relational_expression LE_OP shift_expression                                               {$$=addSibling(addNext(newLeaf("CMP"),$1),$3);strcpy($$->value, "<=");}        | relational_expression GE_OP shift_expression                                               {$$=addSibling(addNext(newLeaf("CMP"),$1),$3);strcpy($$->value, ">=");}        ;equality_expression        : relational_expression                                                       {$$=$1;}        | equality_expression EQ_OP relational_expression                                               {$$=addSibling(addNext(newLeaf("CMP"),$1),$3);strcpy($$->value, "==");}        | equality_expression NE_OP relational_expression                                               {$$=addSibling(addNext(newLeaf("CMP"),$1),$3);strcpy($$->value, "!=");}        ;and_expression        : equality_expression                                                      {$$=$1;}        | and_expression '&' equality_expression                                               {$$=addSibling(addNext(newLeaf("OP"),$1),$3);strcpy($$->value, "&");}        ;exclusive_or_expression        : and_expression                                                      {$$=$1;}        | exclusive_or_expression '^' and_expression                                                {$$=addSibling(addNext(newLeaf("OP"),$1),$3);strcpy($$->value, "^");}        ;inclusive_or_expression        : exclusive_or_expression                                                      {$$=$1;}        | inclusive_or_expression '|' exclusive_or_expression                                                {$$=addSibling(addNext(newLeaf("OP"),$1),$3);strcpy($$->value, "|");}        ;logical_and_expression        : inclusive_or_expression                                                     {$$=$1;}        | logical_and_expression AND_OP inclusive_or_expression                                               {$$=addSibling(addNext(newLeaf($2),$1),$3);}        ;logical_or_expression        : logical_and_expression                                                     {$$=$1;}        | logical_or_expression OR_OP logical_and_expression                                               {$$=addSibling(addNext(newLeaf($2),$1),$3);}        ;conditional_expression        : logical_or_expression                                                     {$$=$1;}        | logical_or_expression '?' expression ':' conditional_expression	                                    {$$=addSibling(newLeaf("?:"), addSibling($1, addSibling(addNext(newLeaf("exp"),$3), $5)));}        ;assignment_expression        : conditional_expression                                                        {$$=$1;}                       | unary_expression assignment_operator assignment_expression	                                     {$$=addSibling(addNext($2, $1), $3);}	//| error ';'           {$$=newLeaf("ERROR"); yyerrok;}        ;assignment_operator        : '='                                    {$$=newLeaf("ASSIGN"); strcpy($$->value, "=");}        | MUL_ASSIGN                    {$$=newLeaf("ASSIGN");strcpy($$->value, $1);}        | DIV_ASSIGN                     {$$=newLeaf("ASSIGN");strcpy($$->value, $1);}        | MOD_ASSIGN                   {$$=newLeaf("ASSIGN");strcpy($$->value, $1);}        | ADD_ASSIGN                    {$$=newLeaf("ASSIGN");strcpy($$->value, $1);}        | SUB_ASSIGN                     {$$=newLeaf("ASSIGN");strcpy($$->value, $1);}        | LEFT_ASSIGN                   {$$=newLeaf("ASSIGN");strcpy($$->value, $1);}        | RIGHT_ASSIGN                  {$$=newLeaf("ASSIGN");strcpy($$->value, $1);}        | AND_ASSIGN                    {$$=newLeaf("ASSIGN");strcpy($$->value, $1);}        | XOR_ASSIGN                    {$$=newLeaf("ASSIGN");strcpy($$->value, $1);}        | OR_ASSIGN                      {$$=newLeaf("ASSIGN");strcpy($$->value, $1);}        ;expression        : assignment_expression                       {$$=$1;}        | expression ',' assignment_expression                         

⌨️ 快捷键说明

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