?? 復件 parse.y
字號:
%{
#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;
}
;
params : param_list
{$$=$1;}
|VOID
{$$=NULL;}
|
;
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
{
$$ = NULL;
}
| 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 SEMI
{
$$=newStmtNode(IfK);
$$->child[0]=$3;
$$->child[1]=$5;
$$->child[2]=NULL;
}
| IF LPAREN expression RPAREN statement ELSE statement SEMI
{
$$=newStmtNode(IfK);
$$->child[0]=$3;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -