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

📄 parse.y.bak

📁 完成一个简化的C语言编译程序
💻 BAK
📖 第 1 页 / 共 2 页
字号:
=%{
#include "stdlib.h"
#include "globals.h"
#include "util.h"
#include "scaner.h"
#include "parser.h"   
#define YYSTYPE TreeNode* 
static char* savedName;
static int savedLineNo;
static TreeNode* savedTree; 
static ExpType current_type;
int   array_size;
char* current_name;
%}
  
%token INT CHAR FLOAT VOID
%token NUM FLOATNUM CONSTCHAR
%token ID
%token IF ELSE WHILE BREAK RETURN
%token LBRACKET RBRACKET LPAREN RPAREN LBC RBC SEMI COMMA
%token ASSIGN EQ LT LE GT GE NE 
%token AND OR NOT
%token PLUS MINUS TIMES OVER 
%token ERROR   


%%

program  :   declaration_list
             { savedTree=$1;}
             ;
             
declaration_list  : declaration_list declaration
                    {
                      YYSTYPE t=$1;
                      if(t!=NULL)
                      {
                         while(t->sibling!=NULL)
                               t=t->sibling;
                         t->sibling=$2;
                         $$=$1;      
                      }
                      else 
                          $$=$2;                  
                    }
                   | declaration {$$=$1;}
                   ;
                    
declaration  : var_declaration
              {$$=$1;}
             | fun_declaration
              {$$=$1;}
             ;
             
var_declaration : type_specifier id SEMI
                {
                 $$=newDeclNode(SingleVarK);
                 $$->type=current_type;
                 $$->attr.name=$2->attr.name;
                }  
                | type_specifier id LBRACKET NUM 
                {array_size=atoi(tokenString);} RBRACKET SEMI
                {
                 $$ = newDeclNode(ArrayVarK);
                 $$->type=current_type;
                 $$->attr.name=$2->attr.name;
                 $$->array_size=array_size;
                } 
                ;      
                    
type_specifier  : INT
                 {current_type=Integer;} 
                 |CHAR
                 {current_type=Char;}
                 |FLOAT
                 {current_type=Float;}
                 |VOID
                 {current_type=Void;} 
                 ;

id              : ID
                 {
				  $$=newExpNode(IdK);
				  $$->attr.name=copyString(tokenString);
				 }
				 ;

fun_declaration : type_specifier id LPAREN params RPAREN compound_stmt
                { 
                  $$=newDeclNode(FuncVarK);
                  $$->type=current_type;
                  $$->attr.name=$2->attr.name;
                  $$->child[0]=$4;
                  $$->child[1]=$6;
                  
                }
                |type_specifier id LPAREN RPAREN compound_stmt
                {
                        $$ = newDeclNode(FuncVarK);
                        $$->type = current_type;
						$$->attr.name=$2->attr.name;
                        $$->child[1] = $5;
                } 
                ; 
               
params          : param_list
                { $$=$1;}
                ;
                
param_list      :param_list COMMA param
                 {
                   YYSTYPE t=$1;
                   if(t!=NULL)
                   {
                        while(t->sibling!=NULL)
                          t=t->sibling;
                        t->sibling=$3;
                        $$=$1;
                   }
                   else
                         $$=$3;
                 }
                 |param
                 {$$=$1;} 
                 ;

param             : type_specifier id
                  {
                  $$=newDeclNode(SingleVarK);
                  $$->attr.name=$2->attr.name;
                  $$->type=current_type;
                  } 
                  | type_specifier id LBRACKET RBRACKET 
                  {
                  $$=newDeclNode(ArrayVarK);
                  $$->attr.name=$2->attr.name;
                  $$->type=current_type;
                  $$->array_size=0;
                  }
                  ; 
                  
compound_stmt       : LBC local_declarations RBC
                      {
                        $$ = newStmtNode(CompoundK);
                        $$->child[0] = $2;
                      }
                    | LBC local_declarations statement_list RBC
                      {
                        YYSTYPE t = $2;
                        if (t != NULL)
                        { 
                          while (t->sibling != NULL)
                            t = t->sibling;
                          t->sibling = $3;
                          $$ = newStmtNode(CompoundK);
                          $$->child[0] = $2;
                        }
                        else 
                        {
                          $$ = newStmtNode(CompoundK);
                          $$->child[0] = $3;
                        }
                      }
                    | LBC RBC
                      {
                        $$ = 0;
                      }
                    | LBC statement_list RBC
                      {
                        $$ = newStmtNode(CompoundK);
                        $$->child[0] = $2;
                      }
                    ;
                  
local_declarations : local_declarations var_declaration
                      { 
                        YYSTYPE t = $1;
                        if (t != NULL)
                        { 
                          while (t->sibling != NULL)
                            t = t->sibling;
                          t->sibling = $2;
                          $$ = $1; 
                        }
                        else 
                          $$ = $2;
                      }
                    | var_declaration
                      { $$ = $1; }
                    ;

statement_list      : statement_list statement
                      { 
                        YYSTYPE t = $1;
                        if (t != NULL)
                        { 
                          while (t->sibling != NULL)
                            t = t->sibling;
                          t->sibling = $2;
                          $$ = $1; 
                        }
                        else 
                          $$ = $2;
                      }
                    | statement
                      { $$ = $1; }
                    ;
                  
statement         : expression_stmt
                  {
                    $$=$1;
                  }
                  |compound_stmt
                  {
                    $$=$1;
                  }
                  |if_stmt
                  {
                    $$=$1;
                  }
                  |while_stmt
                  {
                    $$=$1;
                  }              
                  |return_stmt
                  {
                    $$=$1;
                  }
                  |assign_stmt
                  {
                    $$=$1;
                  }
                  |break_stmt
                  {
                    $$=$1;
                  }  
                  ;

expression_stmt    : expression SEMI
                  {
                      $$=newStmtNode(ExpStmtK);
                      $$->child[0]=$1;
                  }
                  |SEMI
                  ;
                  
if_stmt            : IF LPAREN expression RPAREN statement ELSE statement
                   {
                      $$=newStmtNode(IfK);
                      $$->child[0]=$3;
                      $$->child[1]=$5;
                      $$->child[2]=$7;
                   }
                   | IF LPAREN expression  RPAREN statement 
                   {
                      $$=newStmtNode(IfK);
                      $$->child[0]=$3;
                      $$->child[1]=$5;
                      $$->child[2]=0;                     
                   }                 
                   ;

⌨️ 快捷键说明

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