Does C regular expression support lookahead? - c

I am trying to use regular expression in c/c++ using regex.h.
I am trying to use lookahead options, for example:
(?=>#).*
in order to extract strings after a '#'
For some reason it fails to find any matches.
Does regex.h supports lookahead/lookbehind? is there another library I can use?
I am using regex.h, on linux.

I'm pretty sure NSRegularExpression is just a wrapper for libicu, which does support lookaheads. You have a typo in your example, right syntax is (?=#).* according to the link.
It doesn't really seem needed in this case though, why not just #.*?

I suspect it's really lookbehind you're talking about, not lookahead. That would be (?<=#).*, but why make it so complicated? Why not just use #(.*), as some of the other responders suggested, and pull the desired text out of the capturing group?
Also, are you really using NSRegularExpression? That seems unlikely, considering it's an Objective-C class in Apple's iOS/MacOS developer framework.

Related

In vim, how to remove all the C and C++ comments?

How to remove all C and C++ comments in vi?
//
/*
*/
You can't. Parsing C and C++ comments is not something that regular expressions can do. It might work for simple cases, but you never know if the result leaves you with a corrupted source file. E.g. what happens to this:
printf ("//\n");
The proper way is to use an external tool able to parse C. For example some compilers may have an option to strip comments.
Writing a comment stripper is also a basic exercise in lex and yacc programming.
See also this question: Remove comments from C/C++ code
With regular expressions, because of the complexity of C/C++ syntax, you will at best achieve a solution that is 90% correct. Better let the right tool (a compiler) do the job.
Fortunately, Vim integrates nicely with external tools. Based on this answer, you can do:
:%! gcc -fpreprocessed -dD -E "%" 2>/dev/null
Caveats:
requires gcc
it also slightly modifies the formatting (shrunk indent etc.)
Esc:%s/\/\///
Esc:%s/\/\*//
Esc:%s/\*\///
You can use a lexical analyzer like Flex directly applied to source codes. In its manual you can find "How can I match C-style comments?".
If you need an in-depth tutorial, you can find it here; under "Lexical Analysis" section you can find a pdf that introduce you to the tool and an archive with some practical examples, including "c99-comment-eater".

What tools/IDE/languages exists for generating C-code

I wanted to know more about tools that assist in writing code-generators for C. Essentially how do we achieve functionality similar to c++ templates.
Even though it's not a perfect solution and it takes some time to master it, I've used the m4 macro processor in the past for generic C code generation (kinda like C++ templates). You may want to check that out.
You are looking for a way to generate code for very similar classes, where what differs is essentially their type.
You can use a template-based code generator, where "template" means "boilerplate code" with string substitution. This is the simplest scenario. A tool like StringTemplate or CodeSmith will do the job. But there are many others. Just search around.
If you want a more serious generation scenario, where different class structures might be needed according to a set of definitions, then you should go with a fully programmable generator like AtomWeaver. There are others (MPS, Xtext) but these do not rely on templates.

Detecting/Listing variable declarations in C

I would like to list all the variables that have been declared in my C program for analysis. Is there an easy way I can do this? I would think that building a lexer just for this purpose would be cumbersome. Is there another way?
Well, I think I have to be more clear :-). I intend to analyse a lot of C files using a C library that I intend to write, which needs to have this functionality. Hence, it'd be great if I can do this using C (since it can integrate with my library). However I can pre-process in any other language as well. But it'd increase dependencies.
You're probably going to have to write a pretty powerful parser anyway, if you want to handle typedefs and so on. You might want to look at using clang/llvm - you can probably modify it to output the data you want pretty easily.
cscope (http://cscope.sourceforge.net/) can identify and index all symbols in your program and has a command line mode to query the symbol database from command line or GUI tools.
Doing the job properly requires a significant chunk of the C preprocessor and a lexical analyzer, which is quite a lot of a C compiler.
Doing the job ad hoc is easier - but you get to choose how ad hoc you're going to be.

What is the best way to achieve sscanf-like functionality in Perl?

What is the best way to achieve sscanf-like functionality in Perl?
I am looking at the sscanf module.
Which is better:
Going sscanf way?
Regex way? (I am a beginner when it comes to Regex.)
The Perl documentation includes this tidbit:
scanf
scanf() is C-specific, use <> and regular expressions instead, see perlre.
I would say that learning regexes is well worth it; they are part of the core strength of Perl and are a useful tool elsewhere too.
CPAN has a String::Sscanf module for Perl!
Regex is better, but TIMTOWTDI.
There is more than one way to do itTM.
However, regular expressions are quite more versatile than sscanf. They are also not difficult to learn.
In Perl, an attempt to mimic the functionality of sscanf would most likely make heavy use of regular expressions.
You can use the regular expression.
If you want to use the sscanf module, you need to download that module. So it will occupies the space.
Instead of this, you use the regular expression only.
According to me regular expression is the better way

How to get rid of GVim folding comments in your code?

There is someone in my team that swears by using some kind of GVim feature to do manually code folding.
As I'm using another editor and do not really need the folding feature, I think it only pollutes the source code with tags like:
/* {{{1 */
Convincing the person not to use this folding is not an option (got into some heated discussions before).
I'm not really a GVim guy, I'm wondering if there are not any other ways to do the folding without changing the team's code?
Maybe putting the folding directions in a separate file, or
Doing some kind of smart folding that takes the syntax of the programming language into account without changing the source code?
I would imagine he could just add the following to his .vimrc:
set foldmethod=syntax
Assuming he is using a version of VIM that supports that. :)
It only took a Google for "vim folding" to discover that Vim supports six fold methods.
syntax, indent and diff all mean that the user has little control over where the folding happens. That may or may not be a problem.
marker is a problem for you because you don't like the markers.
expr had a little of each of those problems, depending on the expression you create.
If your man wants to define his own fold points, and store them in an separate file, it seems like set foldmethod=manual combined with :mkview and :loadview would do the trick.
You should try it yourself. Once you start using foldmethod=marker there is just no going back. I am working on a project now where I can't use marker folding and it feels like washing clothes in a time before there were washing machines.

Resources