%{ #include extern void yyerror(char *); %} %token YASSIGN %token YBEGIN %token YCOLON %token YCOMMA %token YCONST %token YDOT %token YEND %token YEQUALS %token YIDENTIFIER %token YINTEGER %token YINTNUMBER %token YMULT %token YPLUS %token YPROGRAM %token YSEMICOL %token YVAR %start pascalProgram %% pascalProgram : programHeading YSEMICOL block1 YDOT { printf("parsing completed.\n"); } ; programHeading : YPROGRAM YIDENTIFIER ; block1 : constantDecl YSEMICOL block2 ; constantDecl : YCONST YIDENTIFIER YEQUALS YINTNUMBER | constantDecl YSEMICOL YIDENTIFIER YEQUALS YINTNUMBER ; block2 : variableDecl YSEMICOL block3 ; variableDecl : YVAR variableIdList YCOLON YINTEGER ; variableIdList : YIDENTIFIER | variableIdList YCOMMA YIDENTIFIER ; block3 : YBEGIN stmtList YEND ; stmtList : stmt YSEMICOL | stmtList stmt YSEMICOL ; stmt : variable YASSIGN simpleExpression ; variable : YIDENTIFIER ; simpleExpression : term | simpleExpression YPLUS term | simpleExpression YMULT term ; term : YIDENTIFIER | YINTNUMBER ; %% extern FILE *yyin; void main() { while(!feof(yyin)){ yyparse(); } } void yyerror(s) char *s; { fprintf(stderr, "%s\n", s); }