Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

123
LINES

< > BotCompany Repo | #1030667 // Superpositional Parsing Spike [dev.]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 911K of libraries. Compilation Failed (4422L/23K).

1  
!7
2  
3  
cprint {
4  
  srecord Superposition(Cl options) {
5  
    *(O... options) { this.options = asList(options); }
6  
  }
7  
  
8  
  class State extends ProbabilisticMachine2.State {
9  
    ImmutableListWalker<S> inputWalker;
10  
    Chain stack;
11  
    
12  
    public State makeDescendant() {
13  
      State s = prepareDescendant(new State);
14  
      s.inputWalker = inputWalker;
15  
      s.stack = stack;
16  
      ret s;
17  
    }
18  
    
19  
    void doneWithSubroutine {
20  
      if (stack == null) ret;
21  
      State s = makeDescendant();
22  
      s.stack = dropFirst(s.stack);
23  
      process(s, first(stack));
24  
    }
25  
    
26  
    void step {
27  
      doneWithSubroutine();
28  
    }
29  
  }
30  
  
31  
  record SequenceCons(a, b) > State {
32  
    void step {
33  
      State s = makeDescendant();
34  
      s.stack = chainPlus(s.stack, b);
35  
      process(s, a);
36  
    }
37  
  }
38  
    
39  
  asclass GrammarClass {}
40  
  sclass Statement > GrammarClass {}
41  
  
42  
  abstract class Executable {
43  
    abstract void step(State state) {}
44  
  }
45  
  
46  
  record grab(what) > Executable {
47  
    void step(State state) {
48  
      if (state.inputWalker.atEnd()) ret;
49  
      S token = state.inputWalker!;
50  
      if (eq(token, what)) {
51  
        State s = state.makeDescendant();
52  
        s.inputWalker = s.inputWalker.next();
53  
      }
54  
    }
55  
  } // what: string or a GrammarClass
56  
  
57  
  class EndOfInput > Executable {
58  
    void step(State state) {
59  
      print("end of input reached");
60  
      super.step();
61  
    }
62  
  } // what: string or a GrammarClass
63  
  
64  
  Executable sequence(O... steps) {
65  
    if (empty(steps)) ret new Executable;
66  
    ret foldl_noSeed((a, b) -> new SequenceCons(a, b), steps);
67  
  }
68  
69  
  start-thread {
70  
    Superposition main = sequence(
71  
      new Superposition(
72  
        sequence(
73  
          new grab("I"),
74  
          new grab("think"),
75  
          new Superposition(
76  
            sequence(
77  
              new grab("that"),
78  
              new grab(Statement)
79  
            ),
80  
            new grab(Statement)
81  
          )
82  
        ),
83  
      
84  
        sequence(
85  
          new grab("Do"),
86  
          new grab("you"),
87  
          new grab("think"),
88  
          new grab("that"),
89  
          new grab(Statement),
90  
          new grab("?")
91  
        ),
92  
      
93  
        new grab(Statement)
94  
      ),
95  
      new EndOfInput);
96  
97  
    printIndentedStruct(main);
98  
    
99  
    S input = "I think that you're right";
100  
    new ProbabilisticMachine2 pm;
101  
    State state = pm.addState(new State);
102  
    state.inputWalker = new ImmutableListWalker(javaTokC(input));
103  
    process(state, main);
104  
  }
105  
  
106  
  void process(State state, O code) {
107  
    if (code cast Superposition) {
108  
      fOr (O option : code.options)
109  
        pm.addState(state.prepareDescendant(new State {
110  
          void step {
111  
            process(state, option);
112  
          }
113  
        }));
114  
    } else if (code cast Executable) {
115  
      pm.addState(new State {
116  
        void step {
117  
          code.step(state, code);
118  
        }
119  
      });
120  
    } else
121  
      fail("Unknown code type: " + code);
122  
  }
123  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030667
Snippet name: Superpositional Parsing Spike [dev.]
Eternal ID of this version: #1030667/11
Text MD5: ab7a2476cb382aaddd4111602b68ae54
Transpilation MD5: 704e84cc76d42c8b914e331298078fca
Author: stefan
Category: javax / gazelle
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-02-25 22:24:53
Source code size: 2984 bytes / 123 lines
Pitched / IR pitched: No / No
Views / Downloads: 219 / 427
Version history: 10 change(s)
Referenced in: [show references]