Syntax parsing and evaluating expressions

Lecture



How to write a program that will receive at the input a line containing a numeric expression, for example (10 - 5) * 3, and produce the corresponding result? If there are "high priests" among programmers, then these are those who know how to solve a similar problem. Many, moreover, highly skilled in other areas, programmers have no idea how translators, designed to compile programs written in high-level languages, convert algebraic expressions into computer-executed commands. This procedure is called expression parsing (expression parsing) and is the basis of all compilers and interpreters of languages, spreadsheets, and all other programs in which you want to turn numeric expressions into a form understandable to a computer.

Despite its mysteriousness, parsing expressions is a fairly straightforward process and in many aspects is simpler than some other programming tasks. This is due to the fact that the task of parsing is clearly defined and solved in accordance with strict rules of algebra. In this chapter, a recursive descending parser , or a recursive-descent parser, as well as all the functions necessary for evaluating expressions will be developed. Having mastered the principle of this program, you can easily modify and modify it in accordance with their tasks.

On a note The C language interpreter presented in Part VI of this book uses an improved version of the parser developed here. If you study the C interpreter, the material in this chapter will be especially useful for you.
  • Expressions
  • Expression into lexemes
  • Parsing expressions
  • Simple expression parser
  • Work with variables in the analyzer
  • Syntax check in recursive downstream analyzer

Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Structures and data processing algorithms.

Terms: Structures and data processing algorithms.