Skip to content

Conversation

@ydah
Copy link
Member

@ydah ydah commented Nov 23, 2025

Overview

This PR adds a complete generic linked list implementation to stdlib.y, enabling users to immediately use list(), nonempty_list(), and separated_list() parameterized rules without writing any boilerplate code.

What Changed

  1. Added linked list implementation to stdlib.y

New data structure and functions available via %code requires:

  • lrama_list_node_t - Generic singly-linked list node
  • lrama_list_new(void*) - Create new list
  • lrama_list_append(list, void*) - Append element
  • lrama_list_free(list) - Free list structure
  • lrama_list_length(list) - Count elements
  • lrama_list_get(list, index) - Get nth element
  1. Added semantic actions to list rules

Before:

%rule nonempty_list(X)
    : X
    | nonempty_list(X) X
    ;

After:

%rule nonempty_list(X)
    : X { $$ = lrama_list_new((void*)$1); }
    | nonempty_list(X) X { $$ = lrama_list_append($1, (void*)$2); }
    ;
  1. Simplified separated_list() implementation

Before: separated_list(sep, X) → option(separated_nonempty_list(sep, X))

  • Generated 3 non-terminals: option_..., separated_nonempty_list_..., separated_list_...

After: separated_list(sep, X) directly expands to 3 rules

  • Generated 1 non-terminal: separated_list_...
  • Fewer symbols, simpler grammar
  1. Enhanced stdlib merging in lib/lrama/command.rb
  • Merges prologue sections from stdlib.y into user grammar
  • Prepends %code directives from stdlib.y before user code
  • Ensures helper functions are available when list rules are used

@ydah ydah marked this pull request as draft November 23, 2025 10:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant