/* 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; }
[+\-*/]     { return OP; }
[ \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
    | expr OP expr
    ;
%%

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;
}