Do we have any pattern matching string search ?/ - c

I have a following string format.
({'BTAddress': <'00:22:58:07:76:9B'>, 'Name': <'Some-Name'>, 'Alias': <'some-Name'>, 'Class': <uint32 12316>, 'Adapter': <objectpath '/org/bluez/3196/hci0'>},)
I would like to parse the above string in C. Instead of doing raw parsing, any advice to use pattern wise parsing ?
Thanks for the help.

You can use strtok : http://www.cplusplus.com/reference/clibrary/cstring/strtok/.
You'll have to do some hand work to make it work, but it works fine.
Note: the char pointer that you pass will be manipulated!!

Any POSIX-compliant C compiler should provide <regex.h>, giving you regular expressions that should be able to help with this.

A regular expression library is probably what you need. If you are using the GNU C library that has a regular expression library, for pattern matching. For Win32 compilers, you can use Regex for Windows port of the GNU code.

My suggestions are as follows :
A) try using strtok to split the text at ',' and then split the resulting subtexts only once at ':' ,that way you would get the key,value pairs you desire.
B) use a regEx api under C that would basically solve your issue.
Also i might suggest looking for a key value store under C for your parsed text (C++ has one but i really don't know about C)

Related

Find formatted string

I'm interested in something like strstr() function but that I could pass a formatted string as argument, like what I pass to printf(). To be clear, let's get an example:
Suppose that I want to find this text: "abc:123" where abc could be any string with any size followed by ':' and then followed by some integer number. I suppose a good function could receive as argument something like this: "%s:%d".
Something else, I want to use this embedded, so I can't get big and/or esoteric libraries.
Thanks and best regards!
You can use sscanf. It takes a string and a format as input and you fill variables as a result. Regular expressions are also something to consider
Use should use regular expressions.
This thread may help you: Compiling/Matching POSIX Regular Expressions in C
Gentleman,
I found this CRX and it is exactly what I wanted. Thanks everybody!
Alternatively, you can directly use printf function. The function outputs strings on default uart (mine is uart0).
But prior to using printf, proper set-up and configuration is required.

emacs regexp replace C function call

I'm trying to regexp match a C function, e.g.
func(blah blah);
The match can include newlines.
I've tried:
func([.+]);
which didn't do newlines, and:
func([...]);
func([^...]);
neither of which seemed to do anything. I guess I'm looking for the part of a regexp that will match any number/type of characters between my opening func( and );.
You could try func[[:space:]]*([^)]*). Nested parens in calls will confuse it though.
I think that the general case is not feasible with regular expressions, because the nested function calls are not a regular language.
While Maxim's answer is specific, I'm going to guess you are looking to do something with the matched function you found. To do serious code processing, you can't beat the semantic parser that is a part of CEDET's suite of tools. http://cedet.sf.net is also part of Emacs.
If you use the semantic parser in emacs, you can:
M-x semantic-mode RET
and then in code:
(semantic-fetch-tags)
or
(semantic-current-tag)
to get the current tag. Once you have the tag, you can call:
(semantic-tag-function-arguments mytag)
to get the arguments, which are tags. For one of those, use semantic-tag-name to get the name, or semantic-tag-type to get the data type.
Once you've got your tag data, you can always write out new code with SRecode, which is a code generator which will take in tags, and spit out code, such as function declarations.

What does "[^<]" in a sscanf evaluation expression mean?

Googling "sscanf reference" or various other sscanf search terms brings up plenty of available references regarding the C sscanf function. Many of these references contain explanations of the available tokens that can be used in format string.
I'm looking through another developers code (who no longer works with our company) and I see in a format string that he has multiple tokens that looks like %15[^<]. I know that the %15 portion of the token is taking 15 characters from the reference string and storing them in a string pointer. What I can not find is documentation that explains the function of the [^<] token.
I've looked through multiple reference pages and I can't find a reference to a token like this. Maybe I'm just clicking on the wrong links but what does this mean? Furthermore, is sscanf (and other cstdio functions with format strings) more robust than what traditional documentation outlines? If so, is does anyone have a link to more thorough documentation?
Thanks guys.
The reference you should be checking is the ISO standard, particularly section 7.19.6.2.
For your particular case, it's matching fifteen characters that aren't <.
The first reference I found (here) sure includes it. Search the page for [.
Brackets are used, much like in regular expressions, to express groups of characters that match. If the first character is a ^, the group is inverted. So [^<] will match any character except for the less-than symbol.
That token matches everything but "<". Is it some kind of XML or HTML parser perhaps?
If you're on a platform other than Windows it's propably in the man page for scanf. Otherwise, I advise you to install cygwin. :)

