Convert string to C expression syntax

272 views
Skip to first unread message

nilay

unread,
Jul 30, 2013, 10:58:38 PM7/30/13
to
Hi,
I want to do the following,

==========================================
int main(int argc, char *argv[])
{

...
...
if (*argv[1])
{
...
}

...
}
==========================================

as i tried to show above, I want to pass a filter condition
at command-line input. And I want to convert that input string
to 'if-condition expression' so that it can be used in the program.
How can it be done?? I am struggling very hard to get it right.

Thank you.
-- Nilay
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

James Kuyper

unread,
Jul 31, 2013, 10:22:41 PM7/31/13
to
On 07/30/2013 10:58 PM, nilay wrote:
> Hi,
> I want to do the following,
>
> ==========================================
> int main(int argc, char *argv[])
> {
>
> ...
> ...
> if (*argv[1])
> {
> ...
> }
>
> ...
> }
> ==========================================
>
> as i tried to show above, ...

It's a very bad idea to ask how to write code that does something, and
then to explain what you want the code to do by writing code. If you
don't know how to write such code, the code you do write will probably
do something entirely different from what you want to do. You've
provided an excellent example of this.

The code above takes the second element of argv (without first checking
to determine whether there IS a second element - which means your code
has undefined behavior if it is invoked with no arguments), and checks
whether or not the first char of that string is '\0'. That has almost
nothing to do with your stated goal:

> ... I want to pass a filter condition
> at command-line input. And I want to convert that input string
> to 'if-condition expression' so that it can be used in the program.
> How can it be done?? I am struggling very hard to get it right.

All command line inputs are text strings. If those strings describe
filter conditions, you're going to have to decide what kinds of filter
conditions you want to support, and then define a syntax for specifying
those conditions on the command line. Then you'll have to write code
that reads the command line arguments, parsing them according to the
grammar you've specified, and converting them into some internal
representation. Then write code that uses the internal representation to
implement the filter.
That probably sounds to you like an excessively vague,
nearly-meaningless string of jargon, and there's a reason for that.
Until you've specified what kinds of filter conditions you want your
program to support, it's hard to say anything more specific than that
about how to do it.
--
James Kuyper

Barry Schwarz

unread,
Jul 31, 2013, 10:22:44 PM7/31/13
to
On Tue, 30 Jul 2013 21:58:38 -0500 (CDT), nilay <nilay...@gmail.com>
wrote:

>Hi,
>I want to do the following,
>
>==========================================
>int main(int argc, char *argv[])
>{
> ...
> if (*argv[1])
> {
> ...
> }
> ...
>}
>==========================================
>
>as i tried to show above, I want to pass a filter condition
>at command-line input. And I want to convert that input string
>to 'if-condition expression' so that it can be used in the program.
>How can it be done?? I am struggling very hard to get it right.

It depends on what type of condition you want to check for.

If you want to check if the string exists, you would actually check
argc. C does not support omitted command line arguments except at the
end.

If you want to check for an empty string (and your system allows you
to specify one as a command line argument), then the code you posted
will check that. An empty string is an array of 1 char and that char
has the value '\0'. Your code evaluates the first char in the array
pointed to by argv[1].

If you want to check whether the string has a certain value, you would
use strcmp or strncmp in the condition of your if statement. Something
along the lines of
if (!strcmp(argv[1], "ABC")
{/* equal code */}
else
{/* optional not equal code */}

--
Remove del for email

Gordon Burditt

unread,
Jul 31, 2013, 10:22:47 PM7/31/13
to
nilay <nilay...@gmail.com> wrote:
> Hi,
> I want to do the following,
>
> ==========================================
> int main(int argc, char *argv[])
> {
>
> ...
> ...
> if (*argv[1])
> {
> ...
> }
>
> ...
> }
> ==========================================

You need to check if argv[1] is NULL before trying to use any
character string it might point to.

> as i tried to show above, I want to pass a filter condition
> at command-line input. And I want to convert that input string
> to 'if-condition expression' so that it can be used in the program.

There is no way to convert a string to C code at runtime in Standard
C. The only implementations likely to have that as an extension
are C interpreters. And you'll need to deal with problems like
the expression being invalid C.

You may write code to parse the filter expression yourself. Some
C texts have an exercise to write a 4-function calculator program
- this is a basic start to evaluating exprssions. There are also
tools such as yacc, bison, lex, and flex for writing complex parsers
such as those found in compilers, which generate C. These expect you
to compile the code in advance.

A program called tcpdump does filter-matching, but its filter
expressions are for matching network packets.

If you are interested in pattern-matching STRINGS there are a number
of packages like pcre that take a pattern in a particular format and
a string and see if there is a match.


> How can it be done?? I am struggling very hard to get it right.

Another possibility is to take the filter-expression, put a C program
around it, invoke a compiler, and then use dynamic linking to
call the function. This isn't standard C either.


What are you planning on using as filter expressions? What are they
filtering?

Keith Thompson

unread,
Aug 1, 2013, 1:58:21 PM8/1/13
to
Barry Schwarz <schw...@dqel.com> writes:
[...]
> If you want to check whether the string has a certain value, you would
> use strcmp or strncmp in the condition of your if statement. Something
> along the lines of
> if (!strcmp(argv[1], "ABC")
> {/* equal code */}
> else
> {/* optional not equal code */}

To be clear (since the OP may not be aware of it), strcmp() returns a
value less than, equal to, or greater than 0 if the first argument is
respectively than, equal to, or greater than the right argument.

So (!strcmp(argv[1], "ABC") is true if the string pointed to by argv[1]
is equal to "ABC". If you treat the result of strcmp() as a boolean,
it's true of the strings are unequal, false if they're equal.

Personally, I prefer to be more explicit; I'd write:

if (strcmp(argv[1], "ABC") != 0) ...

but you'll commonly see the !strcmp() form in C code.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Ike Naar

unread,
Aug 1, 2013, 10:05:25 PM8/1/13
to
On 2013-08-01, Keith Thompson <ks...@mib.org> wrote:
> Barry Schwarz <schw...@dqel.com> writes:
> [...]
>> If you want to check whether the string has a certain value, you would
>> use strcmp or strncmp in the condition of your if statement. Something
>> along the lines of
>> if (!strcmp(argv[1], "ABC")
>> {/* equal code */}
>> else
>> {/* optional not equal code */}
>
> To be clear (since the OP may not be aware of it), strcmp() returns a
> value less than, equal to, or greater than 0 if the first argument is
> respectively than, equal to, or greater than the right argument.
>
> So (!strcmp(argv[1], "ABC") is true if the string pointed to by argv[1]
> is equal to "ABC". If you treat the result of strcmp() as a boolean,
> it's true of the strings are unequal, false if they're equal.
>
> Personally, I prefer to be more explicit; I'd write:
>
> if (strcmp(argv[1], "ABC") != 0) ...
>
> but you'll commonly see the !strcmp() form in C code.

Your preferred code does the opposite of the original code.

Keith Thompson

unread,
Aug 2, 2013, 7:34:04 PM8/2/13
to
Ike Naar <i...@ukato.freeshell.org> writes:
> On 2013-08-01, Keith Thompson <ks...@mib.org> wrote:
>> Barry Schwarz <schw...@dqel.com> writes:
>> [...]
>>> If you want to check whether the string has a certain value, you would
>>> use strcmp or strncmp in the condition of your if statement. Something
>>> along the lines of
>>> if (!strcmp(argv[1], "ABC")
>>> {/* equal code */}
>>> else
>>> {/* optional not equal code */}
>>
>> To be clear (since the OP may not be aware of it), strcmp() returns a
>> value less than, equal to, or greater than 0 if the first argument is
>> respectively than, equal to, or greater than the right argument.
>>
>> So (!strcmp(argv[1], "ABC") is true if the string pointed to by argv[1]
>> is equal to "ABC". If you treat the result of strcmp() as a boolean,
>> it's true of the strings are unequal, false if they're equal.
>>
>> Personally, I prefer to be more explicit; I'd write:
>>
>> if (strcmp(argv[1], "ABC") != 0) ...
>>
>> but you'll commonly see the !strcmp() form in C code.
>
> Your preferred code does the opposite of the original code.

D'oh!

I'm tempted to say that this demonstrates the dangerous obscurity
of the !strcmp() form, so that even experienced programmers can get
it wrong, but I'll resist that temptation and just try to pay more
attention next time.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Roberto Waltman

unread,
Aug 5, 2013, 12:24:07 PM8/5/13
to
nilay wrote:
>as i tried to show above, I want to pass a filter condition
>at command-line input. And I want to convert that input string
>to 'if-condition expression' so that it can be used in the program.
>How can it be done??

This may or not be of help for what you need, (as others replied, the
requirements are vague), but you may want to look at the source code
of the "find" command. (In Unix/Linux/FreeBSD etc.)

To see how a list of switches in the command line, such as

find / -type f \( -mtime < 3 -o -size > 10k \) \( -name AB -o
-name BA \) ...

is converted in conditions that can be checked at run time.

In the general case, you need to build an interpreter for a small
embedded language, specific to the conditions you need to check.
--
Roberto Waltman

[ Please reply to the group,
return address is invalid ]

malcolm...@btinternet.com

unread,
Sep 2, 2013, 5:09:12 AM9/2/13
to
On Wednesday, July 31, 2013 3:58:38 AM UTC+1, nilay wrote:
>
> I want to do the following,
>
> as i tried to show above, I want to pass a filter condition
> at command-line input. And I want to convert that input string
> to 'if-condition expression' so that it can be used in the program.
>
> How can it be done?? I am struggling very hard to get it right.
>
C is compiled, so none of the compile time logic is avialable at
runtime. Unlike some other languages you can't write a string in
C like "if(a==b) printf("Hi");" and have the C environment evaluate
it for you.
You can write your own interpreter. Go to my website
http://www.malcolmmclean.site11.com/www
and look at the MiniBasic webpages (below the book) to understand how
to get started in the interpreter game.

Paul D. DeRocco

unread,
Sep 7, 2013, 12:25:49 AM9/7/13
to
> On 7/30/2013 7:58 PM, nilay wrote:
>
> I want to do the following,
>
> ==========================================
> int main(int argc, char *argv[])
> {
>
> ...
> ...
> if (*argv[1])
> {
> ...
> }
>
> ...
> }
> ==========================================
>
> as i tried to show above, I want to pass a filter condition
> at command-line input. And I want to convert that input string
> to 'if-condition expression' so that it can be used in the program.
> How can it be done?? I am struggling very hard to get it right.

You can't do this in C. If you want to do stuff like this, you should
take up Python instead.

--

Ciao, Paul D. DeRocco
Paul mailto:pder...@ix.netcom.com

James Kuyper

unread,
Sep 11, 2013, 6:27:27 PM9/11/13
to
On 09/07/2013 12:25 AM, Paul D. DeRocco wrote:
> > On 7/30/2013 7:58 PM, nilay wrote:
> >
>> I want to do the following,
>>
>> ==========================================
>> int main(int argc, char *argv[])
>> {
>>
>> ...
>> ...
>> if (*argv[1])
>> {
>> ...
>> }
>>
>> ...
>> }
>> ==========================================
>>
>> as i tried to show above, I want to pass a filter condition
>> at command-line input. And I want to convert that input string
>> to 'if-condition expression' so that it can be used in the program.
>> How can it be done?? I am struggling very hard to get it right.
>
> You can't do this in C. If you want to do stuff like this, you should
> take up Python instead.

You can't do this that way in C, nor in any other simple way. It might
be simpler to do it in Python, but it certainly can be done in C. You
just need to write code for parsing the condition expression, and then
evaluating it. If the condition expression has a very simple syntax,
that's trivial to do. However, even if he wants to support the full
complexity of a C-like syntax, it should still be feasible. Components
(or at least, design ideas) borrowed from a C compiler should be
adaptable to this use. C has famously been a bootstrapped language: more
advanced versions of C were first implemented by using more primitive
versions of C to write the compiler, so such components can certain be
written in C.
--
James Kuyper

Nitin Tripathi

unread,
Sep 11, 2013, 6:31:28 PM9/11/13
to
You should use switch case here - if argument is NUMERIC, If its a string then you can look for if else go with strncmp/memcmp.

Paul D. DeRocco

unread,
Sep 12, 2013, 12:34:29 PM9/12/13
to
> On 9/11/2013 3:27 PM, James Kuyper wrote:
>> On 09/07/2013 12:25 AM, Paul D. DeRocco wrote:
>>
>> You can't do this in C. If you want to do stuff like this, you should
>> take up Python instead.
>
> You can't do this that way in C, nor in any other simple way. It might
> be simpler to do it in Python, but it certainly can be done in C. You
> just need to write code for parsing the condition expression, and then
> evaluating it. If the condition expression has a very simple syntax,
> that's trivial to do. However, even if he wants to support the full
> complexity of a C-like syntax, it should still be feasible. Components
> (or at least, design ideas) borrowed from a C compiler should be
> adaptable to this use. C has famously been a bootstrapped language: more
> advanced versions of C were first implemented by using more primitive
> versions of C to write the compiler, so such components can certain be
> written in C.

Well, sure, you can do anything in any language. But you can't do it in
the simple way that the OP seemed to want. I interpret the question as
how to evaluate an expression supplied at runtime, in the same context
as the expressions that are part of the program. Perhaps that wasn't
what was meant--the question wasn't entirely clear.

--

Ciao, Paul D. DeRocco
Paul mailto:pder...@ix.netcom.com

Francis Glassborow

unread,
Sep 12, 2013, 12:38:57 PM9/12/13
to
Yes and if you are running C on an interpreter (they do exist) it comes
about pretty naturally.

Francis
Reply all
Reply to author
Forward
0 new messages