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

📄 cm_parse.y

📁 小型编译系统的源代码
💻 Y
📖 第 1 页 / 共 2 页
字号:
%{
#define YYPARSER /* distinguishes Yacc output from other code files */
//#define YYDEBUG

#include "globals.h"
#include "util.h"
#include "scan.h"
#include "parse.h"

#define YYSTYPE TreeNode *

static char * savedName;        /* for use in assignments */
static int savedLineNo;         /* ditto */
static TreeNode * savedTree;    /* stores syntax tree for later return */

%}

%token INT CHAR FLOAT VOID IF ELSE WHILE FOR RETURN
%token ID NUM FLOATNUM CONSTCHAR
%token AND OR NOT ASSIGN EQ LT LE GT GE NE PLUS MINUS TIMES OVER LPAREN RPAREN SEMICOLON COMMA LBRACKET RBRACKET LBRACE RBRACE
%token ERROR ENDFILE

%% /* Grammar for C- */

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;
                       }
                    | empty
                    	{ $$ = 0; }                       
/*                    | declaration  
                      { $$ = $1; }*/
                    ;
declaration         : var_declaration 
                      { $$ = $1; }
                    | fun_declaration 
                      { $$ = $1; }
                    | fun_definition
                      { $$ = $1; }
                    ;
var_declaration     : type_specifier idlist SEMICOLON
                      {
                        $$ = newDeclNode(VarDeclK);
                        $$->child[0] = $1;
                        $$->child[1] = $2;
                      }
                    ;
idlist							: idlist COMMA id
											{
												YYSTYPE t = $1;
                        if (t!=NULL){
                          while (t->sibling!=NULL)
                            t=t->sibling;
                          t->sibling = $3;
                          $$ = $1;
                        }
                        else 
                          $$ = $3;
											}
										| idlist COMMA array//id LBRACKET NUM { $5 = newExpNode(ConstK); $5->attr.vali = atoi(tokenString); } RBRACKET
											{
												YYSTYPE t = $1;
                        if (t!=NULL){
                          while (t->sibling!=NULL)
                            t=t->sibling;
                          t->sibling = $3;
                          $$ = $1;
                        }
                        else
                          $$ = $3;
											}
										| array
											{
												$$ = $1;
											}
										| id
											{
												$$ = $1;
											}
										;
array								: id LBRACKET NUM { $3 = newExpNode(ConstK); $3->attr.vali = atoi(tokenString); $3->attr.vartype = Integer; } RBRACKET
											{
												$$ = newExpNode(ArrayK);
												$$->attr.name = $1->attr.name;
												delete($1);
                        $$->child[0] = $3;
											}
										;
type_specifier      : INT 
                      { 
                        $$ = newExpNode(TypeK);
                        $$->attr.vartype = Integer;
                      }
                    | CHAR
                      { 
                        $$ = newExpNode(TypeK);
                        $$->attr.vartype = Char;
                      }
                    | FLOAT
                      {
                        $$ = newExpNode(TypeK);
                        $$->attr.vartype = Float;
                      }
                    | VOID
                      {
                        $$ = newExpNode(TypeK);
                        $$->attr.vartype = Void;
                      }
                    ;
id									:	ID
											{
												$$ = newExpNode(IdK);
												$$->lineno = lineno;
												$$->attr.name = copyString(tokenString);
											}
										;
fun_declaration     : type_specifier id LPAREN params RPAREN SEMICOLON
                      {
                        $$ = newDeclNode(FuncDeclK);
                        $$->child[0] = $1;
                        $$->child[1] = $2;
                        $$->child[2] = $4;
                      }
                    | type_specifier id LPAREN RPAREN SEMICOLON
                      {
                        $$ = newDeclNode(FuncDeclK);
                        $$->child[0] = $1;
                        $$->child[1] = $2;
                      }
                    ;
fun_definition      : type_specifier id LPAREN params RPAREN compound_stmt
                      {
                        $$ = newDeclNode(FuncDefK);
                        $$->child[0] = $1;
                        $$->child[1] = $2;
                        $$->child[2] = $4;
                        $$->child[3] = $6;
                      }
                    | type_specifier id LPAREN RPAREN compound_stmt
                      {
                        $$ = newDeclNode(FuncDefK);
                        $$->child[0] = $1;
                        $$->child[1] = $2;
                        $$->child[3] = $5;
                      }
                    ;
params              : param_list
                      { $$ = $1; }
                    | VOID
                      { $$ = 0; }
                    | empty
                    	{ $$ = 0; }
                    ;
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(ParamK);
                        $$->child[0] = $1;
                        $2 = newExpNode(IdK);
                        $2->lineno = lineno;
                        $2->attr.name = copyString(tokenString);
                        $2->varK = Norm;
                        $$->child[1] = $2;
                      }
                    | type_specifier ID {$2 = newExpNode(ArrayK); $2->attr.name = copyString(tokenString);} LBRACKET RBRACKET
                      {
                        $$ = newDeclNode(ParamK);
                        $$->child[0] = $1;
                        $$->child[1] = $2;
                        $2->varK = Array;
                        $2->child[0] = 0;
                      }
                    ;
compound_stmt       : LBRACE local_declarations RBRACE
                      {
                        $$ = newStmtNode(CompoundK);
                        $$->child[0] = $2;
                      }
                    | LBRACE local_declarations statement_list RBRACE
                      {
                        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;
                        }
                      }
                    | LBRACE RBRACE
                      {
                        $$ = 0;
                      }
                    | LBRACE statement_list RBRACE
                      {
                        $$ = 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;
                      }
                    | selection_stmt
                      {
                        $$ = $1;
                      }
                    | while_stmt
                      {
                        $$ = $1;
                      }
                    | for_stmt

⌨️ 快捷键说明

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