Translate

Thursday 10 May 2012

C++ Tokens


A token is the smallest unit that can be processed by a c++ compiler. It can be categorized into different types namely:-

Keywords (or reserved words):-  These are the words used for special purposes or predefined tasks. These words should be written in small letters or lowercase letters. Some of the keyword used in c++ are int, float, char, double, long, void, for, while, do, if, else ...

Identifiers:- It is a name used to represent a variable constant, array name, function name, class, object etc. There are some rules while defining an identifiers:-
i) The first character of identifier should be an alphabet or underscore. The rest can be letters or digits or underscore.
ii) Special characters except underscore can’t be part of identifiers.
iii) Keywords can’t be used as identifiers however keywords written in uppercase form can be used as identifiers.
iv) Lower case and upper case letters are distinct.
v) Maximum length of an identifier can be of 31 characters. However the length varies from one version of compiler to another version.
                Some valid examples:- sum, a1, a2, _1, _a, average, a_b, x123y...
                Some invalid examples:- 1a, a-b, float

Constants:- A value which remain constant throughout the program execution is known as constant. An identifier can be declared as a constant as illustrated below:-
i) # define N 20
ii) # define PI 3.14
iii) const int M=35;           or        const m=35;
Note:- When the constant identifier is of type integer, then keyword int is optional. As a default the identifier is considered as integer. Only one value can be declared as constant at a time with the keyword const.

Variables:- An identifier whose value can be modified/ altered is known as a variable. A variable can be declared as shown below.
int a;
float x;
char ch;
                Here a is a variable of type integer, x is a variable of type float, where as ch is a variable of type char. Values can be assigned with a, x & ch as shown below.
                a=10;                     x=7.5;                   ch=’d’;                 a=a+5;                  x=x-4;

 Literals:- The constant values used in c++ are known as literals, there are four type of literals namely.
i) Integer constant:- A value written without fraction part is known as integer constant. Example: 25, -674, 0 etc.
ii) Floating constant:- A value written with fraction part is floating value. Value of this type can be written with or without exponent form. Example: 2.34, -9.2154, 1.21E10
iii) Character constant:- A single character written within single quotation marks is known as character constant. Example: ‘g’, ‘9’, ‘$’ etc
iv) String constant:- It is an array of characters enclosed in double quotation marks. Example: “Shubham”, “03-aug-2009”. Double quotation mark is a delimiter which determines length of a string.

Data types:- In c++ a data type indicates what type of value can be assigned with an identifier. The datatype can be grouped itno 3 types:-
a)      Basic data type or built in data type:- This data type provided by  the compiler can be used to declare an identifier as constant or variable. There are 5 types of basic data types:-
i) Integer:- This datatype is used to assign a value without fraction. It needs 2 bytes of storage space. Short int, signed int, unsigned int, long int are also part of integer used for different purpose. Its range is -32768 to 32767
ii) Float:- This datatype is used to assign a floating value written with or without exponents. It needs 8 bytes of storage space. Its range is 1.7E-308 to 1.7E308. Its range can be extended by using long double datatype.
iii) Character:-  This datatype deals with single character enclosed in single quotes. It uses 1 byte of memory space.
iv) void:- It represents no value i.e. an empty set. It is mainly used in functions to                                                                    indicate that these functions do not return any value.
b)      Derived data type:- This type of data type can be derived from basic datatypes. Example: array, pointers, references.
c)       User defined datatype:- This type can be obtained by grouping inter related data. Example structure, class, type def.

 Variable declaration and initialisation:- A variable to be used in a program should be declared with its proper datatype.
Suppose variable a is of type integer, b is of type float, c is of type character and d is of type long integer, then declaration can be carried out as listed below:-
int a;
float b;
long int d;
char c;
A variable can be initialised with pre determine value as  shown below
int s=0;
char ch= ‘y’;

Operators:- C++ is rich in supporting many types of operators as illustrated below.
A statement is a command given to the system to carry out a specific task. The statement consists of operators and operands where operators specify type of operation to be carried on operand.
Example:
c=a+b; 
Here a and b are identifiers whose value is to be added and assigned in c. ‘+’ and  ‘=’ are operators.

1 comment:

  1. Hi,you have done a great job,i totally understand about it ,dude i also written about it ,check out https://madhavbansal.blogspot.com/2019/04/tokens-or-lexical-units-in-c.html
    also https://madhavbansal.blogspot.com/

    ReplyDelete

Total Pageviews