Header Ads

Header ADS

Compiler Design Lap Report


Index

Experiment No

Experiment Name

        01

Write a simple lex specification to recognize the following verbs- am, is, are, was, were, be, being, been, do, does, did, have, has, had, will, would, shall, should, can, could, go.

         02

Write a simple lex specification to recognize the following words as different parts of speech- am, is, are, go, very, simply, quickly, gently, to, from, behind, between, if, then, and.

 

         03

Write a simple lex specification to recognize all real numbers.

 

         04

Write a simple lex specification to recognize different Keywords.                  

         05

Write a simple lex specification to recognize integer number.

         06

Write a simple lex specification to recognize different punctuation symbols.

         07

Write a simple lex specification to recognize a digit or not.

         08

Write a simple lex specification to recognize the identifier.

           09

Write a simple lex specification to recognize float.

           10

Write a simple lex specification to recognize for the positive and negative integer and float number.

           11

Write a lex program to recognize different types of operator. 

Experiment No :- 01

Experiment Name :- Write a simple lex specification to recognize the following verbs- am, is, are, was, were, be, being, been, do, does, did, have, has, had, will, would, shall, should, can, could, go.

Theory

Scanner :-

Conceptually, a compiler operates in phases, each of which transforms the source.

Program from one representation to another. The lexical analyzer is the first phase of a compiler. Its main task is to read the input characters & produce as output a sequence of tokens that the parser uses for syntax analysis. The lexical analyzer generator is also known as lexer/scanner.

Interaction with the parser :-

The lexical analyzer is an independent compilation phase that communicates with the parser over a well-defined and simple interface. The interaction, summarized in the following figure, is commonly implemented by making the lexical analyzer be a subroutine of the parser.

 

Lex :-

Lex is a tool for gathering scanners. LEX reads the given input files or standard input for a description of a scanner to generate. The description is in the form of pairs of regular expressions and C code, called RULES. So the LEX specification is given below:

Declarations

%%

Transition rules

%%

Auxiliary procedures / Compiler :-

Here we use the LINUX Operating Systems FLEX to write the LEX code. Where FLEX stands for-Fast Lexical Analyzer generator. FLEX generates as output a C source file, lex.yy.c which defines a routine yylex(), this file is compiled and linked with the – lfl library to produce an exe file. When the executable is run, it analyzes its input for occurrences of the regular expression. Whenever it finds one, it executes the corresponding C code. So the command line can be as follows:

Flex test.l

Gcc lex.yy.c – lfl

./a.out

Identifier :-

Languages use identification as names of variables, arrays, functions and the like. A grammar for a language often treats an identifier as a token. A parser based on such a grammar ants to see the same token say id, each time an identifier appears in the input. For example:

                            Count = count + increment;

Would be converted by the lexical analyzer into the token stream Id = id+id;

Keywords :-

Many language use fixed character strings such as begin, end, if, and so on, as punctuation marks or to identify certain constructs. These character strings, called keywords, generally satisfy the rules for forming identifiers, so a mechanism is needed for deciding when a lexeme forms a keyword and when it forms an identifier. The problem is easier to resolve if keywords are reserved, i.e. if they are cannot be used as identifiers. Then a character string forms an identifier only if it is not a keywords.

Implementation Environment

Hardware Information :-

Processor                                                       Cyrix GX-Media

Speed                                                              233 MHz

External Cache Memory                              512 KB

Internal Cache Memory                              8 KB

RAM                                                                32 MB

Hard Disk Drive                                             8.6 GB

Operating System                                        Linux (Red Hat 5.2)

Program Listing :-

%%

am|is|are|was|were|be|being|been|do|does|did|have|has|had|will|would|shall|should|can|could|go                                     {printf(“Verb\n”);}    

.*                                                                                 {printf(“Not a Verb \n”);}                                                   

%%

int yywrap(){}

int main()

{

printf(“Enter anyone of this item: \n

am|is|are|was|were|be|being|been|do|does|did|have|has|had|will|would|shall|should|can|could|go \n\n”);

yylex();

}

Input and Output

Input :- 

am

is

are

up

Output :- 

Enter anyone of this item:

am|is|are|was|were|be|being|been|do|does|did|have|has|had|will|would|shall|should|can|could|go

am: is a verb

is: is a verb

are: is a verb

up: is not a verb

Discussion

Advantages :-

In this program we can identify some predefined verbs and can easily feed the output of this program to a parser program. It helps us to understand how a scanner works and also how it interact with the parser.

Limitations :-

This program used only some of the reserved keywords and works on C like statement.

Efficiency :-

            Under a little limitation the efficiency of this program is 100%.

 

Experiment No :- 02

Experiment Name :- Write a simple lex specification to recognize the following words as different parts of speech- am, is, are, go, very, simply, quickly, gently, to, from, behind, between, if, then, and.

