Answer the question
In order to leave comments, you need to log in
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:
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>();
Answer the question
In order to leave comments, you need to log in
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.
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...
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;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question