%{ #include #include int linecount = 0; void yyerror(char *s) { fprintf(stderr,"file is not okay -- problem at line %d\n",linecount); exit(1); } int yywrap() { return 1; } %} %token ID %token PROGRAM %token END %token VARIABLES %token VAR %token STATEMENTS %token IF %token THEN %token ELSE %token WHILE %token LBRACE %token RBRACE %token COLON %token SEMICOLON %token FUNCTIONS %token COMMA %token DEFINE %token LPARENTHESIS %token RPARENTHESIS %% program : PROGRAM ID variablesSection functionsSection statementsSection END ; variablesSection : VARIABLES LBRACE variableDeclarations RBRACE ; variableDeclarations : | variableDeclarations variableDeclaration ; variableDeclaration : ID COLON ID SEMICOLON {printf("emitting var %s of type %s\n",$3,$1);} ; functionsSection : FUNCTIONS LBRACE functionDeclarations RBRACE ; functionDeclarations : | functionDeclarations functionDeclaration ; functionDeclaration : DEFINE ID COLON ID LPARENTHESIS argsList RPARENTHESIS LBRACE statements RBRACE ; statementsSection : STATEMENTS LBRACE statements RBRACE ; statements : | statements statement ; statement : VAR variableDeclaration | whileLoop | ifStruct | subroutineCall SEMICOLON ; whileLoop : WHILE LPARENTHESIS subroutineCall RPARENTHESIS LBRACE statements RBRACE ; ifStruct : IF LPARENTHESIS subroutineCall RPARENTHESIS LBRACE statements RBRACE ; | IF LPARENTHESIS subroutineCall RPARENTHESIS LBRACE statements RBRACE ELSE LBRACE statements RBRACE ; subroutineCall : ID LPARENTHESIS callArgsList RPARENTHESIS ; argsList : | argPair | argsList COMMA argPair ; argPair : ID ID ; callArgsList : | ID | callArgsList COMMA ID ; %% int main(int argc, char **argv) { // yydebug = 1; yyparse(); printf("input is okay\n"); }