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

📄 myparser.y

📁 编译原理的课程设计,输出汇编,很好的 南开大学的同学做的
💻 Y
📖 第 1 页 / 共 2 页
字号:
%{
/************************7****************************************************
myparser.y
ParserWizard generated YACC file.

Date: 2007年12月13日
****************************************************************************/
#include <stdio.h>
#include "mylexer.h"
#include "systab.h"
int total=0;
int label=0;
typedef struct treeNode
{ 
   struct treeNode * child[3];
   struct treeNode * sibling;
   int lineno;
   int val;
   int count;
   int nodekind;
   int kind;
   int op;
   char  name[255];
} TreeNode;
static TreeNode * savedTree;
FILE *out ; 
#define IfK 10
#define AssignK 11
#define IDk 9
#define Constk 8
#define Exprk 7
#define Termk 6
#define Repk 12
#define Com 5
#define Vark 4
#define Typek 3
#define Prink 13
#define Inputk 14
#define StmtKind 1
#define ExprKind 2
#define IDk1 15


#define YYSTYPE TreeNode *
static void cGen( TreeNode * tree);
void genExp(TreeNode * root);
YYSTYPE newStmNode(int,int);
//YYSTYPE newIDNode(int kind,int count,char* string);
//YYSTYPE newNUMNode(int kind,int count,int val);
YYSTYPE newExprNode(int kind,int count);
%}


%token ID NUM EQ IF
%token CHAR DOUBLE INT ELSE
%token LT PLUS MINUS TIMES 
%token OVER LPAREN RPAREN SEMI
%token WHILE LEFTBIG RIGHTBIG
%token MAIN COMMON FOR PRINT
%token EQ_OP LE LT GT GE NEQ
%token INPUT AND OR
%token NOT LSQ RSQ
/////////////////////////////////////////////////////////////////////////////
// declarations section

// attribute type

%include {
#ifndef YYSTYPE
#define YYSTYPE TreeNode *
#endif
}

// place any declarations here

%%

/////////////////////////////////////////////////////////////////////////////
// rules section

// place your YACC rules here (there must be at least one)
lines	: MAIN LPAREN  RPAREN compound_statement 
			{
				savedTree = $4;
			}
		;

/*lines	:	lines statement '\n' {   printf("ok\n");}
		|	lines  '\n'
		|
		;*/
type_statement
		:	type_specifier id_list SEMI
		{
			++total;//printf("%d: Var Declaration           \n",total);
			$$ = newStmNode(Vark,total);
			$$->child[0] = $1;
			$$->child[1] = $2;			
		}
		;
id_list
		: id { $$ = $1; fprintf(out,"	%s	DWORD 0\n",$$->name);}
		| id_list COMMON id
		{
			 YYSTYPE t = $1;
             if (t != NULL)
             { 
				while (t->sibling != NULL)
                        t = t->sibling;
                t->sibling = $3;
                $$ = $1; 
			 }
             else $$ = $3;
             fprintf(out,"	%s	DWORD 0\n",$3->name);
        }
		;

assign_statement
		:	id EQ expr SEMI
		{	++total;//printf("%d: Assign Statement             \n",total	);
			$$ = newStmNode(AssignK,total);
			$$->child[0] = $1;
			$$->child[1] = $3;		
		}
		;
selection_statement
		:	IF LPAREN expr RPAREN statement 
		{	++total;//printf("%d: If Statement             \n",total);
			$$ = newStmNode(IfK,total);
			$$->child[0] = $3;
			$$->child[1] = $5;
		}
		;
repeat_statement
		:	WHILE LPAREN expr RPAREN statement
		{
			++total;//printf("%d: If Statement             \n",total);
			$$ = newStmNode(Repk,total);
			$$->child[0] = $3;
			$$->child[1] = $5;
		}		
		|	FOR LPAREN expr RPAREN statement
		{
			++total;//printf("%d: If Statement             \n",total);
			$$ = newStmNode(Repk,total);
			$$->child[0] = $3;
			$$->child[1] = $5;
		}
		;
print_statement
		: PRINT LPAREN expr RPAREN SEMI
		{
			++total;
			$$ = newStmNode(Prink,total);
			$$->child[0]=$3;
		}
		;
