Lex and YACC tools in Windows

Source: Internet
Author: User

It is recommended only because the c file generated by it can be compiled in VC ++.
Http://www.bumblebeesoftware.com/downloads.htm
Decompress the package and install the package. Open the integrated development environment, click the project menu, and select libbuilder from the drop-down menu. The libbuilder dialog box is displayed. select attribute.
The compiler Properties dialog box is displayed,

Name: Visual C ++ (32-bit)
Directory: msvc32
Options:
Is
VC ++ version (versoin 7 (. NET )).
Whether Unicode code is supported.
Whether to use w_char_t as the built-in type.
Below are various directories of VC:
Is
Compiler bin directory (C:/program files/Microsoft Visual Studio. NET 2003/vc7/bin)
Compiler bin directory (C:/program files/Microsoft Visual Studio. NET 2003/common7/IDE)
Compiler include directory (C:/program files/Microsoft Visual Studio. NET 2003/vc7/include)
Compiler include directory (C:/program files/Microsoft Visual Studio. NET 2003/vc7/platformsdk/include)
Compiler library directory (C:/program files/Microsoft Visual Studio. NET 2003/vc7/LIB)
Compiler library directory (C:/program files/Microsoft Visual Studio. NET 2003/vc7/platformsdk/LIB)
Note: The installation directory in the brackets is my own compiler. Maybe you are different from me and you need to adjust it accordingly.
Click OK after all these settings are complete. Returns libbuilder. Click build to compile the Lib library of lex and YACC used by VC ++. Generate the library file in the D:/Parser Generator 2/CPP/lib/msvc32 directory under the software installation directory.
Then we add the Directory D:/Parser Generator 2/CPP/include in the VC ++ environment settings.
Lib library Directory D:/Parser Generator 2/CPP/lib/msvc32
Specific settings
Open the integrated development environment of VC ++. net.
Click tool menu
Click the Select menu in the drop-down menu.
In the pop-up box, click projects in the list box on the left, and then click the VC ++ directory.
In the list box that shows the following content directory label, select "select include directory" and select "library file" to set.
The specific setting is to click the second button under the five buttons in the list box and add a row. When "contains directory" is selected, enter D:/Parser Generator 2/CPP/include, enter "D:/Parser Generator 2/CPP/lib/msvc32" when selecting "Library File". You can also click "Browse" on the right to select a file.

In this way, we can use VC ++ to compile the c file generated by Parser Generator 2. Specifically
Open the integrated development environment of Parser Generator 2.
Click the project menu, click the parsewizard menu in the drop-down menu, and create a project along the way. Click rebuild all under the project menu. The corresponding C file and H file are generated in the project folder.
Create an empty console project with VC ++ and add the c file generated with Parser Generator 2 to the project. Finally, the library file for the response is the lex and YACC library files generated in the D:/Parser Generator 2/CPP/lib/msvc32 directory. Next, compile the program. If it succeeds, everything will be fine.

 

 


YACC and Lex have been introduced in the following articles.
/Html/linuxshijie/20070909/80 .html
The article in the link also describes how to use the YACC and Lex tools in Linux. The following describes how to use YACC and Lex in windows.

Xiaolin is the author of the following content. Please respect the author's hard work and do not repost it.

YACC and Lex have been successfully transplanted to Windows. One common tool is Parser Generator. This tool uses YACC and Lex to generate visual C ++, Borland C ++, other C/C ++, and related Java code. The following describes how this tool generates code and uses Visual C ++ for compilation.
Note: The use and configuration of Parser Generator have been described in detail in Parser Generator help. Interested friends can go there to find more useful information.

Go to http://www.bumblebeesoftware.com/downloads.htmto download Parser Generator.
After the installation is complete, we can find some examples in its installation directory and the library files and source files required for compiling the program. These files are very important in compilation.

Compile the YACC file separately.
The following is a separate YACC file to implement a simple calculator function.
% {
/*************************************** *********************
Www.linmu100.com
**************************************** ********************/
# Include <ctype. h>
# Include <stdio. h>
# Define yystype double/* Double Type for YACC stack */
%}

% Token number

%
Lines: lines expr '/N' {printf ("% G/N", $2 );}
| Lines '/N'
|/* E */
| Error '/N' {yyerror ("reenter last line:"); yyerrok ();}
;

Expr: expr '+ 'TERM {$ =$ 1 + $3 ;}
| Expr '-'TERM {$ = =$ 1-$3 ;}
| Term
;

Term: term '*' factor {$ = =$ 1*$3 ;}
| Term '/' factor {$ = $1/$3 ;}
| Factor
;

Factor: '('expr')' {$ =$ 2 ;}
| '('Expr error {$ = $2; yyerror ("missing')'"); yyerrok ();}
| '-' Factor {$ =-$2 ;}
| Number
;

