Skip to content

Add support Start-Symbol: %start - #576

Merged
ydah merged 4 commits into
ruby:masterfrom
ydah:support-start
Aug 17, 2025
Merged

ydah merged 4 commits into
ruby:masterfrom
ydah:support-start

Conversation

@ydah

@ydah ydah commented Mar 1, 2025

Copy link
Copy Markdown
Member

Fixes: #568

@yui-knk

yui-knk commented Apr 29, 2025

Copy link
Copy Markdown
Collaborator

I tried the grammar file below with Bison 3.8.2.

%{
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
%}

%code provides {
static int yylex(YYSTYPE *val, YYLTYPE *loc);
static int yyerror(YYLTYPE *loc, const char *str);
}

%union {
    int val;
}
%token LF
%token <val> NUM
%type <val> expr
%left '+' '-'
%left '*' '/'

%locations

%start program_1 program_2 program_3 '/'

%%

program_1 : expr '+' expr;
program_2 : expr '-' expr;
program_3 : expr '*' expr;


expr : NUM
     | '(' expr ')'  { $$ = $2; }
     ;

%%

Output file says YY_PARSE_ terminals are required on state 0. Could you check how multiple start symbols are used?

State 0

    0 $accept: • YY_PARSE_program_1 program_1 $end
    1        | • YY_PARSE_program_2 program_2 $end
    2        | • YY_PARSE_program_3 program_3 $end
    3        | • YY_PARSE_(NULL) '/' $end

    YY_PARSE_(NULL)     shift, and go to state 1
    YY_PARSE_program_1  shift, and go to state 2
    YY_PARSE_program_2  shift, and go to state 3
    YY_PARSE_program_3  shift, and go to state 4

@yui-knk

yui-knk commented Apr 29, 2025

Copy link
Copy Markdown
Collaborator

@ydah
ydah force-pushed the support-start branch 2 times, most recently from e42b5b7 to 2133a0f Compare June 11, 2025 05:23
@ydah

ydah commented Aug 1, 2025 •

Copy link
Copy Markdown
Member Author

Output file says YY_PARSE_ terminals are required on state 0. Could you check how multiple start symbols are used?

This is because Bison did not generate an error, so I made the same behavior. I don't know any particular use cases.

@ydah
ydah requested a review from yui-knk August 1, 2025 15:31
@ydah
ydah force-pushed the support-start branch 2 times, most recently from d15c656 to 8c43e68 Compare August 2, 2025 06:51
@ydah

ydah commented Aug 4, 2025

Copy link
Copy Markdown
Member Author

@yui-knk An error is now raised if more than one %start is defined. Please point out if there is something wrong with your understanding of the sentence.

Comment thread lib/lrama/grammar.rb Outdated
end

# Add $accept rule to the top of rules
def add_accept_rules

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method adds single accept rule then it's better to use add_accept_rule (without "s") or something like that.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change in 4337cce

Comment thread lib/lrama/grammar.rb
def set_start_nterm(id)
if @start_nterm.nil?
@start_nterm = id
else

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree to the idea to not support multiple start symbols until we think it's use case is reasonable for us.
By the way, could you comment the fact that Lrama's %start behaves differently when multiple symbols are passed to %start?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the following comments. What do you think?

@yui-knk yui-knk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left comments. The direction looks good to me.

@ydah
ydah requested a review from yui-knk August 17, 2025 06:40
@ydah
ydah merged commit 7f4415c into ruby:master Aug 17, 2025
22 checks passed
@ydah
ydah deleted the support-start branch August 17, 2025 08:08
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.

Support Start-Symbol: %start

2 participants