input_statement
		: INPUT LPAREN id RPAREN SEMI
		{
			++total;
			$$ = newStmNode(Inputk,total);
			$$->child[0]=$3;
		}
		;		
				
statement
		:	assign_statement		{$$=$1;}
		|	selection_statement		{$$=$1;}
		|	repeat_statement		{$$=$1;}
		|	type_statement			{$$=$1;}
		|	print_statement			{$$=$1;}
		|	compound_statement		{$$=$1;}
		|	input_statement			{$$=$1;}
		;
statement_list
		: statement { $$ = $1; }
		| statement_list statement
		{
			 YYSTYPE t = $1;
             if (t != NULL)
             { 
				while (t->sibling != NULL)
                        t = t->sibling;
                t->sibling = $2;
                $$ = $1; 
			 }
             else $$ = $2;
        }
		;
	
compound_statement
		: LEFTBIG RIGHTBIG
		| LEFTBIG statement_list RIGHTBIG 
		{
			++total;
			$$=newStmNode(Com,total);
			$$->child[0]=$2;
		}
		;
expr	:uexpr AND uexpr
		{
			++total;
			$$=newExprNode(Exprk,total);
			$$->child[0] = $1;
			$$->child[1] = $3;
//			printf("*************\n");
			$$->op= AND;
		}		
		|uexpr OR uexpr
		{
			++total;
			$$=newExprNode(Exprk,total);
			$$->child[0] = $1;
			$$->child[1] = $3;
			$$->op = OR;
		}
		| uexpr {
			$$=$1;
		}
		;
uexpr	: simple_exp LT simple_exp {
				++total;
				$$=newExprNode(Exprk,total);
				$$->child[0] = $1;
				$$->child[1] = $3;
//				printf("*************\n");
				$$->op= LT;
			}
		| simple_exp EQ_OP simple_exp {
				++total;
				$$=newExprNode(Exprk,total);
				$$->child[0] = $1;
				$$->child[1] = $3;
//				printf("*************\n");
				$$->op= EQ_OP;
			}			
			
		| simple_exp GT simple_exp {
				++total;
				$$=newExprNode(Exprk,total);
				$$->child[0] = $1;
				$$->child[1] = $3;
//				printf("*************\n");
				$$->op = GT;
			}			
		| simple_exp LE simple_exp {
				++total;
				$$=newExprNode(Exprk,total);
				$$->child[0] = $1;
				$$->child[1] = $3;
//				printf("*************\n");
				$$->op = LE;
			}
		| simple_exp GE simple_exp {
				++total;
				$$=newExprNode(Exprk,total);
				$$->child[0] = $1;
				$$->child[1] = $3;
//				printf("*************\n");
				$$->op = GE;
			}			
		| simple_exp NEQ simple_exp {
				++total;
				$$=newExprNode(Exprk,total);
				$$->child[0] = $1;
				$$->child[1] = $3;
//				printf("*************\n");
				$$->op = NEQ;
			}						
		| simple_exp {$$=$1;}
		;

simple_exp	
		: NOT simple_exp
             {	
                ++total;
				$$=newExprNode(Exprk,total);
				$$->child[0] = $2;
				$$->op=NOT;	
			 }
		| simple_exp PLUS term	  
							  {
								++total;
								//printf("%d: Expr             op:   +  \n",total);
								$$=newExprNode(Exprk,total);
								$$->child[0] = $1;
								$$->child[1] = $3;
								$$->op=PLUS;
								}
		| simple_exp MINUS term		{
								//printf("%d: Expr             op:   -  \n",total);
								++total;
								$$=newExprNode(Exprk,total);
								$$->child[0] = $1;
								$$->child[1] = $3;							
								$$->op=MINUS;
								}
		|	term			    {$$=$1; }  
		;
term	:term TIMES factor	{
								++total;
//								printf("%d: Term             op:   *  \n",total);
								$$=newExprNode(Exprk,total);
								$$->child[0] = $1;
								$$->child[1] = $3;					
								$$->op=TIMES;
								}
								
		|	term OVER factor	{
//								printf("%d: Term             op:   /  \n",total);
								++total;
								$$=newExprNode(Exprk,total);
								$$->child[0] = $1;
								$$->child[1] = $3;
								$$->op=OVER;
								}
		|	factor				{$$=$1;}
		;
		
