Site hosted by Angelfire.com: Build your free website today!
« January 2009 »
S M T W T F S
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Entries by Topic
All topics  «
Blog Tools
Edit your Blog
Build a Blog
RSS Feed
View Profile
You are not logged in. Log in
My Blog
Tuesday, 20 January 2009
*** Description of the project ***
Mood:  a-ok
Now Playing: test

*** Description of the project ***

   - BNF like parser

The crux of the program is a C/C++ function
that would take as input an array of chars (*str), the number of the sequence (seq_nbr) and the extent number (ext) and would
return the 'string' (pointer to character array) of the formed sequence.

// str     : specification string
// seq_nbr : sequence number
// ext     : extent
char *make_spec(char *str, long seq_nbr, long ext) {

    char *buffer = str;
    ...
return buffer;}


str hold the specification string. This string is an EBNF like string-syntax.
The function has to return the string of a single element. The element number to be
returned is specified in the seq_nbr parameter. If this number of out of bound it simple
returns an error. In this case the prev global variable contains the previsous allowable number
The global element arrays are loaded from the 'global_element/'sub-directory. The filename of each array is in all-caps (Ex: NUM.txt loads the %NUM array).
Lets say this directory contains 3 files. NUM.txt, INT.txt and ALL.txt the at program
start-up 3 arrays will be present in the program memory: %NUM, %INT, %ALL. And when-ever
one of these 3 arrays is named in the *str string it will expand to the specified value.
If an inexistant global element array name is specified in the specification string (*str)
than an error results. Ex. In the program I have %NUM, %INT, %ALL as global element arrays. If
str = "%REL" then because this array is not in the program (nor is the name of none of the files
in the sub-directory (NUM.txt, INT.txt, ALL.txt) then we have entered a non-existant array, which is
an error).
Caching:
For effichiancy reasons since the returned string is constly to compute it would be wise to cache it
in a structure that can latter be retrived if the same value is required again. Upon the call to
make_spec function there would have to be a lookup in the structure of the specification string (str)
and if not found then the computed value (buffer) would be added to that structure for latter use, of
course in the program there would be a global variable (MAX_MEM) indicating the maximum number of bytes
available. If memory > MAX_MEM then the oldest string in the structure is discarted and replaced by the new one.

The Produced elements in the returned string (buffer) have to be numbered in alphabetical numbering scheme i.e.
a = 1, b = 2, c = 3, ... z = 26.


1) alternative elements are specified by the '|' char
Ex: foo or bar or baz => foo | bar | baz
(see Ex. 10)

2) seperator elements are specified by the ';' char
Ex: foo baz then bar => foo baz ; bar
(see Ex. 11)

Note: this is usefull in the case were there is more than one element as in the
case above (foo baz)

3) optional elements are preceded and followed by the '[' and ']' char
Ex: foo or foo bar => foo [bar]
(see Ex. 12)

4) global elements are preceded by the '%' char
Ex: foo %NUM => foo $integer_0
                foo $real_0
                foo $double_0
   * when %NUM global hold the elements (integer, real, double)
(see Ex. 13)

5) exclusion elements are preceded by the '!' char
Ex:  foo %NUM !real => foo $integer_0
                       foo $double_0
(see Ex. 14)

6) escape elements are preceded by the '\' char
Ex: foo \%NUM => foo %NUM
(say i wonna include '%' char as part of the string and therefore not to be interpreted)
(see Ex. 15)

the seq_nbr parameter is the sequence number. Lets say I have like in the first Ex.
str = "foo | bar | baz"


if seq_nbr equals 1 then the element would be foo
if seq_nbr equals 2 then the element would be bar
if seq_nbr equals 3 then the element would be baz

the ext parameter is the extent. This tell us to which extent to produce element string
with elipses. (...).

For the purpose of make it clear I'll show 9 examples with specific values of the function's input
parameters str, ext and seq_nbr. I'll show 3 cases par global element type.
("foo ...", "baz foo ...", "foo %NUM ...")
For illustrating purpose only I'v expanded the global element array to all its cases in the bellow examples.
The value returned by the function is the string correcponding to the numerical position corresponding to the letter
 for example if seq_nbr = 2 we return 'b', f seq_nbr = 26 we return 'z'


Note: Examples (1,2,3) are with simple element (alone) with elipses.
      Examples (4,5,6) are with simple element elipses.
      Examples (7,8,9) are with global element elipses.

 