%
Int main (void)
{
Return yyparse ();
}

Int yylex (void)
{
Int C;
While (C = getchar () = '');
If (C = '.' | isdigit (c )){
Ungetc (C, stdin );
Scanf ("% lf", & yylval );
Return number;
}
Return C;
}

We use Parser Generator's project --> parser Wizard to create a project, as shown in. Pay attention to the settings in the Red Circle:

Then set the file and template:

The rest can be completed by default:

At this time, we can see that Parser Generator automatically generates a myparser. y file, and the syntax rules can be added here.


Now we completely overwrite the YACC source code shown at the beginning to the myparser. y file, and then compile the file. After successful compilation, three files will be generated: myparser. C, myparser. H, myparser. v
All we need to do is compile myparser. C and myparser. H files with VC.

Open Microsoft Visual C ++ 6.0 and create a new project.

Then, import the two files myparser. C and myparser. h generated by Parser Generator into the project. (I will not talk about it here ^-^)
To set environment variables, first import the library files and source files of Parser Generator. Set these environment variables in tools> options.


After setting these files, add the yl. Lib library in project> Settings:

The yacc_vc.exe file will be generated after the compilation is completed.
Assume that the demo.txt file is in the same directory as yacc_vc.exe. The content of this file is as follows:
1 + 3*5
3*4-23
7-9*30-999
Run the following command: yacc_vc.exe <demo.txt to obtain the calculation result:

Compile a separate Lex file.
The following is a separate Lex file to implement a simple function of calculating the number of words.
% {
/*************************************** *********************
Www.linmu100.com
**************************************** ********************/
Int WC = 0;/* Word Count */
%}

%
[A-Za-Z] + {WC ++ ;}
/N |. {/* gobble up */}

%
Int main (void)
{
Int n = yylex ();
Return N;
}

Int yywrap (void)
{
Printf ("Word Count: % d/N", WC );
Return 1;
}

We use Parser Generator to create a project:

The next step is completed by default. At this time, Parser Generator automatically generates a mylexer. l file, and the lexical rules can be added here.
Similarly, we use the Lex file shown above to completely overwrite mylexer. l file. After compilation, three files are generated, and then myparser is compiled using VC. c, myparser. h.
The configuration parameters of vcss and the unique access file are exactly the same. At the end, A. EXE file can be generated to calculate the number of file single words.

Compile the YACC and Lex integration files.
The following are the content of the YACC file and the lex file, which jointly implement a simple custom syntax rule.
YACC file content:
% {
/*
Www.linmu100.com
*/
# Include <stdio. h>
# Include <string. h>
Void yyerror (const char * Str)
{
Fprintf (stderr, "error: % s/n", STR );
}
Int yywrap ()
{
Return 1;
}
Main ()
{
Yyparse ();
}
Char * heater = "XL's test ";
%}

% Token tokheater tokheat toktarget toktemperature
% Union
{
Int number;
Char * string;
}

% Token <number> state
% Token <number> Number
% Token <string> word
%

Commands:
| Commands command
;
Command:
Heat_switch | target_set | heater_select
;
Heat_switch:
Tokheat state
{
If ($2)
Printf ("/theater '% s' turned on/N", heater );
Else
Printf ("/theat '% s' turned off/N", heater );
}
;
Target_set:
Toktarget toktemperature number
{
Printf ("/theater '% s' temperature set to % d/N", heater, $3 );
}
;
Heater_select:
Tokheater word
{
Printf ("/tselected heater '% s'/N", $2 );
Heater = $2;
}
;
Lex file content:
% {
/*
Www.linmu100.com
*/
# Include <stdio. h>
# Include <string. h>
# Include "myparser. H" // note that the header file here must be the same as the header file name generated by Parser Generator.
Extern char * yytext;
%}
%
[0-9] + {yylval. Number = atoi (yytext); return number ;}
Heater return tokheater;
Heat return tokheat;
On | off {yylval. Number =! Strcmp (yytext, "on"); return state ;}
Target Return toktarget;
Temperature Return toktemperature;
[A-z0-9] + {yylval. String = strdup (yytext); Return word ;}
/N/* ignore end of line */;
[/T] +/* ignore whitespace */;
%

Now use Parser Generator to create a new project:


The ingress content is as follows:
Heat on
Target temperature 99
Heater asdfsieiwef99adsf
Then, execute both_y_l.exe <demo.txt in the command line of this directory to get:

The above is the use of Parser Generator and how to compile it with VC.

/
/* ----- Lex & YACC ----Www.linmu100.com----*/
/
/* ----- Linux tool, Lex & YACC, and Windows operations ----*/
/
/* ----- Linux configuration, UNIX, open-source software, Linux technology, makefile ----*/
/
/* ---------------------- @ Xiaolin --------------------*/
/

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.