Skip to main content

Posts

Showing posts with the label compiler design

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...

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", ...

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]);   } ...