Skip to content

Instantly share code, notes, and snippets.

@meldsza
Last active March 10, 2020 09:48
Show Gist options
  • Save meldsza/826cfef82de5176f35004b71c5572c17 to your computer and use it in GitHub Desktop.
Save meldsza/826cfef82de5176f35004b71c5572c17 to your computer and use it in GitHub Desktop.
CD LAB - 10/03/2020
%{
#include "y.tab.h"
%}
%%
[aA] {return A;}
[bB] {return B;}
\n {return NL;}
. {return yytext[0];}
%%
int yywrap()
{
return 1;
}
%{
#include <stdio.h>
#include <stdlib.h>
%}
%token NL A B
%%
stm: sta B NL { puts("Valid Statement");exit(0);}
;
sta: A sta
|
;
%%
int yyerror(char *msg)
{
printf("Invalid statement\n");
exit(0);
}
int main()
{
printf("Enter input: \n");
yyparse();
return 0;
}
%{
#include "y.tab.h"
%}
%%
[aA] {return A;}
[bB] {return B;}
\n {return NL;}
. {return yytext[0];}
%%
int yywrap()
{
return 1;
}
%{
#include <stdio.h>
#include <stdlib.h>
%}
%token NL A B
%%
stm: sta NL { puts("Valid Statement");exit(0);}
;
sta: A sta B | A B;
%%
int yyerror(char *msg)
{
printf("Invalid statement\n");
exit(0);
}
int main()
{
printf("Enter input: \n");
yyparse();
return 0;
}
%{
#include "y.tab.h"
extern yylval;
%}
%%
[0-9]+ {yylval = atoi(yytext); return NUM;}
\n {return NL;}
. {return yytext[0];}
%%
int yywrap()
{
return 1;
}
%{
#include <stdio.h>
#include <stdlib.h>
%}
%left '+' '-'
%left '*' '/'
%token NL NUM
%%
stm: T NL { printf("Expression Result = %d\n", $$);exit(0);}
;
T : T '+' T { $$ = $1 + $3; }
| T '-' T { $$ = $1 - $3; }
| T '*' T { $$ = $1 * $3; }
| T '/' T { $$ = $1 / $3; }
| '-' NUM { $$ = -$2; }
| '(' T ')' { $$ = $2; }
| NUM { $$ = $1; }
;
%%
int yyerror(char *msg)
{
printf("Invalid statement\n");
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