Small Parser / processor

J

Ja0009

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
 
M

Mike Wahler

Ja0009 said:
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
 
J

Ja0009

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
 
C

Christopher Benson-Manica

Mike Wahler said:
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 ;)
 
D

Derk Gwen

(e-mail address removed) (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.

# (e-mail address removed) (Ja0009) wrote in message # > 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
#
#
 
J

Ja0009

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
 
C

Cedric LEMAIRE

(e-mail address removed) (Ja0009) wrote in message
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 said:
(e-mail address removed) (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.

# (e-mail address removed) (Ja0009) wrote in message # > 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
#
#
 
J

Ja0009

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

(e-mail address removed) (Ja0009) wrote in message
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 said:
(e-mail address removed) (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.

# (e-mail address removed) (Ja0009) wrote in message # > 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
#
#
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top