Theory

Scanner :- 

Conceptually, a compiler operates in phases, each of which transforms the source.

Program from one representation to another. The lexical analyzer is the first phase of a compiler. Its main task is to read the input characters & produce as output a sequence of tokens that the parser uses for syntax analysis. The lexical analyzer generator is also known as lexer/scanner.

Interaction with the parser :- 

The lexical analyzer is an independent compilation phase that communicates with the parser over a well-defined and simple interface. The interaction, summarized in the following figure, is commonly implemented by making the lexical analyzer be a subroutine of the parser.

 

Lex :-

Lex is a tool for gathering scanners. LEX reads the given input files or standard input for a description of a scanner to generate. The description is in the form of pairs of regular expressions and C code, called RULES. So the LEX specification is given below:

Declarations

%%

Transition rules

%%

Auxiliary procedures / Compiler :-

Here we use the LINUX Operating Systems FLEX to write the LEX code. Where FLEX stands for-Fast Lexical Analyzer generator. FLEX generates as output a C source file, lex.yy.c which defines a routine yylex(), this file is compiled and linked with the – lfl library to produce an exe file. When the executable is run, it analyzes its input for occurrences of the regular expression. Whenever it finds one, it executes the corresponding C code. So the command line can be as follws:

Flex test.l

Gcc lex.yy.c – lfl

./a.out

Identifier :-

Languages use identification as names of variables, arrays, functions and the like. A grammar for a language often treats an identifier as a token. A parser based on such a grammar ants to see the same token say id, each time an identifier appears in the input. For example:

                            Count = count + increment;

Would be converted by the lexical analyzer into the token stream Id = id+id;

Keywords :-

Many language use fixed character strings such as begin, end, if, and so on, as punctuation marks or to identify certain constructs. These character strings, called keywords, generally satisfy the rules for forming identifiers, so a mechanism is needed for deciding when a lexeme forms a keyword and when it forms an identifier. The problem is easier to resolve if keywords are reserved, i.e. if they are cannot be used as identifiers. Then a character string forms an identifier only if it is not a keywords.

Implementation Environment

Hardware Information :-

Processor                                                       Cyrix GX-Media

Speed                                                              233 MHz

External Cache Memory                              512 KB

Internal Cache Memory                              8 KB

RAM                                                                32 MB

Hard Disk Drive                                             8.6 GB

Operating System                                        Linux (Red Hat 5.2)

 

Program Listing :-

%%

am|is|are|were|go|very|simply|quickly|gently|to|from|behind|between|if|then                                              {printf(“Parts of Speech MATCHED\n”);}

.*                                                {printf (“Not MATCHED\n”);}

%%

int yywrap(){}

int main()

{

printf(“Enter anyone of this item:\n am|is|are|were|go|very|simply|quickly|gently|to|from|behind|between|if|then\n\n”);

yylex();

}

Input and Output

Input  :-

quickly

very

simply

then

go

don

Output :-

Enter anyone of this item:

am|is|are|were|go|very|simply|quickly|gently|to|from|behind|between|if|then

quickly

Parts of Speech MATCHED

very

Parts of Speech MATCHED

simple

Parts of Speech MATCHED

then

Parts of Speech MATCHED

go

Parts of Speech MATCHED

don

Not MATCHED

Discussion

Advantages :-

In this program we can identify some predefined verbs and can easily feed the output of this program to a parser program. It helps us to understand how a scanner works and also how it interact with the parser.

Limitations :-

This program used only some of the reserved keywords and works on C like statement.

Efficiency :-

            Under a little limitation the efficiency of this program is 100%.

 

Experiment No :- 03

Experiment Name :- Write a simple lex specification to recognize all real numbers.

Theory

Scanner :-

Conceptually, a compiler operates in phases, each of which transforms the source.

Program from one representation to another. The lexical analyzer is the first phase of a compiler. Its main task is to read the input characters & produce as output a sequence of tokens that the parser uses for syntax analysis. The lexical analyzer generator is also known as lexer/scanner.

Interaction with the parser :-

The lexical analyzer is an independent compilation phase that communicates with the parser over a well-defined and simple interface. The interaction, summarized in the following figure, is commonly implemented by making the lexical analyzer be a subroutine of the parser.


Lex :-

Lex is a tool for gathering scanners. LEX reads the given input files or standard input for a description of a scanner to generate. The description is in the form of pairs of regular expressions and C code, called RULES. So the LEX specification is given below:

Declarations

%%

Transition rules

%%

Auxiliary procedures / Compiler :-

Here we use the LINUX Operating Systems FLEX to write the LEX code. Where FLEX stands for-Fast Lexical Analyzer generator. FLEX generates as output a C source file, lex.yy.c which defines a routine yylex(), this file is compiled and linked with the – lfl library to produce an exe file. When the executable is run, it analyzes its input for occurrences of the regular expression. Whenever it finds one, it executes the corresponding C code. So the command line can be as follws:

Flex test.l

Gcc lex.yy.c – lfl

./a.out

Identifier :-

Languages use identification as names of variables, arrays, functions and the like. A grammar for a language often treats an identifier as a token. A parser based on such a grammar ants to see the same token say id, each time an identifier appears in the input. For example:

                            Count = count + increment;

Would be converted by the lexical analyzer into the token stream Id = id+id;

Keywords :-

Many language use fixed character strings such as begin, end, if, and so on, as punctuation marks or to identify certain constructs. These character strings, called keywords, generally satisfy the rules for forming identifiers, so a mechanism is needed for deciding when a lexeme forms a keyword and when it forms an identifier. The problem is easier to resolve if keywords are reserved, i.e. if they are cannot be used as identifiers. Then a character string forms an identifier only if it is not a keywords.

Implementation Environment

Hardware Information :-

Processor                                                       Cyrix GX-Media

Speed                                                              233 MHz

External Cache Memory                              512 KB

Internal Cache Memory                              8 KB

RAM                                                                32 MB

Hard Disk Drive                                             8.6 GB

Operating System                                        Linux (Red Hat 5.2)

Program Listing :-

%%

[-]?[0-9]*|[-]?[0-9]*”.”[0-9]*| [-]?[0-9]*”/”[0-9]*         {printf(“Real number\n”);}

.*                                                                                     {printf(“Not a Real number \n”);}

%%

int yywrap(){}

int main()

{

printf (“Enter a number to check, if it is real number:\n”);

yylex();

}

Input and Output

Input :-

-.3

.332

2.5e+23

a

Output :-

Enter a number to check, if it is real number:

-.3

Real number

.332

Real number

2.5e+23

Real number

a

Not a Real number

Discussion

Advantages :-

In this program we can identify some predefined verbs and can easily feed the output of this program to a parser program. It helps us to understand how a scanner works and also how it interact with the parser.

Limitations :-

This program used only some of the reserved keywords and works on C like statement.

Efficiency :-

            Under a little limitation the efficiency of this program is 100%.

  

Experiment No :- 04

Experiment Name :- Write a simple Lex specification to recognize different keywords.

Theory

Scanner :-

Conceptually, a compiler operates in phases, each of which transforms the source.

Program from one representation to another. The lexical analyzer is the first phase of a compiler. Its main task is to read the input characters & produce as output a sequence of tokens that the parser uses for syntax analysis. The lexical analyzer generator is also known as lexer/scanner.

Interaction with the parser :-

The lexical analyzer is an independent compilation phase that communicates with the parser over a well-defined and simple interface. The interaction, summarized in the following figure, is commonly implemented by making the lexical analyzer be a subroutine of the parser.

 

Lex :-

Lex is a tool for gathering scanners. LEX reads the given input files or standard input for a description of a scanner to generate. The description is in the form of pairs of regular expressions and C code, called RULES. So the LEX specification is given below:

Declarations

%%

Transition rules

%%

Auxiliary procedures / Compiler :-

Here we use the LINUX Operating Systems FLEX to write the LEX code. Where FLEX stands for-Fast Lexical Analyzer generator. FLEX generates as output a C source file, lex.yy.c which defines a routine yylex(), this file is compiled and linked with the – lfl library to produce an exe file. When the executable is run, it analyzes its input for occurrences of the regular expression. Whenever it finds one, it executes the corresponding C code. So the command line can be as follws:

Flex test.l

Gcc lex.yy.c – lfl

./a.out

Identifier :-

Languages use identification as names of variables, arrays, functions and the like. A grammar for a language often treats an identifier as a token. A parser based on such a grammar ants to see the same token say id, each time an identifier appears in the input. For example:

                            Count = count + increment;

Would be converted by the lexical analyzer into the token stream Id = id+id;

Keywords :-

Many language use fixed character strings such as begin, end, if, and so on, as punctuation marks or to identify certain constructs. These character strings, called keywords, generally satisfy the rules for forming identifiers, so a mechanism is needed for deciding when a lexeme forms a keyword and when it forms an identifier. The problem is easier to resolve if keywords are reserved, i.e. if they are cannot be used as identifiers. Then a character string forms an identifier only if it is not a keywords.

Implementation Environment

Hardware Information :-

Processor                                                       Cyrix GX-Media

Speed                                                              233 MHz

External Cache Memory                              512 KB

Internal Cache Memory                              8 KB

RAM                                                                32 MB

Hard Disk Drive                                             8.6 GB

Operating System                                        Linux (Red Hat 5.2)

Program Listing :-

%%

Auto|double|int|struct|break|else|long|switch|case|enum|register|typedef|char|extern|return|union|continue|for|signed|void|do|if|static|while|default|goto|size of|volatile|const|float|short                            {printf(“KEYWORD\n”);}

