Tuesday, 10 January 2012

C- Preprocessor


C’ preprocessors
Preprocessor is the program that processes the source code before it passes through the compiler.
  • It is operated under control of preprocessor command lines or directives.
  • Preprocessor directives are placed in the source program before the main() function.
  • Action performed by the preprocessor is given in the followingdiagram:



Syntax rules for ‘C’ preprocessor:
  • Preprocessor is the one line directive.
  • Preprocessor directives are special instructions for the preprocessor.
  • Preprocessors always begin with #, which must be firs character on the line.
  • They do not require semicolon at the end of line.

List of commonly used directives is as below:
SR. No.
Directive Name
Function
1
#define
Defines a macro substitution
2
#undef
Undefines a macro
3
#include
Specifies the file to be included
4
#if
Test a compile time condition
5
#else
Specifies alternatives when #if test fails.
6
#ifdef
Test for a macro definition
7
#endif
Specifies the end of #if
8
#ifndef
Tests whether a macro is not defined
These directives can be categorized ito three categories.
  1. MACRO Substitution directives:
Macro substitution is a process where an identifier in a program is replaced by a predefined string composed of one or more tokens.
  1. #define: It is used to define a macro.
Syntax:
#define <identifier> <string / expression>

  1. #undef: It is used to undefine a macro.
Syntax:
#undef <identifier>
This is useful when we want to restrict the definition only to a particular part of the program.
Examples:
Program 1) Using simple macro
#include<stdio.h>
#define pi 3.14
main()
{
float r,area;
clrscr();
printf("Enter r: ");
scanf("%f",&r);
area=pi*r*r;
printf("Area = %5.2f\n",area);
}
OUTPUT:
Enter r: 3.2
Area = 32.15

Program 2) Using macro with arguments.
#include<stdio.h>
#define square(x) x * x
main()
{
int n;
float res;
clrscr();
printf("Enter N: ");
scanf("%d",&n);
res = 60 / square(n);
printf("Res = %5.2f\n",res);
}
OUTPUT
Enter N: 2
Res = 60.00
Program 3) Using macro with arguments.
Source Code
OUTPUT
#include<stdio.h>
#define square(x) (x * x)
main()
{
int n;
float res;
printf("Enter N: ");
scanf("%d",&n);
res = 60 / square(n);
printf("Res = %5.2f\n",res);
}
Enter N: 2
Res = 15.00

Program 4) Using macro with single arguments.
Source Code
OUTPUT
#include<stdio.h>
#define square(x) x * x
main()
{
int n;
float res;
clrscr();
printf("Enter N: ");
scanf("%d",&n);
res = square(n) / 4;
printf("Res = %5.2f\n",res);
}


Enter N: 6
Res = 9.00



Program 5) Using macro with two arguments.
Source Code
OUTPUT
#include<stdio.h>
#define max(a, b) ((a > b) ? a : b)
main()
{
int a,b;
printf("Enter a,b : ");
scanf("%d %d",&a,&b);
printf("Max = %d\n",max(a,b));
}


Enter a,b : 7 5
Max = 7

Enter a,b : 5 7
Max = 7




Program 6) Using nested macro.
#include<stdio.h>
#define square(x) x * x
#define cube(x) x * square(x)
main()
{
int n;
clrscr();
printf("Enter N: ");
scanf("%d",&n);
printf("Cube = %d\n",cube(n));
}

OUTPUT
Enter N: 4
Cube = 64

Program 7) Using #undef #include<stdio.h>
#define max(a, b) ((a > b) ? a : b)
void main()
{
int a,b;
printf("Enter a,b : ");
scanf("%d %d",&a,&b);
if (max(a,b) != 0)
printf("Max = %d\n",max(a,b));
else
{
#ifdef max
#undef max
#endif
}
}

  1. File inclusion directives:
An external file containing functions or macro definitions can be included as a part of a program so that we need not rewrite those functions or macro definitions. This achieved by the preprocessor directive #include

Syntax :
#include<filename>
These format can be used when user want to use the standard functions and macros only. Means the file is searched only in the standard directories.

Or #include “file name”
Second format can be used when the functions and macros are defined in user defined files. At this point, the preprocessor inserts the entire contents of filename into the source code of the program. Here the file is first searched in the current directory and then I the standard directories.

Nesting of included files is allowed. That is, an included file can include othe files. However a file can not include itself.

If an included file is not found, an error is reported and compilation is terminated.



Types of Computer


CHASSIS ABCs
The computer case or chassis is the body of the computer. Cases allow for manufacturers to include all computer comments together and have it able to be moved, if needed. Below we have listed types of cases as well as general specifications and information about the cases.
TYPES OF CASES
Below is a listing of the major types of cases and what you can expect from each of them.
MINI TOWER
Advantages:
Excellent size which can be placed on top or below of a computer desk.
Disadvantages:
While this case does offer upgradability, it can be filled up much faster than the Mid-Sized tower.
Recommendations:
Great PC for end-users and small businesses.
MID-SIZE TOWER
Advantages:
Excellent case which can fit below and on top of your computer desk.
Plenty of expandability for new devices for businesses, end-users, and advanced users.
One of the most used computer cases found today.
Disadvantages:
None
Recommendations:
This case is an excellent choice for all users and businesses.
FULL-SIZED TOWER
Advantages:
Excellent computer for upgradability.
Excellent case for a server machine.
Disadvantages:
Cost is going to be a lot more than a standard case.
Generally a large case which cannot be placed on top or beneath a desk.
Recommendations:
We recommend that this type of case be purchased by advanced users or users who plan to have a stand alone machine as a server.
DESKTOP
Advantages:
Excellent desk computer.
Great use of desk space when monitor is placed on top of the computer.
Disadvantages:
With some types of desktop cases can be very difficult to upgrade.
Does not really work on the floor.
Recommendations:
Excellent choice for a business and home user computer. End users planning to upgrade or place computer on floor we recommend going with tower computer unless the manufacturer provides a desktop that can be converted to tower.
SLIMLINE DESKTOP
Advantages:
Excellent for workstations in large companies.
Great computer for a low budget PC.
Excellent computer for locations which may not have large area of workspace.
Disadvantages:
Generally little or no room for adding additional peripherals.
Usually require an LPX
motherboard.Recommendations:
We recommend that this type of case be purchased only for companies employees as workstations.
Palmtop/PDAs

Data Measurement Chart

 
Data Measurement Chart
Data Measurement
Size


Bit
Single Binary Digit (1 or 0)


Byte
8 bits


Kilobyte (KB)
1,024 Bytes


Megabyte (MB)
1,024 Kilobytes


Gigabyte (GB)
1,024 Megabytes


Terabyte (TB)
1,024 Gigabytes


Petabyte (PB)
1,024 Terabytes


Exabyte (EB)
1,024 Petabytes


   
Connection Speed Chart
Internet Technology
Data Rate (per second)
Data Rate (per second)
Data Rate (per second)
Data Rate (per second)
28.8K Modem
28.8 Kbps
28,800 Bits
3,600 Bytes
3.5 Kilobytes
36.6K Modem
36.6 Kbps
36,600 Bits
4,575 Bytes
4.4 Kilobytes
56K Modem
56 Kbps
56,000 Bits
7,000 Bytes
6.8 Kilobytes
ISDN
128 Kbps
128,000 Bits
16,000 Bytes
15 Kilobytes
T1
1.544 Mbps
1,544,000 Bits
193,000 Bytes
188 Kilobytes
DSL
512 Kbps to 8 Mbps
8,000,000 Bits
1,000,000 Bytes
976 Kilobytes
Cable Modem
512 Kbps to 52 Mbps
53,000,000 Bits
6,625,000 Bytes
6,469 Kilobytes (6.3MB/sec)
T3
44.736 Mbps
44,736,000 Bits
5,592,000 Bytes
5,460 Kilobytes (5.3MB/sec)
Gigabit Ethernet
1 Gbps
1,000,000,000 Bits
125,000,000 Bytes
122,070 Kilobytes (119MB/sec)
OC-256
13.271 Gbps
13,271,000,000 Bits
1,658,875,000 Bytes
1,619,995 Kilobytes (1.5GB/sec)
 
Computer Technology Chart
Computer Technology
Data Rate (per second)
Data Rate (per second)
Data Rate (per second)
Data Rate (per second)
ADB
256 Kbps
256,000 Bits
32,000 Bytes
31.2 Kilobytes
USB
12 Mbps
12,000,000 Bits
1,500,000 Bytes
1,464 Kilobytes (1.42MB/sec)
USB 2.0
480 Mbps
480,000,000 Bits
60,000,000 Bytes
58,593 Kilobytes (57.2MB/sec)
FireWire (a.k.a. "IEEE 1394" or "i.Link")
400 Mbps
400,000,000 Bits
50,000,000 Bytes
48,828 Kilobytes (47.6MB/sec)
Ultra ATA/33 (a.k.a. Ultra DMA/33)
33 MB/sec






