📄 y.y
字号:
%{
/* YACC -- LALR(1) PARSER */
/* Sub-set of C Language */
#include <stdio.h>
#include <stdlib.h>
#include "y.tab.h"
extern FILE *yyin;
FILE *out;
extern int yylex();
%}
%token VOID INT CHAR RETURN IF ELSE WHILE
%token ID NUMBER RELOP LIMOP CHART STRING
%left '+' '-'
%left '*' '/'
%start program
%%
program : declaration_list { fprintf(out, "program -> declaration_list\n"); }
;
declaration_list : declaration_list declaration { fprintf(out, "declaration_list -> declaration_list declaration\n"); }
| declaration { fprintf(out, "declaration_list -> declaration\n"); }
;
declaration : var_declaration { fprintf(out, "declaration -> var_declaration\n"); }
| fun_declaration { fprintf(out, "declaration -> fun_declaration\n"); }
;
var_declaration : type_specifer ID ';' { fprintf(out, "var_declaration -> type_specifer ID;\n\n"); }
| type_specifer ID '[' NUMBER ']' ';' { fprintf(out, "var_declaration -> type_specifer ID[NUM];\n\n"); }
;
type_specifer : INT { fprintf(out, "type_specifer -> INT\n"); }
| VOID { fprintf(out, "type_specifer -> VOID\n"); }
;
fun_declaration : type_specifer ID '(' params ')' compound_stmt { fprintf(out, "fun_declaration -> type_specifer ID (params) compound_stmt\n\n\n"); }
| type_specifer ID '(' params ')' ';' { fprintf(out, "fun_declaration -> type_specifer ID (params)\n\n\n"); }
;
params : param_list { fprintf(out, "params -> param_list\n"); }
| VOID { fprintf(out, "params -> VOID\n"); }
;
param_list : param_list ',' param { fprintf(out, "param_list -> param_list, param\n"); }
| param { fprintf(out, "param_list -> param\n"); }
;
param : type_specifer ID { fprintf(out, "param -> type_specifer ID\n"); }
| type_specifer ID '[' ']' { fprintf(out, "param -> type_specifer ID[]\n"); }
;
compound_stmt : '{' local_declarations statement_list '}' { fprintf(out, "compound_stmt -> {local_declarations statement_list}\n"); }
;
local_declarations : local_declarations var_declaration { fprintf(out, "local_declarations -> local_declarations var_declaration\n"); }
| { fprintf(out, "local_declaration -> $\n"); }
;
statement_list : statement_list statement { fprintf(out, "statement_list -> statement_list statement\n"); }
|
;
statement : expression_stmt { fprintf(out, "statement -> expression_stmt\n\n"); }
| compound_stmt { fprintf(out, "statement -> compound_stmt\n\n"); }
| if_stmt { fprintf(out, "statement -> if_stmt\n\n"); }
| while_stmt { fprintf(out, "statement -> while_stmt\n\n"); }
| return_stmt { fprintf(out, "statement -> return_stmt\n\n"); }
;
expression_stmt : expression ';' { fprintf(out, "expression_stmt -> expression;\n"); }
| ';' { fprintf(out, "expression_stmt -> ;\n"); }
;
if_stmt : IF '(' expression ')' statement { fprintf(out, "if_stmt -> IF (expression) statement\n"); }
| IF '(' expression ')' statement ELSE statement { fprintf(out, "if_stmt -> IF (expression) statement ELSE statement\n"); }
;
while_stmt : WHILE '(' expression ')' statement { fprintf(out, "WHILE (expression) statement\n"); }
;
return_stmt : RETURN ';' { fprintf(out, "RETURN;\n"); }
| RETURN expression ';' { fprintf(out, "RETURN expression;\n"); }
;
expression : var '=' expression { fprintf(out, "expression -> var = expression\n"); }
| simple_expression { fprintf(out, "expression -> simple_expression\n"); }
;
var : ID { fprintf(out, "var -> ID\n"); }
| ID '[' expression ']' { fprintf(out, "var -> ID[expression]\n"); }
;
simple_expression : additive_expression RELOP additive_expression { fprintf(out, "simple_expression -> additive_expression relop additive_expression\n"); }
| additive_expression { fprintf(out, "simple_expression -> additive_expression\n"); }
;
additive_expression : additive_expression addop term { fprintf(out, "additive_expression -> additive_expression addop term\n"); }
| term { fprintf(out, "additive_expression -> term\n"); }
;
addop : '+' { fprintf(out, "addop -> +\n"); }
| '-' { fprintf(out, "addop -> -\n"); }
;
term : term mulop factor { fprintf(out, "term -> term mulop factor\n"); }
| factor { fprintf(out, "term -> factor\n"); }
;
mulop : '*' { fprintf(out, "mulop -> *\n"); }
| '/' { fprintf(out, "mulop -> /\n"); }
;
factor : '(' expression ')' { fprintf(out, "factor -> (expression)\n"); }
| var { fprintf(out, "factor -> var\n"); }
| call { fprintf(out, "facotr -> call\n"); }
| NUMBER { fprintf(out, "factor -> NUMBER\n"); }
;
call : ID '(' args ')' { fprintf(out, "call -> ID (args)\n"); }
;
args : arg_list { fprintf(out, "args -> arg_list\n"); }
| { fprintf(out, "args -> $\n"); }
;
arg_list : arg_list ',' expression { fprintf(out, "arg_list -> arg_list, expression\n"); }
| expression { fprintf(out, "arg_list expression\n"); }
;
%%
void yyerror(const char *msg)
{
fprintf(out, "ERROR: %s\n", msg);
}
int main(int argc, char *argv[])
{
if (argc > 2)
{
yyin = fopen(argv[1], "r");
if (!yyin)
{
fprintf(stderr, "Can not open %s\n", argv[1]);
exit(1);
}
out = fopen(argv[2], "w");
if (!out)
{
fprintf(stderr, "Can not open %s\n", argv[2]);
exit(1);
}
yyparse();
fclose(yyin);
fclose(out);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -