Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #44181 > unrolled thread

Parsing exercise in Forth

Started bykrishna.myneni@ccreweb.org
First post2015-12-10 21:08 -0800
Last post2015-12-29 15:22 -0800
Articles 14 on this page of 34 — 13 participants

Back to article view | Back to comp.lang.forth


Contents

  Parsing exercise in Forth krishna.myneni@ccreweb.org - 2015-12-10 21:08 -0800
    Re: Parsing exercise in Forth Raimond Dragomir <raimond.dragomir@gmail.com> - 2015-12-11 00:09 -0800
      Re: Parsing exercise in Forth krishna.myneni@ccreweb.org - 2015-12-11 08:59 -0800
      Re: Parsing exercise in Forth krishna.myneni@ccreweb.org - 2015-12-11 09:02 -0800
    Re: Parsing exercise in Forth Gerard Sontag <gerardsontag@gmail.com> - 2015-12-11 02:28 -0800
      Re: Parsing exercise in Forth krishna.myneni@ccreweb.org - 2015-12-11 09:09 -0800
    Re: Parsing exercise in Forth Pablo Hugo Reda <pabloreda@gmail.com> - 2015-12-11 06:02 -0800
      Re: Parsing exercise in Forth krishna.myneni@ccreweb.org - 2015-12-11 09:12 -0800
    Re: Parsing exercise in Forth "WJ" <w_a_x_man@yahoo.com> - 2015-12-11 17:12 +0000
      Re: Parsing exercise in Forth krishna.myneni@ccreweb.org - 2015-12-11 09:24 -0800
      Re: Parsing exercise in Forth "WJ" <w_a_x_man@yahoo.com> - 2015-12-12 17:41 +0000
    Re: Parsing exercise in Forth John Misciagno <jfmisciagno@gmail.com> - 2015-12-11 15:56 -0800
      Re: Parsing exercise in Forth krishna.myneni@ccreweb.org - 2015-12-11 20:21 -0800
        Re: Parsing exercise in Forth John Misciagno <jfmisciagno@gmail.com> - 2015-12-11 22:21 -0800
    Re: Parsing exercise in Forth Julian Fondren <julian.fondren@gmail.com> - 2015-12-11 19:21 -0800
      Re: Parsing exercise in Forth krishna.myneni@ccreweb.org - 2015-12-11 20:35 -0800
      Re: Parsing exercise in Forth "WJ" <w_a_x_man@yahoo.com> - 2015-12-12 20:08 +0000
        Re: Parsing exercise in Forth Julian Fondren <julian.fondren@gmail.com> - 2015-12-12 17:32 -0800
        Re: Parsing exercise in Forth Albert van der Horst <albert@spenarnc.xs4all.nl> - 2015-12-13 11:21 +0000
          Re: Parsing exercise in Forth Julian Fondren <julian.fondren@gmail.com> - 2015-12-13 05:14 -0800
            Re: Parsing exercise in Forth Albert van der Horst <albert@spenarnc.xs4all.nl> - 2015-12-13 17:33 +0000
    Re: Parsing exercise in Forth humptydumpty <ouatubi@gmail.com> - 2015-12-12 00:14 -0800
      Re: Parsing exercise in Forth humptydumpty <ouatubi@gmail.com> - 2015-12-12 03:48 -0800
      Re: Parsing exercise in Forth krishna.myneni@ccreweb.org - 2015-12-12 14:52 -0800
        Re: Parsing exercise in Forth humptydumpty <ouatubi@gmail.com> - 2015-12-13 01:59 -0800
    Re: Parsing exercise in Forth franck.bensusan@gmail.com - 2015-12-12 02:53 -0800
      Re: Parsing exercise in Forth krishna.myneni@ccreweb.org - 2015-12-12 14:58 -0800
    Re: Parsing exercise in Forth Doug Hoffman <glidedog@gmail.com> - 2015-12-12 10:33 -0500
    Re: Parsing exercise in Forth krishna.myneni@ccreweb.org - 2015-12-12 14:47 -0800
    Re: Parsing exercise in Forth "WJ" <w_a_x_man@yahoo.com> - 2015-12-12 23:13 +0000
    Re: Parsing exercise in Forth humptydumpty <ouatubi@gmail.com> - 2015-12-13 10:28 -0800
    Re: Parsing exercise in Forth Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2015-12-17 17:33 +0000
      Re: Parsing exercise in Forth krishna.myneni@ccreweb.org - 2015-12-23 05:07 -0800
    Re: Parsing exercise in Forth jg <josefg@gmail.com> - 2015-12-29 15:22 -0800

Page 2 of 2 — ← Prev page 1 [2]


#44237

FromAlbert van der Horst <albert@spenarnc.xs4all.nl>
Date2015-12-13 17:33 +0000
Message-ID<566dabeb$0$23777$e4fe514c@news.xs4all.nl>
In reply to#44236
Julian Fondren <julian.fondren@gmail.com> writes:

>On Sunday, December 13, 2015 at 5:21:06 AM UTC-6, Albert van der Horst wrote:
>> WANT class
>>
>> class subshell
>>
>> M: max-occupation  @ M; ,

>This segfaults for me at M: , on a slightly modified lina64 and on a
>raw lina32, both 5.2.1.


Oops. This was a sketchy outline. I expected nobody to run this.
WARNING UNTESTED CODE.

>As does this:

>  want class
>  class foo
>  m: foo  \ Segmentation fault (core dumped)

>Do you have something already loaded, that CLASS expects?

No but class expects an item on the stack during the evaluation
of the example build.

>> I can't beleive how complicated this parsing problem is being made.
>> For the parsing I get by with only the $/ command of my mini-string set.

>$/ is a pretty good factor though :)  Like Mr. Bawd's SKIP and SCAN ,
>if you're not aware of them, who knows what kind of code you'll
>write.

>But I believe that this will segfault when EVALUATE is passed the
>string ( 0 -1 )

>  "1s(2" parse

>And I wonder what precisely will happen when "1s 1s 1s" is given to
>EVALUATE as a result of:

>  "1s 1s 1s(2)" PARSE

Where 1s is selecting an object, the same object would be selected
three times, that wouldn't be too bad.

You'd better not do

" ""sudo rm -rf /"" SYSTEM " PARSE

I mean where does it stop.

>If I had known good data and just wanted to process it, a parser like
>this would be perfectly fine.  And, there really is a lot of data
>like this in the real world: it's called machine-generated data.

Right.

>-- Julian
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

[toc] | [prev] | [next] | [standalone]


#44210

Fromhumptydumpty <ouatubi@gmail.com>
Date2015-12-12 00:14 -0800
Message-ID<70340b88-ef54-4d04-9598-842a6f79cdf8@googlegroups.com>
In reply to#44181
vineri, 11 decembrie 2015, 05:08:34 UTC, krishna...@ccreweb.org a scris:
> For practice, try your hand at this parsing and string field validation exercise, which will actually be useful in an application I am writing. It shouldn't be too difficult, although I haven't done this myself, yet. Solutions in Forth-like languages are welcome, if you want to show off your language, but I'm interested in standard Forth solutions.
> 
> PROBLEM: Parse a string specifying the electron configuration of an atom into a array of the number of electrons within each subshell. Validate the string fields.
> 
> Example 1: The electron configuration for the lowest energy state of the neutral boron atom may be specified by the following string:
> 
> s" 1s(2) 2s(2) 2p(1)"
> 
> meaning there are two electrons in the "1s" (sub)shell, two electrons in the "2s" subshell, and one electron in the "2p" subshell. Therefore there are three array entries:
> 
>   Array Index  Entry
>   ------------------
>     0          2
>     1          2
>     2          1
> 
> Storage of the electrons numbers into the array depend on the mapping between subshell and array index, as follows:
> 
> Index  Subshell
>   0     "1s"
>   1     "2s"
>   2     "2p"
>   3     "3s"
>   4     "3p"
>   5     "3d"
>   6     "4s"
>   7     "4p"
>   8     "4d"
>   9     "4f"
> 
> It's not necessary to go beyond these ten subshells, at this point. Any subshell specifiers not in the above list should return an error, indicating an invalid entry.
> 
> Configuration string properties:
> 
> 1) The order of entry of the subshells in the string shouldn't matter. Any subshells that are not specified in the string imply that the number of electrons in that subshell is zero, and the array entries should be zeroed (usually by initialization, prior to parsing the configuration string).
> 
> 2) Subshells with a different identifier, "spdf", in the second character have a maximum number of electrons, given by
> 
>    s  2
>    p  6
>    d 10
>    f 14
> 
> Therefore, a subshell entry such as "1s(3)" or "2p(7)" are invalid entries, and should return an error. A negative number of electrons is also not permitted, but zero is a valid count, e.g. "2p(0)", although unnecessary per 1).
> 
> 3) Whitespace between the subshell fields is not required, so a string such as s" 1s(1)2p(1)" is valid.
> 
> Example 2:  s" 1s(2)2s(1)3p(1)"  valid entry which results in the following array elements:
> 
>   Index  Element
>   ---------------
>     0     2
>     1     1
>     2     0
>     3     0
>     4     1
> (subsequent elements are all zero)
> 
> Example 3: s" 1s(2)2s(2)2d(1)"  ** invalid entry, no subshell "2d"
> 
> Example 4: s" 1s(2) 2s(2) 3s(4)" ** invalid entry, s subshell can't have 4 electrons.
> 
> 
> Cheers,
> Krishna

Hi!

I hope it will serve you well :)

\ klf.fs  ELECTRONS OF ATOM PARSER
\       ELECTRONS STRUCTURE IS STORED INTO A MATRIX
\       PUT IT INTO A KEYBOARD LOOP AND HAVE AN INTERACTIVE INPUT
\               WITH STOP AT FAILURE POINT!
\       ---
\       USER PROGRAMMER INTERFACE:
\               INIT-PARSER ( -- )
\               DISPATCH    ( C -- )
\       ---
\       DEVELOPED AND TESTED UNDER GFORTH 0.7.3
\

DECIMAL
        VARIABLE L      CREATE LS S" SPDF" STRING,      \ NUMBER LEVEL
        VARIABLE N      CREATE NS S" 1234" STRING,      \ LETTER LEVEL
        VARIABLE E                                      \ ELEMENT-ARRAY POINTER
        VARIABLE X                                      \ NUMBER OF ELECTRONS
        VARIABLE S                                      \ STATE
        
        LS C@ NS C@ *   CONSTANT E#                     \ ATOM ELECTRONS MATRIX
: .E            ( EA --  ;PRINT ATOM ELECTRONS )
        E# 0
        DO   
                I [ LS C@ ]L /MOD
                NS 1+ + C@ EMIT
                LS 1+ + C@ EMIT
                DUP I + C@
                S>D <# [CHAR] ) HOLD #S [CHAR] ( HOLD #> TYPE
        LOOP    DROP
;

        1000 CONSTANT SIGINVALID
: SIGPARSERR    
        SIGINVALID THROW
;

: C2I           ( C CA U -- I  ; CHAR TO INDEX )
        0 DO    
                2DUP I + C@ =
                IF 2DROP I UNLOOP EXIT THEN
        LOOP
        SIGPARSERR
; 
: N-CASE        ( C -- )
        DUP
        NS COUNT C2I N !
        1 S +!  
        EMIT
;       
: L-CASE        ( C -- )
        DUP  
        \ IF NUMBER OF LEVELS > 4
        LS 1+  N @ 1+  [ LS C@ ]L MIN  C2I L !
        1 S +!
        EMIT
;
: (-CASE                ( C -- )
        DUP [CHAR] ( =
        IF   X OFF 1 S +!
        ELSE SIGPARSERR
        THEN EMIT 
;
        CREATE MX               \ MAXIMUM #ELECTRONS MATRIX
        2 C, 0 C,  0 C,  0 C,
        2 C, 6 C,  0 C,  0 C, 
        2 C, 6 C, 10 C,  0 C,
        2 C, 6 C, 10 C, 16 C,

: INDEX         ( -- I )
        N @ [ LS C@ ]L * L @ +
;
: X?            ( -- F )
        N @ 4 MIN               \ IF NUMBER OF LEVELS > 4
        [ LS C@ ]L * L @ +      ( I )
        MX + C@ 1+              ( I LIM )
        X @ 0 ROT WITHIN        ( F )
;
: STORE         ( -- )
        X?
        IF   INDEX E @ + X @ SWAP C!
        ELSE SIGPARSERR
        THEN
;
: )-CASE        ( C -- )
        DUP [CHAR] ) =
        IF   STORE  S OFF
        ELSE SIGPARSERR
        THEN EMIT
;
: X-CASE                ( C -- )
        DUP DIGIT?
        IF
                X @ BASE @ * + X !
                X? 0=
                        IF SIGPARSERR THEN
                1 S +!  EMIT
        ELSE
                DUP [CHAR] ) =
                IF   )-CASE
                ELSE SIGPARSERR
                THEN
        THEN
;


\ --- USER PROGAMMER INTERFACE

        CREATE STATES
        ' N-CASE , ' L-CASE , ' (-CASE , ' X-CASE , ' X-CASE , ' )-CASE ,
: DISPATCH      ( C -- )
        S @ CELLS STATES + @
        CATCH THROW
;
: INIT-PARSER
        E ! E @ E# 0 FILL
        S OFF
;


1 [IF]

\ --- USAGE

: E!            ( CA U EA -- )
        INIT-PARSER
        2DUP TYPE CR
        BOUNDS ?DO
                I C@ DUP BL <>
                IF   TOUPPER DISPATCH
                ELSE DROP
                THEN
        LOOP
;
        CREATE NE E# ALLOT      \ NEON ATOM
        CR ." RESILIENCE FOR MISPLACED BLANKS." CR CR
        S" 1S(2) 2S(2)2 P (6)"   NE ' E! CATCH DROP CR
        NE .E CR .S CR
        CR ." ANOMALIES: STOPS AT FAILURE POINT." CR CR
        S" xS(2)2S(2)2P(6)"   NE ' E! CATCH DROP CR
        S" 1S(2)2F(2)2P(6)"   NE ' E! CATCH DROP CR
        S" 1S(2)2S(22)2P(6)"  NE ' E! CATCH DROP CR
        S" 1S(2)2S(2]2P(6)"   NE ' E! CATCH DROP CR
        S" 1S(2)2S(2)4F(011)"   NE ' E! CATCH DROP CR
[THEN]



At action:

bocmp$ gforth klf.fs -e bye

RESILIENCE FOR MISPLACED BLANKS.

1S(2) 2S(2)2 P (6)
1S(2)2S(2)2P(6)
1S(2)1P(0)1D(0)1F(0)2S(2)2P(6)2D(0)2F(0)3S(0)3P(0)3D(0)3F(0)4S(0)4P(0)4D(0)4F(0)
<0> 

ANOMALIES: STOPS AT FAILURE POINT.

xS(2)2S(2)2P(6)

