Sunday 26 May 2013

A flex and bison program for C like variable declaration...

This time we are trying to implement the C like variable declaration in flex-bison:-

Grammar:
S->S F | epsilon
F->E '\n'
E->K V
K->KEY
V->COMMALST | ID ';'
COMMALST->ID ',' CLST
CLST->ID ',' CLST | ID ';'

Here is the flex specification file: saved as identi.l

%{
//to identify the keyword and identifier
#include<stdlib.h>
#include"identi.tab.h"
%}
%%
"int" |
"char" |
"float" |
"long" |
"double" |
"signed" {return KEY;}
[A-Za-z_]+[0-9A-Za-z_]* {return ID;}
[,;\n] {return *yytext;}
. ;
%%

Here is the bison specification file: saved as identi.y
%{
#include<stdio.h>
void yyerror(char*,...);
int yywrap(void);
%}
%token KEY ID;
%%
S:S F
 |
 ;
F:E '\n'
 ;
E:K V {printf("\nValid Declaration\n");}
 ;
K:KEY
 ;
V:COMMALST
 |ID ';'
 ;
COMMALST:ID ',' CLST
 ;
CLST:ID ',' CLST
 |ID ';'
 ;
%%
int main(void)
{
 yyparse();
 return 0;
}
int yywrap(void)
{
 return 1;
}
void yyerror(char* msg, ...)
{
 printf("\nError Occurred\n");
}

Now all you have to do is run the following commands:


flex identi.l

bison -d identi.y

gcc -o identi lex.yy.c identi.tab.c


Now you have to run the program on your linux OS as:

./identi

INPUT EXAMPLES:

int a;

float ad3,t4,e3;


Wednesday 22 May 2013

Why do we need IP addressing when we have MAC addresses already?

Why use IP address when we have the device level unique MAC address?

(Please see MultiHoming for some related info...)

IP addresses give hierarchy and order to the wildly large Internet. Order is of utmost importance when you want to search someone. Take this example:
I give you a Bane Havoc's DNA fingerprint(he has no history-sheet, believe me) and ask you, can you find him for me?
What will you do? You will ask a friend if he knows him. If he doesn't, you simply hunt for the person who might. Let's say X knows who can tell you about our target(say Y). Now, you go to Y and the process repeats for an indefinite period.

Analogy: You have the MAC address and you go from one system to another, scan the tables they maintain, to find if they have a record(Think of how unreliable this can be). Solution: Let each system maintain a record for each MAC-device, but, then, size of table becomes infinite!!

Now say, finally a certain Mr. Z give's you the address for Bane. You reach there and knock his door. What if he has changed his house, or better(actually worse) still he is dead?

Analogy: Say somehow you get some system which maintains a table that has a record for our MAC. We try to connect to it and get no response. Why? Because, the system is now not on that network path. (Earlier it was in London, now it is in New York).
Or, the system is not up anymore.

So, what we conclude is using the flat network running only on MAC addresses, the network is neither efficient nor reliable. So, we append IP Address to our network protocols.

I now give you this data: Bane Havoc works in Merc Tech as one of the sales manager. Now find him for me.
You will now go to Merc Tech. Ask them for a list of Sales Managers and from that list fetch the address for Bane. Bingo you got it.

Analogy: Networks and sub-networks. You need to reach a host. You have to simply have its IP address. Go to main network, get re-routed to subnetwork and the IP address is resolved using ARP protocol to find the MAC address of the end device and the data is transmitted. Each router in the path needs to know about the upper layer of the network hierarchy (c.f.r.: routing protocols).

Now you have efficiency and reliability in your network. MAC is still used but once you have reached the subnet of which your target is a part.

Now you can have concept of DNS.

An example. If net was MAC only; and you had to connect to google.com, you would be using MAC address of google hosts. Say one day google system is corrupted. Now they set up a new system and a new MAC address is available. Now the problem is who will update all those voluminous tables around the globe? It would be a fatal catastrophe.

But if we have IP address network, even if google changes a system only the table of google's private gateway has to be updated (the ARP table) the world still happily uses the famous IP address, further simplified by an alias.

[MUSE: if we tried a DNS with MAC network and one day DNS itself had to change the system, and one of the websites also changed system, believe me that website would be practically off the network completely!!!]


Burned-in MAC address (BIA), also known as Burned-In Address, is the last six bytes of a MAC address that are assigned by the manufacturer of a network interface card (NIC). This address sometimes can be overridden by the user, in which the second bit of the most significant byte of the Organisationally Unique Identifier (OUI) is changed to a binary 1. In this case, the NIC is said to be using as a Locally Administered Address (LAA) as its MAC address. If the NIC is not configured to use an LAA as its BIA, then the second bit of the most significant byte of the OUI is changed to a binary 0. In this case, the NIC is said to be using a Universally Administered Address (UAA) as its MAC address.

Monday 13 May 2013

Second package of flex and bison usage... another grammar implementation...

This is my second attempt at using flex and bison... you might find it useful...

Grammar:
E->E+E
E->(E)
E->T
T->id


Here is the flex specification file: saved as clexer2.l

%{
//family cparser2.y - clang2
#include<stdlib.h>
#include"cparser2.tab.h"
int yylval;
%}
%%
[0-9]+ {yylval=atoi(yytext);return id;}
[+()] return *yytext;
\n |
. ;
%%


Here is the bison specification file: saved as cparser2.y

%{
//family clexer2.l clang2
#include<stdio.h>
int yywrap(void);
void yyerror(char*);
%}
%left '+'
%left '('
%left ')'
%token id;
%%
S:S E {$$=$2;}
|
;
E:E'+'E {$$=$1+$3; printf("Ans: %d\n",$$);}
|'('E')' {$$=$2;}
|T {$$=$1;}
;
T:id {$$=$1;}
;
%%
int main(void)
{
yyparse();
return 0;
}
int yywrap(void)
{
return 1;
}
void yyerror(char* msg)
{
printf("%s",msg);
}


Now all you have to do is run the following commands:



flex clexer2.l

bison -d cparser2.y

gcc -o clang2 lex.yy.c cparser2.tab.c


Now you have to run the program on your linux OS as:

./clang2

Flex Bison Beginning.... baby steps....

I have been trying to learn lex and yacc of late. Well the tools that I actually use are flex and bison.
Anyways, here is a small program to demonstrate the use of both in conjunction...

Grammar:
E->E+T
E->T
T->id


Here is the flex specification file: saved as clexer1.l

%{
//This lexer is for cparser1.y together they make clang1
#include<stdio.h>
#include"cparser1.tab.h"
int yylval;
%}
%%
[0-9]+ {yylval=atoi(yytext);return id;}
[+] return *yytext;
\n |
. ;
%%


Here is the bison specification file: saved as cparser1.y

%{
//this is the parser for the grammar E->E+T; E->T; T->id used in conjunction with clexer1.l
#include<stdio.h>
#include<stdlib.h>
void yyerror(char*);
int yywrap(void);
%}
%token id
%%
S:S E {$$=$2;}      //merely to augment the grammar
|       //represents epsilon
;
E:E'+'T {$$=$1+$3; printf("Ans: %d\n",$$);}
|T {$$=$1;}
;
T:id {$$=$1;}
;
%%
int main(void)
{
yyparse();
return 0;
}
void yyerror(char *msg)
{
printf("%s\n",msg);
}
int yywrap(void)
{
return 1;
}


Now all you have to do is run the following commands:



flex clexer1.l

bison -d cparser1.y

gcc -o clang1 lex.yy.c cparser1.tab.c


Now you have to run the program on your linux OS as:

./clang1