Ultra ATA/66 (a.k.a. Ultra DMD/66 or Fast ATA-2)
66 MB/sec






SCSI-1
5 MB/sec






SCSI-2
5-10 MB/sec






Fast SCSI-2
10-20 MB/sec






Wide SCSI-2
20 MB/sec






Fast Wide SCSI-2
20 MB/sec






Ultra SCSI-3 (8-bit)
20 MB/sec






Ultra SCSI-3 (16-bit)
40 MB/sec






Ultra-2 SCSI
40 MB/sec






Wide Ultra-2 SCSI
80 MB/sec






Ultra-3 SCSI
160 MB/sec






Serial ATA (Gen 1)
150 MB/sec
1.2 Gbps




Serial ATA (Gen 2)
300 MB/sec
2.4 Gbps




Serial ATA (Gen 3)
600 MB/sec
4.8 Gbps




 
Video Format Chart
Video Formats
NTSC Resolution (525 Vertical Lines)






8mm
250 Horizontal Lines






VHS
250 Horizontal Lines






VHS-C
250 Horizontal Lines






Hi8
400 Horizontal Lines






S-VHS
400 Horizontal Lines






DV
500 Horizontal Lines






DVD
500 Horizontal Lines






 
Audio/Video Chart
Audio/Video Technology
Data Rate (per second)
Data Rate (per second)
Data Rate (per second)
Data Rate (per second)
CD
1.4112 Mbps
1,411,200 Bits
176,400 Bytes
172 Kilobytes
DVD
10 Mbps
10,000,000 Bits
1,250,000 Bytes
1,220 Kilobytes (1.1MB/sec)
DV
30.1 Mbps
30,195,712 Bits
3,774,464 Bytes
3,686 Kilobytes (3.6MB/sec)
DLP
37 Mbps to 43 Mbps
43,000,000 Bits
5,375,000 Bytes
5,249 Kilobytes (5.2MB/sec)
 
Data Comparison Chart
Kilobits (Kbps)
Bits (bps)
Megabits (Mbps)
Bytes (Bps)
Kilobytes (KBps)
Megabytes (MBps)
Minutes On 650MB CD
Minutes On 4.7GB DVD
28
28,000
0.028
3,500
3.41
0.00334
3,245
24,031
30
30,000
0.03
3,750
3.66
0.00358
3,029
22,429
56
56,000
0.056
7,000
6.83
0.00668
1,622
12,015
80
80,000
0.08
10,000
9.76
0.00954
1,135
8,410
100
100,000
0.1
12,500
12.20
0.01192
908
6,728
150
150,000
0.15
18,750
18.31
0.01788
605
4,485
200
200,000
0.2
25,000
24.41
0.02384
454
3,364
300
300,000
0.3
37,500
36.62
0.03576
302
2,242
500
500,000
0.5
62,500
61.03
0.05960
181
1,345
800
800,000
0.8
100,000
97.65
0.09537
113
841
900
900,000
0.9
112,500
109.86
0.10729
100
747
1,000
1,000,000
1
125,000
122.07
0.11921
90
672
2,000
2,000,000
2
250,000
244.14
0.23842
45
336
4,000
4,000,000
4
500,000
488.28
0.47684
22
168
5,000
5,000,000
5
625,000
610.35
0.59605
18
134
10,000
10,000,000
10
1,250,000
1,220.70
1.19209
9
67
15,000
15,000,000
15
1,875,000
1,831.05
1.78814
6
44
30,000
30,000,000
30
3,750,000
3,662.10
3.57628
3
22



Data Representation

Data Representation refers to the methods used internally to represent information stored in a computer. Computers store lots of different types of information:
  • numbers
  • text
  • graphics of many varieties (stills, video, animation)
  • sound
At least, these all seem different to us. However, ALL types of information stored in a computer are stored internally in the same simple format: a sequence of 0's and 1's. How can a sequence of 0's and 1's represent things as diverse as your photograph, your favorite song, a recent movie, and your term paper?
It all depends on how we interpret the information. Computers use numeric codes to represent all the information they store. These codes are similar to those you may have used as a child to encrypt secret notes: let 1 stand for A, 2 stand for B, etc. With this code, any written message can be represented numerically. The codes used by computers are a bit more sophisticated, and they are based on the binary number system (base two) instead of the more familiar (for the moment, at least!) decimal system. Computers use a variety of different codes. Some are used for numbers, others for text, and still others for sound and graphics.

