(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
#
#