Compilation principle-Experiment two-Flex lexical analyzer

Source: Internet
Author: User

FLEX Lexical Analyzer

First, Lex and YACC Introduction

Lex is a tool for generating scanners. A scanner is a program that recognizes lexical patterns in text. A matching regular expression may contain related actions. This action may also include returning a token. When Lex receives input in the form of a file or text, it attempts to match the text to a regular expression. It reads one input character at a time until a matching pattern is found. If a matching pattern can be found, Lex performs the associated action (possibly including returning a tag). On the other hand, if there are no regular expressions that can be matched, further processing will stop, and Lex will display an error message.

YACC represents yet another Compiler Compiler. The GNU version of YACC is called Bison. It is a tool that translates all the syntax of any programming language into a YACC parser for that language.

(Download Flex and bison.) The URLs are http://gnuwin32.sourceforge.net/packages/flex.htm and http://gnuwin32.sourceforge.net/packages/bison.htm, respectively. )

Second, the configuration Environment (WIN7)

① Download Flex and Bison and install to D:\GnuWin32 (try to be the root directory)

② because we use the flex and Bison are GNU tools, so for convenience, the use of C/s

The compiler also uses the GNU compiler, GCC, and of course we need the Windows version of GCC. So prepare the VC 6.0 in advance.

③ Check if Lex file compilation is possible

1. Create a new text file, change the name to LEX.L, and typing the code below

%{

int yywrap (void);

%}

%%

%%

int yywrap (void)

{

return 1;

}

2. Create a new text file, change the name to Yacc.y, and typing the code below

%{

void Yyerror (const char *s);

%}

%%

Program

;

%%

void Yyerror (const char *s)

{

}

int main ()

{

Yyparse ();

return 0;

}

Let's not discuss the meaning of the above code for the moment.

Open the console and go to the folder where you just created the file (LEX.L,YACC.Y).

1. Enter Flex LEX.L

2. Enter Bison YACC.Y

If we see two more files (YACC.TAB.C,LEX.YY.C) on the current folder, then the LEX&&YACC has been successfully installed and the experiment is ready.

Iii. examples of textbooks

1. according to the textbook p58 3.7.3 code , save the Testscan.lex file, and then put it in the same folder as Flex, then enter the DOS, input Flex Testscan.lex and return, To generate a lex.yy.c file

2. Compile and generate Lexyy.exe with the C compiler (VC 6.0)

3. Build the Aaa.txt input file, enter DOS, enter the Lexyy.exe directory, enter Lexyy.exe aaa.txt bbb.txt.

{
int A;
a=10;
}

After analysis:

{, {

int, int
NUM, a
;, ;

NUM, a
=, =
ID, 10
;, ;

}, }

4. Textbook p61 7

According to the requirements of the correct code, and then enter DOS, into the Lexyy.exe directory, enter Lexyy.exe abc.txt abcdef.txt, and then open abcdef.txt to see the results

int i=10;
do{
printf (i);
i++;
}while (I!>10)
if (1<2&&2<4)
Else (1| | 2)

After analysis:

int, int
NUM, I
=, =
ID, 10
;, ;

NUM, do
{, {

NUM, printf
(, (
NUM, I
), )
;, ;

NUM, I
+, +
+, +
;, ;

}, }
While, while
(, (
NUM, I
!, !
, >
ID, 10
), )

If, if
(, (
ID, 1
<, <
ID, 2
&&id, 2
<, <
ID, 4
), )

else, Else
(, (
ID, 1
|| ID, 2
), )

Five, the Code

%{
#include <stdio.h>
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
%}

Digit [0-9]
Number {digit}+
Letter [A-za-z]
identifier {Letter} ({letter}|{ digit}) *
newline [\ n]
whitespace [\t]+
%%
"If" {fprintf (Yyout, "%s,%s\n", Yytext,yytext);}
"Else" {fprintf (Yyout, "%s,%s\n", Yytext,yytext);}
"For" {fprintf (Yyout, "%s,%s\n", Yytext,yytext);}
"While" {fprintf (Yyout, "%s,%s\n", Yytext,yytext);}
"int" {fprintf (Yyout, "%s,%s\n", Yytext,yytext);}
"=" {fprintf (Yyout, "%s,%s\n", Yytext,yytext);}
"+" {fprintf (Yyout, "%s,%s\n", Yytext,yytext);}
"-" {fprintf (Yyout, "%s,%s\n", Yytext,yytext);}
"*" {fprintf (Yyout, "%s,%s\n", Yytext,yytext);}
"/" {fprintf (Yyout, "%s,%s\n", Yytext,yytext);}
"<" {fprintf (Yyout, "%s,%s\n", Yytext,yytext);}
">" {fprintf (Yyout, "%s,%s\n", Yytext,yytext);}
"(" {fprintf (Yyout, "%s,%s\n", Yytext,yytext);}
")" {fprintf (Yyout, "%s,%s\n", Yytext,yytext);}
"{" {fprintf (Yyout, "%s,%s\n", Yytext,yytext);}
"}" {fprintf (Yyout, "%s,%s\n", Yytext,yytext);}
";" {fprintf (Yyout, "%s,%s\n", Yytext,yytext);}
":" {fprintf (Yyout, "%s,%s\n", Yytext,yytext);}
"'" {fprintf (Yyout, "%s,%s\n", Yytext,yytext);}
"," {fprintf (Yyout, "%s,%s\n", Yytext,yytext);}
"!" {fprintf (Yyout, "%s,%s\n", Yytext,yytext);}
"= =" {fprintf (Yyout, "%s,%s\n", Yytext,yytext);}
">=" {fprintf (Yyout, "%s,%s\n", Yytext,yytext);}
"<=" {fprintf (Yyout, "%s,%s\n", Yytext,yytext);}
"! =" {fprintf (Yyout, "%s,%s\n", Yytext,yytext);}
{Number} {fprintf (Yyout, "%s,%s\n", "ID", Yytext);}
{identifier} {fprintf (Yyout, "%s,%s\n", "NUM", Yytext);}
{whitespace} {/* Skip blank */}
"/*" {char C;
int done=false;
do{
while ((C=input ()) = = ' * ');
while ((C=input ()) = = ' * ');
if (c== '/') done=true;
}while (!done);
}
{fprintf (Yyout, "%d,%s\n", 0,yytext);}
%%

Main (ARGC,ARGV)
int argc;
Char **argv;
{
++ARGV,--argc;
if (argc>0)
Yyin=fopen (Argv[0], "R");
Else
Yyin=stdin;
++ARGV,--argc;
if (argc>0)
Yyout=fopen (Argv[0], "w");
Else
Yyout=stdout;
Yylex ();
}
int Yywrap ()
{
return 1;
}

Compilation principle-Experiment two-Flex lexical analyzer

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.