Header Ads

Header ADS

Write a simple lex specification to recognize different punctuation symbols.

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%.

 

 

 

No comments

Powered by Blogger.