1S(2)2F(2)2P(6)
1S(2)2
1S(2)2S(22)2P(6)
1S(2)2S(2
1S(2)2S(2]2P(6)
1S(2)2S(2
1S(2)2S(2)4F(011)
1S(2)2S(2)4F(01
bocmp$ 

Have a nice day,
humptydumpty

[toc] | [prev] | [next] | [standalone]


#44212

Fromhumptydumpty <ouatubi@gmail.com>
Date2015-12-12 03:48 -0800
Message-ID<0016a99e-3da6-45f9-9150-30e0da4e7fbe@googlegroups.com>
In reply to#44210
sâmbătă, 12 decembrie 2015, 08:14:42 UTC, humptydumpty a scris:
> vineri, 11 decembrie 2015, 05:08:34 UTC, krishna...@ccreweb.org a scris:
> > For practice, try your hand at this parsing and string field validation exercise, which will actually be useful in an application I am writing. It shouldn't be too difficult, although I haven't done this myself, yet. Solutions in Forth-like languages are welcome, if you want to show off your language, but I'm interested in standard Forth solutions.
> > 
> > PROBLEM: Parse a string specifying the electron configuration of an atom into a array of the number of electrons within each subshell. Validate the string fields.
> > 
> > Example 1: The electron configuration for the lowest energy state of the neutral boron atom may be specified by the following string:
> > 
> > s" 1s(2) 2s(2) 2p(1)"
> > 
> > meaning there are two electrons in the "1s" (sub)shell, two electrons in the "2s" subshell, and one electron in the "2p" subshell. Therefore there are three array entries:
> > 
> >   Array Index  Entry
> >   ------------------
> >     0          2
> >     1          2
> >     2          1
> > 
> > Storage of the electrons numbers into the array depend on the mapping between subshell and array index, as follows:
> > 
> > Index  Subshell
> >   0     "1s"
> >   1     "2s"
> >   2     "2p"
> >   3     "3s"
> >   4     "3p"
> >   5     "3d"
> >   6     "4s"
> >   7     "4p"
> >   8     "4d"
> >   9     "4f"
> > 
> > It's not necessary to go beyond these ten subshells, at this point. Any subshell specifiers not in the above list should return an error, indicating an invalid entry.
> > 
> > Configuration string properties:
> > 
> > 1) The order of entry of the subshells in the string shouldn't matter. Any subshells that are not specified in the string imply that the number of electrons in that subshell is zero, and the array entries should be zeroed (usually by initialization, prior to parsing the configuration string).
> > 
> > 2) Subshells with a different identifier, "spdf", in the second character have a maximum number of electrons, given by
> > 
> >    s  2
> >    p  6
> >    d 10
> >    f 14
> > 
> > Therefore, a subshell entry such as "1s(3)" or "2p(7)" are invalid entries, and should return an error. A negative number of electrons is also not permitted, but zero is a valid count, e.g. "2p(0)", although unnecessary per 1).
> > 
> > 3) Whitespace between the subshell fields is not required, so a string such as s" 1s(1)2p(1)" is valid.
> > 
> > Example 2:  s" 1s(2)2s(1)3p(1)"  valid entry which results in the following array elements:
> > 
> >   Index  Element
> >   ---------------
> >     0     2
> >     1     1
> >     2     0
> >     3     0
> >     4     1
> > (subsequent elements are all zero)
> > 
> > Example 3: s" 1s(2)2s(2)2d(1)"  ** invalid entry, no subshell "2d"
> > 
> > Example 4: s" 1s(2) 2s(2) 3s(4)" ** invalid entry, s subshell can't have 4 electrons.
> > 
> > 
> > Cheers,
> > Krishna
> 
> Hi!
> 
> I hope it will serve you well :)
> 
> \ klf.fs  ELECTRONS OF ATOM PARSER
> \       ELECTRONS STRUCTURE IS STORED INTO A MATRIX
> \       PUT IT INTO A KEYBOARD LOOP AND HAVE AN INTERACTIVE INPUT
> \               WITH STOP AT FAILURE POINT!
> \       ---
> \       USER PROGRAMMER INTERFACE:
> \               INIT-PARSER ( -- )
> \               DISPATCH    ( C -- )
> \       ---
> \       DEVELOPED AND TESTED UNDER GFORTH 0.7.3
> \
> 
> DECIMAL
>         VARIABLE L      CREATE LS S" SPDF" STRING,      \ NUMBER LEVEL
>         VARIABLE N      CREATE NS S" 1234" STRING,      \ LETTER LEVEL
>         VARIABLE E                                      \ ELEMENT-ARRAY POINTER
>         VARIABLE X                                      \ NUMBER OF ELECTRONS
>         VARIABLE S                                      \ STATE
>         
>         LS C@ NS C@ *   CONSTANT E#                     \ ATOM ELECTRONS MATRIX
> : .E            ( EA --  ;PRINT ATOM ELECTRONS )
>         E# 0
>         DO   
>                 I [ LS C@ ]L /MOD
>                 NS 1+ + C@ EMIT
>                 LS 1+ + C@ EMIT
>                 DUP I + C@
>                 S>D <# [CHAR] ) HOLD #S [CHAR] ( HOLD #> TYPE
>         LOOP    DROP
> ;
> 
>         1000 CONSTANT SIGINVALID
> : SIGPARSERR    
>         SIGINVALID THROW
> ;
> 
> : C2I           ( C CA U -- I  ; CHAR TO INDEX )
>         0 DO    
>                 2DUP I + C@ =
>                 IF 2DROP I UNLOOP EXIT THEN
>         LOOP
>         SIGPARSERR
> ; 
> : N-CASE        ( C -- )
>         DUP
>         NS COUNT C2I N !
>         1 S +!  
>         EMIT
> ;       
> : L-CASE        ( C -- )
>         DUP  
>         \ IF NUMBER OF LEVELS > 4
>         LS 1+  N @ 1+  [ LS C@ ]L MIN  C2I L !
>         1 S +!
>         EMIT
> ;
> : (-CASE                ( C -- )
>         DUP [CHAR] ( =
>         IF   X OFF 1 S +!
>         ELSE SIGPARSERR
>         THEN EMIT 
> ;
>         CREATE MX               \ MAXIMUM #ELECTRONS MATRIX
>         2 C, 0 C,  0 C,  0 C,
>         2 C, 6 C,  0 C,  0 C, 
>         2 C, 6 C, 10 C,  0 C,
>         2 C, 6 C, 10 C, 16 C,
> 
> : INDEX         ( -- I )
>         N @ [ LS C@ ]L * L @ +
> ;
> : X?            ( -- F )
>         N @ 4 MIN               \ IF NUMBER OF LEVELS > 4
>         [ LS C@ ]L * L @ +      ( I )
>         MX + C@ 1+              ( I LIM )
>         X @ 0 ROT WITHIN        ( F )
> ;
> : STORE         ( -- )
>         X?
>         IF   INDEX E @ + X @ SWAP C!
>         ELSE SIGPARSERR
>         THEN
> ;
> : )-CASE        ( C -- )
>         DUP [CHAR] ) =
>         IF   STORE  S OFF
>         ELSE SIGPARSERR
>         THEN EMIT
> ;
> : X-CASE                ( C -- )
>         DUP DIGIT?
>         IF
>                 X @ BASE @ * + X !
>                 X? 0=
>                         IF SIGPARSERR THEN
>                 1 S +!  EMIT
>         ELSE
>                 DUP [CHAR] ) =
>                 IF   )-CASE
>                 ELSE SIGPARSERR
>                 THEN
>         THEN
> ;
> 
> 
> \ --- USER PROGAMMER INTERFACE
> 
>         CREATE STATES
>         ' N-CASE , ' L-CASE , ' (-CASE , ' X-CASE , ' X-CASE , ' )-CASE ,
> : DISPATCH      ( C -- )
>         S @ CELLS STATES + @
>         CATCH THROW
> ;
> : INIT-PARSER
>         E ! E @ E# 0 FILL
>         S OFF
> ;
> 
> 
> 1 [IF]
> 
> \ --- USAGE
> 
> : E!            ( CA U EA -- )
>         INIT-PARSER
>         2DUP TYPE CR
>         BOUNDS ?DO
>                 I C@ DUP BL <>
>                 IF   TOUPPER DISPATCH
>                 ELSE DROP
>                 THEN
>         LOOP
> ;
>         CREATE NE E# ALLOT      \ NEON ATOM
>         CR ." RESILIENCE FOR MISPLACED BLANKS." CR CR
>         S" 1S(2) 2S(2)2 P (6)"   NE ' E! CATCH DROP CR
>         NE .E CR .S CR
>         CR ." ANOMALIES: STOPS AT FAILURE POINT." CR CR
>         S" xS(2)2S(2)2P(6)"   NE ' E! CATCH DROP CR
>         S" 1S(2)2F(2)2P(6)"   NE ' E! CATCH DROP CR
>         S" 1S(2)2S(22)2P(6)"  NE ' E! CATCH DROP CR
>         S" 1S(2)2S(2]2P(6)"   NE ' E! CATCH DROP CR
>         S" 1S(2)2S(2)4F(011)"   NE ' E! CATCH DROP CR
> [THEN]
> 
> 
> 
> At action:
> 
> bocmp$ gforth klf.fs -e bye
> 
> RESILIENCE FOR MISPLACED BLANKS.
> 
> 1S(2) 2S(2)2 P (6)
> 1S(2)2S(2)2P(6)
> 1S(2)1P(0)1D(0)1F(0)2S(2)2P(6)2D(0)2F(0)3S(0)3P(0)3D(0)3F(0)4S(0)4P(0)4D(0)4F(0)
> <0> 
> 
> ANOMALIES: STOPS AT FAILURE POINT.
> 
> xS(2)2S(2)2P(6)
> 
> 1S(2)2F(2)2P(6)
> 1S(2)2
> 1S(2)2S(22)2P(6)
> 1S(2)2S(2
> 1S(2)2S(2]2P(6)
> 1S(2)2S(2
> 1S(2)2S(2)4F(011)
> 1S(2)2S(2)4F(01
> bocmp$ 
> 
> Have a nice day,
> humptydumpty


> : X-CASE                ( C -- )
>         DUP DIGIT?
>         IF
>                 X @ BASE @ * + X !
>                 X? 0=
>                         IF SIGPARSERR THEN
>                 1 S +!  EMIT
>         ELSE
>                 DUP [CHAR] ) =
>                 IF   )-CASE
>                 ELSE SIGPARSERR
>                 THEN
>         THEN
> ;

could be written shorter :)