.*                                                                                     {printf (“Not a KEYWORD \n”);}

%%

int yywrap(){}

int main()

{

printf(“ Enter string to check keyword or not:\n”);

yylex();

}

Input and Output

Input :-

int

if

is

Output :-

Enter string to check keyword or not:

int

KEYWORD

if

KEYWORD

is

Not a KEYWORD

Discussion

Advantages :-

In this program we can identify some predefined verbs and can easily feed the output of this program to a parser program. It helps us to understand how a scanner works and also how it interact with the parser.

Limitations :-

This program used only some of the reserved keywords and works on C like statement.

Efficiency :-

            Under a little limitation the efficiency of this program is 100%.

 

Experiment No :- 05

Experiment Name :- Write a simple lex specification to recognize integer number.

Theory

Scanner :-

Conceptually, a compiler operates in phases, each of which transforms the source.

Program from one representation to another. The lexical analyzer is the first phase of a compiler. Its main task is to read the input characters & produce as output a sequence of tokens that the parser uses for syntax analysis. The lexical analyzer generator is also known as lexer/scanner.

Interaction with the parser :-

The lexical analyzer is an independent compilation phase that communicates with the parser over a well-defined and simple interface. The interaction, summarized in the following figure, is commonly implemented by making the lexical analyzer be a subroutine of the parser.

 

Lex :-

Lex is a tool for gathering scanners. LEX reads the given input files or standard input for a description of a scanner to generate. The description is in the form of pairs of regular expressions and C code, called RULES. So the LEX specification is given below:

Declarations

%%

Transition rules

%%

Auxiliary procedures / Compiler :-

Here we use the LINUX Operating Systems FLEX to write the LEX code. Where FLEX stands for-Fast Lexical Analyzer generator. FLEX generates as output a C source file, lex.yy.c which defines a routine yylex(), this file is compiled and linked with the – lfl library to produce an exe file. When the executable is run, it analyzes its input for occurrences of the regular expression. Whenever it finds one, it executes the corresponding C code. So the command line can be as follws:

Flex test.l

Gcc lex.yy.c – lfl

./a.out

Identifier :-

Languages use identification as names of variables, arrays, functions and the like. A grammar for a language often treats an identifier as a token. A parser based on such a grammar ants to see the same token say id, each time an identifier appears in the input. For example:

                            Count = count + increment;

Would be converted by the lexical analyzer into the token stream Id = id+id;

Keywords :-

Many language use fixed character strings such as begin, end, if, and so on, as punctuation marks or to identify certain constructs. These character strings, called keywords, generally satisfy the rules for forming identifiers, so a mechanism is needed for deciding when a lexeme forms a keyword and when it forms an identifier. The problem is easier to resolve if keywords are reserved, i.e. if they are cannot be used as identifiers. Then a character string forms an identifier only if it is not a keywords.

Implementation Environment

Hardware Information :-

Processor                                                       Cyrix GX-Media

Speed                                                              233 MHz

External Cache Memory                              512 KB

Internal Cache Memory                              8 KB

RAM                                                                32 MB

Hard Disk Drive                                             8.6 GB

Operating System                                        Linux (Red Hat 5.2)

Program Listing :-

%%

[-]?[0-9]*        {printf(“Integer Number \n”);}

.*                      {printf (“Not an Integer Number \n”);}

%%

int yywrap(){}

int main()

{

printf(“Enter a number to check, if it is integer:\n”);

yylex();

}

Input and Output

Input :-

13

15.2

Output :-

Enter a number to check, if it is integer:

13

Integer Number

15.2

Not an Integer Number

Discussion

Advantages :-

In this program we can identify some predefined verbs and can easily feed the output of this program to a parser program. It helps us to understand how a scanner works and also how it interact with the parser.

Limitations :-

This program used only some of the reserved keywords and works on C like statement.

Efficiency :-

            Under a little limitation the efficiency of this program is 100%.

 

Experiment No :- 06

Experiment Name :-  Write a simple lex specification to recognize different punctuation symbols.

Theory

Scanner :-

Conceptually, a compiler operates in phases, each of which transforms the source.

Program from one representation to another. The lexical analyzer is the first phase of a compiler. Its main task is to read the input characters & produce as output a sequence of tokens that the parser uses for syntax analysis. The lexical analyzer generator is also known as lexer/scanner.

Interaction with the parser :-

The lexical analyzer is an independent compilation phase that communicates with the parser over a well-defined and simple interface. The interaction, summarized in the following figure, is commonly implemented by making the lexical analyzer be a subroutine of the parser.

 

Lex :-

Lex is a tool for gathering scanners. LEX reads the given input files or standard input for a description of a scanner to generate. The description is in the form of pairs of regular expressions and C code, called RULES. So the LEX specification is given below:

