[cfe-dev] What's the best way to...?

Sebastian Redl sebastian.redl at getdesigned.at
Thu Jan 29 10:04:20 PST 2009


Alexei Svitkine wrote:
> Hi,
>
> I'm looking to determine if some given code fragment is "complete" -
> and by that I mean that, for example, it can be completely enclosed by
> an AST node if parsed in-context.
>
> For example,
>
> printf("%d\n",
>
> is not complete by my definition.
>
> But "printf("%d\n", i);" is. Similarly, "if (blah) {" is not. But "if
> (blah) { doStuff(); }" is. Or likewise "int fib(int n)" is not
> complete - but with a semicolon at the end, its a function
> declaration, or with a function body following, it's a function
> definition - and thus complete.
>
> I'm looking at what's the best way to do such an analysis using the
> clang libraries. I have some ideas, but I thought I'd ask the list to
> make sure I'm not missing some better way...
>   
Very wild idea, and probably not practicable: put a layer between lexer
and parser. Usually it just forwards tokens, but when the lexer runs out
of tokens, you do a stack trace to find out what part of the parser
called you. Then you know what the parser would now expect.

Of course, that probably requires careful analysis of the parser and
some huge state tables, not to mention that stack tracing as part of the
normal program flow is simply impracticable.

Sebastian



More information about the cfe-dev mailing list