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

!759

static boolean printNulls = true, full = false;

static L<S> inputs = toLinesFullTrim([[
  hello
  "hello!"
  hello you
  hello < you
  [hello]
  [hello] [you]
  [hello < you]
  
  [ ? < Is < it < safe < to < allow [PowerTOP] to [make < all < the < changes < located on < the < Tunables < tab] ]
  [ I < was < going to < create < a < startup script < for < it ]
]]);

static S snlRules = [[
  // highest (strongest binding) level 
  <identifier> = idword
  <quoted> = subword
  ? = idword
  ! = idword
  
  <subword> = word
  <idword> = word
  
  // juxtaposition on highest level
  <word> <word> = word
  
  // bracketing takes us to highest level
  [ <lowest> ] = subword
  ( <lowest> ) = subword
  
  // < is intermediate
  <word> = arrows
  <word> < <arrows> = arrows
  
  = lowest // allow empty here
  <arrows> = lowest
]];

static class SNL {
  S op;
  new L<SNL> subs;
  
  *() {}
  *(S op) {}
  *(S op, L<SNL> *subs) {}
}

p {
  for (S input : inputs) {
    SNL snl = parseSNL(input);
    print();
    print("PARSING: " + input);
    print();
    print("  " + structure(snl));
    print();
  }
}

static SNL parseSNL(S input) {
  Explain exp = explainFull(input, snlRules, "lowest");
  ret explainToSNL(exp);
}

static SNL explainToSNL(Explain e) {
  if (e == null)
    ret null;
    
  S c = e.className();
  
  if (eq(c, "word")) ret new SNL(e.string());
  exp.sub
        else {
      //printStructure(exp.e);
      printTree(0, exp);
    }
}

static void printTree(int indent, Explain e) {
  if (e == null) {
    // this would print "null" for every <identifier> etc
    if (printNulls)
      print(indent(indent) + "null");
  } else {
    print(indent(indent) + e.className() + " | " + quote(e.string()));
    for (Explain sub : e.subs)
      printTree(indent+2, sub);
  }
}

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: 663 / 584
Referenced in: [show references]