compiler
unknown
plain_text
2 years ago
2.9 kB
3
Indexable
//calculator %{ #include <stdio.h> #include <stdlib.h> #include "lex.yy.c" int yywrap(); int yylex(); int yyerror(char *msg); int flag = 0; %} %token N %left '+' '-' '*' '/' '%' '(' ')' %% S: E { printf("Answer is : %d\n", $$); return 0; } ; E: E'+'E {$$=$1+$3;} | E'-'E {$$=$1-$3;} | E'*'E {$$=$1*$3;} | E'/'E {$$=$1/$3;} | E'%'E {$$=$1%$3;} | '(' E ')' {$$=$2;} | N {$$=$1;} ; %% int yyerror(char *msg) { printf("invalid arithmetic expression\n"); flag = 1; exit(0); } int main() { printf("enter the arithmetic operation\n"); yyparse(); if(flag==0){ printf("Valid arithmetic operation\n"); } } ----------------------------------- #include <stdio.h>//parse #include <string.h> // Function declarations void S(); void F(); // Input string and current position char input[100]; int position = 0; // Function to check if the current symbol matches the expected symbol int match(char symbol) { if (input[position] == symbol) { position++; return 1; } return 0; } // Function for non-terminal S void S() { if (input[position] == '\0') return; // Empty production if (input[position] == '-' && input[position + 1] == '-' && input[position + 2] == '>') { match('-'); match('-'); match('>'); F(); S(); F(); } } // Function for non-terminal F void F() { if (match('-') && match('-') && match('>')) return; if (match('<') && match('-') && match('-')) return; } // Main function int main() { printf("Enter the input string: "); scanf("%s", input); S(); // Check if the entire input string is parsed if (position == strlen(input)) printf("Input is valid according to the grammar.\n"); else printf("Input is invalid according to the grammar.\n"); return 0; } ------------------ //tree %{ #include <stdio.h> #include <stdlib.h> #include "lex.yy.c" int yywrap(); int yylex(); int yyerror(char *msg); int flag = 0; char st[100]; int top=0; void A1() { st[top++]=yytext[0]; } void A2() { printf("%c", st[--top]); } void A3() { printf("%c", yytext[0]); } %} %token ID %left '+' '-' '*' '/' '%' '(' ')' UMINUS %% S: E E: E'+'{A1();}T{A2();} | E'-'{A1();}T{A2();} | T ; T: T'*'{A1();}F{A2();} | T'/'{A1();}F{A2();} | F ; F: '('E{A2();}')' | '-'{A1();}F{A2();} | ID{A3();} ; %% int yyerror(char *msg) { printf("invalid arithmetic expression\n"); flag = 1; exit(0); } //driver code int main() { printf("enter the arithmetic operation\n"); yyparse(); printf("\n"); } --------------------------------
Editor is loading...