/* match3.y MR 04/07/99 - 05/07/99 Input file to 'bison' (or 'yacc'). How to generate the parser program: 1) Run bison on this file: bison match3.y 2) Compile the bison output: gcc match3.tab.c -o match3 */ %token ID %token OTHER /* Disambiguating rules: These are such that an ID is glued to a following ;, ( or { if possible; that is, the ID ;, ID (, or ID { combination is shifted, instead of reducing the ID alone. */ %right ID %left ';' %left '(' %left '{' %start list %% list : /*empty*/ | list item ; /* Functie __deklaraties__ zitten hier nog niet in: */ item : ID rbr block { printf( "FDEF\n" ); } | ID rbr { printf( "FCALL\n" ); } | ID { printf( "id\n" ); } | block { printf( "block, not func body\n" ); } | ';' { printf( ";\n" ); } | rbr { printf( "rbr, no func call\n" ); } | OTHER { printf( "other\n" ); } ; /* Er zijn 5 basic building blocks: ID Identifier (starting with letter) ; OTHER Any other token than ID, ;, (, ), {, } ( ...anything... ) ``rbr'' (round brackets) { ...anything... } ``block'' */ block : '{' list '}' ; /* ``Round BRackets'' */ rbr : '(' list ')' ; %% #include main() { yyparse(); } yyerror( char * str ) { printf( "match3: %s\n", str ); } int isLetter( int c ) { if ( 'a' <= (char)c && (char)c <= 'z' ) { return 1; } else if ( 'A' <= (char)c && (char)c <= 'Z' ) { return 1; } else { return 0; } } int isDigit( int c ) { return ( '0' <= (char)c && (char)c <= '9' ); } int yylex( void ) { int ic; /* Eat up white space */ while ( ic = getchar(), ic == ' ' || ic == '\t' || ic == '\n' ) { ; } if ( ic == EOF ) { return 0; /* End of input text */ } else if ( isLetter(ic) ) { do { ic = getchar(); } while ( isLetter(ic) || isDigit(ic) ); ungetc( ic, stdin ); /* yylval = ...; */ return ID; } else if ( ic == '(' || ic == ')' || ic == '{' || ic == '}' || ic == ';' ) { return ic; /* Return special operator as character */ } else { return OTHER; } /* Nog toe te voegen als ``OTHER'': comment, literal numbers/characters/strings ... */ /* Vooral z.d.d. dingen binnen strings en binnen comment niet als functie calls/definities worden gezien. */ }