Declarations

%%

Transition rules

%%

Auxiliary procedures / Compiler :-

Here we use the LINUX Operating Systems FLEX to write the LEX code. Where FLEX stands for-Fast Lexical Analyzer generator. FLEX generates as output a C source file, lex.yy.c which defines a routine yylex(), this file is compiled and linked with the – lfl library to produce an exe file. When the executable is run, it analyzes its input for occurrences of the regular expression. Whenever it finds one, it executes the corresponding C code. So the command line can be as follws:

Flex test.l

Gcc lex.yy.c – lfl

./a.out

Identifier :-

Languages use identification as names of variables, arrays, functions and the like. A grammar for a language often treats an identifier as a token. A parser based on such a grammar ants to see the same token say id, each time an identifier appears in the input. For example:

                            Count = count + increment;

Would be converted by the lexical analyzer into the token stream Id = id+id;

Keywords :-

Many language use fixed character strings such as begin, end, if, and so on, as punctuation marks or to identify certain constructs. These character strings, called keywords, generally satisfy the rules for forming identifiers, so a mechanism is needed for deciding when a lexeme forms a keyword and when it forms an identifier. The problem is easier to resolve if keywords are reserved, i.e. if they are cannot be used as identifiers. Then a character string forms an identifier only if it is not a keywords.

Implementation Environment

Hardware Information :-

Processor                                                       Cyrix GX-Media

Speed                                                              233 MHz

External Cache Memory                              512 KB

Internal Cache Memory                              8 KB

RAM                                                                32 MB

Hard Disk Drive                                             8.6 GB

Operating System                                        Linux (Red Hat 5.2)

Program Listing :-

%%

[,.;:’_!?”-]*                                                  {printf (“Punctuation Symbol \n”);}

.*                                                              {printf (“ Not a Punctuation Symbol \n”);}

%%

int yywrap(){}

int main ()

{

printf (“Enter a character to check punctuation symbol: \n”);

yylex();

}

Input and Output

Input :-

?

;

,

/

+

=

Output :-

?

Punctuation Symbol

;

Punctuation Symbol

,

Punctuation Symbol

/  

Punctuation Symbol

+

Not a Punctuation Symbol

=

Not a Punctuation Symbol

Discussion

Advantages :-

In this program we can identify some predefined verbs and can easily feed the output of this program to a parser program. It helps us to understand how a scanner works and also how it interact with the parser.

Limitations :-

This program used only some of the reserved keywords and works on C like statement.

Efficiency :-

            Under a little limitation the efficiency of this program is 100%.

 

Experiment No :-  07

Experiment Name :-  Write a simple lex specification to recognize a digit or not.

Theory

Scanner :-

Conceptually, a compiler operates in phases, each of which transforms the source.

Program from one representation to another. The lexical analyzer is the first phase of a compiler. Its main task is to read the input characters & produce as output a sequence of tokens that the parser uses for syntax analysis. The lexical analyzer generator is also known as lexer/scanner.

Interaction with the parser :-

The lexical analyzer is an independent compilation phase that communicates with the parser over a well-defined and simple interface. The interaction, summarized in the following figure, is commonly implemented by making the lexical analyzer be a subroutine of the parser.

 

Lex :-

Lex is a tool for gathering scanners. LEX reads the given input files or standard input for a description of a scanner to generate. The description is in the form of pairs of regular expressions and C code, called RULES. So the LEX specification is given below:

Declarations

%%

Transition rules

%%

Auxiliary procedures / Compiler :-

Here we use the LINUX Operating Systems FLEX to write the LEX code. Where FLEX stands for-Fast Lexical Analyzer generator. FLEX generates as output a C source file, lex.yy.c which defines a routine yylex(), this file is compiled and linked with the – lfl library to produce an exe file. When the executable is run, it analyzes its input for occurrences of the regular expression. Whenever it finds one, it executes the corresponding C code. So the command line can be as follws:

Flex test.l

Gcc lex.yy.c – lfl

./a.out

Identifier :-

Languages use identification as names of variables, arrays, functions and the like. A grammar for a language often treats an identifier as a token. A parser based on such a grammar ants to see the same token say id, each time an identifier appears in the input. For example:

                            Count = count + increment;

Would be converted by the lexical analyzer into the token stream Id = id+id;

Keywords :-

Many language use fixed character strings such as begin, end, if, and so on, as punctuation marks or to identify certain constructs. These character strings, called keywords, generally satisfy the rules for forming identifiers, so a mechanism is needed for deciding when a lexeme forms a keyword and when it forms an identifier. The problem is easier to resolve if keywords are reserved, i.e. if they are cannot be used as identifiers. Then a character string forms an identifier only if it is not a keywords.

Implementation Environment

Hardware Information :-

Processor                                                       Cyrix GX-Media

Speed                                                              233 MHz

