Thread: parse an expression in c

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    3

    parse an expression in c

    Hello,

    I would like to parse an expression which is a condition of the CA-Clipper language and then translate it into SQL. Do you think that tools like LEX and YACC could help me to solve my problem? Or do you think there is an other better way?

    Here is a translation sample:

    ("Mart" $ Customers->Name .and. Orders->Price >= 120) .or. Oders->Date = ctod("18/06/08")

    will be translated to:

    where exists (select 0 from Customers as C, Orders as O where C.Id = O.Customers_Id and (C.Name like "%Mart%" and O.Price >= 120) or O.Date = #18/06/08#)

    Thank you in advance,

    Mathmax

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, don't know about CA-clipper language and SQL, so not much help from here.
    I used once bison/lex/yacc and all this stuff. They are quite handy in parsing expressions and they are not difficult to use.

    But you will need to learn about them. So if you are not familiar depends if you are in the "mood" of using new things, more debugging etc etc

    But generally parsing can be done also with plain C. The best way is to make your own functions that do so. Like breaking the expression in strings. Each string can be stored in a struct that has and ID part.

    So you ll have fgetc() and when you ll find a " for example you will store the following characters until you ll find another ". Save the string in a struct and have the struct.ID = string.

    Then you will also have to check for (, ) which will complicate things.

    Personally I would use C. The best way is bison/lex/yacc or other tools. You learn a useful tool and you ll have less debugging than creating functions with C.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    3
    Thanks for the advices.

    I've tried to compile a sample to understand how things work. I'm novice in c-programming (originally a .net programmer).

    I get the following error message at compile time:

    Unresolved external symbol "_yylval".

    I've included an header file with the following content:

    #ifndef YYSTYPE
    #define YYSTYPE int
    #endif
    extern YYSTYPE yylval;
    Do you know what is the problem ?

    Regards,

    mathmax

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You must define yyval in one of your source files.
    Merely adding it to the header and adding "extern" doesn't work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    3
    How to define _yylval ?
    Is there any external static lib to include for compiling a program generated by Bison or Flex ?

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    What Elysia means is probably you should define it. Like just declare it in a source file.

    I don't really remember how to use these tools, so don't know what yylval is suppose to be.

    Googling though maybe you need

    #include "y.tab.h"

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Regular expression parsing is a rather advanced topic. Not so sure if a newcomer to C should start with parsing.

    But if you must you can google recursive descent parsers to see how they work. Then you can use the other tools to aid you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM