Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse an expression

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Overview

There is a saying in the Dharma: "if there is no truth, you can see the Tathagata." The author understands that, in fact, the so-called "reality" is the essence of things, that is to say, we understand the nature of things not only from the appearance or subjective feelings, but through its external performance to achieve its essence, that is, the so-called "Tathagata".

To put it simply, where should we start if we want to use a little bit of compilation technology to develop a script parsing or interpretation engine? Or what is the "reality" of script interpretation technology?

If you want to get started, you should at least have an understanding or basic grasp of the concept and practice of "recursion". So the key to this door is the parsing of expressions, and it is no exaggeration to say that a perfect parsing or interpretation of expressions is half (probably at least half) of the successful development of a similar interpretation script. You can also experience the beauty of using "recursion" in the process of development.

What's the expression? I have asked myself thousands of times, what is the exact definition of an expression? The answer given on Baidu is:

A combination of numbers, operators, numeric grouping symbols (parentheses), free variables, and constrained variables to obtain a meaningful arrangement of values. A constraint variable is assigned a numeric value in the expression, while a free variable can specify a numerical value in addition to the expression.

Personally, I think the above definition can basically express its meaning.

In short, an expression is a combination of operators, constants (and possibly string constants), and variables according to a certain rule, which eventually has a value; so what is this rule? It's mysterious! But in fact, it is not too mysterious, all books on compilation principles will mention the so-called "operator first grammar"-intuitively speaking, the so-called operator first grammar is that operators can appear continuously in an expression, while constants or variables cannot appear continuously. Otherwise, it is not a legitimate "operator first grammar". It's that simple!

Simple expression parsing

Let's start with a simple example:

10-(2-3)

Even primary school students know how the above example is calculated! The answer is 5 instead of 11. Why? Anyone who has learned a little bit of math knows that parentheses take precedence over other operators; what if we use a computer program to deal with it?

OK, let's first simulate a thing called "stack" (anyone with computer common sense knows that the characteristic of stack is "first in, first out" or "last in, first out"). Then we construct a so-called operator priority list (not very strict), which currently has only a few symbols, including "+", "-", "(", ") and" # ", and we assume that" # "has the lowest default priority. Here we have the following table (vertical operator on the left and horizontal operator on the right):

Operator

+

-

(

)

#

+

>

>

>

-

>

>

>

(

-

>

>

#

>

>

>

>

>

>

/

>

>

>

>

>

(

-

>

>

#

>

[

>

>

Arithmetic

>

>

>

>

>

Relationship

>

>

>

Logic

>

Assignment

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report