Posts

Implementation of Semantic analysis (Type checking)

Contents of type1.l file  %{ #include<stdio.h> #include<string.h> #include"y.tab.h" %} %% [a-zA-Z]+[a-zA-Z0-9]*"(" {strcpy(yylval.DataType,yytext);return function;} int|float|char {strcpy(yylval.DataType,yytext); return Type;} [a-zA-Z]+[a-zA-Z0-9]*"," {strcpy(yylval.DataType,yytext);return parameter;} [a-zA-Z]+[a-zA-Z0-9]*"){}" {strcpy(yylval.DataType,yytext);return functionbody; } ");" {strcpy(yylval.DataType,yytext);return functioncall;} [a-zA-Z]+[a-zA-Z0-9]* {strcpy(yylval.ID,yytext); return Name;} [0-9]+ {strcpy(yylval.DataType,"int"); return Type;} [0-9]+.[0-9]+ {strcpy(yylval.DataType,"float"); return Type;} "'"[a-zA-Z]+"'" {strcpy(yylval.DataType,"char"); return Type;} ";" return SC; "=" return EQ; "," return C; "\n" {} %% Contents of type1.y file  %{ #include<stdio.h> #include...

Implementation of Symbol Table using Lex

File name sym.l %{ #include<stdio.h> #include<string.h> typedef struct node {     char ID[10],DataType[10];     struct node * next; } node_t; node_t *head = NULL,*temp=NULL,*current=NULL; %} %% int|float|char {if(head==NULL){head = (node_t*)malloc(sizeof(node_t)); strcpy(head->DataType,yytext);}else{strcpy(temp->DataType,yytext);}} [a-zA-Z]+[a-zA-Z0-9]* {if(head->next==NULL){strcpy(head->ID,yytext);head->next=NULL;}else{strcpy(temp->ID,yytext);temp->next=NULL;}} ";"  {if(temp==NULL){temp=(struct node*)malloc(sizeof(struct node));head->next=temp;}else{temp->next=(struct node*)malloc(sizeof(struct node));temp=(node_t*)temp->next;}} "\n" {node_t *current = head;     while (current != NULL) {         printf("%s\t%s\n", current->ID,current->DataType);         current = current->next;     }             ...

Implementation of Calculator using lex and yacc

file name Cal.l  %{ #include<stdio.h> #include "y.tab.h" //extern int yylval; %} %% [0-9]+ { yylval=atoi(yytext); return NUMBER; } [\n] return 0; . return yytext[0]; %% int yywrap() { return 1; } file name  cal.y %{ #include<stdio.h> int flag=0; %} %token NUMBER %left '+' '-' %left '*' '/' '%' %left '(' ')' %% ArithmeticExpression: E{          printf("\nResult=%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;}  | NUMBER {$$=$1;} ; %% int main() {   yyparse();   if(flag==0)    printf("\nEntered arithmetic expression is Valid\n\n");   return 0; } int yyerror() {    printf("\nEntered arithmetic expression is Invalid\n\n");    flag=1;    re...

Implementation of Lexical Analyzer

%{ int lc=0; %} %% printf printf("\nPrintf found at line no. %d",lc); scanf  printf("\nScanf found at line no. %d",lc); if | else | while | do | switch | case | for | return printf("\n%s Keyword found at line no. %d ",yytext,lc); "void main" printf("\n%s Keyword found at line no. %d ",yytext,lc); \"[^"]*\" printf("\nQuoted string found at line no. %d",lc); #include | #include<stdio.h> printf("\nHeader found at line no. %d",lc); int | float | char | double | long printf("\n%s Datatype found at line no. %d",yytext,lc); ";" | "," | ":" | "{" | "}" | "(" | ")" | "." printf("\n%s Punctuation Symbol found at line no. %d",yytext,lc); [a-z]+[a-z,0-9]* printf("\n %s Variable found at line no. %d\n",yytext,lc); [0-9]+ printf("\n%s Number found at l...

List of assignments for LP IV

1. Implement a Lexical Analyzer using LEX for a subset of C.  2. Implement a parser for an expression grammar using YACC and LEX for the subset of C. Cross check your output with Stanford LEX and YACC. 3. Generate and populate appropriate Symbol Table. 4. Implementation of Semantic Analysis Operations (like type checking, verification of function parameters, variable declarations and coercions) possibly using an Attributed Translation Grammar. 5. Implement the front end of a compiler that generates the three address code for a simple language. 6. A Register Allocation algorithm that translates the given code into one with a fixed number of regsters. 7. Implementation of Instruction Scheduling Algorithm. 8. Implement Local and Global Code Optimizations such as Common Sub-expression Elimination, Copy Propagation, Dead-Code Elimination, Loop and Basic-Block Optimizations.  9. Mini-Pr...

MongoDB code for Mapreduce,Aggregation and Index

[student@localhost ~]$ su Password: su: Authentication failure [student@localhost ~]$ su Password: [root@localhost student]# systemctl start mongod [root@localhost student]# ./mongo bash: ./mongo: No such file or directory [root@localhost student]# mongo MongoDB shell version: 2.4.6 connecting to: test > use spp switched to db spp > db.createCollection("fruits"); { "ok" : 1 } > db.fruits.insert({"name":"Apple","cost":200}) > db.fruits.insert({"name":"Apple","cost":200}) > db.fruits.insert({"name":"Apple","cost":300}) > db.fruits.mapReduce(function(){emit(this.name,this.price);}function(key,values){return Array.sum(values)},{out:"MapReduce_F"}).find(); Thu Jan  1 14:51:33.905 SyntaxError: Unexpected token function > db.fruits.mapReduce(function(){emit(this.name,this.cost);}function(key,values){return Array.sum(values)},{out:...

Java MongoDB Connectivity -code snippet for Insert,Update,Search,Delete operations

Insert Save a document (data) into a collection (table) named “user”. DBCollection table = db . getCollection ( "user" ) ; BasicDBObject document = new BasicDBObject ( ) ; document . put ( "name" , "mkyong" ) ; document . put ( "age" , 30 ) ; document . put ( "createdDate" , new Date ( ) ) ; table . insert ( document ) ;      Update Update a document where “name=mkyong”. DBCollection table = db . getCollection ( "user" ) ; BasicDBObject query = new BasicDBObject ( ) ; query . put ( "name" , "mkyong" ) ; BasicDBObject newDocument = new BasicDBObject ( ) ; newDocument . put ( "name" , "mkyong-updated" ) ; BasicDBObject updateObj = new BasicDBObject ( ) ; updateObj . put ( "$set" , newDocument ) ; table . update ( query , updateObj ) ; Search Find document where “name=mkyong”, and display it with DBCursor DBCol...