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

92
LINES

< > BotCompany Repo | #1002687 // Parsing Simplified NL (NOT current)

JavaX source code - run with: x30.jar

1  
!759
2  
3  
static boolean printNulls = true, full = false;
4  
5  
static L<S> inputs = toLinesFullTrim([[
6  
  hello
7  
  "hello!"
8  
  hello you
9  
  hello < you
10  
  [hello]
11  
  [hello] [you]
12  
  [hello < you]
13  
  
14  
  [ ? < Is < it < safe < to < allow [PowerTOP] to [make < all < the < changes < located on < the < Tunables < tab] ]
15  
  [ I < was < going to < create < a < startup script < for < it ]
16  
]]);
17  
18  
static S snlRules = [[
19  
  // highest (strongest binding) level 
20  
  <identifier> = idword
21  
  <quoted> = subword
22  
  ? = idword
23  
  ! = idword
24  
  
25  
  <subword> = word
26  
  <idword> = word
27  
  
28  
  // juxtaposition on highest level
29  
  <word> <word> = word
30  
  
31  
  // bracketing takes us to highest level
32  
  [ <lowest> ] = subword
33  
  ( <lowest> ) = subword
34  
  
35  
  // < is intermediate
36  
  <word> = arrows
37  
  <word> < <arrows> = arrows
38  
  
39  
  = lowest // allow empty here
40  
  <arrows> = lowest
41  
]];
42  
43  
static class SNL {
44  
  S op;
45  
  new L<SNL> subs;
46  
  
47  
  *() {}
48  
  *(S op) {}
49  
  *(S op, L<SNL> *subs) {}
50  
}
51  
52  
p {
53  
  for (S input : inputs) {
54  
    SNL snl = parseSNL(input);
55  
    print();
56  
    print("PARSING: " + input);
57  
    print();
58  
    print("  " + structure(snl));
59  
    print();
60  
  }
61  
}
62  
63  
static SNL parseSNL(S input) {
64  
  Explain exp = explainFull(input, snlRules, "lowest");
65  
  ret explainToSNL(exp);
66  
}
67  
68  
static SNL explainToSNL(Explain e) {
69  
  if (e == null)
70  
    ret null;
71  
    
72  
  S c = e.className();
73  
  
74  
  if (eq(c, "word")) ret new SNL(e.string());
75  
  exp.sub
76  
        else {
77  
      //printStructure(exp.e);
78  
      printTree(0, exp);
79  
    }
80  
}
81  
82  
static void printTree(int indent, Explain e) {
83  
  if (e == null) {
84  
    // this would print "null" for every <identifier> etc
85  
    if (printNulls)
86  
      print(indent(indent) + "null");
87  
  } else {
88  
    print(indent(indent) + e.className() + " | " + quote(e.string()));
89  
    for (Explain sub : e.subs)
90  
      printTree(indent+2, sub);
91  
  }
92  
}

Author comment

Began life as a copy of #1002683

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1002687
Snippet name: Parsing Simplified NL (NOT current)
Eternal ID of this version: #1002687/1
Text MD5: cc87904aaa58bb060d74cd82281df0be
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-02-17 23:23:24
Source code size: 1885 bytes / 92 lines
Pitched / IR pitched: No / No
Views / Downloads: 605 / 526
Referenced in: [show references]