: X-CASE                ( C -- )
        DUP DIGIT?
        IF
                X @ BASE @ * + X !
                X? 0=
                        IF SIGPARSERR THEN
                1 S +!  EMIT
        ELSE
                )-CASE
        THEN
;

humptydumpty

[toc] | [prev] | [next] | [standalone]


#44225

Fromkrishna.myneni@ccreweb.org
Date2015-12-12 14:52 -0800
Message-ID<0e67c253-c287-4773-9200-ed23fa1c660a@googlegroups.com>
In reply to#44210
On Saturday, December 12, 2015 at 2:14:42 AM UTC-6, humptydumpty wrote:
> vineri, 11 decembrie 2015, 05:08:34 UTC, krishna...@ccreweb.org a scris:
> > For practice, try your hand at this parsing and string field validation exercise, which will actually be useful in an application I am writing. It shouldn't be too difficult, although I haven't done this myself, yet. Solutions in Forth-like languages are welcome, if you want to show off your language, but I'm interested in standard Forth solutions.
> > 
> > PROBLEM: Parse a string specifying the electron configuration of an atom into a array of the number of electrons within each subshell. Validate the string fields.
> > 
> > Example 1: The electron configuration for the lowest energy state of the neutral boron atom may be specified by the following string:
> > 
> > s" 1s(2) 2s(2) 2p(1)"
> > 
> > meaning there are two electrons in the "1s" (sub)shell, two electrons in the "2s" subshell, and one electron in the "2p" subshell. Therefore there are three array entries:
> > 
> >   Array Index  Entry
> >   ------------------
> >     0          2
> >     1          2
> >     2          1
> > 
> > Storage of the electrons numbers into the array depend on the mapping between subshell and array index, as follows:
> > 
> > Index  Subshell
> >   0     "1s"
> >   1     "2s"
> >   2     "2p"
> >   3     "3s"
> >   4     "3p"
> >   5     "3d"
> >   6     "4s"
> >   7     "4p"
> >   8     "4d"
> >   9     "4f"
> > 
> > It's not necessary to go beyond these ten subshells, at this point. Any subshell specifiers not in the above list should return an error, indicating an invalid entry.
> > 
> > Configuration string properties:
> > 
> > 1) The order of entry of the subshells in the string shouldn't matter. Any subshells that are not specified in the string imply that the number of electrons in that subshell is zero, and the array entries should be zeroed (usually by initialization, prior to parsing the configuration string).
> > 
> > 2) Subshells with a different identifier, "spdf", in the second character have a maximum number of electrons, given by
> > 
> >    s  2
> >    p  6
> >    d 10
> >    f 14
> > 
> > Therefore, a subshell entry such as "1s(3)" or "2p(7)" are invalid entries, and should return an error. A negative number of electrons is also not permitted, but zero is a valid count, e.g. "2p(0)", although unnecessary per 1).
> > 
> > 3) Whitespace between the subshell fields is not required, so a string such as s" 1s(1)2p(1)" is valid.
> > 
> > Example 2:  s" 1s(2)2s(1)3p(1)"  valid entry which results in the following array elements:
> > 
> >   Index  Element
> >   ---------------
> >     0     2
> >     1     1
> >     2     0
> >     3     0
> >     4     1
> > (subsequent elements are all zero)
> > 
> > Example 3: s" 1s(2)2s(2)2d(1)"  ** invalid entry, no subshell "2d"
> > 
> > Example 4: s" 1s(2) 2s(2) 3s(4)" ** invalid entry, s subshell can't have 4 electrons.
> > 
> > 
> > Cheers,
> > Krishna
> 
> Hi!
> 
> I hope it will serve you well :)
> 
> \ klf.fs  ELECTRONS OF ATOM PARSER
...
> 
> bocmp$ gforth klf.fs -e bye
> 
> RESILIENCE FOR MISPLACED BLANKS.
> 
> 1S(2) 2S(2)2 P (6)
> 1S(2)2S(2)2P(6)
> 1S(2)1P(0)1D(0)1F(0)2S(2)2P(6)2D(0)2F(0)3S(0)3P(0)3D(0)3F(0)4S(0)4P(0)4D(0)4F(0)
> <0> 
> 
> ANOMALIES: STOPS AT FAILURE POINT.
> 
> xS(2)2S(2)2P(6)
> 
> 1S(2)2F(2)2P(6)
> 1S(2)2
> 1S(2)2S(22)2P(6)
> 1S(2)2S(2
> 1S(2)2S(2]2P(6)
> 1S(2)2S(2
> 1S(2)2S(2)4F(011)
> 1S(2)2S(2)4F(01
> bocmp$ 
> 
> Have a nice day,
> humptydumpty

Thanks, H.D. I will try it out. Incidentally, I had to make use of the FSL module, quadratic.x within the app for which the parser code is intended. We worked on that module together many years ago!

Cheers,
Krishna

[toc] | [prev] | [next] | [standalone]


#44232

Fromhumptydumpty <ouatubi@gmail.com>
Date2015-12-13 01:59 -0800
Message-ID<be8dd885-2471-48ce-91b9-d71a1fd0b81c@googlegroups.com>
In reply to#44225
sâmbătă, 12 decembrie 2015, 22:52:34 UTC, krishna...@ccreweb.org a scris:
> On Saturday, December 12, 2015 at 2:14:42 AM UTC-6, humptydumpty wrote:
> > vineri, 11 decembrie 2015, 05:08:34 UTC, krishna...@ccreweb.org a scris:
> > > For practice, try your hand at this parsing and string field validation exercise, which will actually be useful in an application I am writing. It shouldn't be too difficult, although I haven't done this myself, yet. Solutions in Forth-like languages are welcome, if you want to show off your language, but I'm interested in standard Forth solutions.
> > > 
> > > PROBLEM: Parse a string specifying the electron configuration of an atom into a array of the number of electrons within each subshell. Validate the string fields.
> > > 
> > > Example 1: The electron configuration for the lowest energy state of the neutral boron atom may be specified by the following string:
> > > 
> > > s" 1s(2) 2s(2) 2p(1)"
> > > 
> > > meaning there are two electrons in the "1s" (sub)shell, two electrons in the "2s" subshell, and one electron in the "2p" subshell. Therefore there are three array entries:
> > > 
> > >   Array Index  Entry
> > >   ------------------
> > >     0          2
> > >     1          2
> > >     2          1
> > > 
> > > Storage of the electrons numbers into the array depend on the mapping between subshell and array index, as follows:
> > > 
> > > Index  Subshell
> > >   0     "1s"
> > >   1     "2s"
> > >   2     "2p"
> > >   3     "3s"
> > >   4     "3p"
> > >   5     "3d"
> > >   6     "4s"
> > >   7     "4p"
> > >   8     "4d"
> > >   9     "4f"
> > > 
> > > It's not necessary to go beyond these ten subshells, at this point. Any subshell specifiers not in the above list should return an error, indicating an invalid entry.
> > > 
> > > Configuration string properties:
> > > 
> > > 1) The order of entry of the subshells in the string shouldn't matter. Any subshells that are not specified in the string imply that the number of electrons in that subshell is zero, and the array entries should be zeroed (usually by initialization, prior to parsing the configuration string).
> > > 
> > > 2) Subshells with a different identifier, "spdf", in the second character have a maximum number of electrons, given by
> > > 
> > >    s  2
> > >    p  6
> > >    d 10
> > >    f 14
> > > 
> > > Therefore, a subshell entry such as "1s(3)" or "2p(7)" are invalid entries, and should return an error. A negative number of electrons is also not permitted, but zero is a valid count, e.g. "2p(0)", although unnecessary per 1).
> > > 
> > > 3) Whitespace between the subshell fields is not required, so a string such as s" 1s(1)2p(1)" is valid.
> > > 
> > > Example 2:  s" 1s(2)2s(1)3p(1)"  valid entry which results in the following array elements:
> > > 
> > >   Index  Element
> > >   ---------------
> > >     0     2
> > >     1     1
> > >     2     0
> > >     3     0
> > >     4     1
> > > (subsequent elements are all zero)
> > > 
> > > Example 3: s" 1s(2)2s(2)2d(1)"  ** invalid entry, no subshell "2d"
> > > 
> > > Example 4: s" 1s(2) 2s(2) 3s(4)" ** invalid entry, s subshell can't have 4 electrons.
> > > 
> > > 
> > > Cheers,
> > > Krishna
> > 
> > Hi!
> > 
> > I hope it will serve you well :)
> > 
> > \ klf.fs  ELECTRONS OF ATOM PARSER
> ...
> > 
> > bocmp$ gforth klf.fs -e bye
> > 
> > RESILIENCE FOR MISPLACED BLANKS.
> > 
> > 1S(2) 2S(2)2 P (6)
> > 1S(2)2S(2)2P(6)
> > 1S(2)1P(0)1D(0)1F(0)2S(2)2P(6)2D(0)2F(0)3S(0)3P(0)3D(0)3F(0)4S(0)4P(0)4D(0)4F(0)
> > <0> 
> > 
> > ANOMALIES: STOPS AT FAILURE POINT.
> > 
> > xS(2)2S(2)2P(6)
> > 
> > 1S(2)2F(2)2P(6)
> > 1S(2)2
> > 1S(2)2S(22)2P(6)
> > 1S(2)2S(2
> > 1S(2)2S(2]2P(6)
> > 1S(2)2S(2
> > 1S(2)2S(2)4F(011)
> > 1S(2)2S(2)4F(01
> > bocmp$ 
> > 
> > Have a nice day,
> > humptydumpty
> 
> Thanks, H.D. I will try it out. Incidentally, I had to make use of the FSL module, quadratic.x within the app for which the parser code is intended. We worked on that module together many years ago!
> 
> Cheers,
> Krishna

Hi!

You welcome! I remember, I'm glad of that! :)
If I could help somehow, I'll do it (in my limited way).

Have a nice day,
humptydumpty

[toc] | [prev] | [next] | [standalone]


#44211

Fromfranck.bensusan@gmail.com
Date2015-12-12 02:53 -0800
Message-ID<064bd2f7-fa8d-4592-b783-6c91a4296c48@googlegroups.com>
In reply to#44181
Le vendredi 11 décembre 2015 06:08:34 UTC+1, krishna...@ccreweb.org a écrit :
> For practice, try your hand at this parsing and string field validation exercise, which will actually be useful in an application I am writing. It shouldn't be too difficult, although I haven't done this myself, yet. Solutions in Forth-like languages are welcome, if you want to show off your language, but I'm interested in standard Forth solutions.


Hi,

Here is a solution in Oforth : 


-- List of valid subshells with limit
[ [ "1s",2 ], ["2s",2], ["2p",6], ["3s", 2], ["3p", 6] ]
[ [ "3d",10], ["4s",2], ["4p",6], ["4d",10], ["4f",14] ] +
   const: SubShells

: shellValue (parsed, shell) -- n
| s |
   -- If the shell is not into the config parsed, result is 0
   shell first dup ->s parsed valueAt dup ifNull: [ drop 0 return ]

   -- Checks for errors on value read
   asInteger dup      ifNull: [ s "Not integer"   ExRuntime throw ]
   dup shell second > ifTrue: [ s "Limit reached" ExRuntime throw ]
;

: parseConfig	// s -- [ n ]
| parsed | 
   -- Parse string as a list of pairs : [ ["1s","2"], ["2s","2"], ...]
   wordsWith(')') map(#[ wordsWith('(') ]) filter(#notEmpty) ->parsed

   -- Checks that the first value of each pair is a valid subshell
   #[ first SubShells keyAt notNull ] parsed conform 
      ifFalse: [ "Unknow subshell" Exception throw ]

   -- Creates the list of results for shells into SubShells constant
   #[ parsed shellValue ] SubShells map 
;

----------------------------------------------------------------------
 
Tests : 

"1s(2)2s(a)2p(1)" parseConfig .
[1:interpreter] ExRuntime : Not integer <2s>

"1s(2) 2s(2) 3s(4)" parseConfig .
[1:interpreter] ExRuntime : Limit reached <3s>

"1s(2)2s(2)2d(1)" parseConfig .
[1:interpreter] Exception : Unknow subshell

"1s(2)2s(1)3p(1)" parseConfig .
[2, 1, 0, 0, 1, 0, 0, 0, 0, 0] ok

"1s(2) 2s(1) 3d(5)   4f (10)" parseConfig .
[2, 1, 0, 0, 0, 5, 0, 0, 0, 10] ok

"2s(2) 4f(10) 1s(1)" parseConfig .
[1, 2, 0, 0, 0, 0, 0, 0, 0, 10] ok

Cheers,
Franck

[toc] | [prev] | [next] | [standalone]


#44226

Fromkrishna.myneni@ccreweb.org
Date2015-12-12 14:58 -0800
Message-ID<71fe78b2-a8ae-4e4c-b732-b8b17889d1dd@googlegroups.com>
In reply to#44211
On Saturday, December 12, 2015 at 4:53:40 AM UTC-6, franck....@gmail.com wrote:
> Le vendredi 11 décembre 2015 06:08:34 UTC+1, krishna...@ccreweb.org a écrit :
> > For practice, try your hand at this parsing and string field validation exercise, which will actually be useful in an application I am writing. It shouldn't be too difficult, although I haven't done this myself, yet. Solutions in Forth-like languages are welcome, if you want to show off your language, but I'm interested in standard Forth solutions.
> 
> 
> Hi,
> 
> Here is a solution in Oforth : 
> 
> 
> -- List of valid subshells with limit
> [ [ "1s",2 ], ["2s",2], ["2p",6], ["3s", 2], ["3p", 6] ]
> [ [ "3d",10], ["4s",2], ["4p",6], ["4d",10], ["4f",14] ] +
>    const: SubShells
> 
> : shellValue (parsed, shell) -- n
> | s |
>    -- If the shell is not into the config parsed, result is 0
>    shell first dup ->s parsed valueAt dup ifNull: [ drop 0 return ]
> 
>    -- Checks for errors on value read
>    asInteger dup      ifNull: [ s "Not integer"   ExRuntime throw ]
>    dup shell second > ifTrue: [ s "Limit reached" ExRuntime throw ]
> ;
> 
> : parseConfig	// s -- [ n ]
> | parsed | 
>    -- Parse string as a list of pairs : [ ["1s","2"], ["2s","2"], ...]
>    wordsWith(')') map(#[ wordsWith('(') ]) filter(#notEmpty) ->parsed
> 
>    -- Checks that the first value of each pair is a valid subshell
>    #[ first SubShells keyAt notNull ] parsed conform 
>       ifFalse: [ "Unknow subshell" Exception throw ]
> 
>    -- Creates the list of results for shells into SubShells constant
>    #[ parsed shellValue ] SubShells map 
> ;
> 
> ----------------------------------------------------------------------
>  
> Tests : 
> 
> "1s(2)2s(a)2p(1)" parseConfig .
> [1:interpreter] ExRuntime : Not integer <2s>
> 
> "1s(2) 2s(2) 3s(4)" parseConfig .
> [1:interpreter] ExRuntime : Limit reached <3s>
> 
> "1s(2)2s(2)2d(1)" parseConfig .
> [1:interpreter] Exception : Unknow subshell
> 
> "1s(2)2s(1)3p(1)" parseConfig .
> [2, 1, 0, 0, 1, 0, 0, 0, 0, 0] ok
> 
> "1s(2) 2s(1) 3d(5)   4f (10)" parseConfig .
> [2, 1, 0, 0, 0, 5, 0, 0, 0, 10] ok
> 
> "2s(2) 4f(10) 1s(1)" parseConfig .
> [1, 2, 0, 0, 0, 0, 0, 0, 0, 10] ok
> 
> Cheers,
> Franck

Nice. Good illustration of the utility of list processing.

Thanks,
Krishna

[toc] | [prev] | [next] | [standalone]


#44215

FromDoug Hoffman <glidedog@gmail.com>
Date2015-12-12 10:33 -0500
Message-ID<566c3e66$0$306$14726298@news.sunsite.dk>
In reply to#44181
On 12/11/15 12:08 AM, krishna.myneni@ccreweb.org wrote:
> ... this parsing and string field validation ...
> ... I'm interested in standard Forth solutions.

I have library code that assists with this IMO. Strings and arrays.
There is overhead for the extension but it's only an extra few k or so
compiled. This solution uses Forth primitives, and objects where
they are useful. Dictionary definitions are used to validate subshell
names (via search-wordlist) and also store data for them. The output
arrays are as persistent as you wish: named dictionary lists or
nameless heap objects to be discarded.

-Doug

Works with any 200x Forth.
Needs the FMS-SI (single inheritance) library code located here:
http://soton.mpeforth.com/flag/fms/index.html

include FMS-SI.f
include FMS-SILib.f

: subshell ( idx maxE "name" -- idx+1 )
   create c, dup c, 1+ does> ( -- maxE idx ) dup c@ swap 1+ c@ ;
0
  2 subshell 1s
  2 subshell 2s
  6 subshell 2p
  2 subshell 3s
  6 subshell 3p
10 subshell 3d
  2 subshell 4s
  6 subshell 4p
10 subshell 4d
14 subshell 4f constant n

string+ input
string+ sub$
create bl$ bl c,

: parse-shell ( obj addr len -- ) input !:
   {: obj | idx maxE -- :}  n obj new: 0 obj fill:
    bl$ 1 pad 0 input replall: \ remove all blanks
   begin
     '(' input word: \ find substring prior to '('
   while
     input @sub: 2dup sub$ !: get-current search-wordlist
     0= if cr ." no subshell " sub$ p: exit then
     ( xt ) execute to idx to maxE
     ')' input word: drop 1 input start: +! \ find #elec sub$
     0 0 input @sub: >number 2drop drop dup maxE >
     if drop cr ." too many electrons at " sub$ p: exit then
     ( #elect) idx obj to: \ store in array object
     ')' input skipChars: \ skip sub$, prepare for next parse
   repeat obj p: ; \ lastly print the array

0 [if]
1-array neutral-boron neutral-boron s" 1s(2) 2s(2) 2p(1)" parse-shell
0 2
1 2
2 1
3 0
4 0
5 0
6 0
7 0
8 0
9 0

1-array ex2  ex2 s" 1s(2)2s(1)3p(1)" parse-shell
0 2
1 1
2 0
3 0
4 1
5 0
6 0
7 0
8 0
9 0

1-array ex3  ex3 s" 1s(2)2s(2)2d(1)" parse-shell
no subshell 2d
1-array ex4 ex4 s" 1s(2) 2s(2) 3s(4)" parse-shell
too many electrons at 3s

heap> 1-array dup s" 1s(2) 2s(2) 4f(12)" parse-shell
0 2
1 2
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 12
[then]

[toc] | [prev] | [next] | [standalone]


#44224

Fromkrishna.myneni@ccreweb.org
Date2015-12-12 14:47 -0800
Message-ID<610c7aea-98a8-472c-a20a-a2f21c9edd25@googlegroups.com>
In reply to#44181
On Thursday, December 10, 2015 at 11:08:34 PM UTC-6, krishna...@ccreweb.org wrote:
...

Dear All,

I made the mistake of responding to a troll in this thread. Live and learn. Please don't exacerbate the problem by doing the same.

Thanks,
Krishna

[toc] | [prev] | [next] | [standalone]


#44227

From"WJ" <w_a_x_man@yahoo.com>
Date2015-12-12 23:13 +0000
Message-ID<n4i9mf$u78$1@dont-email.me>
In reply to#44181
krishna.myneni wrote:

> PROBLEM: Parse a string specifying the electron configuration
> of an atom into a array of the number of electrons within each
> subshell. Validate the string fields.
> 
> Example 1: The electron configuration for the lowest energy
> state of the neutral boron atom may be specified by the
> following string:
> 
> s" 1s(2) 2s(2) 2p(1)"
> 
> meaning there are two electrons in the "1s" (sub)shell, two
> electrons in the "2s" subshell, and one electron in the "2p"
> subshell. Therefore there are three array entries:
> 
>   Array Index  Entry
>   ------------------
>     0          2
>     1          2
>     2          1
> 
> Storage of the electrons numbers into the array depend on the
> mapping between subshell and array index, as follows:
> 
> Index  Subshell
>   0     "1s"
>   1     "2s"
>   2     "2p"
>   3     "3s"
>   4     "3p"
>   5     "3d"
>   6     "4s"
>   7     "4p"
>   8     "4d"
>   9     "4f"
> 
> It's not necessary to go beyond these ten subshells, at this
> point. Any subshell specifiers not in the above list should
> return an error, indicating an invalid entry.
> 
> Configuration string properties:
> 
> 1) The order of entry of the subshells in the string shouldn't
> matter. Any subshells that are not specified in the string
> imply that the number of electrons in that subshell is zero,
> and the array entries should be zeroed (usually by
> initialization, prior to parsing the configuration string).
> 
> 2) Subshells with a different identifier, "spdf", in the
> second character have a maximum number of electrons, given by
> 
>    s  2
>    p  6
>    d 10
>    f 14
> 
> Therefore, a subshell entry such as "1s(3)" or "2p(7)" are
> invalid entries, and should return an error. A negative number
> of electrons is also not permitted, but zero is a valid count,
> e.g. "2p(0)", although unnecessary per 1).
> 
> 3) Whitespace between the subshell fields is not required,
> so a string such as s" 1s(1)2p(1)" is valid.
> 
> Example 2:  s" 1s(2)2s(1)3p(1)"  valid entry which results in
> the following array elements:
> 
>   Index  Element
>   ---------------
>     0     2
>     1     1
>     2     0
>     3     0
>     4     1
> (subsequent elements are all zero)
> 
> Example 3: s" 1s(2)2s(2)2d(1)"  ** invalid entry, no subshell "2d"
> 
> Example 4: s" 1s(2) 2s(2) 3s(4)" ** invalid entry, s subshell
> can't have 4 electrons.

Ocaml:

#load "str.cma";;
open Str;;

let subshells = "1s 2s 2p 3s 3p 3d 4s 4p 4d 4f"
  |> split (regexp " ") |> List.mapi (fun i s->(s,i));;

let limits = ["s", 2; "p", 6; "d", 10; "f", 14];;

let parse_config string =
  let result = Array.make 10 0 in
  string |> global_replace (regexp "[ \t]") ""
  |> split (regexp "[()]") |>
  (let rec loop = function
      [] -> ()
    | [_] -> failwith "Odd number of items."
    | k::v::rest -> 
        let count = int_of_string v in
          if count > List.assoc (String.sub k 1 1) limits then
            failwith (Printf.sprintf "%d too large for %s" count k);
          (try  result.( List.assoc k subshells ) <- count
           with  Not_found -> failwith ("Invalid shell: " ^ k));
          loop rest
  in loop) ;
  result
  ;;

parse_config "1s(2)2s(1)3p(1)";;

[|2; 1; 0; 0; 1; 0; 0; 0; 0; 0|]

parse_config " 1s (2) 2    s ( 1  )3   p(1)  ";;

[|2; 1; 0; 0; 1; 0; 0; 0; 0; 0|]

parse_config "1s(2)2s(2)2d(1)";;

Exception: Failure "Invalid shell: 2d".

parse_config " 1s(2) 2s(2) 3s(4)";;

Exception: Failure "4 too large for 3s".

parse_config "1s(2)2s(1)3p";;

Exception: Failure "Odd number of items.".

[toc] | [prev] | [next] | [standalone]


#44238

Fromhumptydumpty <ouatubi@gmail.com>
Date2015-12-13 10:28 -0800
Message-ID<744f7ce4-dbe8-4487-b71a-e4c2d14faa5d@googlegroups.com>
In reply to#44181
vineri, 11 decembrie 2015, 05:08:34 UTC, krishna...@ccreweb.org a scris:
> For practice, try your hand at this parsing and string field validation exercise, which will actually be useful in an application I am writing. It shouldn't be too difficult, although I haven't done this myself, yet. Solutions in Forth-like languages are welcome, if you want to show off your language, but I'm interested in standard Forth solutions.
> 
> PROBLEM: Parse a string specifying the electron configuration of an atom into a array of the number of electrons within each subshell. Validate the string fields.
> 
> Example 1: The electron configuration for the lowest energy state of the neutral boron atom may be specified by the following string:
> 
> s" 1s(2) 2s(2) 2p(1)"
> 
> meaning there are two electrons in the "1s" (sub)shell, two electrons in the "2s" subshell, and one electron in the "2p" subshell. Therefore there are three array entries:
> 
>   Array Index  Entry
>   ------------------
>     0          2
>     1          2
>     2          1
> 
> Storage of the electrons numbers into the array depend on the mapping between subshell and array index, as follows:
> 
> Index  Subshell
>   0     "1s"
>   1     "2s"
>   2     "2p"
>   3     "3s"
>   4     "3p"
>   5     "3d"
>   6     "4s"
>   7     "4p"
>   8     "4d"
>   9     "4f"
> 
> It's not necessary to go beyond these ten subshells, at this point. Any subshell specifiers not in the above list should return an error, indicating an invalid entry.
> 
> Configuration string properties:
> 
> 1) The order of entry of the subshells in the string shouldn't matter. Any subshells that are not specified in the string imply that the number of electrons in that subshell is zero, and the array entries should be zeroed (usually by initialization, prior to parsing the configuration string).
> 
> 2) Subshells with a different identifier, "spdf", in the second character have a maximum number of electrons, given by
> 
>    s  2
>    p  6
>    d 10
>    f 14
> 
> Therefore, a subshell entry such as "1s(3)" or "2p(7)" are invalid entries, and should return an error. A negative number of electrons is also not permitted, but zero is a valid count, e.g. "2p(0)", although unnecessary per 1).
> 
> 3) Whitespace between the subshell fields is not required, so a string such as s" 1s(1)2p(1)" is valid.
> 
> Example 2:  s" 1s(2)2s(1)3p(1)"  valid entry which results in the following array elements:
> 
>   Index  Element
>   ---------------
>     0     2
>     1     1
>     2     0
>     3     0
>     4     1
> (subsequent elements are all zero)
> 
> Example 3: s" 1s(2)2s(2)2d(1)"  ** invalid entry, no subshell "2d"
> 
> Example 4: s" 1s(2) 2s(2) 3s(4)" ** invalid entry, s subshell can't have 4 electrons.
> 
> 
> Cheers,
> Krishna

