Small white says compiling principle -6-lex and YACC Environment configuration-Multi graph

Source: Internet
Author: User
Tags eol lexer

The use of Lex and yacc is simple, but the environment configuration is a variety of problems, this chapter explains the environment configuration of Lex and YACC under Windows.

Software Requirements:
System win7-64 bit (win7-32, Win8, win10 All)
C + + compiler: VS2010 (2008,2013,2015 also all through)
Lex and Yacc compiler: ParGen.exe

Basic Flow:
Install Pargen.exe, using the default directory installed in C:\Program Files (x86) \parser Generator 2\
Install VS2010, this various tutorials, no longer repeat

Start the Pargen program and select Project->parserwizard ...

Select Parserwizard, start the project creation, here I create the function, named test, directory location can choose their own, the target language is C + +, compiler for VC + + (32-bit)

Next, choose whether to create a lex, a yacc, or both. Here I choose Lex and YACC, ready to implement a calculator that does not support variables, use Lex to identify tokens, and use YACC to recognize the syntax.

Next, set the file name of YACC and the parser used here, the default option I use here, without modification, the file name defaults to MYPARSER.Y

Next, set the file name of Lex and the name of the parser used, the default option I use here, the file name defaults to MYLEXER.L

Click the Finish button to create the project complete with two files Mylexer.l and myparser.y

Project management, you can use the Window->project menu to view all the files under the project

When you click the Zoom button in the File editing window, the other file edits will be overwritten, you can use the Window->project menu to view it, or you can use window->tile vertically to view the tile of all files.

Edit the Mylexer.l file and paste in the following:

%{//this code would be added into the header of generated. cpp file#include <iostream>#include "myparser.h"using namespace STD;//already defined in yacc.y, use%token ...//enum{lt, EQ, GT, IF, ELSE, ID, number, PLUS, minus, times, over, INT, Double,char, LP,RP};Const Char* tokenstr[] = {"LT","EQ","GT","IF","ELSE","ID","Number","PLUS","Minus","Times","Over","INT","DOUBLE","CHAR"};Static voidPrint_token (intTokenChar* Lex);%}%name Mylexerdelim [\t]ws {delim}+letter [a-za-z]digit] [0-9]id {Letter} ({letter}|{ digit}) */ * can support 12.34 * /Number {digit}+ (\.{ digit}+)?%%%{//this code would be added into Yyaction functionYystype yyfar& yylval = * (Yystype yyfar*) yyparserptr->yylvalptr;//double Yylval;%}{WS} {/* Do nothing * /}"int"{Print_token (INT, Yytext);returnINT;}"Double"{Print_token (DOUBLE, yytext);}"Char"{Print_token (CHAR, yytext);}"+"{Print_token (PLUS, Yytext);returnPLUS;}"-"{Print_token (minus, yytext);returnMinus;}"*"{Print_token (times, Yytext);returnTimes;}"/"{Print_token (over, yytext);returnOver;}"("{returnLP;}")"{returnRP;}"\ n"{returnEOL;} {ID} {returnID;}    {Number} {yylval = Atof (Yytext);returnnumber;}"//".*      {returnCOMMENT;}"."{printf("Mystery character%s\n", Yytext); }%%Static voidPrint_token (intTokenChar* Lex) {#ifdef Lex_deub    cout<<"token:"<< token<<" "<<"Lex:"<<lex<<endl;#endif}

Compile the Myparser.y file, paste in the following content

%{#include"Mylexer.h"%}%nameMyparser//class definition{//Place any extra class}//Constructor{extra initialisation Code here}//destructor{Extra Cleanup Code here}// Place any declarations here%include{#ifndef yystype#define Yystype double#endif}%tokenNumber ID%tokenPLUS minus times over%tokenLP RP EOL COMMENT%TOKENINT DOUBLE CHAR%leftPLUS minus%leftTimes over%rightUminus%%Lines:lines expr EOL {printf ("%g\n", $);   }        |  Lines EOL |        Lines COMMENT | ; expr:expr PLUS Expr {$$= $+ $; }|expr minus expr{$$= $- $; }|expr Times expr{$$= $* $; }|expr over expr{$$= $/ $; }| LP Expr rp{$$= $; }|'-'Expr%precuminus{$$= - $; }| Number {$$= $;}//$$=$1 can ignored| Id//should be complemented;%%intMainintARGC, Char*ARGV[]) {printf ("a cacluator which support +,-, *,/and (): \ n"); printf"e.g. 12.2+3* (2+5) \ n ");intn =1;    Mylexer lexer; Myparser parser;if(Parser.yycreate (&lexer)) {if(Lexer.yycreate (&parser)) {//lexer.yyin = new Ifstream (argv[1]);            //lexer.yyout = new Ofstream (argv[2]);n = parser.yyparse ();//parse_tree.get_label ();            //parse_tree.gen_code (*lexer.yyout);}} getchar ();returnn;}

Clicking the Compile Build button in the upper-right corner of the Pargen generates the corresponding. h and. CPP Code

Create a new VS2010 project and add the generated. h and. CPP code to the project. For simplicity, create a vs project in the Comple\test directory Vstest

Select the console project, the project catalog, and the project name Vstest, after clicking the OK button

Continue to the next configuration

The attachment option here selects the empty item and then clicks the Finish button, which completes the Vstest project creation

Project Right-add an existing item, that is, add the. h and. cpp files that have been generated


In the VS interface, click the Compile button to see the current compilation, the compilation error will be displayed, the YY header file cannot be found because the Pargen installed header file is not added to the project's include directory

The following Pargen installed header files into the project include configuration


In the VS interface, click the Compile button to view the compilation. The current header file can be found normally and there will be a lot of link error-link errors. This is because the corresponding LIB file has not yet been loaded in.

Add the following library files to the directory where the library is located

Add the name of the library you want to use: ylmtri.lib, note using semicolons to separate

Click the Compile button again, you will find that you can compile, but when running, there will be ylmtri.dll errors. This is because we use the dynamic DLL library and need to copy the corresponding DLL files from the Pargen directory to the exe file sibling directory of the project directory

The following begins copying Ylmtri.dll from the Pargen directory to the VsTest.exe sibling directory

Click the Compile and Run button again, you can run normally, the effect is as follows

It's finally over.

I lpstudy reprint please indicate the source: http://blog.csdn.net/lpstudy/article/details/51330063

Small white says compiling principle -6-lex and YACC Environment configuration-Multi graph

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.