Идентификация типов всех переменных в проекте C

Проблема связана с this.value - кнопка не имеет .value - ваш HTML подразумевает, что вы хотите проверить .text() кнопки (или использовать this.innerText для большей эффективности): [118 ]

$("#clickme").click(function(e) {
  var curr_qty = $('#qty').val();
  $('.qtyButton').filter(function() {
    return $(this).text() == curr_qty
  }).css('color', 'red');  // changed to red as blue wasn't clear enough
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type='text' id='qty' value="1" />
<button class="qtyButton">1</button>
<button class="qtyButton">2</button>
<button class="qtyButton">3</button>
<hr/>
<button id='clickme'>click me</button>

7
задан S.Lott 22 April 2009 в 20:01
поделиться

5 ответов

There are a number of Python parser packages that can be used to describe a syntax and then it will generate Python code to parse that syntax.

Ned Batchelder wrote a very nice summary

Of those, Ply was used in a project called pycparser that parses C source code. I would recommend starting with this.

Some of those other parser projects might also have sample C parsers.

Edit: just noticed that pycparser even has a sample Python script to just parse C type declarations like the old cdecl program.

5
ответ дан 6 December 2019 в 21:19
поделиться

How about approaching it from the other side completely. You already have a parser that fully understands all of the nuances of the C type system: the compiler itself. So, compile the project with full debug support, and go spelunking in the debug data.

For a system based on formats supported by binutils, most of the detail you need can be learned with the BFD library.

Microsoft's debug formats are (somewhat) supported by libraries and documents at MSDN, but my Google-fu is weak today and I'm not putting my hands on the articles I know exist to link here.

The Keil 8051 compiler (I haven't used their ARM compiler here) uses Intel OMF or OMF2 format, and documents that the debug symbols are for their debugger or "any Intel-compatible emulators". Specs for OMF as used by Keil C51 are available from Keil, so I would imagine that similar specs are available for their other compilers too.

A quick scan of Keil's web site seems to indicate that they abandoned their proprietary ARM compiler in favor of licensing ARM's RealView Compiler, which appears to use ELF objects with DWARF format debug info. Dwarf should be supported by BFD, and should give you everything you need to know to verify that the types and names match.

3
ответ дан 6 December 2019 в 21:19
поделиться

Check out ANTLR. It's a parser generator, with bindings for python. The ANTLR site provides a whole bunch of grammars for common languages, C included. You could download the grammar for C and add actions in appropriate places to collect the information you're interested in. There's even a neat graphical tool for creating and debugging the grammars. (I know that seems hokey, but it's actually quite handy and not obnoxious)

I just did something sort of similar, except to get my symbol information I'm actually extracting it from GDB.

2
ответ дан 6 December 2019 в 21:19
поделиться

То, что вы пытаетесь сделать, - это облегченная форма статического анализа. Возможно, вам повезет, если вы посмотрите на инструменты, на которые ссылается Википедия .

Анализ кода C сам по себе звучит для меня как неправильное направление: в этом и заключается безумие. Если вы настаиваете, то [f] lex и yacc (bison) являются инструментами, которые, вероятно, используются вашими авторами компиляторов.

Или, если ctags или cscope дадут вам 80% пути, исходный код для обоих широко доступен , Последние 20% - это просто вопрос программирования. :)

2
ответ дан 6 December 2019 в 21:19
поделиться

I did something similar for a project I was working on a few years ago. I ended up writing the first half of a C compiler. Don't be alarmed by that prospect. It is actually much easier than it sounds, especially if you are only looking for certain tokens (variable definitions, in this case).

Look for documentation online about how to scan C source code, detect tokens of interest, and parse the results. A good place to start is Wikipedia's artricle on lexical analysis.

0
ответ дан 6 December 2019 в 21:19
поделиться
Другие вопросы по тегам:

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