Small Parser / processor

Discussion in 'C Programming' started by Ja0009, Oct 12, 2003.

  1. Ja0009

    Ja0009 Guest

    Hello,
    I need a small help with peace of code, does not have to be
    whole code.
    I need function or similar that can read from infile.txt open rules.txt
    and read or import 'rules' to process and write to outfile.txt
    results.
    Rules in rules.txt
    are like this:
    [Case1]
    9,5 = 70435
    186,6 ="SELKOW"
    86,3 ="ACC"
    [Case2]
    9,4 = 4330
    22,2 <> "GY"
    112, 2 = 18

    where
    9,5 = 70435
    means that from character 9 next 5 char are 70435

    Output should be like
    "#0001 - Case1"
    "#0002 - Case2"
    "#0003 - NO RULE FOUND" where "#0001 is line number and
    CaseX is Casename

    Thank you in advance
     
    Ja0009, Oct 12, 2003
    #1
    1. Advertising

  2. Ja0009

    Mike Wahler Guest

    "Ja0009" <> wrote in message
    news:...
    > Hello,
    > I need a small help with peace of code,



    If you want help with a piece of code, you need
    to show us that code, and ask specific questions.

    > does not have to be
    > whole code.


    Which implies to me that you already have part of it.
    Where is it?

    > I need function or similar that can read from infile.txt open rules.txt
    > and read or import 'rules' to process and write to outfile.txt
    > results.
    > Rules in rules.txt
    > are like this:
    > [Case1]
    > 9,5 = 70435
    > 186,6 ="SELKOW"
    > 86,3 ="ACC"
    > [Case2]
    > 9,4 = 4330
    > 22,2 <> "GY"
    > 112, 2 = 18
    >
    > where
    > 9,5 = 70435
    > means that from character 9 next 5 char are 70435
    >
    > Output should be like
    > "#0001 - Case1"
    > "#0002 - Case2"
    > "#0003 - NO RULE FOUND" where "#0001 is line number and
    > CaseX is Casename


    If you want this program written for you, hire a
    programmer. I'm available for hire. :)

    -Mike
     
    Mike Wahler, Oct 12, 2003
    #2
    1. Advertising

  3. Ja0009

    Ja0009 Guest

    I am just figguring out.
    So I'll make variables to hold strins, in each line test
    for value and than apply to function or expression for futher processing.
    Thanks.
    P.S. I am concerned the most with how to apply rules.txt to my program.
    Regards


    (Ja0009) wrote in message news:<>...
    > Hello,
    > I need a small help with peace of code, does not have to be
    > whole code.
    > I need function or similar that can read from infile.txt open rules.txt
    > and read or import 'rules' to process and write to outfile.txt
    > results.
    > Rules in rules.txt
    > are like this:
    > [Case1]
    > 9,5 = 70435
    > 186,6 ="SELKOW"
    > 86,3 ="ACC"
    > [Case2]
    > 9,4 = 4330
    > 22,2 <> "GY"
    > 112, 2 = 18
    >
    > where
    > 9,5 = 70435
    > means that from character 9 next 5 char are 70435
    >
    > Output should be like
    > "#0001 - Case1"
    > "#0002 - Case2"
    > "#0003 - NO RULE FOUND" where "#0001 is line number and
    > CaseX is Casename
    >
    > Thank you in advance
     
    Ja0009, Oct 13, 2003
    #3
  4. Mike Wahler <> spoke thus:

    > If you want this program written for you, hire a
    > programmer. I'm available for hire. :)


    I hope this is tongue-in-cheek and not indicative of actual unemployment ;)

    --
    Christopher Benson-Manica | Upon the wheel thy fate doth turn,
    ataru(at)cyberspace.org | upon the rack thy lesson learn.
     
    Christopher Benson-Manica, Oct 14, 2003
    #4
  5. Ja0009

    Mike Wahler Guest

    "Christopher Benson-Manica" <> wrote in message
    news:bmfi2g$nit$...
    > Mike Wahler <> spoke thus:
    >
    > > If you want this program written for you, hire a
    > > programmer. I'm available for hire. :)

    >
    > I hope this is tongue-in-cheek and not indicative of actual unemployment

    ;)

    I can always use extra 'beer money' :)

    -Mike
     
    Mike Wahler, Oct 14, 2003
    #5
  6. Ja0009

    Derk Gwen Guest

    (Ja0009) wrote:
    # I am just figguring out.
    # So I'll make variables to hold strins, in each line test
    # for value and than apply to function or expression for futher processing.
    # Thanks.
    # P.S. I am concerned the most with how to apply rules.txt to my program.
    # Regards

    Is there a reason why this has to be C? In other languages like Perl or Tcl,
    you can probably code a solution in an hour or much less.

    # (Ja0009) wrote in message news:<>...
    # > Hello,
    # > I need a small help with peace of code, does not have to be
    # > whole code.
    # > I need function or similar that can read from infile.txt open rules.txt
    # > and read or import 'rules' to process and write to outfile.txt
    # > results.
    # > Rules in rules.txt
    # > are like this:
    # > [Case1]
    # > 9,5 = 70435
    # > 186,6 ="SELKOW"
    # > 86,3 ="ACC"
    # > [Case2]
    # > 9,4 = 4330
    # > 22,2 <> "GY"
    # > 112, 2 = 18
    # >
    # > where
    # > 9,5 = 70435
    # > means that from character 9 next 5 char are 70435
    # >
    # > Output should be like
    # > "#0001 - Case1"
    # > "#0002 - Case2"
    # > "#0003 - NO RULE FOUND" where "#0001 is line number and
    # > CaseX is Casename
    # >
    # > Thank you in advance
    #
    #

    --
    Derk Gwen http://derkgwen.250free.com/html/index.html
    If your job was as meaningless as theirs, wouldn't you go crazy too?
     
    Derk Gwen, Oct 14, 2003
    #6
  7. Ja0009

    Ja0009 Guest

    Thanks Derk,
    should be basic,pascal or c,c++.
    As I am not familiar with basic or pascal, in C/C++ not so much experienced but
    I can try :)
    You are right I would do same thing in ASP/VB or Java/JavaScript or
    XSL good but hm here I have a problem :(.
    RGS

    Derk Gwen <> wrote in message news:<>...
    > (Ja0009) wrote:
    > # I am just figguring out.
    > # So I'll make variables to hold strins, in each line test
    > # for value and than apply to function or expression for futher processing.
    > # Thanks.
    > # P.S. I am concerned the most with how to apply rules.txt to my program.
    > # Regards
    >
    > Is there a reason why this has to be C? In other languages like Perl or Tcl,
    > you can probably code a solution in an hour or much less.
    >
    > # (Ja0009) wrote in message news:<>...
    > # > Hello,
    > # > I need a small help with peace of code, does not have to be
    > # > whole code.
    > # > I need function or similar that can read from infile.txt open rules.txt
    > # > and read or import 'rules' to process and write to outfile.txt
    > # > results.
    > # > Rules in rules.txt
    > # > are like this:
    > # > [Case1]
    > # > 9,5 = 70435
    > # > 186,6 ="SELKOW"
    > # > 86,3 ="ACC"
    > # > [Case2]
    > # > 9,4 = 4330
    > # > 22,2 <> "GY"
    > # > 112, 2 = 18
    > # >
    > # > where
    > # > 9,5 = 70435
    > # > means that from character 9 next 5 char are 70435
    > # >
    > # > Output should be like
    > # > "#0001 - Case1"
    > # > "#0002 - Case2"
    > # > "#0003 - NO RULE FOUND" where "#0001 is line number and
    > # > CaseX is Casename
    > # >
    > # > Thank you in advance
    > #
    > #
     
    Ja0009, Oct 14, 2003
    #7
  8. (Ja0009) wrote in message news:<>...

    If it could be in a scripting language, you'll find below your program
    written
    in CodeWorker (http://www.codeworker.org).

    If not, it gives you the outline to follow for writing it in a
    general-purpose programming language.

    //------------------------- file "ja0009.gen"
    ----------------------------
    traceLine("Example of command line:");
    traceLine("\tCodeWorker -script ja0009.gen");
    traceLine("\t\t-D INPUT=infile.txt -D RULES=rules.txt -D
    OUTPUT=outfile.txt");

    if !getProperty("INPUT") error("input filename expected; type -D
    INPUT=<infile.txt>");
    if !getProperty("RULES") error("filename of rules expected; type -D
    RULES=<rules.txt>");
    if !getProperty("INPUT") error("output filename expected; type -D
    OUTPUT=<outfile.txt>");

    /**
    * Parsing of rules. Structure of the parse tree:
    * cases[]: array of cases,
    * |
    * +-- antecedents[]: array of conditions the rule must validate,
    * |
    * +-- position: position in the line (starting at
    0),
    * |
    * +-- operator: '=' or "<>"
    * |
    * +-- constant: string or value to compare
    **/
    parseAsBNF(
    {
    rules ::=
    => local iCase = 1;
    #ignore(blanks)
    [
    case(iCase)
    =>increment(iCase);
    ]*
    ;
    case(iCase : value) ::= '[' "Case" #readText(iCase) ']'
    [antecedent(iCase)]*;
    antecedent(iCase : value) ::=
    #readInteger:iPosition ',' #readInteger
    => decrement(iPosition);
    => pushItem this.cases[iCase].antecedents;
    => localref current = this.cases[iCase].antecedents#back;
    => insert current.position = iPosition;
    ['=' | "<>"]:current.operator
    [#readNumeric:current.constant |
    #readCString:current.constant]
    ;
    }, project, getProperty("RULES"));

    // Make the output file empty
    deleteFile(getProperty("OUTPUT"));

    /**
    * Splitting of the input file to lines, and iteration of cases up to
    find
    * one that matches. Result of the study in the output file.
    **/
    parseAsBNF(
    {
    infile_parser ::=
    => local iLine = 1;
    [
    [->['\n' | #empty]]:sLine
    => {
    local iCase; // number of the rule that matches
    foreach i in this.cases {
    local bAccept; // do we accept a case?
    foreach j in i.antecedents {
    local bFound = (sLine.findNextString(j.constant,
    j.position) == j.position);
    set bAccept = (bFound == (j.operator == '='));
    if !bAccept break; // all antecedents must match
    }
    if bAccept {
    set iCase = i.key();
    break;
    }
    }
    local iFormatedNumber = iLine.completeLeftSpaces(4);
    set iFormatedNumber = iFormatedNumber.replaceString(' ',
    '0');
    local sOutput = '#' + iFormatedNumber + " - ";
    if iCase set sOutput += "Case" + iCase;
    else set sOutput += "NO RULE FOUND";
    appendFile(getProperty("OUTPUT"), sOutput + endl());
    }
    => increment(iLine);
    ]*
    ;
    }, project, getProperty("INPUT"));
    //---------------------------- end of file
    --------------------------------

    >
    > Derk Gwen <> wrote in message news:<>...
    > > (Ja0009) wrote:
    > > # I am just figguring out.
    > > # So I'll make variables to hold strins, in each line test
    > > # for value and than apply to function or expression for futher processing.
    > > # Thanks.
    > > # P.S. I am concerned the most with how to apply rules.txt to my program.
    > > # Regards
    > >
    > > Is there a reason why this has to be C? In other languages like Perl or Tcl,
    > > you can probably code a solution in an hour or much less.
    > >
    > > # (Ja0009) wrote in message news:<>...
    > > # > Hello,
    > > # > I need a small help with peace of code, does not have to be
    > > # > whole code.
    > > # > I need function or similar that can read from infile.txt open rules.txt
    > > # > and read or import 'rules' to process and write to outfile.txt
    > > # > results.
    > > # > Rules in rules.txt
    > > # > are like this:
    > > # > [Case1]
    > > # > 9,5 = 70435
    > > # > 186,6 ="SELKOW"
    > > # > 86,3 ="ACC"
    > > # > [Case2]
    > > # > 9,4 = 4330
    > > # > 22,2 <> "GY"
    > > # > 112, 2 = 18
    > > # >
    > > # > where
    > > # > 9,5 = 70435
    > > # > means that from character 9 next 5 char are 70435
    > > # >
    > > # > Output should be like
    > > # > "#0001 - Case1"
    > > # > "#0002 - Case2"
    > > # > "#0003 - NO RULE FOUND" where "#0001 is line number and
    > > # > CaseX is Casename
    > > # >
    > > # > Thank you in advance
    > > #
    > > #
     
    Cedric LEMAIRE, Oct 16, 2003
    #8
  9. Ja0009

    Ja0009 Guest

    Thanks,
    I believe that CodeWorker does the work
    perfect.
    Anywhere it needs to be in C or C++
    At the moment I am writing it in C
    but am I still trying to pass (read) rules to
    program (function)
    Ned

    (Cedric LEMAIRE) wrote in message news:<>...
    > (Ja0009) wrote in message news:<>...
    >
    > If it could be in a scripting language, you'll find below your program
    > written
    > in CodeWorker (http://www.codeworker.org).
    >
    > If not, it gives you the outline to follow for writing it in a
    > general-purpose programming language.
    >
    > //------------------------- file "ja0009.gen"
    > ----------------------------
    > traceLine("Example of command line:");
    > traceLine("\tCodeWorker -script ja0009.gen");
    > traceLine("\t\t-D INPUT=infile.txt -D RULES=rules.txt -D
    > OUTPUT=outfile.txt");
    >
    > if !getProperty("INPUT") error("input filename expected; type -D
    > INPUT=<infile.txt>");
    > if !getProperty("RULES") error("filename of rules expected; type -D
    > RULES=<rules.txt>");
    > if !getProperty("INPUT") error("output filename expected; type -D
    > OUTPUT=<outfile.txt>");
    >
    > /**
    > * Parsing of rules. Structure of the parse tree:
    > * cases[]: array of cases,
    > * |
    > * +-- antecedents[]: array of conditions the rule must validate,
    > * |
    > * +-- position: position in the line (starting at
    > 0),
    > * |
    > * +-- operator: '=' or "<>"
    > * |
    > * +-- constant: string or value to compare
    > **/
    > parseAsBNF(
    > {
    > rules ::=
    > => local iCase = 1;
    > #ignore(blanks)
    > [
    > case(iCase)
    > =>increment(iCase);
    > ]*
    > ;
    > case(iCase : value) ::= '[' "Case" #readText(iCase) ']'
    > [antecedent(iCase)]*;
    > antecedent(iCase : value) ::=
    > #readInteger:iPosition ',' #readInteger
    > => decrement(iPosition);
    > => pushItem this.cases[iCase].antecedents;
    > => localref current = this.cases[iCase].antecedents#back;
    > => insert current.position = iPosition;
    > ['=' | "<>"]:current.operator
    > [#readNumeric:current.constant |
    > #readCString:current.constant]
    > ;
    > }, project, getProperty("RULES"));
    >
    > // Make the output file empty
    > deleteFile(getProperty("OUTPUT"));
    >
    > /**
    > * Splitting of the input file to lines, and iteration of cases up to
    > find
    > * one that matches. Result of the study in the output file.
    > **/
    > parseAsBNF(
    > {
    > infile_parser ::=
    > => local iLine = 1;
    > [
    > [->['\n' | #empty]]:sLine
    > => {
    > local iCase; // number of the rule that matches
    > foreach i in this.cases {
    > local bAccept; // do we accept a case?
    > foreach j in i.antecedents {
    > local bFound = (sLine.findNextString(j.constant,
    > j.position) == j.position);
    > set bAccept = (bFound == (j.operator == '='));
    > if !bAccept break; // all antecedents must match
    > }
    > if bAccept {
    > set iCase = i.key();
    > break;
    > }
    > }
    > local iFormatedNumber = iLine.completeLeftSpaces(4);
    > set iFormatedNumber = iFormatedNumber.replaceString(' ',
    > '0');
    > local sOutput = '#' + iFormatedNumber + " - ";
    > if iCase set sOutput += "Case" + iCase;
    > else set sOutput += "NO RULE FOUND";
    > appendFile(getProperty("OUTPUT"), sOutput + endl());
    > }
    > => increment(iLine);
    > ]*
    > ;
    > }, project, getProperty("INPUT"));
    > //---------------------------- end of file
    > --------------------------------
    >
    > >
    > > Derk Gwen <> wrote in message news:<>...
    > > > (Ja0009) wrote:
    > > > # I am just figguring out.
    > > > # So I'll make variables to hold strins, in each line test
    > > > # for value and than apply to function or expression for futher processing.
    > > > # Thanks.
    > > > # P.S. I am concerned the most with how to apply rules.txt to my program.
    > > > # Regards
    > > >
    > > > Is there a reason why this has to be C? In other languages like Perl or Tcl,
    > > > you can probably code a solution in an hour or much less.
    > > >
    > > > # (Ja0009) wrote in message news:<>...
    > > > # > Hello,
    > > > # > I need a small help with peace of code, does not have to be
    > > > # > whole code.
    > > > # > I need function or similar that can read from infile.txt open rules.txt
    > > > # > and read or import 'rules' to process and write to outfile.txt
    > > > # > results.
    > > > # > Rules in rules.txt
    > > > # > are like this:
    > > > # > [Case1]
    > > > # > 9,5 = 70435
    > > > # > 186,6 ="SELKOW"
    > > > # > 86,3 ="ACC"
    > > > # > [Case2]
    > > > # > 9,4 = 4330
    > > > # > 22,2 <> "GY"
    > > > # > 112, 2 = 18
    > > > # >
    > > > # > where
    > > > # > 9,5 = 70435
    > > > # > means that from character 9 next 5 char are 70435
    > > > # >
    > > > # > Output should be like
    > > > # > "#0001 - Case1"
    > > > # > "#0002 - Case2"
    > > > # > "#0003 - NO RULE FOUND" where "#0001 is line number and
    > > > # > CaseX is Casename
    > > > # >
    > > > # > Thank you in advance
    > > > #
    > > > #
     
    Ja0009, Oct 17, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roberto Nunnari

    new small xml parser Open Source

    Roberto Nunnari, Feb 2, 2004, in forum: XML
    Replies:
    0
    Views:
    448
    Roberto Nunnari
    Feb 2, 2004
  2. Replies:
    6
    Views:
    2,959
    Karl Waclawek
    Mar 7, 2006
  3. Ja0009

    Small C Parser

    Ja0009, Oct 19, 2003, in forum: C Programming
    Replies:
    0
    Views:
    484
    Ja0009
    Oct 19, 2003
  4. Ola Natvig
    Replies:
    1
    Views:
    285
    Brian Quinlan
    Mar 8, 2005
  5. brahatha
    Replies:
    1
    Views:
    661
Loading...

Share This Page