1 Star 0 Fork 0

sen/TinyCompiler

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
grammar.y 7.73 KB
一键复制 编辑 原始数据 按行查看 历史
Bo Pang 提交于 2017-06-11 20:36 . 06-11 20:36 fix operator priority
%{
#include "ASTNodes.h"
#include <stdio.h>
NBlock* programBlock;
extern int yylex();
void yyerror(const char* s)
{
printf("Error: %s\n", s);
}
%}
%union
{
NBlock* block;
NExpression* expr;
NStatement* stmt;
NIdentifier* ident;
NVariableDeclaration* var_decl;
NArrayIndex* index;
std::vector<shared_ptr<NVariableDeclaration>>* varvec;
std::vector<shared_ptr<NExpression>>* exprvec;
std::string* string;
int token;
}
%token <string> TIDENTIFIER TINTEGER TDOUBLE TYINT TYDOUBLE TYFLOAT TYCHAR TYBOOL TYVOID TYSTRING TEXTERN TLITERAL
%token <token> TCEQ TCNE TCLT TCLE TCGT TCGE TEQUAL
%token <token> TLPAREN TRPAREN TLBRACE TRBRACE TCOMMA TDOT TSEMICOLON TLBRACKET TRBRACKET TQUOTATION
%token <token> TPLUS TMINUS TMUL TDIV TAND TOR TXOR TMOD TNEG TNOT TSHIFTL TSHIFTR
%token <token> TIF TELSE TFOR TWHILE TRETURN TSTRUCT
%type <index> array_index
%type <ident> ident primary_typename array_typename struct_typename typename
%type <expr> numeric expr assign
%type <varvec> func_decl_args struct_members
%type <exprvec> call_args
%type <block> program stmts block
%type <stmt> stmt var_decl func_decl struct_decl if_stmt for_stmt while_stmt
%type <token> comparison
%left TPLUS TMINUS
%left TMUL TDIV TMOD
%start program
%%
program : stmts { programBlock = $1; }
;
stmts : stmt { $$ = new NBlock(); $$->statements->push_back(shared_ptr<NStatement>($1)); }
| stmts stmt { $1->statements->push_back(shared_ptr<NStatement>($2)); }
;
stmt : var_decl | func_decl | struct_decl
| expr { $$ = new NExpressionStatement(shared_ptr<NExpression>($1)); }
| TRETURN expr { $$ = new NReturnStatement(shared_ptr<NExpression>($2)); }
| if_stmt
| for_stmt
| while_stmt
;
block : TLBRACE stmts TRBRACE { $$ = $2; }
| TLBRACE TRBRACE { $$ = new NBlock(); }
;
primary_typename : TYINT { $$ = new NIdentifier(*$1); $$->isType = true; delete $1; }
| TYDOUBLE { $$ = new NIdentifier(*$1); $$->isType = true; delete $1; }
| TYFLOAT { $$ = new NIdentifier(*$1); $$->isType = true; delete $1; }
| TYCHAR { $$ = new NIdentifier(*$1); $$->isType = true; delete $1; }
| TYBOOL { $$ = new NIdentifier(*$1); $$->isType = true; delete $1; }
| TYVOID { $$ = new NIdentifier(*$1); $$->isType = true; delete $1; }
| TYSTRING { $$ = new NIdentifier(*$1); $$->isType = true; delete $1; }
array_typename : primary_typename TLBRACKET TINTEGER TRBRACKET {
$1->isArray = true;
$1->arraySize->push_back(make_shared<NInteger>(atol($3->c_str())));
$$ = $1;
}
| array_typename TLBRACKET TINTEGER TRBRACKET {
$1->arraySize->push_back(make_shared<NInteger>(atol($3->c_str())));
$$ = $1;
}
struct_typename : TSTRUCT ident {
$2->isType = true;
$$ = $2;
}
typename : primary_typename { $$ = $1; }
| array_typename { $$ = $1; }
| struct_typename { $$ = $1; }
var_decl : typename ident { $$ = new NVariableDeclaration(shared_ptr<NIdentifier>($1), shared_ptr<NIdentifier>($2), nullptr); }
| typename ident TEQUAL expr { $$ = new NVariableDeclaration(shared_ptr<NIdentifier>($1), shared_ptr<NIdentifier>($2), shared_ptr<NExpression>($4)); }
| typename ident TEQUAL TLBRACKET call_args TRBRACKET {
$$ = new NArrayInitialization(make_shared<NVariableDeclaration>(shared_ptr<NIdentifier>($1), shared_ptr<NIdentifier>($2), nullptr), shared_ptr<ExpressionList>($5));
}
;
func_decl : typename ident TLPAREN func_decl_args TRPAREN block
{ $$ = new NFunctionDeclaration(shared_ptr<NIdentifier>($1), shared_ptr<NIdentifier>($2), shared_ptr<VariableList>($4), shared_ptr<NBlock>($6)); }
| TEXTERN typename ident TLPAREN func_decl_args TRPAREN { $$ = new NFunctionDeclaration(shared_ptr<NIdentifier>($2), shared_ptr<NIdentifier>($3), shared_ptr<VariableList>($5), nullptr, true); }
func_decl_args : /* blank */ { $$ = new VariableList(); }
| var_decl { $$ = new VariableList(); $$->push_back(shared_ptr<NVariableDeclaration>($<var_decl>1)); }
| func_decl_args TCOMMA var_decl { $1->push_back(shared_ptr<NVariableDeclaration>($<var_decl>3)); }
;
ident : TIDENTIFIER { $$ = new NIdentifier(*$1); delete $1; }
;
numeric : TINTEGER { $$ = new NInteger(atol($1->c_str())); }
| TDOUBLE { $$ = new NDouble(atof($1->c_str())); }
;
expr : assign { $$ = $1; }
| ident TLPAREN call_args TRPAREN { $$ = new NMethodCall(shared_ptr<NIdentifier>($1), shared_ptr<ExpressionList>($3)); }
| ident { $<ident>$ = $1; }
| ident TDOT ident { $$ = new NStructMember(shared_ptr<NIdentifier>($1), shared_ptr<NIdentifier>($3)); }
| numeric
| expr comparison expr { $$ = new NBinaryOperator(shared_ptr<NExpression>($1), $2, shared_ptr<NExpression>($3)); }
| expr TMOD expr { $$ = new NBinaryOperator(shared_ptr<NExpression>($1), $2, shared_ptr<NExpression>($3)); }
| expr TMUL expr { $$ = new NBinaryOperator(shared_ptr<NExpression>($1), $2, shared_ptr<NExpression>($3)); }
| expr TDIV expr { $$ = new NBinaryOperator(shared_ptr<NExpression>($1), $2, shared_ptr<NExpression>($3)); }
| expr TPLUS expr { $$ = new NBinaryOperator(shared_ptr<NExpression>($1), $2, shared_ptr<NExpression>($3)); }
| expr TMINUS expr { $$ = new NBinaryOperator(shared_ptr<NExpression>($1), $2, shared_ptr<NExpression>($3)); }
| TLPAREN expr TRPAREN { $$ = $2; }
| TMINUS expr { $$ = nullptr; /* TODO */ }
| array_index { $$ = $1; }
| TLITERAL { $$ = new NLiteral(*$1); delete $1; }
;
array_index : ident TLBRACKET expr TRBRACKET
{ $$ = new NArrayIndex(shared_ptr<NIdentifier>($1), shared_ptr<NExpression>($3)); }
| array_index TLBRACKET expr TRBRACKET
{
$1->expressions->push_back(shared_ptr<NExpression>($3));
$$ = $1;
}
assign : ident TEQUAL expr { $$ = new NAssignment(shared_ptr<NIdentifier>($1), shared_ptr<NExpression>($3)); }
| array_index TEQUAL expr {
$$ = new NArrayAssignment(shared_ptr<NArrayIndex>($1), shared_ptr<NExpression>($3));
}
| ident TDOT ident TEQUAL expr {
auto member = make_shared<NStructMember>(shared_ptr<NIdentifier>($1), shared_ptr<NIdentifier>($3));
$$ = new NStructAssignment(member, shared_ptr<NExpression>($5));
}
;
call_args : /* blank */ { $$ = new ExpressionList(); }
| expr { $$ = new ExpressionList(); $$->push_back(shared_ptr<NExpression>($1)); }
| call_args TCOMMA expr { $1->push_back(shared_ptr<NExpression>($3)); }
comparison : TCEQ | TCNE | TCLT | TCLE | TCGT | TCGE
| TAND | TOR | TXOR | TSHIFTL | TSHIFTR
;
if_stmt : TIF expr block { $$ = new NIfStatement(shared_ptr<NExpression>($2), shared_ptr<NBlock>($3)); }
| TIF expr block TELSE block { $$ = new NIfStatement(shared_ptr<NExpression>($2), shared_ptr<NBlock>($3), shared_ptr<NBlock>($5)); }
| TIF expr block TELSE if_stmt {
auto blk = new NBlock();
blk->statements->push_back(shared_ptr<NStatement>($5));
$$ = new NIfStatement(shared_ptr<NExpression>($2), shared_ptr<NBlock>($3), shared_ptr<NBlock>(blk));
}
for_stmt : TFOR TLPAREN expr TSEMICOLON expr TSEMICOLON expr TRPAREN block { $$ = new NForStatement(shared_ptr<NBlock>($9), shared_ptr<NExpression>($3), shared_ptr<NExpression>($5), shared_ptr<NExpression>($7)); }
while_stmt : TWHILE TLPAREN expr TRPAREN block { $$ = new NForStatement(shared_ptr<NBlock>($5), nullptr, shared_ptr<NExpression>($3), nullptr); }
struct_decl : TSTRUCT ident TLBRACE struct_members TRBRACE {$$ = new NStructDeclaration(shared_ptr<NIdentifier>($2), shared_ptr<VariableList>($4)); }
struct_members : /* blank */ { $$ = new VariableList(); }
| var_decl { $$ = new VariableList(); $$->push_back(shared_ptr<NVariableDeclaration>($<var_decl>1)); }
| struct_members var_decl { $1->push_back(shared_ptr<NVariableDeclaration>($<var_decl>2)); }
%%
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/thzsen/TinyCompiler.git
git@gitee.com:thzsen/TinyCompiler.git
thzsen
TinyCompiler
TinyCompiler
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385