External Cache Memory                              512 KB

Internal Cache Memory                              8 KB

RAM                                                                32 MB

Hard Disk Drive                                             8.6 GB

Operating System                                        Linux (Red Hat 5.2)

Program Listing :-

%%

[0-9]                                           {printf (“digit\n”);}

.*                                                {printf (“Not a digit\n”);}

%%

int yywrap(){}

int main()

{

printf (“Enter something to check whether input is digit or not:\n”);

yylex();

}

Input and Output

Input :-

1

2

A

Output :-

Enter something to check whether input is digit or not:

1

Digit

2

Digit

65

Not a Digit

A

Not a Digit

Tv

Not a Digit

Discussion

Advantages :-

In this program we can identify some predefined verbs and can easily feed the output of this program to a parser program. It helps us to understand how a scanner works and also how it interact with the parser.

Limitations :-

This program used only some of the reserved keywords and works on C like statement.

Efficiency :-

            Under a little limitation the efficiency of this program is 100%.

 

Experiment Name :-  08

Experiment Name :-  Write a simple lex specification to recognize the identifier.

Theory

Scanner :-

Conceptually, a compiler operates in phases, each of which transforms the source.

Program from one representation to another. The lexical analyzer is the first phase of a compiler. Its main task is to read the input characters & produce as output a sequence of tokens that the parser uses for syntax analysis. The lexical analyzer generator is also known as lexer/scanner.

Interaction with the parser :-

The lexical analyzer is an independent compilation phase that communicates with the parser over a well-defined and simple interface. The interaction, summarized in the following figure, is commonly implemented by making the lexical analyzer be a subroutine of the parser.


Lex :-

Lex is a tool for gathering scanners. LEX reads the given input files or standard input for a description of a scanner to generate. The description is in the form of pairs of regular expressions and C code, called RULES. So the LEX specification is given below:

Declarations

%%

Transition rules

%%

Auxiliary procedures / Compiler :-

Here we use the LINUX Operating Systems FLEX to write the LEX code. Where FLEX stands for-Fast Lexical Analyzer generator. FLEX generates as output a C source file, lex.yy.c which defines a routine yylex(), this file is compiled and linked with the – lfl library to produce an exe file. When the executable is run, it analyzes its input for occurrences of the regular expression. Whenever it finds one, it executes the corresponding C code. So the command line can be as follows:

Flex test.l

Gcc lex.yy.c – lfl

./a.out

Identifier :-

Languages use identification as names of variables, arrays, functions and the like. A grammar for a language often treats an identifier as a token. A parser based on such a grammar ants to see the same token say id, each time an identifier appears in the input. For example:

                            Count = count + increment;

Would be converted by the lexical analyzer into the token stream Id = id+id;

Keywords :-

Many language use fixed character strings such as begin, end, if, and so on, as punctuation marks or to identify certain constructs. These character strings, called keywords, generally satisfy the rules for forming identifiers, so a mechanism is needed for deciding when a lexeme forms a keyword and when it forms an identifier. The problem is easier to resolve if keywords are reserved, i.e. if they are cannot be used as identifiers. Then a character string forms an identifier only if it is not a keywords.

Implementation Environment

Hardware Information :-

Processor                                                       Cyrix GX-Media

Speed                                                              233 MHz

External Cache Memory                              512 KB

Internal Cache Memory                              8 KB

RAM                                                                32 MB

Hard Disk Drive                                             8.6 GB

Operating System                                        Linux (Red Hat 5.2)

Program Listing :-

%%

[a-zA-Z][a-zA-Z0-9_]*                  {printf(“Identifier\n”);}

.*                                                    {printf(“Not an Identifier\n”);}

%%

int yywrap(){}

int main()

{

printf(“Enter string to check Identifier or not:\n”);

yylex();

}

Input and Output

Input :-

Enter string to check Identifier or not:

global

gtb458

bee see

573he

Output :-

global

Identifier

gtb458

Identifier

bee see

Not an Identifier

573he

Not an Identifier

Discussion

Advantages :-

In this program we can identify some predefined verbs and can easily feed the output of this program to a parser program. It helps us to understand how a scanner works and also how it interact with the parser.

Limitations :-

This program used only some of the reserved keywords and works on C like statement.

Efficiency :-

            Under a little limitation the efficiency of this program is 100%.

 

Experiment Name :- 09

Experiment Name :- Write a simple lex specification to recognize float.

Theory

Scanner :-

Conceptually, a compiler operates in phases, each of which transforms the source.

Program from one representation to another. The lexical analyzer is the first phase of a compiler. Its main task is to read the input characters & produce as output a sequence of tokens that the parser uses for syntax analysis. The lexical analyzer generator is also known as lexer/scanner.

Interaction with the parser :-