Hi!

First revision here:

\ klf.fs  ELECTRONS OF ATOM PARSER
\       ELECTRONS STRUCTURE IS STORED INTO A MATRIX
\       PUT IT INTO A KEYBOARD LOOP AND HAVE AN INTERACTIVE INPUT
\               WITH STOP AT FAILURE POINT!
\       ---
\       USER PROGRAMMER INTERFACE:
\               INIT-PARSER ( EA --  ;electrons-matrix ADDRESS )
\               DISPATCH    ( C -- ; )
\       ---
\       DEVELOPED AND TESTED UNDER GFORTH 0.7.3
\
\       rev.1:  X-CASE eliminated redundant check
\               .E print electrons, not dump electrons matrix
\               various comments

DECIMAL
        VARIABLE L      CREATE LS S" SPDF" STRING,      \ NUMBER LEVEL
        VARIABLE N      CREATE NS S" 1234" STRING,      \ LETTER LEVEL
        VARIABLE E                                      \ ELEMENT-ARRAY POINTER
        VARIABLE X                                      \ NUMBER OF ELECTRONS
        VARIABLE S                                      \ STATE

        LS C@ NS C@ *   CONSTANT E#                     \ ELECTRONS MATRIX SIZE

: .E            ( EA --  ;PRINT ATOM ELECTRONS )
        E# 0
        DO
                DUP I + C@ DUP
                IF
                        I [ LS C@ ]L /MOD
                        NS 1+ + C@ EMIT
                        LS 1+ + C@ EMIT
                        S>D <# [CHAR] ) HOLD #S [CHAR] ( HOLD #> TYPE
                ELSE
                        DROP
                THEN
        LOOP    DROP
