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

📄 ccc_1.y

📁 c语言的简化编译器
💻 Y
字号:
/* subset of c, yacc grammer. */
/* version 0.01               */
/* chenhongyu. 2004-9-15      */

%{
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include "error.h"
#include "tree.h" 


int yylex();

extern SCRIPTCOLLECTION *thescriptcollection;
%}


%start scriptcollection

%union {
  struct SCRIPTCOLLECTION *scriptcollection;    
  struct TOPLEVEL *toplevel;
  struct FUNCTION *function;  
  struct TYPE *type;
  struct EXP *exp;
  struct DECL *decl;
  struct FORINIT *forinit;
  struct STM *stm;
  struct LVALUE *lvalue;
  
  char *identifier;
  int intconst;
  int boolconst; 
  char *stringconst;
};

%token tINT tVOID tBOOL tSTRING
%token tAND tOR tEQUALS tGEQUALS tLEQUALS tNEQUALS
%token tIF tELSE tWHILE tFOR tRETURN tBREAK tCONTINUE

%token tERROR 

%token <identifier> tIDENTIFIER
%token <intconst> tINTCONST
%token <boolconst> tBOOLCONST
%token <stringconst> tSTRINGCONST

%type <scriptcollection> scriptcollection
%type <toplevel> toplevels netoplevels toplevel
%type <function> function
%type <type> type
%type <exp> initialization exp unaryexp unarypostfixexp exps neexps
%type <decl> simpledecl formals neformals formal
%type <forinit> forinits neforinits forinit
%type <stm> nestms stm compoundstm
%type <lvalue> lvalue

/* Resolve dangling else conflict: */
%left ')'
%left tELSE

/* Resolve binary operator conflicts: */
%left '='
%left tOR tAND
%left tEQUALS tNEQUALS tLEQUALS tGEQUALS '<' '>'
%left '+' '-'
%left '*' '/'
%left '%'


%%
scriptcollection : toplevels 
          { thescriptcollection = makeSCRIPTCOLLECTION($1); } 
;

toplevels : /* empty */ 
            {$$ = NULL;}
          | netoplevels
            {$$ = $1;}
;

netoplevels : toplevel
              {$$ = $1;}  
            | toplevel netoplevels
              {$$ = $1; $$->next = $2;} 
;

toplevel : function 
           {$$ = makeTOPLEVELfunction($1);}
         | simpledecl ';'
           {$$ = makeTOPLEVELsimpledecl($1);}
;

simpledecl : type tIDENTIFIER initialization
             {$$ = makeDECLsimplevar($1,$2,$3);}
;

initialization : /* empty */ 
                 {$$ = NULL;}
               | '=' exp
                 {$$ = $2;}
;

type : tINT 
       {$$ = makeTYPEint();}
     | tBOOL 
       {$$ = makeTYPEbool();}
     | tSTRING 
       {$$ = makeTYPEstring();} 
;

function : type tIDENTIFIER '(' formals ')' compoundstm
           {$$ = makeFUNCTION($2,$1,$4,$6);}
         | tVOID tIDENTIFIER '(' formals ')' compoundstm
           {$$ = makeFUNCTION($2, makeTYPEvoid(), $4, $6);} 

;

formals : /* empty */ 
            {$$ = NULL;}
        | neformals
            {$$ = $1;}
;

neformals : formal 
            {$$ = $1;}
          | formal ',' neformals 
            {$$ = $1; $$->next = $3;}
;

formal : type tIDENTIFIER
         {$$ = makeDECLformal($1,$2);}
;

compoundstm : '{' '}'
              {$$ = NULL;}
            | '{' nestms '}'
              {$$ = $2;}      
;

nestms : stm
         {$$ = $1;}
       | nestms stm
         {$$ = makeSTMsequence($1,$2);}
;


stm : ';'
      {$$ = makeSTMskip();}
    | tRETURN ';'
      {$$ = makeSTMreturn(NULL);}
    | tRETURN exp ';'
      {$$ = makeSTMreturn($2);}
    | tIF '(' exp ')' stm
      {$$ = makeSTMif($3,makeSTMscope($5));}
    | tIF '(' exp ')' stm tELSE stm
      {$$ = makeSTMifelse($3,makeSTMscope($5),makeSTMscope($7));}
    | tWHILE '(' exp ')' stm
      {$$ = makeSTMwhile($3,$5);}
    | tFOR '(' forinits ';' exp ';' exps ')' stm
      {$$ = makeSTMfor($3,$5,$7,makeSTMscope($9));}
    | compoundstm
      {$$ = makeSTMscope($1);}
    | simpledecl
      {$$ = makeSTMdecl($1);}
    | exp ';'
      {$$ = makeSTMexp($1);}  
;

forinits : /* empty */
           {$$ = NULL;}
         | neforinits
           {$$ = $1;}
;

neforinits : forinit
             {$$ = $1;}
           | forinit ',' neforinits
             {$$ = $1; $$->next = $3;}
;

forinit : simpledecl
          {$$ = makeFORINITdecl($1);}
        | exp
          {$$ = makeFORINITexp($1);}
;



exp : lvalue '=' exp
      {$$ = makeEXPassignment($1,$3);}
    | exp tEQUALS exp
      {$$ = makeEXPequals($1,$3);}
    | exp tNEQUALS exp
      {$$ = makeEXPnequals($1,$3);}
    | exp '<' exp
      {$$ = makeEXPless($1,$3);}
    | exp '>' exp
      {$$ = makeEXPgreater($1,$3);}
    | exp tLEQUALS exp
      {$$ = makeEXPlequals($1,$3);}
    | exp tGEQUALS exp
      {$$ = makeEXPgequals($1,$3);}
    | exp '+' exp
      {$$ = makeEXPplus($1,$3);}
    | exp '-' exp
      {$$ = makeEXPminus($1,$3);}
    | exp '*' exp
      {$$ = makeEXPmult($1,$3);}
    | exp '/' exp
      {$$ = makeEXPdiv($1,$3);}
    | exp '%' exp
      {$$ = makeEXPmodulo($1,$3);}
    | exp tAND exp
      {$$ = makeEXPand($1,$3);}
    | exp tOR exp
      {$$ = makeEXPor($1,$3);}
    | unaryexp
      {$$ = $1;}
;

unaryexp : '-' unaryexp
           {$$ = makeEXPuminus($2);}
         | '!' unaryexp
           {$$ = makeEXPnot($2);}
         | '(' type ')' unaryexp
           {$$ = makeEXPcast($2,$4);}
         | unarypostfixexp
           {$$ = $1;}
;

unarypostfixexp : tINTCONST
                  {$$ = makeEXPintconst($1);}
                | tBOOLCONST
                  {$$ = makeEXPboolconst($1);}
                | tSTRINGCONST
                  {$$ = makeEXPstringconst($1);}
                | lvalue
                  {$$ = makeEXPlvalue($1);}
                | '(' exp ')'
                  {$$ = $2;}
                | tIDENTIFIER '(' exps ')'          
                  {$$ = makeEXPcall($1,$3);}
;

exps : /* empty */ 
       {$$ = NULL;}
     | neexps
       {$$ = $1;}
;

neexps : exp 
         {$$ = $1;}
       | exp ',' neexps 
         {$$ = $1; $$->next = $3;}
;

lvalue : tIDENTIFIER
         {$$ = makeLVALUEid($1);}
;




⌨️ 快捷键说明

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