Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try <strong><a href="http://treetop.rubyforge.org/" rel="nofollow noreferrer">Treetop</a></strong>. It is a Ruby-like DSL to describe grammars. Parsing the string you've given should be quite easy, and by using a real parser you'll easily be able to extend your grammar later.</p> <p>An example grammar for the type of string that you want to parse (save as <code>sentences.treetop</code>):</p> <pre><code>grammar Sentences rule sentence # A sentence is a combination of one or more expressions. expression* &lt;Sentence&gt; end rule expression # An expression is either a literal or a parenthesised expression. parenthesised / literal end rule parenthesised # A parenthesised expression contains one or more sentences. "(" (multiple / sentence) ")" &lt;Parenthesised&gt; end rule multiple # Multiple sentences are delimited by a pipe. sentence "|" (multiple / sentence) &lt;Multiple&gt; end rule literal # A literal string contains of word characters (a-z) and/or spaces. # Expand the character class to allow other characters too. [a-zA-Z ]+ &lt;Literal&gt; end end </code></pre> <p>The grammar above needs an accompanying file that defines the classes that allow us to access the node values (save as <code>sentence_nodes.rb</code>).</p> <pre><code>class Sentence &lt; Treetop::Runtime::SyntaxNode def combine(a, b) return b if a.empty? a.inject([]) do |values, val_a| values + b.collect { |val_b| val_a + val_b } end end def values elements.inject([]) do |values, element| combine(values, element.values) end end end class Parenthesised &lt; Treetop::Runtime::SyntaxNode def values elements[1].values end end class Multiple &lt; Treetop::Runtime::SyntaxNode def values elements[0].values + elements[2].values end end class Literal &lt; Treetop::Runtime::SyntaxNode def values [text_value] end end </code></pre> <p>The following example program shows that it is quite simple to parse the example sentence that you have given.</p> <pre><code>require "rubygems" require "treetop" require "sentence_nodes" str = 'maybe (this is|that was) some' + ' ((nice|ugly) (day|night)|(strange (weather|time)))' Treetop.load "sentences" if sentence = SentencesParser.new.parse(str) puts sentence.values else puts "Parse error" end </code></pre> <p>The output of this program is:</p> <pre><code>maybe this is some nice day maybe this is some nice night maybe this is some ugly day maybe this is some ugly night maybe this is some strange weather maybe this is some strange time maybe that was some nice day maybe that was some nice night maybe that was some ugly day maybe that was some ugly night maybe that was some strange weather maybe that was some strange time </code></pre> <p>You can also access the syntax tree:</p> <pre><code>p sentence </code></pre> <p><a href="http://gist.github.com/321995" rel="nofollow noreferrer">The output is here</a>.</p> <p>There you have it: a scalable parsing solution that should come quite close to what you want to do in about 50 lines of code. Does that help?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload