title: Lemon Parser REPL
body: <p>I'm trying to build a Smalltalk REPL based on <a href="https://github.com/etoile/Languages" rel="nofollow noreferrer">LanguageKit</a>, which uses a lemon gramma. Currently the parser only supports parsing complete class definitions, but not statements outside of the method syntax. </p>
  
  <p>For example this gets parsed:</p>
  
  <pre><code>methodName [
      NSObject description.
      NSObject debugDescription.
  ]
  </code></pre>
  
  <p>But it will fail if I try to parse only the statements:</p>
  
  <pre><code>NSObject description.
  NSObject debugDescription.
  </code></pre>
  
  <p>The following won't accept multiple statements (e.g. <code>Transcript show: 'hello'. Transcript show: 'world'.</code>):</p>
  
  <pre><code>file ::= statement_list(M).
  {
      [p setAST:M];
  }
  </code></pre>
  
  <p>Here the minimal gramma:</p>
  
  <pre><code>%include {
  #include &lt;assert.h&gt;
  #import &lt;Foundation/Foundation.h&gt;
  #import &lt;LanguageKit/LanguageKit.h&gt;
  #import &lt;LanguageKit/LKToken.h&gt;
  #import "SmalltalkParser.h"
  
  }
  %name SmalltalkParse
  %token_prefix TOKEN_
  %token_type {id}
  %extra_argument {SmalltalkParser *p}
  %left PLUS MINUS STAR SLASH EQ LT GT COMMA.
  %left WORD.
  
  
  file ::= method(M).
  {
      [p setAST:M];
  }
  file ::= statement_list(M).
  {
      [p setAST:M];
  }
  file ::= statement(M).
  {
      [p setAST:M];
  }
  file ::= .
  
  
  
  method(M) ::= signature(S) LSQBRACK statement_list(E) RSQBRACK.
  {
      M = [LKInstanceMethod methodWithSignature:S locals:nil statements:E];
  }
  
  signature(S) ::= WORD(M).
  {
      S = [LKMessageSend messageWithSelectorName:M];
  }
  signature(S) ::= keyword_signature(M).
  {
      S = M;
  }
  
  
  statement_list(L) ::= statements(T).
  {
      L = T;
  }
  statement_list(L) ::= statements(T) statement(S).
  {
      [T addObject:S];
      L = T;
  }
  
  statements(L) ::= statements(T) statement(S) STOP.
  {
      [T addObject:S];
      L = T;
  }
  statements(L) ::= .
  {
      L = [NSMutableArray array];
  }
  
  statement(S) ::= expression(E).
  {
      S = E;
  }
  
  %syntax_error 
  {
      [NSException raise:@"ParserError" format:@"Parsing failed"];
  }
  
  message(M) ::= simple_message(S).
  {
      M = S;
  }
  
  simple_message(M) ::= WORD(S).
  {
      M = [LKMessageSend messageWithSelectorName:S];
  }
  
  expression(E) ::= simple_expression(S).
  {
      E = S;
  }
  
  simple_expression(E) ::= WORD(T) simple_message(M).
  {
      [M setTarget:T];
      E = M;
  }
  </code></pre>
  
  <p>The complete gramma can be found here: <a href="https://github.com/etoile/Languages/blob/master/Smalltalk/smalltalk.y" rel="nofollow noreferrer">smalltalk.y</a>. I have been reading other grammas and also searching stackoverflow, but don't see a difference for example with this <a href="https://stackoverflow.com/a/46840708/23028">gramma</a> and don't understand why this isn't working.</p>