VVVYGD
96 天前
我已经用 AI 转换好语法了,希望你把这门语言发🐑光大
she.y
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void yyerror(const char *s) { fprintf(stderr, "Error: %s\n", s); }
int yylex(void);
%}
%token IDENTIFIER NUMBER
%token FUNC_START IF ELSE RETURN
%left '+' '-' '*'
%%
program:
function_list statement_list
;
function_list:
/* empty */
| function_list function_def
;
function_def:
IDENTIFIER '=' FUNC_START '(' IDENTIFIER ')' '{' statement_list '}'
;
statement_list:
/* empty */
| statement_list statement
;
statement:
IF '(' expression ')' '{' statement_list '}'
| ELSE '{' statement_list '}'
| RETURN expression ';'
| expression ';'
;
expression:
NUMBER
| IDENTIFIER
| IDENTIFIER '(' expression ')' /* function call */
| expression '+' expression
| expression '-' expression
;
%%
she.l
%{
#include "y.tab.h"
%}
%%
"想要你一个态度" return FUNC_START;
"姐妹们觉得呢" return IF;
"我接受不等于我同意" return ELSE;
"反手举报" return RETURN;
[0-9]+ { yylval = atoi(yytext); return NUMBER; }
[a-zA-Z_][a-zA-Z0-9_]* return IDENTIFIER;
"(" return '(';
")" return ')';
"{" return '{';
"}" return '}';
";" return ';';
"=" return '=';
"+" return '+';
"-" return '-';
[ \t\r\n]+ ; // skip whitespace
. { printf("Unknown char: %s\n", yytext); }
%%