Memory Structure in Computer

  • Memory consists of bits (0 or 1)
    • a single bit can represent two pieces of information
  • bytes (=8 bits)
    • a single byte can represent 256 = 2x2x2x2x2x2x2x2 = 28 pieces of information
  • words (=2,4, or 8 bytes)
    • a 2 byte word can represent 2562 pieces of information (approximately 65 thousand).
  • Byte addressable - each byte has its own address.

Binary Numbers

Normally we write numbers using digits 0 to 9. This is called base 10. However, any positive integer (whole number) can be easily represented by a sequence of 0's and 1's. Numbers in this form are said to be in base 2 and they are called binary numbers. Base 10 numbers use a positional system based on powers of 10 to indicate their value. The number 123 is really 1 hundred + 2 tens + 3 ones. The value of each position is determined by ever-higher powers of 10, read from left to right. Base 2 works the same way, just with different powers. The number 101 in base 2 is really 1 four + 0 twos + 1 one (which equals 5 in base 10). For more of a comparison, click here.

Text

Text can be represented easily by assigning a unique numeric value for each symbol used in the text. For example, the widely used ASCII code (American Standard Code for Information Interchange) defines 128 different symbols (all the characters found on a standard keyboard, plus a few extra), and assigns to each a unique numeric code between 0 and 127. In ASCII, an "A" is 65," B" is 66, "a" is 97, "b" is 98, and so forth. When you save a file as "plain text", it is stored using ASCII. ASCII format uses 1 byte per character 1 byte gives only 256 (128 standard and 128 non-standard) possible characters The code value for any character can be converted to base 2, so any written message made up of ASCII characters can be converted to a string of 0's and 1's.

Graphics

Graphics that are displayed on a computer screen consist of pixels: the tiny "dots" of color that collectively "paint" a graphic image on a computer screen. The pixels are organized into many rows on the screen. In one common configuration, each row is 640 pixels long, and there are 480 such rows. Another configuration (and the one used on the screens in the lab) is 800 pixels per row with 600 rows, which is referred to as a "resolution of 800x600." Each pixel has two properties: its location on the screen and its color.
A graphic image can be represented by a list of pixels. Imagine all the rows of pixels on the screen laid out end to end in one long row. This gives the pixel list, and a pixel's location in the list corresponds to its position on the screen. A pixel's color is represented by a binary code, and consists of a certain number of bits. In a monochrome (black and white) image, only 1 bit is needed per pixel: 0 for black, 1 for white, for example. A 16 color image requires 4 bits per pixel. Modern display hardware allows for 24 bits per pixel, which provides an astounding array of 16.7 million possible colors for each pixel!

Compression

Files today are so information-rich that they have become very large. This is particularly true of graphics files. With so many pixels in the list, and so many bits per pixel, a graphic file can easily take up over a megabyte of storage. Files containing large software applications can require 50 megabytes or more! This causes two problems: it becomes costly to store the files (requires many floppy disks or excessive room on a hard drive), and it becomes costly to transmit these files over networks and phone lines because the transmission takes a long time. In addition to studying how various types of data are represented, you will have the opportunity today to look at a technique known as data compression. The basic idea of compression is to make a file shorter by removing redundancies (repeated patterns of bits) from it. This shortened file must of course be de-compressed - have its redundancies put back in - in order to be used. However, it can be stored or transmitted in its shorter compressed form, saving both time and money.

History of the Programming Languages

  • 1940's - Rewire Circuits
  • Early 1950's - Machine Language: zeros and ones
  • 1950's - Assembly Language
    • Symbolic Version of Machine Language, Not Portable. Translated by Assembler
  • Late 1950's - FORTRAN,
    • Backus at IBM
    • FORMula TRANSlator
    • First High-Level Programming Language
    • First Compiler
  • 1960's - Simula
    • Elements of Object-Oriented Languages
  • 1970's
    • Pascal, N. Wirth, Teaching Language
    • C
      • Language for Systems Programming.
      • Small and Fairly Portable.
      • Used to Implement Unix on PDP-11.
      • Thompson and Ritchie at Bell Labs.
  • Small Talk
    • Fully Object-Oriented
    • Interpreted and Slow
    • Xerox Park
  • 1980's, C++
    • Stroustrup at Bell Labs
    • Evolved from C
    • Large, Complex but Fairly Portable
    • Sun Microsystems 1995
    • Evolved from C++ and SmallTalk
    • Object-Oriented
    • Completely Portable

Chapter2-Lexical Analysis(TCS)


Chap 2 : LEXICAL ANALYSIS

