/* Parenthesis Checker using Yacc/Bison and Flex */
/* parenthesis.l - Lexical analyzer (Flex input file) */
%{
#include <stdio.h>
#include "y.tab.h"
%}
%%
[a-zA-Z]+ { return ID; }
[(] { return LPAREN; }
[)] { return RPAREN; }
[ \t] { /* ignore whitespace */ }
\n { return EOL; }
. { /* ignore other characters */ }
%%
/* parenthesis.y - Parser (Yacc/Bison input file) */
%{
#include <stdio.h>
#include <stdlib.h>
void yyerror(const char *s);
int yylex();
%}
%token ID OP LPAREN RPAREN EOL
%%
input:
/* empty */
| input line
;
line:
expr EOL { printf("Balanced parentheses!\n"); }
| EOL { /* ignore empty lines */ }
;
expr:
ID
| LPAREN expr RPAREN
;
%%
void yyerror(const char *s) {
fprintf(stderr, "Error: syntax error\n");
}
int main() {
printf("Parenthesis Checker\n");
printf("Enter expressions (Ctrl+D to exit):\n");
yyparse();
return 0;
}
int yywrap() {
return 1;
}
LyogUGFyZW50aGVzaXMgQ2hlY2tlciB1c2luZyBZYWNjL0Jpc29uIGFuZCBGbGV4ICovCgovKiBwYXJlbnRoZXNpcy5sIC0gTGV4aWNhbCBhbmFseXplciAoRmxleCBpbnB1dCBmaWxlKSAqLwolewojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgInkudGFiLmgiCiV9CgolJQpbYS16QS1aXSsgICB7IHJldHVybiBJRDsgfQpbKF0gICAgICAgICB7IHJldHVybiBMUEFSRU47IH0KWyldICAgICAgICAgeyByZXR1cm4gUlBBUkVOOyB9ClsrXC0qL10gICAgIHsgcmV0dXJuIE9QOyB9ClsgXHRdICAgICAgIHsgLyogaWdub3JlIHdoaXRlc3BhY2UgKi8gfQpcbiAgICAgICAgICB7IHJldHVybiBFT0w7IH0KLiAgICAgICAgICAgeyAvKiBpZ25vcmUgb3RoZXIgY2hhcmFjdGVycyAqLyB9CiUlCgovKiBwYXJlbnRoZXNpcy55IC0gUGFyc2VyIChZYWNjL0Jpc29uIGlucHV0IGZpbGUpICovCiV7CiNpbmNsdWRlI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