⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tiny.y

📁 这是一个基于命令行的编译器
💻 Y
字号:
/****************************************************//* File: tiny.y                                     *//* The TINY Yacc/Bison specification file           *//* Compiler Construction: Principles and Practice   *//* Kenneth C. Louden                                *//****************************************************/%{#define YYPARSER /* distinguishes Yacc output from other code files */#include "globals.h"#include "util.h"#include "scan.h"#include "parse.h"#define YYSTYPE TreeNode *static char * savedName; /* for use in assignments */static int savedLineNo;  /* ditto */static TreeNode * savedTree; /* stores syntax tree for later return */%}%token IF THEN ELSE END REPEAT UNTIL READ WRITE%token ID NUM %token ASSIGN EQ LT PLUS MINUS TIMES OVER LPAREN RPAREN SEMI%token ERROR %% /* Grammar for TINY */program     : stmt_seq                 { savedTree = $1;}             ;stmt_seq    : stmt_seq SEMI stmt                 { YYSTYPE t = $1;                   if (t != NULL)                   { while (t->sibling != NULL)                        t = t->sibling;                     t->sibling = $3;                     $$ = $1; }                     else $$ = $3;                 }            | stmt  { $$ = $1; }            ;stmt        : if_stmt { $$ = $1; }            | repeat_stmt { $$ = $1; }            | assign_stmt { $$ = $1; }            | read_stmt { $$ = $1; }            | write_stmt { $$ = $1; }            | error  { $$ = NULL; }            ;if_stmt     : IF exp THEN stmt_seq END                 { $$ = newStmtNode(IfK);                   $$->child[0] = $2;                   $$->child[1] = $4;                 }            | IF exp THEN stmt_seq ELSE stmt_seq END                 { $$ = newStmtNode(IfK);                   $$->child[0] = $2;                   $$->child[1] = $4;                   $$->child[2] = $6;                 }            ;repeat_stmt : REPEAT stmt_seq UNTIL exp                 { $$ = newStmtNode(RepeatK);                   $$->child[0] = $2;                   $$->child[1] = $4;                 }            ;assign_stmt : ID { savedName = copyString(tokenString);                   savedLineNo = lineno; }              ASSIGN exp                 { $$ = newStmtNode(AssignK);                   $$->child[0] = $4;                   $$->attr.name = savedName;                   $$->lineno = savedLineNo;                 }            ;read_stmt   : READ ID                 { $$ = newStmtNode(ReadK);                   $$->attr.name =                     copyString(tokenString);                 }            ;write_stmt  : WRITE exp                 { $$ = newStmtNode(WriteK);                   $$->child[0] = $2;                 }            ;exp         : simple_exp LT simple_exp                  { $$ = newExpNode(OpK);                   $$->child[0] = $1;                   $$->child[1] = $3;                   $$->attr.op = LT;                 }            | simple_exp EQ simple_exp                 { $$ = newExpNode(OpK);                   $$->child[0] = $1;                   $$->child[1] = $3;                   $$->attr.op = EQ;                 }            | simple_exp { $$ = $1; }            ;simple_exp  : simple_exp PLUS term                  { $$ = newExpNode(OpK);                   $$->child[0] = $1;                   $$->child[1] = $3;                   $$->attr.op = PLUS;                 }            | simple_exp MINUS term                 { $$ = newExpNode(OpK);                   $$->child[0] = $1;                   $$->child[1] = $3;                   $$->attr.op = MINUS;                 }             | term { $$ = $1; }            ;term        : term TIMES factor                  { $$ = newExpNode(OpK);                   $$->child[0] = $1;                   $$->child[1] = $3;                   $$->attr.op = TIMES;                 }            | term OVER factor                 { $$ = newExpNode(OpK);                   $$->child[0] = $1;                   $$->child[1] = $3;                   $$->attr.op = OVER;                 }            | factor { $$ = $1; }            ;factor      : LPAREN exp RPAREN                 { $$ = $2; }            | NUM                 { $$ = newExpNode(ConstK);                   $$->attr.val = atoi(tokenString);                 }            | ID { $$ = newExpNode(IdK);                   $$->attr.name =                         copyString(tokenString);                 }            | error { $$ = NULL; }            ;%%int yyerror(char * message){ fprintf(listing,"Syntax error at line %d: %s\n",lineno,message);  fprintf(listing,"Current token: ");  printToken(yychar,tokenString);  Error = TRUE;  return 0;}/* yylex calls getToken to make Yacc/Bison output * compatible with ealier versions of the TINY scanner */static int yylex(void){ return getToken(); }TreeNode * parse(void){ yyparse();  return savedTree;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -