in reply to Re: Parse:RecDescent grammar help
in thread Parse:RecDescent grammar help
Firstly, as Tachyon says these rules can get really funky really quickly, especialy when you want to try and model nested things of conditional instructions (as I'm finding out for with my tinkering).
That aside I've remodelled my rules(using my last example code) to accomodate the
type of entries you wanted. First here are some definitions.... in my little language these are my types...
(Just as a side note the $a = "hello" and $b = "world" should have already worked with my existing program, this bit is so you can embed 'variable's and 'array's in things)
# --- Rules --- parse : stmt(s?) EOF { $item[1] } stmt : variable ';' { $item[1] } | array ';' { $item[1] } | hash ';' { $item[1] } | <error> arrayelement : term ',' { [ @item[0, 1] ] } | term { [ @item[0, 1] ] } arrayname : ARRAY IDENTIFIER { [ 'array', $item[2] ] } array : arrayname EQUAL arrayelement(s?) { [ @item[2, 4] ] } hashelement : IDENTIFIER HASHASSIGN term ',' { [ @item[0,1 +,3] ] } | IDENTIFIER HASHASSIGN term { [ @item[0,1,3] +] } hash : HASH IDENTIFIER EQUAL '{' hashelement(s?) '} +' { [ @item [0, 2, 5] ] } variablename : VAR IDENTIFIER { [ 'variable', $item[2] ] } variable : variablename EQUAL term { [ @item[0, 2, 4] ] } term : QUOTE IDENTIFIER QUOTE { [ 'identifier', $it +em[2] ] } | LITERAL { [ 'literal', $item[1] ] } | arrayname { $item[1] } | variablename { $item[1] }
$VAR1 = [ [ 'hash', 'stuff', [ [ 'hashelement', 'animal', [ 'array', 'pets' ] ], [ 'hashelement', 'age', [ 'literal', '5' ] ], [ 'hashelement', 'name', [ 'identifier', 'fluffy' ] ], [ 'hashelement', 'colour', [ 'variable', 'col' ] ] ] ] ];
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Parse:RecDescent grammar help
by pip9ball (Acolyte) on Oct 26, 2004 at 13:39 UTC | |
by thekestrel (Friar) on Oct 27, 2004 at 03:49 UTC | |
|
Re^3: Parse:RecDescent grammar help
by pip9ball (Acolyte) on Oct 27, 2004 at 02:11 UTC | |
by thekestrel (Friar) on Oct 27, 2004 at 03:41 UTC |