factor	:	LPAREN expr RPAREN  {$$=$2;}
		|	id	          	    {$$=$1;}	
		|	num 			    {$$=$1;}	
		;
id		:	ID   { ++total; //printf("%d: ID Declaration symbol:%s \n",total,yytext);
			$$ =newExprNode(IDk,total);
			//$$->name=yytext;
			strcpy($$->name,yytext);
//			insert_st($$->name);
		}
//		|
//			ID LSQ expr RSQ	{
//				$$ = newExprNode(IDk1,total);
//				strcpy($$->name,yytext);
				
//			}
        ;
        
num		:	NUM	 { ++total; //printf("%d: Const Declaration symbol:%d \n",total,atoi(yytext));
			$$ = newExprNode(Constk,total);
			$$->val=atoi(yytext);
			}
			
		;
		
type_specifier
		:	CHAR			{++total;//$$ =newIDNode(Typek,total,"Char");
		}
		|   INT             {++total;//$$ =newIDNode(Typek,total,"Integer");
		}
		|	DOUBLE		    {++total;//$$ =newIDNode(Typek,total,"Double");
		}
		;
%%

/////////////////////////////////////////////////////////////////////////////
// programs section

YYSTYPE newStmNode(int kind,int count)
{ 
	YYSTYPE t = (YYSTYPE ) malloc(sizeof(TreeNode));
	int i;
    for (i=0;i<3;i++) 

	t->child[i] = NULL;
    t->sibling = NULL;
    t->kind=StmtKind;
	//t->nodekind = ExpK;
	t->nodekind= kind;
//	t->lineno = lineno;
	t->count = count;
  return t;
}
/*
YYSTYPE newIDNode(int kind,int count,char* string)
{ 
	YYSTYPE t = (YYSTYPE ) malloc(sizeof(TreeNode));
	int i;
    for (i=0;i<3;i++) 
		t->child[i] = NULL;
    t->sibling = NULL;
	t->nodekind= kind;
	t->count = count;
	strcpy(t->name,string);
  return t;
}
YYSTYPE newNUMNode(int kind,int count,int val)
{
	YYSTYPE t = (YYSTYPE ) malloc(sizeof(TreeNode));
	int i;
    for (i=0;i<3;i++) 
		t->child[i] = NULL;
    t->sibling = NULL;
	t->nodekind= 8;
	t->count = count;
	t->val=val;
  return t;
}*/

YYSTYPE newExprNode(int kind,int count)
{
	YYSTYPE t = (YYSTYPE ) malloc(sizeof(TreeNode));
	int i;
    for (i=0;i<3;i++) 
		t->child[i] = NULL;
    t->sibling = NULL;
    t->kind=ExprKind;
	t->nodekind= kind;
	t->count = count;
	if(kind!=IDk && kind!=Constk) 
	fprintf(out,"	_t%d DWORD 0\n",count);
//	t->op=op;
  return t;
}
output(TreeNode* root){
	
	TreeNode* t;   
	int i;
	if(root==NULL) return ;
	if(root->child[0]!=NULL) printf("%d ",root->child[0]->count);
	if(root->child[1]!=NULL) printf("%d ",root->child[1]->count);
	if(root->child[2]!=NULL) printf("%d ",root->child[2]->count);
	
	for(i=0;i<3;i++){
		if(root->child[i]==NULL) continue;
		t=root->child[i]->sibling;
		//if(t==NULL) continue;
		while(t!=NULL)
		{
//			printf("%d ",t->count);
			t=t->sibling;
		}       
	}
}

find(TreeNode* root){
//	printf("%d\n",root->nodekind);
	if(root->child[0]!=NULL) find(root->child[0]);
	if(root->child[1]!=NULL) find(root->child[1]);
	if(root->child[2]!=NULL) find(root->child[2]);
	if(root->nodekind==IfK) {
//		printf("%d If Statement               ",root->count);
	}

⌨️ 快捷键说明

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