Lex and YACC tutorial (9). Use Lex and YACC in Windows

Source: Internet
Author: User

Lex and YACC application method (9). Use Lex and YACC in Windows

Papaya 20070904

I. Sequencing

I don't want the last article in The lex and YACC series to be so difficult. It has been three months away. Can not help but sigh freely support
Time allocation is so little that it is not the "slave" of the new age "~
This article mainly introduces how to use Lex and YACC in windows.
The end of this series of articles.

Ii. Method Introduction

Windows uses lex and YACC in a variety of ways, which are listed below:

A. cygwin

Cygwin is a Unix/Linux Simulation Environment running on Windows, developed by Cygnus solutions.
At first, Cygnus improved development tools such as GCC, GDB, and gas so that they could generate and explain the objective of Win32.
File. And then port these tools to the Windows platform.
There are many options for value shifting. First, the source code of these tools is greatly modified based on Win32 APIs. But the workload is too heavy.
We adopt another method, that is, to develop a shared library (cygwin. dll), call the Unix-style APIs that do not exist in Win32 APIs (such
Fork, spawn, signals, select, sockets, and so on) is encapsulated in it. That is to say, they write a Unix
The simulation layer of the system library. In this way, you only need to connect the source code of these tools with the shared library, you can use the Unix master
The on-board cross compiler to generate a set of tools that can run on the Windows platform.

Based on these development tools transplanted to the Windows platform, Cygnus gradually releases other tools (almost no
You only need some configuration scripts to modify the source code. In this way, on the Windows platform
Simulate a Unix environment.

Cygwin is a powerful tool set. With it, You can compile and run Unix programs without a unix machine,
This can help program developers port applications from Unix/Linux to windows, or
Develop Unix/Linux applications. Cygwin aims at compatibility rather than execution efficiency.

B. mingw + msys

Mingw (minimalist GNU for Windows) was originally a branch of the GNU development tool in cygwin, essentially
A collection of header files and function libraries that allow GCC (GNU Compiler c) without a third-party dynamic link library)
Develop Win32 programs. Mingw consists of GNU binary utilities, GCC, and GDB. It also includes some important
Library: libc (C Runtime), an api library dedicated to the Win32 environment. Mingw programs and MS Visual Studio
Programs can be common to each other.
Mingw allows console-mode programs to use Microsoft's Standard C Runtime Library (msvcrt. dll), so you can use
GCC can write the ANSI program in the Console mode, or use the C Runtime Library provided by Microsoft. This function is provided by Windows 32 API
Not available.
Cygwin + GCC and mingw are both GCC compiling environments in windows. Program compiled by cygwin + GCC, in Windows
Cygwin. dll is required for execution, and mingw is not required. Compared with cygwin, mingw focuses on execution efficiency.

Mingw is only a development environment, but is actually an implementation of GCC in windows, not including Linux/Unix.
(Such as Bash and SH), and msys makes up for this. Mingw is often used in combination with msys.

Msys (minimal GNU (POSIX) System on Windows) is a small GNU environment, including basic
Bash, make, etc. It provides a similar environment of the Bourne shell.

C. Download the Win32 source code or version of flex and bison

In fact, this is the essence of the above two methods. Web site: http://gnuwin32.sourceforge.net

D. Dev-C ++ compile the Code Compiled by bison flex

In fact, Dev C ++ is used to replace mingw GCC or cygwin GCC.
Dev-C ++ is a C & C ++ development tool. It is a free software and complies with the GPL protocol. It integrates GCC, mingw32
And can get the latest tool support from devpak.org. All this work comes from
What fans around the world do. Dev-C ++ IDE is developed using Delphi.
However, it should be noted that the variable declaration location problem will cause compilation failure, that is, the previous example is used in Dev-C ++
Under the Authentication Settings, compilation fails. It's easy to solve, just move it to the function body manually.

E. Use Parser Generator

Parser generator can generate C/C ++/Java lexyacc code on Windows. Support Borland C ++ Builder
And Visual C ++ compiling environments. Here I use the lexyacc code generated by Visual C ++. NET and eclipse tests.

(Local testing environment Visual C ++. Net/eclipse 3.1 + Parser Generator 2.07)
 
Before using Parser Generator, you must build the relevant Lib. Open Parser Generator IDE, project-> libbuilder
-> Select visual c ++ (32-bit)-> properties-> set options in the pop-up dialog box, as shown in the following example:

Compile version: Version 7 (. NET)
UNICODE: True
Treat w_char as build-in type: True
Compiler bin directory: e:/program files/Microsoft Visual Studio. NET 2003/vc7/bin
Compiler bin directory (2): e:/program files/Microsoft Visual Studio. NET 2003/common7/ide
Compiler include directory: e:/program files/Microsoft Visual Studio. NET 2003/vc7/include
Compiler include directory (2): e:/program files/Microsoft Visual Studio. NET 2003/vc7/platformsdk/include
Compiler library Directory: e:/program files/Microsoft Visual Studio. NET 2003/vc7/lib
Compiler library directory (2): e:/program files/Microsoft Visual Studio. NET 2003/vc7/platformsdk/lib

Note: Adjust the directory location as needed.
 
After setting, confirm and return to libbuilder. Click build to compile the Lib library of lex and YACC required by Visual C ++. Generate Database
File in the software installation directory e:/program files/parser/CPP/lib/msvc32.
Then you can add
Header file directory
E:/program files/parser/CPP/include
Library file directory
E:/program files/parser/CPP/lib/msvc32
Dependency
YL. Lib YLD. Lib ylmt. Lib ylmtd. Lib ylmtr. Lib ylmtrd. Lib ylmtri. Lib ylmtrid. Lib

(Visual C ++ environment setting method can be seen in the Oracle database development (a). windows under the configuration using proc "http://blog.csdn.net/liwei_cmg/archive/2007/06/06/1641330.aspx article, a detailed description, here is not detailed)
 
Java code generation is also very similar. Add the built-in example E:/program files/parser/Java/examples/calc code to the Eclipse project.
Add E:/program files/parser/Java/lib/YL. jar to the project and start Java application.

Project file list:

Calc_lexer.java
Calc_parser.java
Symbol. Java
Symboltable. Java

YL. Jar

Run as-> JAVA application, and enter the test data in the console.
 
For more information about how to use Parser Generator, see the online manual.


Iii. Postscript

Most of the methods mentioned in this article are mentioned here. As for the specific content, you can try it yourself. This is not part of this series.
. The cross-compilation problem mentioned in this article has been described in the mobile app and Development column as a whole,
In the future series of "Linux platform development", we will provide a more detailed description.
 

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.