Skip to main content

Posts

Showing posts from January, 2021

Implementation of Calculator using LEX and YACC

 Implementation of Calculator using LEX and YACC Lex<Cal.L> %{ #include"y.tab.h" #include<math.h> %} %% ([0-9]+|([0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?) {yylval.dval=atof(yytext);return NUMBER;} log | LOG {return LOG;} In {return nLOG;} sin | SIN {return SINE;} cos | COS {return COS;} tan | TAN {return TAN;} mem {return MEM;} [\t]; \$ return 0; \n|. return yytext[0]; %% Yacc<Cal.Y> %{ double memvar; %} %union { double dval; } %token<dval>NUMBER %token<dval>MEM %token LOG SINE nLOG COS TAN %left '-' '+' %left '*' '/' %right '^' %left LOG SINE nLOG COS TAN %nonassoc UMINUS %type<dval>expression %% start:statement'\n' |start statement'\n' ; statement:MEM'='expression {memvar=$3;} | expression{printf("Answer=%g\n",$1);} ; expression:expression'+'expression {$$=$1+$3;} | expression '-' expression {$$=$1-$3;} | expression '*' expression {$$=$1*$3;} | expr...

Generate YACC specification for a few synthetic categories (a)Program to recognize a valid arithmetic expression that uses +,-,* and /. (b) program to recognize a valid variable which starts with a letter followed by any number of letters or digits

  Generate YACC specification for a few synthetic categories (a)Program to recognize a valid arithmetic expression that uses +,-,* and /. (b) program to recognize a valid variable which starts with a letter followed by any number of letters or digits a) Program to recognize a valid arithmetic expression that uses +,-,* and /. Program - %{ /* This LEX program returns the tokens for the expression */ #include “y.tab.h” %} %% “=” {printf(“\n Operator is EQUAL”);} “+” {printf(“\n Operator is PLUS”);} “-“ {printf(“\n Operator is MINUS”);} “/” {printf(“\n Operator is DIVISION”);} “*” {printf(“\n Operator is MULTIPLICATION”);} [a-z A-Z]*[0-9]* { printf(“\n Identifier is %s”,yytext); return ID; } return yytext[0]; \n return 0; %% int yywrap() { return 1; } Program Name : arith_id.y %{ #include /* This YYAC program is for recognizing the Expression */ %} %% statement: A’=’E | E { printf(“\n Valid arithmetic expression”); $$ = $1; }; E: E’+’ID | E’-’ID | E’*’ID | E...

Design and implement a lexical analyzer for given language using C and the lexical analyzer should ignore redundant spaces, tabs, and new lines.

Design and implement  a lexical analyzer for given language using  C and the lexical analyzer should ignore redundant spaces, tabs, and new lines - Program- #include <string.h> #include <ctype.h> #include <stdio.h> void keyword(char str[10]) { if (strcmp("for", str) == 0 || strcmp("while", str) == 0 || strcmp("do", str) == 0 || strcmp("int", str) == 0 || strcmp("float", str) == 0 || strcmp("char", str) == 0 || strcmp("double", str) == 0 || strcmp("static", str) == 0 || strcmp("switch", str) == 0 || strcmp("case", str) == 0) printf("\n%s is a keyword", str); else printf("\n%s is an identifier", str); } main() { FILE *f1, *f2, *f3; char c, str[10], st1[10]; int num[100], lineno = 0, tokenvalue = 0, i = 0, j = 0, k = 0; printf("\nEnter the c program"); /*gets(st1); */ f1 = fopen("input", "w"); while...

Implementation of LEXICAL ANALYZER for IF STATEMENT with the help of Lex Tool in CPP

 Implementation of  LEXICAL ANALYZER for IF STATEMENT with  the help of Lex  Tool in CPP- Program- #include<iostream> #include<fstream> #include<stdlib.h> #include<string.h> #include<ctype.h> using namespace std; int isKeyword(char buffer[]) {   char keywords[32][10] = {     "auto",     "break",     "case",     "char",     "const",     "continue",     "default",     "do",     "double",     "else",     "enum",     "extern",     "float",     "for",     "goto",     "if",     "int",     "long",     "register",     "return",     "short",     "signed",     "sizeof",     "static",     "struct",     "switch",     "typedef",     "union",     "unsigned", ...

Implementation of LEXICAL ANALYZER for IF STATEMENT with the help of Lex Tool in C

  Implementation of LEXICAL ANALYZER for IF STATEMENT with the help of Lex Tool- C Program- #include<stdio.h> #include<stdlib.h> #include<string.h> #include<ctype.h> int isKeyword(char buffer[]) { char keywords[32][10] = { "auto", "break", "case", "char", "const", "continue", "default", "do", "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", "long", "register", "return", "short", "signed", "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", "while" }...

Operator Precedence Parse Program in C

Operator Precedence Parse Program in C- Program- #include<stdio.h> #include<conio.h> void main() {   char stack[20], ip[20], opt[10][10][1], ter[10];   int i, j, k, n, top = 0, col, row;   clrscr();   for (i = 0; i < 10; i++) {     stack[i] = NULL;     ip[i] = NULL;     for (j = 0; j < 10; j++) {       opt[i][j][1] = NULL;     }   }   printf("Enter the no.of terminals :\n");   scanf("%d", & n);   printf("\nEnter the terminals :\n");   scanf("%s", & ter);   printf("\nEnter the table values :\n");   for (i = 0; i < n; i++) {     for (j = 0; j < n; j++) {       printf("Enter the value for %c %c:", ter[i], ter[j]);       scanf("%s", opt[i][j]);     }   }   printf("\n**** OPERATOR PRECEDENCE TABLE ****\n");   for (i = 0; i < n; i++) {     printf("\t%c", ter[i]);   } ...

operating system question answer

  Q1. Explain the two models of interprocess communication? What are the strengths and weaknesses of the two approaches?   Interprocess communication is the mechanism provided by the operating system that allows processes to communicate with each other. This communication could involve a process letting another process know that some event has occurred or transferring of data from one process to another. A diagram that illustrates interprocess communication is as follows: The models of interprocess communication are as follows : 1.Shared Memory Model Shared memory is the memory that can be simultaneously accessed by multiple processes. This is done so that the processes can communicate with each other. All POSIX systems, as well as Windows operating systems use shared memory. Advantage of Shared Memory Model Memory communication is faster on the shared memory model as compared to the mes...