Use the bison and flex tools [zz]

Source: Internet
Author: User

Here is an example of creating a simple calculator using bison:
Http://www.cs.berkeley.edu /~ Maratb/cs164/bison.html

Using the bison and flex tools to learn how to compile is far more efficient than simply reading a book and writing it yourself.ProgramMore vivid. In this way, you will not waste effort on processing complex characters, regular expressions, and finally do your best, but have no results and lose your interest in learning.

Me
Here is a simple calculator program that can be used to add, subtract, multiply, and divide operations, and support the processing of parentheses and 26 letters as variables. I used to write such a program using a suffix expression.
A single infix expression is changed to a suffix expression that contains hundreds of rows.CodeAnyway, I still don't know how to deal with the complicated stacks in it (I used the STL List Implementation ).

Lexical processing file Calc. Lex The content is as follows:
% {
/*
* A simple calculator Lex lexical File
*/
# Include <stdlib. h>

Void yyerror (char *);
# Include "Calc. Tab. H"
%}
%
/* A-Z is the variable */
[A-Z] {
Yylval = * yytext-'A ';
Return variable;
}
/* Integer */
[0-9] + {
Yylval = atoi (yytext );
Return integer;
}
/* Operator */
[-+ () = \ * \ N] {return * yytext ;}
/* Blank space ignored */
[/T];
/* Other characters are invalid */
. Yyerror ("invalid input character ");
%
Int yywrap (void)
{
Return 1;
}
The goal of lexical processing is to identify what each member is. There are two types of integers and variable. The task of lexical analysis of each component is complete.

Syntax processing file Calc. Y The content is as follows:
% Token integer variable
% Left '+ ''-'
% Left '*''/'
% {
# Include <stdio. h>
Void yyerror (char *);
Int yylex (void );
Int sym [26];
%}
%
Program:
Program Statement '\ N'
|
;
Statement:
Expr {printf ("% d \ n", $1 );}
| Variable '= 'expr {sym [$1] = $3 ;}
;
Expr:
Integer
| Variable {$ = sym [$1];}
| Expr '+ 'expr {$ = $1 + $3 ;}
| Expr '-'expr {$ =$ 1-$3 ;}
| Expr '*' expr {$ = =$ 1*$3 ;}
| Expr '/'expr {$ =$ 1/$3 ;}
| '('Expr')' {$ $ = $2 ;}
;
%
Void yyerror (char * s)
{
Fprintf (stderr, "% s \ n", S );
}
Int main (void)
{
Printf ("A simple calculator. \ n ");
Yyparse ();
Return 0;
}
The syntax analysis file is written by describing the BNF expression. The rules become understandable as the entries are refined. You don't have to worry about how to implement the analysis of these syntaxes, but you just need to tell how to build these syntaxes.

The compilation command is as follows:
> Bison-D Calc. Y
> Flex Calc. Lex
> GCC Calc. Tab. c Lex. yy. C-o calc

 

------------------------------------------------

Yyparse is a function generated by bison for parsing syntax. Meanwhile, yylex is a function generated by flex for parsing lexical information. The function generated by bison calls yylex to continuously process and process suitable expressions, after calculation.

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.