;

        1000 CONSTANT SIGINVALID
: SIGPARSERR
        SIGINVALID THROW
;

: C2I           ( C CA U -- I  ; CHAR TO INDEX )
        0 DO
                2DUP I + C@ =
                IF 2DROP I UNLOOP EXIT THEN
        LOOP
        SIGPARSERR
;
: N-CASE        ( C -- )
        DUP
        NS COUNT C2I N !
        1 S +!
        EMIT
;
: L-CASE        ( C -- )
        DUP
        \ IF NUMBER OF LEVELS > 4
        LS 1+  N @ 1+  [ LS C@ ]L MIN  C2I L !
        1 S +!
        EMIT
;
: (-CASE                ( C -- )
        DUP [CHAR] ( =
        IF   X OFF 1 S +!
        ELSE SIGPARSERR
        THEN EMIT
;

        CREATE MX                                       \ MAXIMUM #ELECTRONS MATRIX
        2 C, 0 C,  0 C,  0 C,
        2 C, 6 C,  0 C,  0 C,
        2 C, 6 C, 10 C,  0 C,
        2 C, 6 C, 10 C, 16 C,

: INDEX         ( -- I )
        N @ [ LS C@ ]L * L @ +
;
: X?            ( -- F )
        N @ 4 MIN               \ IF NUMBER OF LEVELS > 4
        [ LS C@ ]L * L @ +      ( I )
        MX + C@ 1+              ( I LIM )
        X @ 0 ROT WITHIN        ( F )
;
: STORE         ( -- )
        X?
        IF   INDEX E @ + X @ SWAP C!
        ELSE SIGPARSERR
        THEN
;
: )-CASE        ( C -- )
        DUP [CHAR] ) =
        IF   STORE  S OFF
        ELSE SIGPARSERR
        THEN EMIT