The lexical analyzer is an independent compilation phase that communicates with the parser over a well-defined and simple interface. The interaction, summarized in the following figure, is commonly implemented by making the lexical analyzer be a subroutine of the parser.

 

Lex :-

Lex is a tool for gathering scanners. LEX reads the given input files or standard input for a description of a scanner to generate. The description is in the form of pairs of regular expressions and C code, called RULES. So the LEX specification is given below:

Declarations

%%

Transition rules

%%

Auxiliary procedures :-

Here we use the LINUX Operating Systems FLEX to write the LEX code. Where FLEX stands for-Fast Lexical Analyzer generator. FLEX generates as output a C source file, lex.yy.c which defines a routine yylex(), this file is compiled and linked with the – lfl library to produce an exe file. When the executable is run, it analyzes its input for occurrences of the regular expression. Whenever it finds one, it executes the corresponding C code. So the command line can be as follows:

Flex test.l

Gcc lex.yy.c – lfl

./a.out

Identifier :-

Languages use identification as names of variables, arrays, functions and the like. A grammar for a language often treats an identifier as a token. A parser based on such a grammar ants to see the same token say id, each time an identifier appears in the input. For example:

                            Count = count + increment;

Would be converted by the lexical analyzer into the token stream Id = id+id;

Keywords :-

Many language use fixed character strings such as begin, end, if, and so on, as punctuation marks or to identify certain constructs. These character strings, called keywords, generally satisfy the rules for forming identifiers, so a mechanism is needed for deciding when a lexeme forms a keyword and when it forms an identifier. The problem is easier to resolve if keywords are reserved, i.e. if they are cannot be used as identifiers. Then a character string forms an identifier only if it is not a keywords.

Implementation Environment

Hardware Information :-

Processor                                                       Cyrix GX-Media

Speed                                                              233 MHz

External Cache Memory                              512 KB

Internal Cache Memory                              8 KB

RAM                                                                32 MB

Hard Disk Drive                                             8.6 GB

Operating System                                        Linux (Red Hat 5.2)

Program Listing :-

%%

[-]?[0-9]+”.”[0-9]+                  {printf(“Float Number\n”);}

.*                                               {printf(“Not a Float Number\n”);}

%%

int yywrap(){}

int main()

{

printf(“Enter a number to check, if it is float or not:\n”);

yylex();

}

Input and Output

Input :-

564.65

-329.52

2378

1204

Output :-

Enter a number to check, if it is float or not:

564.65

Float Number

-329.52

Float Number

2378

Not a Float Number

1204

Not a Float Number

Discussion

Advantages :-

In this program we can identify some predefined verbs and can easily feed the output of this program to a parser program. It helps us to understand how a scanner works and also how it interact with the parser.

Limitations :-

This program used only some of the reserved keywords and works on C like statement.

Efficiency :-

            Under a little limitation the efficiency of this program is 100%.

 

Experiment Name :- 10

Experiment Name :- Write a simple lex specification to recognize for the positive and negative integer and float number.

Theory

Scanner :-

Conceptually, a compiler operates in phases, each of which transforms the source.

Program from one representation to another. The lexical analyzer is the first phase of a compiler. Its main task is to read the input characters & produce as output a sequence of tokens that the parser uses for syntax analysis. The lexical analyzer generator is also known as lexer/scanner.

Interaction with the parser :-

The lexical analyzer is an independent compilation phase that communicates with the parser over a well-defined and simple interface. The interaction, summarized in the following figure, is commonly implemented by making the lexical analyzer be a subroutine of the parser.

 

Lex :-

Lex is a tool for gathering scanners. LEX reads the given input files or standard input for a description of a scanner to generate. The description is in the form of pairs of regular expressions and C code, called RULES. So the LEX specification is given below:

Declarations

%%

Transition rules

%%

Auxiliary procedures / Compiler :-

Here we use the LINUX Operating Systems FLEX to write the LEX code. Where FLEX stands for-Fast Lexical Analyzer generator. FLEX generates as output a C source file, lex.yy.c which defines a routine yylex(), this file is compiled and linked with the – lfl library to produce an exe file. When the executable is run, it analyzes its input for occurrences of the regular expression. Whenever it finds one, it executes the corresponding C code. So the command line can be as follows:

Flex test.l

Gcc lex.yy.c – lfl

./a.out

Identifier :-

Languages use identification as names of variables, arrays, functions and the like. A grammar for a language often treats an identifier as a token. A parser based on such a grammar ants to see the same token say id, each time an identifier appears in the input. For example:

                            Count = count + increment;

Would be converted by the lexical analyzer into the token stream Id = id+id;

Keywords :-

Many language use fixed character strings such as begin, end, if, and so on, as punctuation marks or to identify certain constructs. These character strings, called keywords, generally satisfy the rules for forming identifiers, so a mechanism is needed for deciding when a lexeme forms a keyword and when it forms an identifier. The problem is easier to resolve if keywords are reserved, i.e. if they are cannot be used as identifiers. Then a character string forms an identifier only if it is not a keywords.

