-
-
Save meldsza/856d9a07abc81e28c55028345010079a to your computer and use it in GitHub Desktop.
CD LAB 25-02-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" | |
%} | |
%% | |
[0-9] {return DIGIT;} | |
[a-zA-Z] {return LETTER;} | |
[_] {return UND;} | |
\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 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; | |
} |
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" | |
%} | |
%% | |
"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; | |
} |
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> | |
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