English 中文(简体)
执行运营商在东南非共市的优先地位的一般方式是什么。
原标题:What is the general way to implement operators precedence in Python

附录一

执行类似事情的一般方式是什么?

最佳回答

我不敢肯定你对你们的兴趣有多大细节,但正如你希望执行教区一样。 通常有两个步骤:

The lexer reads over the text and converts it to tokens. For example, it might read "2 + 3 * 2" and convert it to INTEGER PLUS INTEGER STAR INTEGER

The parserreads in the tokens and tries to line with them. 例如,你可能会有这些规则:

Expr := Sum | Product | INTEGER;
Sum := Expr PLUS Expr;
Product := Expr STAR Expr;

它读作标语,试图适用规则,使起首规则在读物时标明。 在此情况下,可以:

Expr := Sum
Expr := Expr PLUS Expr
Expr := INTEGER(2) PLUS Expr
Expr := INTEGER(2) PLUS Product
Expr := INTEGER(2) PLUS Expr STAR Expr
Expr := INTEGER(2) PLUS Integer(3) STAR Expr
Expr := INTEGER(2) PLUS Integer(3) STAR Integer(2)

There are many types of parsers. In this example I read from left to right, and started from the initial expression, working down until I d replaced everything with a token, so this would be an LL parser. As it does this replacement, it can generate an abstract syntax tree that represents the data. The tree for this might look something like:

Screenshot of the AST

您可以看到,产品规则是《概述》规则中的一项儿童,因此最终将首先:2 + (3 2)。 如果表达方式不同,我们就可能会用这一树来结束:

Screenshot of the AST

我们现在重新计算<代码>(2+ 3) * 2。 树冠以什么方式 generate树。


If you actually want to parse expressions, odds are you don t want to write the parser by hand. There are parser generators that take a configuration (called a grammar) similar to the one I used above, and generate the actual parser code. Parser generators will let you specify which rule should take priority, so for example:

Expr := Sum | Product | INTEGER;
Sum := Expr PLUS Expr; [2]
Product := Expr STAR Expr; [1]

我将产品规则列为优先事项1,而苏姆则列为优先事项2,因此,鉴于所生成的产品名称将有利于产品。 你们还可以设计图表本身,以便确定优先事项(这是比较常见的做法)。 例如:

Expr := Sum | INTEGER;
Sum := Expr PLUS Product;
Product := Term STAR INTEGER;

This forces the Products to be under the Sums in the AST. Naturally this grammar is very limited (for example, it wouldn t match 2 * 3 + 2), but a comprehensive grammar can be written that still embeds an order of operations automatically

问题回答

You would need to write a parser for your fairly simple programming language. If you want to do this in Python, start by reading Ned Batchelder s blog post Python Parsing Tools.





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签