Implementation Environment

Hardware Information :-

Processor                                                       Cyrix GX-Media

Speed                                                              233 MHz

External Cache Memory                              512 KB

Internal Cache Memory                              8 KB

RAM                                                                32 MB

Hard Disk Drive                                             8.6 GB

Operating System                                        Linux (Red Hat 5.2)

Program Listing :-

%%

[+]?[0-9]+                {printf(“Positive Integer\n”);}

[-]{1}[0-9]+              {printf(“Negative Integer\n”);}

[+]?[0-9]+[.]{1}[0-9]+      {printf(“Positive Fraction\n”);}

[-]?[0-9]+[.]{1}[0-9]+       {printf(“Negative Fraction\n”);}

%%

int yywrap(){}

int main()

{

printf(“Enter number to find positive and negative; integer and float number:\n”);

yylex();

}

Input and Output

Input :-

3452

-2389

245.67

-238.23

Output :-

Enter number to find positive and negative; integer and float number:

3452

Positive Integer

-2389

Negative Integer

245.67

Positive Fraction

-238.23

Negative Fraction

Discussion

Advantages :-

In this program we can identify some predefined verbs and can easily feed the output of this program to a parser program. It helps us to understand how a scanner works and also how it interact with the parser.

Limitations :-

This program used only some of the reserved keywords and works on C like statement.

Efficiency :-

            Under a little limitation the efficiency of this program is 100%.

 

Experiment No :- 11

Experiment Name :- Write a lex program to recognize different types of operator.

Theory

Scanner :-

Conceptually, a compiler operates in phases, each of which transforms the source.

Program from one representation to another. The lexical analyzer is the first phase of a compiler. Its main task is to read the input characters & produce as output a sequence of tokens that the parser uses for syntax analysis. The lexical analyzer generator is also known as lexer/scanner.

Interaction with the parser :-

The lexical analyzer is an independent compilation phase that communicates with the parser over a well-defined and simple interface. The interaction, summarized in the following figure, is commonly implemented by making the lexical analyzer be a subroutine of the parser.

 

Lex :-

Lex is a tool for gathering scanners. LEX reads the given input files or standard input for a description of a scanner to generate. The description is in the form of pairs of regular expressions and C code, called RULES. So the LEX specification is given below:

Declarations

%%

Transition rules

%%

Auxiliary procedures / Compoler :-

Here we use the LINUX Operating Systems FLEX to write the LEX code. Where FLEX stands for-Fast Lexical Analyzer generator. FLEX generates as output a C source file, lex.yy.c which defines a routine yylex(), this file is compiled and linked with the – lfl library to produce an exe file. When the executable is run, it analyzes its input for occurrences of the regular expression. Whenever it finds one, it executes the corresponding C code. So the command line can be as follows:

Flex test.l

Gcc lex.yy.c – lfl

./a.out

Identifier :-

Languages use identification as names of variables, arrays, functions and the like. A grammar for a language often treats an identifier as a token. A parser based on such a grammar ants to see the same token say id, each time an identifier appears in the input. For example:

                            Count = count + increment;

Would be converted by the lexical analyzer into the token stream Id = id+id;

Keywords :-

Many language use fixed character strings such as begin, end, if, and so on, as punctuation marks or to identify certain constructs. These character strings, called keywords, generally satisfy the rules for forming identifiers, so a mechanism is needed for deciding when a lexeme forms a keyword and when it forms an identifier. The problem is easier to resolve if keywords are reserved, i.e. if they are cannot be used as identifiers. Then a character string forms an identifier only if it is not a keywords.

Implementation Environment

Hardware Information :-

Processor                                                       Cyrix GX-Media

Speed                                                              233 MHz

External Cache Memory                              512 KB

Internal Cache Memory                              8 KB

RAM                                                                32 MB

Hard Disk Drive                                             8.6 GB

Operating System                                        Linux (Red Hat 5.2)

Program Listing :-

%{

#include <stdio.h>;

%}

%%

"-"|"+"|"*"|"!"|"="|"%"             {printf("Operator\n\n");}

.*                                                     {printf("Not an operator\n\n");}

%%

main()

{

yylex();

return 0;

}

int yywrap()

{

}

Input and Output

Input :-

+

-

sd

Output :-

+

Operator

 

-

Operator

 

CSE

Not an operator

 

30

Not an operator

 

Discussion

Advantages :-

In this program we can identify some predefined verbs and can easily feed the output of this program to a parser program. It helps us to understand how a scanner works and also how it interact with the parser.

Limitations :-

This program used only some of the reserved keywords and works on C like statement.

Efficiency :-

            Under a little limitation the efficiency of this program is 100%.

No comments

Powered by Blogger.