;
: X-CASE                ( C -- )
        DUP DIGIT?
        IF
                X @ BASE @ * + X !
                X? 0=
                        IF SIGPARSERR THEN
                1 S +!  EMIT
        ELSE
                )-CASE
        THEN
;


\ --- USER PROGAMMER INTERFACE

        CREATE STATES
        ' N-CASE , ' L-CASE , ' (-CASE , ' X-CASE , ' X-CASE , ' )-CASE ,
: DISPATCH      ( C -- )
        S @ CELLS STATES + @
        CATCH THROW
;
: INIT-PARSER   ( EA --  ;electrons-matrix ADDRESS )
        E ! E @ E# 0 FILL
        S OFF
;


1 [IF]

\ --- USAGE

: E!            ( CA U EA -- )
        INIT-PARSER
        2DUP TYPE CR
        BOUNDS ?DO
                I C@ DUP BL <>
                IF   TOUPPER DISPATCH
                ELSE DROP
                THEN
        LOOP
;
        CREATE NE E# ALLOT      \ NEON ATOM
        CR ." RESILIENCE FOR MISPLACED BLANKS." CR CR
        S" 1S(2) 2S(2)2 P (6)"   NE ' E! CATCH DROP CR
        NE .E CR .S CR
        CR ." ANOMALIES: STOPS AT FAILURE POINT." CR CR
        S" xS(2)2S(2)2P(6)"   NE ' E! CATCH DROP CR
        S" 1S(2)2F(2)2P(6)"   NE ' E! CATCH DROP CR
        S" 1S(2)2S(22)2P(6)"  NE ' E! CATCH DROP CR
        S" 1S(2)2S(2]2P(6)"   NE ' E! CATCH DROP CR
        S" 1S(2)2S(2)4F(011)"   NE ' E! CATCH DROP CR
[THEN]

Output:

bocmp$ gforth klf.fs -e bye

RESILIENCE FOR MISPLACED BLANKS.

1S(2) 2S(2)2 P (6)
1S(2)2S(2)2P(6)
1S(2)2S(2)2P(6)
<0> 

ANOMALIES: STOPS AT FAILURE POINT.

xS(2)2S(2)2P(6)

