📄 myparser.y
字号:
%{
/****************************************************************************
myparser.y
ParserWizard generated YACC file.
Date: 2006年4月25日
****************************************************************************/
#include <ctype.h>
#include <stdio.h>
#include "lex.h"
#define YYSTYPE double /* double type for YACC stack */
%}
/////////////////////////////////////////////////////////////////////////////
// declarations section
// place any declarations here
%token
PROGRAM BEGIN END CONST ARRAY IF THEN ELSE
WHILE DO REPEAT UNTIL FOR TO READ WRITE
INTEGER REAL BOOLEAN
ID
INT_CONS REAL_CONS TRUE FALSE
PLUS MINUS TIMES DIVIDE
EQ NEQ LT LE GT GE AND OR NOT ASSIGN
COMMA COLON SEMICOLON
LPAR RPAR LBRACK RBRACK LBRACE RBRACE
//preferences
%nonassoc THEN
%nonassoc ELSE //优先级的问题,then比else低,才不会产生冲突
%left AND OR
%right NOT
%nonassoc EQ NEQ LT LE GT GE
%left PLUS MINUS
%left TIMES DIVIDE
%right UMINUS
%start prog
%%
/////////////////////////////////////////////////////////////////////////////
// rules section
// place your YACC rules here (there must be at least one)
prog : PROGRAM ID SEMICOLON decl BEGIN sentStr END
;
decl : CONST constDef SEMICOLON
| varDecl SEMICOLON
| funcDecl //???, delete the SEMICOLON
;
constDef : ID EQ cons
| constDef SEMICOLON ID EQ cons
;
cons : INT_CONS
| REAL_CONS
| TRUE
| FALSE
;
varDecl : type COLON idList
| type COLON arrayList
| varDecl SEMICOLON type COLON idList
| varDecl SEMICOLON type COLON arrayList
;
type : INTEGER //???
| REAL
| BOOLEAN
;
idList : ID
| idList COMMA ID
;
arrayList : ARRAY ID LBRACK dimDecl RBRACK
| arrayList COMMA ARRAY ID LBRACK dimDecl RBRACK
;
dimDecl : INT_CONS //???, undefined, right_recursion??, POS_INT_CONS is not needed??
| dimDecl COMMA INT_CONS
;
funcDecl : type ID LPAR funcParam RPAR LBRACE sentStr RBRACE //???
| funcDecl type ID LPAR funcParam RPAR LBRACE sentStr RBRACE
;
//funcDecl : type ID LPAR funcParam RPAR LBRACE sentStr RBRACE;
funcParam : type COLON ID
| funcParam COMMA type COLON ID
;
sentStr : sent
| sentStr SEMICOLON sent
;
sent : ID ASSIGN expr //??? var->ID
| IF expr THEN sent
| IF expr THEN sent ELSE sent
| WHILE expr DO sent
| REPEAT sent UNTIL expr //??? sentStr->sent
| FOR ID ASSIGN expr TO expr DO sent
| BEGIN sentStr END
| READ idList
| WRITE exprList
| funcCall
;
exprList : expr
| exprList COMMA expr
;
expr : expr EQ expr
| expr NEQ expr
| expr LT expr
| expr LE expr
| expr GT expr
| expr GE expr
| expr AND expr
| expr OR expr
| NOT expr
| expr PLUS expr
| expr MINUS expr
| expr TIMES expr
| expr DIVIDE expr //delete MOD
| LPAR expr RPAR
| MINUS expr %prec UMINUS
| INT_CONS
| REAL_CONS
| TRUE
| FALSE
| ID
| funcCall
;
funcCall : ID LPAR funcCall_Param RPAR
;
funcCall_Param : cons
| ID
| funcCall_Param COMMA cons
| funcCall_Param COMMA ID
;
%%
/////////////////////////////////////////////////////////////////////////////
// programs section
void yyerror(char *s)
{
fprintf(stderr, "error:%s\n", s);
return;
}
int yygettoken(void)
{
int c;
c = word();
/*while ((c = getchar()) == ' ');
if (c == '.' || isdigit(c)) {
ungetc(c, stdin);
// place your token retrieving code here
scanf("%lf", &yylval);
return NUMBER;
}*/
if(c==REAL_CONS)
yylval = realToken;
if(c==INT_CONS)
yylval = intToken;
if(c==TRUE)
yylval = 1;
if(c==FALSE)
yylval = 0;
return c;
}
int main(void)
{
char sourcefile[20], destfile[20];
printf("输入源文件名。\n");
scanf("%s", sourcefile);
fp_r = fopen(sourcefile, "r");
printf("输入目标文件名。\n");
scanf("%s", destfile);
fp_w = fopen(destfile, "w");
if(!fp_r || !fp_w)
{
printf("IO operation failed.\n");
return 0;
}
init(); //lex.h
return yyparse();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -