W
W
Worgen2019-04-30 12:41:22
Algorithms
Worgen, 2019-04-30 12:41:22

How to calculate values ​​from AST tree?

Good afternoon, you need to make a calculator that would count expressions of the following types:
(2+5*(2+1))*2, 8*(2+1/(2+5)) and so on.
Must be implemented using an AST tree.
I have already made a parser for this, it outputs the following:
5cc8180fecc33601859615.png
All this is stored in my list in my created class:

class Node
    {
        public string type;
        public string value;

        public Node() { }
        public Node(string t, string v) { type = t; value = v; }
    }

//список который выводит после парсинга
List<Node> parse = new List<Node>();

Tell me, please, how can I calculate all this right now? I don't quite understand how to implement it.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Denis Zagaevsky, 2019-04-30
@zagayevskiy

As already mentioned, this is not an AST, but a list of tokens (I must say, inconvenient tokens). You can parse this in AST with the simplest recursive descent algorithm, for example. Then calculate.

R
Ruslan., 2019-04-30
@LaRN

Look at this article:
https://habr.com/en/post/281495/
There are many interesting links in the comments, for example this one:
journal.stuffwithstuff.com/2011/03/19/pratt-parser...

N
Nikolai Gromov, 2017-04-03
@T_verdisla_V

A solution was found, perhaps not quite optimal, but still a solution.
The following changes had to be made to ProductsManager.php:

public function addNewProduct($data) 
    {
        // Создаем новую сущность Post.
        $product = new Products();
        
        $product->setName($data['name']);
        $product->setPrice($data['price']);
        $product->setDescription($data['description']);
        $product->setStatus($data['status']);
        
        // Добавляем сущность в менеджер сущностей.
        $this->entityManager->persist($product);
        
        // Добавляем теги к посту.
        $prod = $this->addCategoryToProduct($data['category_id']);
                $product->setCategory($prod);
        // Применяем изменения к базе данных.
        $this->entityManager->flush();
    }    
    
    // Добавляет/обновляет теги в заданном посте.
    private function addCategoryToProduct($prodCat) 
    {   
        // Добавляем категории к продуктам
            $category = $this->entityManager->getRepository(Category::class)
                      ->find($prodCat);
            if ($category == null){
                $category = new Category();
            }  
            return $category;
    }

Those. add another method that would return an instance of the Category class. For more complex business logic, it will be necessary to make additions (methods) to the Entity itself.
Maybe someone will need my answer.

G
Gregory, 2017-03-31
@difiso

setCategory()You passed a string to the function as a parameter. And you need an object of the class Shop\Entity\Categoryor null.
First, get the category object, and then assign it to the product.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question