Last active
March 10, 2020 09:48
-
-
Save meldsza/826cfef82de5176f35004b71c5572c17 to your computer and use it in GitHub Desktop.
CD LAB - 10/03/2020
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
#include "y.tab.h" | |
%} | |
%% | |
[aA] {return A;} | |
[bB] {return B;} | |
\n {return NL;} | |
. {return yytext[0];} | |
%% | |
int yywrap() | |
{ | |
return 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
#include "y.tab.h" | |
%} | |
%% | |
[aA] {return A;} | |
[bB] {return B;} | |
\n {return NL;} | |
. {return yytext[0];} | |
%% | |
int yywrap() | |
{ | |
return 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
#include "y.tab.h" | |
extern yylval; | |
%} | |
%% | |
[0-9]+ {yylval = atoi(yytext); return NUM;} | |
\n {return NL;} | |
. {return yytext[0];} | |
%% | |
int yywrap() | |
{ | |
return 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
#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