Skip to content

Commit

Permalink
Add error when a terminal symbol is passed to %nterm
Browse files Browse the repository at this point in the history
In bison when a terminal symbol is passed to `%nterm` it should give an error. But it doesn't yet.
It just ignores the terminal symbol and uses the nonterminal symbol.

For example in the following code, `EOI` is a terminal symbol and `EOI` is a nonterminal symbol. When `EOI` is passed to `%nterm` it should give an error but it doesn't.

```yacc
%{
// Prologue
%}

%token EOI 0 "EOI"
%nterm EOI

%%

program: /* empty */
        ;
```

In bison, it gives the following error

```
❯ bison test.y
test.y:6.8-10: error: symbol EOI redeclared as a nonterminal
    6 | %nterm EOI
      |        ^~~
test.y:5.8-10: note: previous definition
    5 | %token EOI 0 "EOI"
      |        ^~~
```

So this PR adds an error when a terminal symbol is passed to `%nterm`.
  • Loading branch information
ydah committed Dec 21, 2024
1 parent 478e297 commit 54333a7
Show file tree
Hide file tree
Showing 5 changed files with 411 additions and 367 deletions.
2 changes: 1 addition & 1 deletion lib/lrama/grammar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Grammar
:after_shift, :before_reduce, :after_reduce, :after_shift_error_token, :after_pop_stack,
:symbols_resolver, :types, :rules, :rule_builders, :sym_to_rules, :no_stdlib, :locations

def_delegators "@symbols_resolver", :symbols, :nterms, :terms, :add_nterm, :add_term,
def_delegators "@symbols_resolver", :symbols, :nterms, :terms, :add_nterm, :add_term, :find_term_by_s_value,
:find_symbol_by_number!, :find_symbol_by_id!, :token_to_symbol,
:find_symbol_by_s_value!, :fill_symbol_number, :fill_nterm_type,
:fill_printer, :fill_destructor, :fill_error_token, :sort_by_number!
Expand Down
4 changes: 4 additions & 0 deletions lib/lrama/grammar/symbols/resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def add_nterm(id:, alias_name: nil, tag: nil)
nterm
end

def find_term_by_s_value(s_value)
terms.find { |s| s.id.s_value == s_value }
end

def find_symbol_by_s_value(s_value)
symbols.find { |s| s.id.s_value == s_value }
end
Expand Down
Loading

0 comments on commit 54333a7

Please sign in to comment.