Никакая таблица символов в Движении?

Насколько Ваше время стоит? Заплатите 40$ и получите RegexBuddy. Я сделал, и я даже обновил от 2.x версия к 3.x. Это заплатило за себя много раз.

14
задан Dinah 12 November 2009 в 22:52
поделиться

3 ответа

Разбор означает просто выяснение структуры программы: разделение модуля на операторы / объявления, разбиение выражений на подвыражения и т. Д. В итоге получается древовидная структура, известная как «синтаксический анализ». tree "или" абстрактное синтаксическое дерево "(AST).

Очевидно, C ++ требует наличия таблицы символов для синтаксического анализа.

На этой странице обсуждаются некоторые причины , почему C ++ требует таблицы символов для анализа .

Конечно, синтаксический анализ - это только часть компиляции, и вам понадобится таблица символов для полной компиляции.

Однако сам синтаксический анализ может быть полезен при написании инструментов анализа (например, какой модуль какие модули импортирует). Таким образом, упрощение процесса синтаксического анализа означает, что легче писать инструменты анализа кода.

22
ответ дан 1 December 2019 в 06:43
поделиться

Для интерпретации и компиляции абсолютно необходимы таблицы символов или аналогичные. Это верно почти для всех языков.

В C и C ++ даже синтаксический анализ язык требует таблицы символов.

10
ответ дан 1 December 2019 в 06:43
поделиться

@Justice is right. To expand on that a little, in C the only actual tricky part is telling types apart from variables. Specifically when you see this:

T t;

You need to know that T is a type for that to be a legal parse. That's something you have to look up in a symbol table. This is relatively simple to figure out as long as types are added to the symbol table as the parse continues. You don't need to do much extra work in the compiler: either T is present in the table or it isn't.

In C++ things are much, much more complicated. There are enormous numbers of ambiguous or potentially ambiguous constructs. The most obvious is this one:

B::C (c);

Aside from the fact that it's not clear if B is a class, a typedef, or a namespace, it's also not clear if C is a type and c an object of that type, or if C is a function (or constructor) taking c as an argument (or even if C is an object with operator() overloaded). You need the symbol table to carry on parsing, although it is still possible to continue quickly enough, as the type of the symbol is in the symbol table.

Things get much, much, much worse than that when templates come into the mix. If C (c) is in a template, you might not know in the actual definition of the template, if C is a type or a function/object. That's because the template can declare C to be either a type or a variable. What this means is that you need the symbol table, but you don't have one -- and you can't have one until the template is actually declared. Even worse, it's not necessarily sufficient to have just the type of the symbol: you can come up with situations which require the full information of the type the symbol represents, including size, alignment, and other machine-specific information.

All this has several practical effects. The two most significant I would say are:

  • Compilation is much faster. I assume Go is faster to compile than C, and C++ has famously slow compilation times for situations involving a lot of templates.
  • You can write parsers that don't depend on having a full compiler. This is very useful for doing code analysis and for refactoring.
9
ответ дан 1 December 2019 в 06:43
поделиться
Другие вопросы по тегам:

Похожие вопросы: