1 Star 0 Fork 0

sen/ThLang

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
parser.y 2.40 KB
Copy Edit Raw Blame History
authored 2024-08-05 14:08 . Update parser.y
%{
#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
#include <cstdio>
#include <string>
#include "nn.h"
int yylex();
int findMapping(const char *type);
void addMapping(const char *type, int token);
void yyerror(const char *s) { printf("Error: %s\n", s); }
int nums;
std::vector<Node*> program;
%}
%union{
std::string *string;
int token;
//char* types;
Node *node;
Type_ *types;
std::string *values;
}
%token <token> TEQUAL
%token <token> NEWLINE SEMICOLON COMMA LPAREN RPAREN
%token <string> NAME INT STRING FLOAT
%token ERRORTOKEN
%type <values> types
%type <node> value
%%
program :
| program stmts { /* 语义动作 */std::cout<<"\n"<<std::endl; }
| program stmts NEWLINE{ /* 语义动作 */std::cout<<"\n"<<std::endl; }
;
stmts : stmt {std::cout<< "语句_单独\n";}
| stmts SEMICOLON stmt {std::cout<< "语句同行分号\n";}
| stmts SEMICOLON {std::cout<<"语句sss分号结尾\n";}
| stmts NEWLINE stmt {std::cout<<"语句\n";}
;
stmt : var {std::cout<<"语句 "<<"\n";}
;
var: var_type_stmt
| var_stmt
;
var_type_stmt:
types NAME {std::cout<<$1<<" "<<$2<<std::endl;nums++;}
| error { /*yyerror("Syntax error"); */}
;
var_stmt:
types NAME TEQUAL value { /* 语义动作 */std::cout<<*$1<<" "<<*$2<<" "<<$4<<std::endl; nums++;}
;
types: NAME {$$ = $1;}
;
value: INT {$$ = new IntAst(atol($1->c_str()));std::cout<<"整数值\n";program.push_back($$);delete $1;}
| STRING {$$ = new StringAst($1->c_str());program.push_back($$);delete $1;}
| NAME {$$ = new NameAst($1->c_str());program.push_back($$);delete $1;}
| FLOAT {$$ = new FloatAst(stold(*$1));program.push_back($$);delete $1;}
| error { /*yyerror("Syntax error"); */}
;
/*
value : INT {
std::string tips = "your int is ";
std::string result = tips + std::to_string($1);
char* str = new char[result.length() + 1];
strcpy(str, result.c_str());
delete $1;
$$ = str;
}
| NAME {
std::string tips = "your NAME is ";
std::string result = tips + $1;
char* str = new char[result.length() + 1];
strcpy(str, result.c_str());
delete $1;
$$ = str;
}
| STRING {
std::string tips = "your STRING is ";
std::string result = tips + $1;
char* str = new char[result.length() + 1];
strcpy(str, result.c_str());
$$ = str;
}
;
*/
%%
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/thzsen/ThLang.git
git@gitee.com:thzsen/ThLang.git
thzsen
ThLang
ThLang
main

Search

0d507c66 1850385 C8b1a773 1850385