Skip to content

Instantly share code, notes, and snippets.

@meldsza
Created February 25, 2020 09:49
Show Gist options
  • Save meldsza/856d9a07abc81e28c55028345010079a to your computer and use it in GitHub Desktop.
Save meldsza/856d9a07abc81e28c55028345010079a to your computer and use it in GitHub Desktop.
CD LAB 25-02-2020
%{
#include "y.tab.h"
%}
%%
[0-9] {return DIGIT;}
[a-zA-Z] {return LETTER;}
[_] {return UND;}
\n {return NL;}
. {return yytext[0];}
%%
int yywrap()
{
return 1;
}
%{
#include <stdio.h>
#include <stdlib.h>
%}
%token NL DIGIT LETTER UND
%%
id: letter_und alphanum NL { printf("Valid\n");exit(0);}
;
letter_und: LETTER | UND
alphanum_: LETTER|UND|DIGIT
;
alphanum: alphanum_ alphanum | alphanum_
;
%%
int yyerror(char *msg)
{
printf("Invalid statements\n");
exit(0);
}
int main()
{
printf("Enter input: \n");
yyparse();
return 0;
}
%{
#include "y.tab.h"
%}
%%
"if" {return IF;}
[sS][0-9]* {return S;}
[a-zA-Z][a-zA-Z0-9]* {return ID;}
[0-9]+ {return NUM;}
">"|"<"|">="|"<="|"=="|"!=" {return RELOP;}
\n {return NL;}
. {return yytext[0];}
%%
int yywrap()
{
return 1;
}
%{
#include <stdio.h>
#include <stdlib.h>
int count;
%}
%token NL S ID NUM IF RELOP
%%
stmt : ifstmt NL {printf("valid \t"); printf("Number of nested statements is : %d\n",count);exit(0);};
ifstmt : IF'('cond')''{'ifstmt'}' {count++;}
| S |
;
cond : X RELOP X
;
X : ID | NUM
;
%%
int yyerror(char *msg)
{
printf("Invalid statement");
exit(0);
}
int main()
{
printf("Enter input: \n");
yyparse();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment