Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I highly recommend <strong><a href="http://pyparsing.wikispaces.com/" rel="nofollow">pyparsing</a></strong>:</p> <blockquote> <p>The pyparsing module is an alternative approach to creating and executing simple grammars, vs. the traditional lex/yacc approach, or the use of regular expressions.</p> <p>The Python representation of the grammar is quite <strong>readable</strong>, owing to the <strong>self-explanatory class names</strong>, and the use of '+', '|' and '^' operator definitions. The parsed results returned from parseString() <strong>can be accessed as a nested list, a dictionary, or an object with named attributes</strong>.</p> </blockquote> <p><strong>Sample code</strong> (Hello world from the pyparsing docs):</p> <pre><code>from pyparsing import Word, alphas greet = Word( alphas ) + "," + Word( alphas ) + "!" # &lt;-- grammar defined here hello = "Hello, World!" print (hello, "-&gt;", greet.parseString( hello )) </code></pre> <p><strong>Output:</strong></p> <pre><code>Hello, World! -&gt; ['Hello', ',', 'World', '!'] </code></pre> <p><em><strong>Edit:</em></strong> Here's a solution to your sample language:</p> <pre><code>from pyparsing import * import json identifier = Word(alphas + nums + "_") expression = identifier("lhs") + Suppress("=") + identifier("rhs") struct_vals = delimitedList(Group(expression | identifier)) structure = Group(identifier + nestedExpr(opener="{", closer="}", content=struct_vals("vals"))) grammar = delimitedList(structure) my_string = "something{name1, name2, opt1=2, opt2=text}, something_else{name3, opt1=58}" parse_result = grammar.parseString(my_string) result_list = parse_result.asList() def list_to_dict(l): d = {} for struct in l: d[struct[0]] = {} for ident in struct[1]: if len(ident) == 2: d[struct[0]][ident[0]] = ident[1] elif len(ident) == 1: d[struct[0]][ident[0]] = None return d print json.dumps(list_to_dict(result_list), indent=2) </code></pre> <p><strong>Output:</strong> (pretty printed as JSON)</p> <pre><code>{ "something_else": { "opt1": "58", "name3": null }, "something": { "opt1": "2", "opt2": "text", "name2": null, "name1": null } } </code></pre> <p>Use the <a href="http://pythonhosted.org/pyparsing/" rel="nofollow">pyparsing API</a> as your guide to exploring the functionality of pyparsing and understanding the nuances of my solution. I've found that the quickest way to master this library is trying it out on some simple languages you think up yourself.</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