Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange problem with context free grammar
    primarykey
    data
    text
    <p>I begin with an otherwise well formed (and well working) grammar for a language. Variables, binary operators, function calls, lists, loops, conditionals, etc. To this grammar I'd like to add what I'm calling the <code>object</code> construct:</p> <pre><code>object : object_name ARROW more_objects ; more_objects : object_name | object_name ARROW more_objects ; object_name : IDENTIFIER ; </code></pre> <p>The point is to be able to access scalars nested in objects. For example:</p> <pre><code>car-&gt;color monster-&gt;weapon-&gt;damage pc-&gt;tower-&gt;motherboard-&gt;socket_type </code></pre> <p>I'm adding <code>object</code> as a <code>primary_expression</code>:</p> <pre><code>primary_expression : id_lookup | constant_value | '(' expression ')' | list_initialization | function_call | object ; </code></pre> <hr> <p>Now here's a sample script:</p> <pre><code>const list = [ 1, 2, 3, 4 ]; for var x in list { send "foo " + x + "!"; } send "Done!"; </code></pre> <p>Prior to adding the nonterminal <code>object</code> as a <code>primary_expression</code> everything is sunshine and puppies. Even after I add it, Bison doesn't complain. No shift and/or reduce conflicts reported. And the generated code compiles without a sound. But when I try to run the sample script above, I get told <code>error on line 2: Attempting to use undefined symbol '{' on line 2.</code></p> <p>If I change the script to:</p> <pre><code>var list = 0; for var x in [ 1, 2, 3, 4 ] { send "foo " + x + "!"; } send "Done!"; </code></pre> <p>Then I get <code>error on line 3: Attempting to use undefined symbol '+' on line 3.</code></p> <p>Clearly the presence of <code>object</code> in the grammar is messing up how the parser behaves [SOMEhow], and I feel like I'm ignoring a rather simple principle of language theory that would fix this in a jiff, but the fact that there aren't any shift/reduce conflicts has left me bewildered.</p> <p>Is there a better way (grammatically) to write these rules? What am I missing? Why aren't there any conflicts?</p> <p>(And here's the <a href="http://code.google.com/p/symphonymud/source/browse/branches/development/muscl/src/muscl/muscl.y?r=466#264" rel="nofollow noreferrer">full grammar file</a> in case it helps)</p> <hr> <p><strong>UPDATE:</strong> To clarify, this language, which compiles into code being run by a virtual machine, is embedded into another system - a game, specifically. It has scalars and lists, and there are no complex data types. When I say I want to add <code>object</code>s to the language, that's actually a misnomer. I am not adding support for user-defined types to my language.</p> <p>The objects being accessed with the <code>object</code> construct are actually objects from the game which I'm allowing the language processor to access through an intermediate layer which connects the VM to the game engine. This layer is designed to decouple as much as possible the language definition and the virtual machine mechanics from the implementation and details of the game engine.</p> <p>So when, in my language I write:</p> <pre><code>player-&gt;name </code></pre> <p>That only gets codified by the compiler. "player" and "name" are not traditional <code>identifier</code>s because they are not added to the symbol table, and nothing is done with them at compile time except to translate the request for the name of the player into 3-address code.</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.
 

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