1S(2)2F(2)2P(6)
1S(2)2
1S(2)2S(22)2P(6)
1S(2)2S(2
1S(2)2S(2]2P(6)
1S(2)2S(2
1S(2)2S(2)4F(011)
1S(2)2S(2)4F(01
bocmp$ 

Have a nice day,
humptydumpty

[toc] | [prev] | [next] | [standalone]


#44285

FromGerry Jackson <gerry@jackson9000.fsnet.co.uk>
Date2015-12-17 17:33 +0000
Message-ID<n4urgu$fhu$1@dont-email.me>
In reply to#44181
On 11/12/2015 05:08, krishna.myneni@ccreweb.org wrote:
> For practice, try your hand at this parsing and string field
> validation exercise, which will actually be useful in an
> application I am writing. It shouldn't be too difficult,
> although I haven't done this myself, yet. Solutions in
> Forth-like languages are welcome, if you want to show off
> your language, but I'm interested in standard Forth solutions.
>
> PROBLEM: Parse a string specifying the electron configuration
> of an atom into a array of the number of electrons within
> each subshell. Validate the string fields.
>

Sorry about the late response.

I wondered what a solution would look like using the Forth interpreter 
to parse the input string via the GForth
EXECUTE-PARSING ( ... caddr u xt -- ... )

10 constant #subshells

create #electrons #subshells allot

: get-electrons  ( caddr2 u2 -- n )
    depth >r evaluate    ( -- n )
    r> depth <> >r
    dup 0< r> or abort" Invalid number of electrons"
;

: subshell  ( n1 n2 -- ) \ n1 subshell no., n2 max no. of electrons
    create , ,
    does> ( n -- )
       over >r 2@  r>            ( -- n n2 n3 n )
       < abort" Electron count too high"    ( -- n n2 )
       #electrons + c!
;

wordlist constant subshell-list
get-current subshell-list set-current

0  2 subshell 1s
1  2 subshell 2s
2  6 subshell 2p
3  2 subshell 3s
4  6 subshell 3p
5 10 subshell 3d
6  2 subshell 4s
7  6 subshell 4p
8 10 subshell 4d
9 14 subshell 4f

set-current

: (ec)  ( -- )
    begin
       '(' parse ?dup
    while
       2>r ')' parse        ( -- caddr2 u2 ) ( R: -- caddr1 u1 )
       get-electrons        ( -- n )
       2r> evaluate         \ Execute one of words 1s to 4f
    repeat
    drop
;

: electron-count  ( caddr u -- )
    -trailing #electrons #subshells erase
    subshell-list >order
    ['] (ec) execute-parsing
    cr ." Subshell string processed"
    previous
;

It works with GForth 0.7.9, it handles spaces in the source string and 
aborts on errors. To improve error handling it would be better to call 
it with a CATCH, also to isolate the subshell wordlist e.g. in outline:

: go  ( caddr u -- )
    get-order n>r 0 set-order
    subshell-list 1 set-order
    ['] electron-count catch
    ?dup if ( display error messages here ) then
    nr> set-order
;

and convert the ABORT"s to THROW with a suitable code.

-- 
Gerry

[toc] | [prev] | [next] | [standalone]


#44318

Fromkrishna.myneni@ccreweb.org
Date2015-12-23 05:07 -0800
Message-ID<156985fb-e010-48c6-bd54-e08cc9e77f37@googlegroups.com>
In reply to#44285
On Thursday, December 17, 2015 at 11:33:43 AM UTC-6, Gerry wrote:
> On 11/12/2015 05:08, krishna.myneni@ccreweb.org wrote:
> > For practice, try your hand at this parsing and string field
> > validation exercise, which will actually be useful in an
> > application I am writing. It shouldn't be too difficult,
> > although I haven't done this myself, yet. Solutions in
> > Forth-like languages are welcome, if you want to show off
> > your language, but I'm interested in standard Forth solutions.
> >
> > PROBLEM: Parse a string specifying the electron configuration
> > of an atom into a array of the number of electrons within
> > each subshell. Validate the string fields.
> >
> 
> Sorry about the late response.
> 

Thanks! I like your subshell parsing words.

Krishna

> I wondered what a solution would look like using the Forth interpreter 
> to parse the input string via the GForth
> EXECUTE-PARSING ( ... caddr u xt -- ... )
> ...

[toc] | [prev] | [next] | [standalone]


#44420

Fromjg <josefg@gmail.com>
Date2015-12-29 15:22 -0800
Message-ID<6f4ac34b-fa5b-4eae-b36b-5c7cebc5c680@googlegroups.com>
In reply to#44181
Quite enjoyable to read all the different solutions.
The solutions gave me plenty to think about.

One more for the mix:

( valid combinations of shells and subshells )
CREATE allowed
-1 , -1 , -1 , -1 ,
 0 , -1 , -1 , -1 ,
 0 ,  0 , -1 , -1 ,
 0 ,  0 ,  0 , -1 ,

CREATE max_electrons
2 , 6 , 10 , 14 ,

( parsing variables )
CREATE parse1 -1 ,
CREATE parse2  0 ,
CREATE places  0 ,
: set1 -1 parse1 !  0 parse2 ! 0 places ! ;
: set2  0 parse1 ! -1 parse2 ! 1 places ! ;
variable space_a
: space? 0 space_a ! 32 = if 1 space_a ! then ;

( final output )
CREATE electrons
-1 , -1 , -1 , -1 , -1 ,
-1 , -1 , -1 , -1 , -1 ,

: valid? dup 0 4 within ;

( shells 1, 2, 3, 4 )
: 1? dup [char] 1 = if drop 0 then ;
: 2? dup [char] 2 = if drop 1 then ;
: 3? dup [char] 3 = if drop 2 then ;
: 4? dup [char] 4 = if drop 3 then ;
: shell? 1? 2? 3? 4? valid?
        invert if cr dup emit abort" not valid shell" then ;

( subshells s, p, d, f )
: s? dup [char] s = if drop 0 set1 then ;
: p? dup [char] p = if drop 1 set1 then ;
: d? dup [char] d = if drop 2 set2 then ;
: f? dup [char] f = if drop 3 set2 then ;
: subshell?  s? p? d? f? valid?
        invert if cr dup emit abort" not valid subshell" then ;

: allowed? ( shell subshell -- shell subshell )
        over cells over 4 * cells + allowed + @
        invert if abort" incorrect shell combination" then ;

: (? [char] (  = invert if cr emit abort" missing (" then ;

: )? [char] )  = invert if cr emit abort" missing )." then ;

: 2_e ( start -- electrons? )
        2 s>number? nip
        invert if drop set1 then ;

: 1_e ( start -- electrons )
        1 s>number? nip
        invert if drop dup 3 - 5 cr type
        abort" Something is wrong with the electron count." then ;

: max_e? ( subshell electrons -- subshell electrons valid? )
        over over swap cells max_electrons + @ 1 +
        0 swap within
        invert if abort" Electron count out of range." then ;

: electrons? ( start -- electrons )
        parse2 @ if dup 2_e then 
        parse1 @ if dup 1_e then nip
        max_e? ;

: check_e ( electrons nth -- )
        dup cells electrons + @
        -1 <> if abort" This shell already used." then ;
        
: #electrons! ( electrons nth -- )
        cells electrons + ! ;

: electrons! ( shell subshell electrons -- )
        >r swap ( subshell shell indices as doubles )
        2dup 0 0 d= if r> 0 check_e #electrons! then
        2dup 0 1 d= if r> 1 check_e #electrons! then
        2dup 1 1 d= if r> 2 check_e #electrons! then
        2dup 0 2 d= if r> 3 check_e #electrons! then
        2dup 1 2 d= if r> 4 check_e #electrons! then
        2dup 2 2 d= if r> 5 check_e #electrons! then
        2dup 0 3 d= if r> 6 check_e #electrons! then
        2dup 1 3 d= if r> 7 check_e #electrons! then
        2dup 2 3 d= if r> 8 check_e #electrons! then
        2dup 3 3 d= if r> 9 check_e #electrons! then
        drop drop ;

: 0unused
    10 0 do electrons i cells + @
        -1 = if 0 i #electrons! then
        loop ;

: init_e 10 0 do -1 electrons i cells + ! loop ;

: shells ( addr len -- )
    init_e
    over + swap
    +do i 0 + c@ shell?
        i 1 + c@ subshell? allowed?
        i 2 + c@ (?
        i 3 + electrons?
        i 4 + places @ + c@ )?
        electrons!
        i 5 + places @ + c@ space?
    5 places @ + space_a @ + +loop
    0unused ;

( print everything nicely )
variable title
s" 1s2s2p3s3p3d4s4p4d4f" drop title !

: print_e
    cr 10 0 do 45 emit loop
       10 0 do cr
            space electrons i cells + @ dup 10 < if space then .
            space 124 emit
            space title @ i 2 * + 2 type
            loop
    cr ;

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.forth


csiph-web