Opencascade Expression Interpreter by Flex & Bison

Source: Internet
Author: User

Opencascade Expression Interpreter by Flex & Bison

[Email protected]

Abstract. Opencascade provide data structure of any expression, relation or function used in mathematics. Flex and Bison is tools for building programs that handle structured input. They were originally tools for building compilers, but they has proven to being useful in many other areas. The Expression interpreter in Opencascade are made by Flex and Bison. So let's know something about Flex and Bison.

Key Words. Opencascade, Expression interpreter, Flex, Bison

1.Introduction

Opencascade's Tkmath provides a mathematical expression for solving the package expr and EXPRINTRP, which is used to parse the mathematical expression or function. The so-called mathematical expression such as: sin (10) +20*6-6/3 and so on. Previously in the "Data structure" book to see a stack of ways to evaluate the simple expression, it feels very good. But if the expression contains trigonometric, logarithmic, exponential, and so on, the program will be complicated. How to solve this problem in a simple and elegant way was also very vacant.

Encountered in the work of the predecessor himself wrote a language for defining geometry, you can also evaluate expressions. Although not to see the source code, it has been admired unceasingly. Later, I contacted the scripting language, like Tcl/tk,python, and found that using these open source libraries could be a simple, stable custom development language. Because this type of script can not only evaluate an expression, it can also evaluate an expression with parameters, such as:

34 sqrt ($x * $x + $y * $y

As long as you write a program in the UNIX environment, you will encounter the mysterious Lex&yacc, like the Gun/linux user familiar Flex&bison, where Flex is a lex implemented by Vern Paxon, Bison is the GNU version of YACC. Students who have learned the principles of compiling should not be unfamiliar with the two artifacts. Using these two tools, not only can you implement an expression parsing calculator, but you can also use it to parse SQL syntax, such as PostgreSQL, which uses them to implement SQL parsing. You can even implement a compiler.

Because it is a tool on UNIX, you need to use Winflexbison on Windows, you can download it from SourceForge: https://sourceforge.net/projects/winflexbison/

This article describes how to use flex to understand the implementation of EXPRINTERP in Opencascade and broaden your horizons. Usage of exprinterp in opencascade see: Evaluate Math Expression

Http://www.cppblog.com/eryar/archive/2013/10/09/203625.html

2.Flex Example

Flex is an automated tool that automatically generates a C function, Yylex (), as well as a scanner (Scanner), according to defined rules. The C function takes the text string as input, parses the characters in the text string according to the defined rules, finds some sequence of characters that match the rule, and executes the action (action) defined in the rule. For example, you can define this in a rule: if you encounter a newline character \ n, then add one to the value of the row counter.

A flex file is a text file that includes a defined set of lexical rules. The file's naming convention is the lowercase L (l) as the file suffix. If you want to be clear, you can also use. FLX or. Flex as the suffix name for the file. When the flex file is complete, execute the following command:

$ Flex Example.flex

After this command executes, a C file is generated and the default file name is LEX.YY.C. The main content of this C file is the definition of function Yylex ().

If you want to compile this file directly into an executable program, there are some areas to be aware of. If the definition of the main () function is not provided in the flex file, there will be no main () function in the C file. When compiling the C file separately, be sure to add the-LFL connection library parameter, if the main () function is provided, you do not have to provide this connection library parameter. The connection library LIBFL provides a default main function. The default main () function simply calls the Yyflex () function, and the main () function that you provide can add as many other processing code as you want.

The lexical specification definition file gives the word formation rules. Lexical files are traditionally used as suffixes with the letter L (lowercase l). The flex file consists of three parts. Or say three paragraphs. Three segments are separated by two percent of each.

Defining segments (Definitions)

%%

Rule segment (rules)

%%

User code Snippet

Here's a simple flex program, the code looks like this:

/*Hello World for Flex.*/%option Noyywrap%{intChar_count =0;intLine_Count =0;%}%%\ n {++char_count; ++Line_Count;}. { ++Char_count;}%%intMainintargcChar*argv[])        {Yylex (); printf ("# of lines =%d, # of chars =%d\n", Line_Count, Char_count); return 0;}

Save the above file as Hello.lex, and then run the command as shown:

Generates a LEX.YY.C source program, compiles the C source file, and links to generate an executable program. Run the program as shown in:

3.Flex and Bison in OCC

There is also an associated syntax rule definition in the Opencascade folder, as shown in:

It is known that the implementation of Opencascade's expression parsing function also leverages flex and bison.

4.Conclusion

With two powerful tools, flex and bison, you can implement an advanced calculator, which is any mathematical expression evaluator.

Opencascade's EXPRINTERP uses flex and bison to implement analytic computations of mathematical expressions. When understanding the use of tools, interested readers may wish to combine the theory of "compiling principles" and other theoretical knowledge, the principle of the tool to explore.

5.References

1. Min, 聯繫. Data structure (C language version). Tsinghua University Press. 1997

2. Zhao Jianhua, Zheng, Dai Nou translation. The principle of compiling. Mechanical industry press. 2011

3. John Levine, Flex & Bison. O ' REILLY. 2009

PDF version:opencascade Expression Interpreter by Flex & Bison

Opencascade Expression Interpreter by Flex & Bison

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.