The function of lexical analyzer is to read the source program, one character at a time and translate it into a sequence of primitive units called tokens. Now, we will study the problem of designing and implementing lexical analyzers.
Here, finally we introduce regular expression, a notation that can be used to describe essentially all the tokens of programming languages. Secondly, we introduce transition diagrams and finite automata to give a way for designing token recognizers, which give us a mechanism to recognize the tokens in the input stream.
In addition to this, we require some mechanism to perform various actions as the tokens are recognized. For e.g. Enter value of the token into a table, generate some output, or produce a diagnostic message.
One advantage of using regular expression to specify tokens is that from regular expression we can easily construct a recognizer for tokens denoted by that regular expression.

The function of scanner:-
  1. The lexical analyzer is the first phase of a compiler. Its main task is to read the input character and produce as output a sequence of tokens that is used by the parser for syntax analysis. This is summarized as below

  1. It removes the comments and white spaces in the form of blank, tab, new line character from the source program.
  2. It creates different tables like symbol table constant table, etc.
  3. As it recognizes new line character, it can keep track of line numbers and hence it can help in error message by associating a line number.
  4. In some compiler it is in charge of making a copy of the source program with error messages marked in it.
  5. If the source language supports some macro preprocessor functions, then these preprocessor functions may also be implemented as lexical analysis takes place.

Token: In general, there is a set of strings in the inputs for which the same token is produced as output.
Pattern: is a set of strings, which is described by a rule called a pattern associated with a token.
Lexeme: is a sequence of characters in the source program that is matched by the pattern for a token.
for eg.
Statement
Lexeme
Pattern
Token
int x;
int
int
Keyword(int)
x
alpha(alpha+digit+’_’)*
Identifier
a=5;
a
alpha(alpha+digit+’_’)*
Identifier

=
=
Operator

5
digit+
literal
INPUT BUFFERING
In this, we use a buffer that is divided into 2 N characters halves. Typically, N is number of characters on one disk block read. E.g. 1024 or 4096



We read N input characters into each half of the buffer with one system read command, rather than invoking a read command for each input character ‘eof’ is read into the buffer.
Two input pointers to the buffers are maintained. The string of characters between two pointers is current lexeme. Initially both pointers point to the first character of the next lexeme to be found. One called the forward pointer, scans ahead until a match for a pattern is found. Once the lexeme is determined, the forward pointer is set to character at its right end. This moving of forward pointer toward left by one position is called as retraction. After the lexeme is processed both pointers are set to the next character. With this scheme, comments and white space can be treated as pattern that yields no token.
If the forward pointer is about to move past the halfway mark, the right half is filled with N new input characters.
If the forward pointer is about to move past the right end of the buffer, the left hand is filled with N new characters and the forward pointer wraps around to the beginning of the buffer.
DISADVANTAGE: The amount of look ahead is limited.
INPUT BUFFERING WITH SENTINELS:
If we use the previous scheme, we must check each time. We move the forward pointer that we have not moved off one half of the buffer. If the forward pointer is moved off one half, then we must reload another half. Except at the ends of the buffer half, the code require to test for each advance of the forward pointer we reduce the two test to one of each buffer have hold a sentinel character at the end.
The sentinel character is a special character and denoted by ‘eof’







E
=
M
eof
*
C
eof







With this arrangement the forward pointer checks whether there is an eof. But when it reaches to the end of buffer half, it performs more tests.

Exercise:
  1. Draw a neat labeled transitions diagram for recognizing any valid identifier.




  1. Draw a neat labeled transitions diagram for recognizing an octal number.
  2. Draw a neat labeled transitions diagram for recognizing a hexa decimal numbers.
  3. Draw a neat labeled transitions diagram for recognizing an integer constant.
  4. Draw a neat labeled transitions diagram for recognizing a float constant.
  5. Draw a neat labeled transitions diagram for recognizing a float constant in exponent form.
  6. Draw a neat labeled transitions diagram for recognizing arithmetic operators such as +, -, *, /, %.
  7. Draw a neat labeled transitions diagram for recognizing relational operators such as <, <=, >, >=, ==, !=.
  8. Draw a neat labeled transitions diagram for recognizing keywords in C language such as do, if, while, else, for
  9. Draw a neat labeled transitions diagram for recognizing keywords in C language such as break, continue, switch, struct, union
  10. Write pseudo code for the following transition diagram.




  1. Write pseudo code for the following transition diagram.




  1. Write pseudo code for the following transition diagram.




  1. Write pseudo code for the following transition diagram.