"Compiling principle" lex lexical analyzer

Source: Internet
Author: User
Tags constant printf reserved

first, the purpose of the experiment

A lexical analyzer is designed and implemented to understand the principle of compiler principle morphemes Method Analyzer.

Ii. contents of the experiment

By designing and implementing a lexical analyzer in your familiar language, this parser outputs the parsed program segments in the required format.

Request an analysis of the program fragment:

[Delphi] View plain copy const a=10;   var b,c;     Procedure P;     Begin C:=b+a;      End    Begin read (b);     While b#0 does begin call P;writeln (2*C); read (b); End end.
The program fragment is stored in the example.txt.


third, the experimental steps

1, the classification of various words in Pl\0 grammar:

(1) Reserved words: const, VAR, procedure, begin, end, if, then, while, do, read, call, write, Writeln

(2) Constant: consists of 0-----9 of these numbers

(3) Identifier: A string of letters and numbers that begin with a letter

(4) Operator: +,-,*,/,:=,>=,<=,#

(5) Delimiter:, 、.、 (,),;

2. correspond all kinds of words to Lex:

(1) Reserved words:

Reservedword [Const|var|procedure|begin|end|if|then|while|do|read|call|write|writeln], because it is not case-sensitive in Lex, the reserved word is written as:

Reservedword [cc][oo][nn][ss][tt]| [VV] [AA] [rr]| [PP] [RR] [OO] [CC] [EE] [DD] [UU] [RR] [ee]|

[BB] [EE] [GG] [II] [nn]| [EE] [NN] [dd]| [II] [ff]| [TT] [HH] [EE] [nn]| [WW] [HH] [II]

[LL] [ee]| [DD] [oo]| [RR] [EE] [AA] [dd]| [CC] [AA] [LL] [ll]| [WW] [RR] [II] [TT] [EE]

[WW] [RR] [II] [TT] [EE] [LL] [NN]

(2) Constant:

Constant ([0-9]) +/*0---9 These numbers can be repeated */

(3) Identifier:

Identfier [A-za-z] ([a-za-z][0-9]) *

(4) Operator:

Operator \+|-|\*|\/|:=|>=|<=|#|=/* in Lex, operators with special meanings are added escape characters \, such as +, * and *,/

(5) Delimiter:

delimiter [, \.;\ (\)]

3. The lexical parser for the pl/0 language requires skipping delimiters (such as spaces, carriage returns, tab characters), and the corresponding Lex definition as:

Delim ["" \n\t]

whitespace{delim}+

4. make some rules for Lex:

{Reservedword}   {count++;p rintf ("\t%d\t (1, '%s ') \ n", Count,yytext);} /* Set rules for reserved words, output settings */

{operator} {count++;p rintf ("\t%d\t (2, '%s ') \ n", Count,yytext);}

{delimiter} {count++;p rintf ("\t%d\t (3, '%s ') \ n", Count,yytext);}

{constant} {count++;p rintf ("\t%d\t (4, '%s ') \ n", Count,yytext);}

{Identfier} {count++;p rintf ("\t%d\t (5, '%s ') \ n", Count,yytext);}

{whitespace} {/* do nothing*/}/* Encounter spaces, do nothing */

5, write sub-program:

Voidmain ()

{

printf ("Lexical Analyzer output type description: \ n");

printf ("1: reserved word \ n");

printf ("2: operator \ n");

printf ("3: Delimiter \ n");

printf ("4: Constant \ n");

printf ("5: identifier \ n");

printf ("\ n");

Yyin=fopen ("Example.txt", "R");

Yylex (); /* Start the analysis*/

Fclose (Yyin);

System ("pause");/* Pause */

}

Intyywrap ()

{

return 1;

}

When Lex finishes reading the input file, it calls the function Yywrap. Returns 0 if 1 indicates that the program's work has been completed.


Iv. Source code for Implementation [CPP]   View plain copy%{       # include <stdio.h>        #include  <stdlib.h>         int count = 0;  %}       Delim  [" " \n\t]    whitespace {delim}+    operator \+|-|\*|\/|:=| >=|<=|#|=   reservedword [cc][oo][nn][ss][tt]| [VV] [AA] [rr]| [PP] [RR] [OO] [CC] [EE] [DD] [UU] [RR] [ee]| [BB] [EE] [GG] [II] [nn]| [EE] [NN] [dd]| [II] [ff]| [TT] [HH] [EE] [nn]| [WW] [HH] [II] [LL] [ee]| [DD] [oo]| [RR] [EE] [AA] [dd]| [CC] [AA] [LL] [ll]| [WW] [RR] [II] [TT] [ee]| [WW] [RR] [II] [TT] [EE] [LL] [nn]  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.