How to compile using Flex and TCC on Windows-how to build an experimental environment for lexical analysis

Source: Internet
Author: User

Yesterday I published the Windows Flex + GCC lexical analysis experiment toolkit, which may have been removed from the homepage as an advertisement or Software published by my blog team, after all, I spent some time organizing these things. This article will avoid the suspicion of the above problems, so it will not be provided for downloading any more. It will explain in detail how to build the experiment environment by yourself. In addition, TCC is used to replace GCC, mainly because the GCC program obtained from the Internet is extracted from Dev-CPP, and the extracted people are not perfect, so no string is provided. h header file, which is the header file of the C language standard library function. It also performs lexical analysis and deals with files and strings. Some string operation functions are easy to use. I also tried to extract string from Dev-CPP. H and corresponding. A ,. the O library file, but it may be because the dev-CPP compilation version does not match, and no matching file is found. I tried to find a string. H + String. cpp independently implemented, but I gave up temporarily. Later, I found someone mentioned another C language compiler, tiny C compiler (TCC. This C compiler is indeed tiny. I downloaded the binary installation package in windows. After decompression (no installation is required), the associated Doc and examples are only over 900 kb in total! I tested the code that I tested in mingw + GCC + flex, compiled it, and ran the program correctly. Therefore, TCC can be used as an alternative to GCC mentioned earlier. The following describes how to set up a Flex + TCC lexical analysis experiment environment, which is actually very simple.

First, download the tool we want to use. There are two, one is Flex, and the available version on the Windows platform is the file download corresponding to the binary distribution. Flex is an EXE installation program. In fact, this program should only be used to understand the pressure. TCC downloads a compressed package and decompress it to a suitable directory. It is recommended that the directory level be as follows:

This basically works. Both flex and TCC are command line tools. For future convenience, you do not have to switch directories frequently, the flex and TCC directories \ flex \ bin and \ TCC can be added to the environment variable path. Of course, like in my previous article, you can write a start statement. bat batch processing. The advantage of doing so is that the path added with BAT is only valid in the current process. In fact, the path of the environment variable is not modified. It is uncomfortable to see too many paths. The following is start. bat source code, saved as start with notepad. BAT (note the extended name) and save it to the flex_tcc directory on the graph:

@echo off
path = %path%;%cd%\flex\bin;%cd%\tcc;
cmd /k cls

After double-clicking, a command line is started and the path is entered. The two directories \ flex \ bin and \ TCC are mounted at the end. Enter flex -- version to display the current flex version, and TCC-version to display the current TCC version. These two commands are valid, that is, flex and TCC are available. Then, find a piece of code to test. The following code is written for yourself:

% {# Include <stdio. h> # include <string. h> # include <stdlib. h> file * fout1; void Ttype (char * type, char * Text, file * stream) {fputs ("(", stream); fputs (type, stream ); fputs (",", stream); fputs (text, stream); fputs (") \ n", stream );} %} digit [0-9] Number {digit} + letter [A-Za-Z] identifier {letter} ({digit} | {letter} | _) * % {identifier} {Ttype ("ident", yytext, fout1) ;}{ number} {Ttype ("num", yytext, fout1 );}. {/* unmatched characters, not processed */} % int main () {If (fout1 = fopen ("output.txt", "W ")) = NULL) {printf ("can not create the file. \ n "); exit (0);} yylex (); fclose (fout1); Return 0;} int yywrap () {return 1 ;}

Use NotePad to save the code as sample. L (the standard extension name is. l, but it seems that flex does not matter) and put it in the flex_tcc directory. Generate and compile the following code,
Double-click Start. BAT and enter flex sample. l in the command line window. A file of Lex. yy. C is generated under the flex_tcc directory, and a executable file of lex.yy.exe is generated by entering TCC lex.yy.c.

In this way, the generation and compilation are completed. Next, test the program, randomly enter some words and numbers, and press Ctrl + Z to end the input. At this time, the flex_tccdirectory will generate an output.txt text file, which is used to judge the words and numbers just entered.

In this way, we have completed the environment for creating a lexical analysis experiment on the Windows platform using Flex and TCC.

Note: The first lab assignment in the Compilation Principle Course is lexical analysis. The assignment assigned by the TA teacher is not too difficult. I only want to use the Linux system. I do not reject Linux, but the computer is too broken, no virtual machine, dual system, and other solutions can be banned, so I have to find an alternative solution on Windows. At present, this is the smallest solution I feel. In addition, TCC is an open-source compiler. Since it is learning the compilation principle, we should start to study TCC code at the right time.

Related Article

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.