Integrate vc with yacc and Lex

Source: Internet
Author: User
Here, we will introduce the YACC and Lex integrated environment parser in a Windows environment produced by bumble-bee software.
Generator. Parser Generator is the implementation of YACC and Lex in windows. It includes a graphical user interface and YACC and Lex versions, called ayacc and Alex respectively. Parser
Generator is very suitable for integration with VC ++. After Parser Generator is installed, perform the following steps to enable VC ++ to compile and connect parser
Generator files. 1. perform the following steps in the directory settings in VC ++. Each step is executed only once. (1) Select the Options command in the Tools menu. The Options dialog box appears on the screen. (2) Select the directories tab. (3) Select include files from the show directories for drop-down list box. (4) In the directories box, click the empty directory at the end and enter the path of the include subdirectory of Parser Generator. (5) Select library files from the show directories for drop-down list box. (6) In the directories box, click the empty directory at the end and fill in the path of the LIB/msdev subdirectory of Parser Generator. (7) Select source files from the show directories for drop-down list box. (8) In the directories box, click the empty directory at the end and fill in the path of the source subdirectory of Parser Generator. (9) Click OK. The Options dialog box will accept the settings and close them. VC ++ can find the library files including YACC. H, Lex. H, YACC, and Lex. 2. For each VC ++ project, you must perform the following steps in VC ++: (1) Select the settings command in the project menu, and the project appears on the screen.
Settings dialog box. (2) Select Win32 debug from the settings for drop-down list box. (3) Select the C/C ++ label. (4) select general from the drop-down list box of category. (5) In the Preprocessor definitions box, enter yydebug at the end of the current text. (6) Select the link tag. (7) select general from the category drop-down list box. (8) In the object/library modules box, after the current text, enter YLD. Lib
Ylmtd. Lib. (9) Select Win32 release from the settings for drop-down list box. (10) Repeat Step 1. (11) Click OK. The Project Settings dialog box will accept and close the settings. VC ++ can now accept the functions and variables required by YACC and Lex from a specific library. 3. Application (1) in the editing window of Parser Generator, enter the YACC source program (the extension must be. Y ). (2) Use the compile FILE command of the Project menu under Parser Generator to compile the source program and generate the corresponding C language source program (. cpp ). (3)compile with vcpus, connect to the C language source program, and generate the executable program (.exe) before execution. 4. Example: this is an example provided by Parser Generator {
/*************************************** *********************
Calc. Y
Simple calculator. Features floating point arithmetic using
The addition, subtraction, multiplication and divide
Operators, and unary minus. expressions can be grouped
Using parentheses, and simple error recovery is supported.
**************************************** ********************/

# Include <ctype. h>
# Include <stdio. h>
# Define yystype double/* Double Type for YACC stack */
%}

% Token number

%
Lines: lines expr'
'{Printf ("% G
", $2 );}
| Lines'
'
|/* E */
| Error'
'{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;
}
The following are some of my computer tests. I hope to help you configure the machine: References: [1] LV yingzhi, Zhang Suqin, Jiang weidu, compilation principles. beijing: Tsinghua University Press, 1998.1 [2] IBM a translation tutorial "YACC and Lex Quick Start" http://www.ibm.com/developerworks/cn/linux/sdk/lex/index.html English resources:

  • Lex and YACC, Levine, Mason and Branson, O Reilly and their partners, 2nd ed.
  • Program Development in UNIX, J. T. Shen, Prentice-Hall India.
  • Compilers: Principles, techniques and tools, ahoo, Sethi and Ullman, Addison-Wesley pub. co., 1985, 11.
  • Lex and YACC and compiler writing guide.
  • Java edition Lex guidance, called
    Jlex.

  • Use the formalizing a grammar instance of lex and YACC.
  • 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.