Getting Variable dynamic in c

I have one requirement in C.
char abc[]="hello";
char hello[]="world";
Using abc whether we can get the hello variable's value in C.
I know it is possible in some of the languages like Perl, Php, Bash,.,
Is it possible in C?
Yes you are right , this is possible in some other language but not in C ,
since abc is a container which resides in a location (for ex: 1000) and hello is one more
container which resides in another location ( for ex : 2000 ) , so we have no contact between
these two arrays ,
we cannot make a value ( strings ) to point some other value.
so finally THIS IS NOT AT ALL POSSIBLE.
No, this is not possible in C without providing a string lookup table of some sort that could link variables with their names.
It's impossible in C, unlike in more dynamic languages like Perl or Python. However, it's important to keep in mind that even in those languages this isn't recommended. I haven't seen a snippet of code putting this to a good use yet. The eval methods available in dynamic languages are used sparingly, and not for dynamically grabbing variable names.
As soon as the C compiler has figured out where to store the underlying pointers, it forgets about the name you gave it. The dynamic languages solve it with a data structure like a hash map which allows you to store the pointers (value) under a key (the name).
Another option is to read in the debug information. This is only available if you compile your code with -g (gcc) or some other, compiler specific option. Note that the debug format is not standardized, so you'll need to figure out what your compiler uses and how to work with it.
It is not possible in C. It can be done in java by reflection in some cases.
POSIX has several functions that allows you to do it, assuming variable hello is global and isn't static:
void *handle = dlopen(NULL, RTLD_NOW);
// error handling omitted
printf("%s variable contains value %s", abc, (char *)dlsym(handle, abc));
dlsym() return value is casted to char * to suppress warning when using compilers that check format string for printf-alike functions.
And you need to make sure you've specified correct compiler options, e.g. -rdynamic -ldl in case of GCC.

What is easiest way to calculate an infix expression using C language?

Suppose the user inputs an infix expression as a string?
What could be the easiest ( By easiest I mean the shortest) way to evaluate the result of that expression using C language?
Probable ways are converting it to a postfix then by using stacks.But its rather a long process.
Is there any way of using functions such as atoi() or eval() that could make the job easier?
C doesn't have an "eval" function built-in, but there are libraries that provide it.
I would highly recommend using TinyExpr. It's free and open-source C code that implements math evaluation from a string. TinyExpr is only 1 C file, and it's about 500 lines of code. I don't think you'll find a shorter or easier way that is actually complete (and not just a toy example).
Here is a complete example of using it, which should demostrate how easy it is:
#include "tinyexpr.h"
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("%f\n", te_interp("5 * 5", 0)); //Prints 25
return 0;
}
If you want to build an expression solver yourself, I would recommend looking at the TinyExpr source-code as a starting point. It's pretty clean and easy to follow.
Certainly the most instructive way (and possibly even the easiest, once you know how) is to learn how to write your own recursive descent parser. A parser for infix expressions in C isn't very long.
Here's one of a number of excellent blog posts by Eli Bendersky on parsing. (This one is the one that's most relevant to you, but I highly recommend all of them.) It contains source code for an infix expression parser -- admittedly in Python, not C, but the conversion should be fairly straightforward, and you'll learn a lot in the process.
you need to parse the string. there's no eval() in C (as in most static languages), so you need to either write your own parser or find some library to help.
since most easy to use parsers are for C++ and not C, i'd rather use a full embeddable language. my absolute favorite is Lua, which can be incredibly lightweight if you don't include the libraries. also, the syntax is nicer than C's, so your users might like it better.
of course, Lua is a full-blown programming language, so it might not be appropriate, or maybe it could help in other ways (to make it easier to extend your application).
One clean (possible not short) way to do it is to build a tree, like a compiler would.
For example, say you have the expression "2+3". The '+' would be the head. The '2' would be the left child and the '3' would be the right child.
Since each expression evaluates to a value, this tree can be extended for infinitely complex expressions: it just needs to be sorted in order of precedence for each operator. Low precedence operators (like '+' go at the top, while high-precedence operators (like '*') go at the bottom. You would then evaluate the expressions on the tree from the bottom up.
You need to build in interpreter of some scripting language.
Convert the string into an array of tokens which are the operands and operators.
Convert the infix token array to a Reverse Polish Notation array.
After the equation is in RPN, then you can pop tokens off the stack and operate on them.
Take a look at the Wikipedia article on Reverse Polish Notation. It shows how to do the conversion and the calculation.

Resources