| 1 |
%{ |
| 2 |
/* first section */ |
| 3 |
%} |
| 4 |
%% |
| 5 |
%{ |
| 6 |
/* second section */ |
| 7 |
%} |
| 8 |
S : /* empty */ { printf("S -> epsilon\n"); } |
| 9 |
| '(' S ')' S { printf("S -> ( S ) S\n"); } |
| 10 |
%ifdef ABC |
| 11 |
/* see how preprocessor can be used */ |
| 12 |
| '*' { printf("S -> *\n"); } |
| 13 |
%endif |
| 14 |
; |
| 15 |
%% |
| 16 |
#include <stdio.h> |
| 17 |
|
| 18 |
main() { |
| 19 |
printf("yyparse() = %d\n",yyparse()); |
| 20 |
} |
| 21 |
|
| 22 |
yylex() { |
| 23 |
int ch; |
| 24 |
|
| 25 |
do { ch = getchar(); } while (ch == ' ' || ch == '\n' || ch == '\t'); |
| 26 |
if (ch == EOF) return 0; |
| 27 |
return ch; |
| 28 |
} |
| 29 |
|
| 30 |
yyerror(s) char*s; { |
| 31 |
printf("%s\n",s); |
| 32 |
} |