Ex. 1: Say I have str = "foo ..." , ext = 2 and seq_nbr = 2
make_spec("foo ...", 2, 2)
then the returned string (buffer) would be: "b) foo foo"
a) foo
b) foo foo


Ex. 2: Say I have str = "foo ..." , ext = 3 and seq_nbr = 3
make_spec("foo ...", 3, 3)
then the returned string (buffer) would be: "c) foo foo foo"
a) foo
b) foo foo
c) foo foo foo


Ex. 3: Say I have str = "foo ..." , ext = 4 and seq_nbr = 4
make_spec("foo ...", 4, 4)
then the returned string (buffer) would be: "d) foo foo foo foo"
a) foo
b) foo foo
c) foo foo foo
d) foo foo foo foo


Ex. 4: Say I have str = "baz foo ..." , ext = 2 and seq_nbr = 2
make_spec("baz foo ...", 2, 2)
then the returned string (buffer) would be: "b) baz foo foo"
a) baz foo
b) baz foo foo


Ex. 5: Say I have str = "baz foo ..." , ext = 3 and seq_nbr = 3
make_spec("baz foo ...", 3, 3)
then the returned string (buffer) would be: "c) baz foo foo foo"
a) baz foo
b) baz foo foo
c) baz foo foo foo


Ex. 6: Say I have str = "baz foo ..." , ext = 4 and seq_nbr = 4
make_spec("baz foo ...", 4, 4)
then the returned string (buffer) would be: "d) baz foo foo foo foo"
a) baz foo
b) baz foo foo
c) baz foo foo foo
d) baz foo foo foo foo


Ex. 7: Say I have str = "foo %NUM ..." , ext = 2 and seq_nbr = 4
make_spec("foo %NUM ...", 4, 2)
then the returned string (buffer) would be: "d) foo $real_0 $real_1"
a) foo $integer_0
b) foo $integer_0 $integer_1
c) foo $real_0
d) foo $real_0 $real_1
e) foo $double_0
f) foo $double_0 $double_1


Ex. 8: Say I have str = "foo %NUM ..." , ext = 3 and seq_nbr = 6
make_spec("foo %NUM ...", 6, 3)
then the returned string (buffer) would be: "f) foo $real_0 $real_1 $real_2"
a) foo $integer_0
b) foo $integer_0 $integer_1
c) foo $integer_0 $integer_1 $integer_2
d) foo $real_0
e) foo $real_0 $real_1
f) foo $real_0 $real_1 $real_2
g) foo $double_0
h) foo $double_0 $double_1
i) foo $double_0 $double_1 $double_2


Ex. 9: Say I have str = "foo %NUM ..." , ext = 4 and seq_nbr = 8
make_spec("foo %NUM ...", 8, 4)
then the returned string (buffer) would be: "h) foo $real_0 $real_1 $real_2 $real_3"
a) foo $integer_0
b) foo $integer_0 $integer_1
c) foo $integer_0 $integer_1 $integer_2
d) foo $integer_0 $integer_1 $integer_2 $integer_3
e) foo $real_0
f) foo $real_0 $real_1
g) foo $real_0 $real_1 $real_2
h) foo $real_0 $real_1 $real_2 $real_3
i) foo $double_0
j) foo $double_0 $double_1
k) foo $double_0 $double_1 $double_2
l) foo $double_0 $double_1 $double_2 $double_3

the returned string (buffer) must be NULL terminated in order to be printed.


Ex. 10: make_spec("foo | bar | baz", 2, 3)
then the returned string (buffer) would be: "b) bar"

Ex. 11: make_spec("foo baz ; bar", 1, 3)
then the returned string (buffer) would be: "a) foo baz"

Ex. 12: make_spec("foo [bar]", 2, 3)
then the returned string (buffer) would be: "b) foo bar"

Ex. 13: make_spec("foo %NUM", 2, 3)
then the returned string (buffer) would be: "b) foo $real_0"

Ex. 14: make_spec("foo %NUM !real", 2, 3)
then the returned string (buffer) would be: "b) foo $double_0"

Ex. 15: make_spec("foo \%NUM", 1, 1)
then the returned string (buffer) would be: "foo %NUM"

If you have any question regarding this project U can email me at cvv3@yahoo.com and I'll be glad to answer your questions.
IMPORTANT: due to SPAM filter include 'make_spec' in the suject line of else your email will be rejected by the SPAM-bot.

 


Posted by cppcode at 3:19 PM EST
Updated: Tuesday, 20 January 2009 3:27 PM EST
Post Comment | Permalink | Share This Post

Newer | Latest | Older