/* 3a. Write a LEX program to count the number of positive & negative integers and fractions */
          
%{
#include"stdio.h"
int posnum=0;
int negnum=0;
int posfra=0;
int negfra=0;
%}
dig [0-9]

%%
\+?{dig}+ posnum++;
-{dig}+ negnum++;
\+?{dig}*\.{dig}+([eE][-+?]?{dig}+)? posfra++;
-{dig}*\.{dig}+([eE][+-]?{dig}+)? negfra++;
%%

main()
{
    yylex();
    printf("%d",negnum);
    printf("\n%d",posnum);
    printf("\n%d",posfra);
    printf("\n%d",negfra);
}

 

 

/* 3b. Write a YACC program to identify a valid expression */


%{
#include"stdio.h"
#include"ctype.h"
%}

%%

%token P
%token R

%%

list: 
    |list '\n'
    |list exp '\n' { printf("\nValid expression.\n"); exit(0);}
    |error '\n' { yyerror(" : Invalid expression.\n"); yyerrok; exit(0); }
;

exp: 
    P
    |exp R P 
;

%%

main()
{
    yyparse();
}
yylex()
{
    int c;
    while((c=getchar())==' ' || c=='\t');
        if(isdigit(c)||isalpha(c)) 
            return P;
    if(c=='+'||c=='-'||c=='*'||c=='/')
        return R;
    return c;
}
yyerror(char *s)

    printf("%s",s);
}