Skip to content

Commit

Permalink
Simplify normalize_rules
Browse files Browse the repository at this point in the history
The first goal of Lrama was generating parse.c file
same with what Bison generates.
For this goal, it was needed to set symbol number and rule number
with same order with Bison.
However Lrama has a lot of tests now. Then it’s not needed to generate
completely same parse.c file with Bison.
  • Loading branch information
yui-knk committed Dec 24, 2023
1 parent c30569b commit 2e2aa78
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 47 deletions.
29 changes: 2 additions & 27 deletions lib/lrama/grammar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -359,29 +359,8 @@ def append_special_symbols
@accept_symbol = term
end

# 1. Add $accept rule to the top of rules
# 2. Extract action in the middle of RHS into new Empty rule
# 3. Append id and extract action then create Rule
#
# Bison 3.8.2 uses different orders for symbol number and rule number
# when a rule has actions in the middle of a rule.
#
# For example,
#
# `program: $@1 top_compstmt`
#
# Rules are ordered like below,
#
# 1 $@1: ε
# 2 program: $@1 top_compstmt
#
# Symbols are ordered like below,
#
# 164 program
# 165 $@1
#
def normalize_rules
# 1. Add $accept rule to the top of rules
# Add $accept rule to the top of rules
accept = @accept_symbol
eof = @eof_symbol
lineno = @rule_builders.first ? @rule_builders.first.line : 0
Expand All @@ -390,8 +369,8 @@ def normalize_rules
setup_rules

@rule_builders.each do |builder|
# Extract actions in the middle of RHS into new rules.
builder.midrule_action_rules.each do |rule|
add_nterm(id: rule._lhs)
@rules << rule
end

Expand All @@ -404,10 +383,6 @@ def normalize_rules
add_nterm(id: rule._lhs, tag: rule.lhs_tag)
@rules << rule
end

builder.midrule_action_rules.each do |rule|
add_nterm(id: rule._lhs)
end
end

@rules.sort_by!(&:id)
Expand Down
10 changes: 5 additions & 5 deletions spec/lrama/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1700,11 +1700,11 @@ class : keyword_class { code 1 } tSTRING { code 2 } keyword_end { code 3 }
grammar = Lrama::Parser.new(y, "parse.y").parse

expect(grammar.nterms.sort_by(&:number)).to match_symbols([
Sym.new(id: T::Ident.new(s_value: "$accept"), alias_name: nil, number: 11, tag: nil, term: false, token_id: 0, nullable: false),
Sym.new(id: T::Ident.new(s_value: "program"), alias_name: nil, number: 12, tag: nil, term: false, token_id: 1, nullable: false),
Sym.new(id: T::Ident.new(s_value: "class"), alias_name: nil, number: 13, tag: T::Tag.new(s_value: "<i>"), term: false, token_id: 2, nullable: false),
Sym.new(id: T::Ident.new(s_value: "$@1"), alias_name: nil, number: 14, tag: nil, term: false, token_id: 3, nullable: true),
Sym.new(id: T::Ident.new(s_value: "$@2"), alias_name: nil, number: 15, tag: nil, term: false, token_id: 4, nullable: true),
Sym.new(id: T::Ident.new(s_value: "$accept"), alias_name: nil, number: 11, tag: nil, term: false, token_id: 0, nullable: false),
Sym.new(id: T::Ident.new(s_value: "program"), alias_name: nil, number: 12, tag: nil, term: false, token_id: 1, nullable: false),
Sym.new(id: T::Ident.new(s_value: "$@1"), alias_name: nil, number: 13, tag: nil, term: false, token_id: 2, nullable: true),
Sym.new(id: T::Ident.new(s_value: "$@2"), alias_name: nil, number: 14, tag: nil, term: false, token_id: 3, nullable: true),
Sym.new(id: T::Ident.new(s_value: "class"), alias_name: nil, number: 15, tag: T::Tag.new(s_value: "<i>"), term: false, token_id: 4, nullable: false),
])
expect(grammar.rules).to eq([
Rule.new(
Expand Down
30 changes: 15 additions & 15 deletions spec/lrama/states_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -993,46 +993,46 @@ class go to state 5
$default reduce using rule 1 ($@1)
program go to state 1
$@1 go to state 2
$@1 go to state 1
program go to state 2
State 1
0 $accept: program • "EOI"
2 program: $@1 • expr
3 expr: • tNUMBER
"EOI" shift, and go to state 3
tNUMBER shift, and go to state 3
expr go to state 4
State 2
2 program: $@1 • expr
3 expr: • tNUMBER
State 2
tNUMBER shift, and go to state 4
0 $accept: program • "EOI"
expr go to state 5
"EOI" shift, and go to state 5
State 3
0 $accept: program "EOI"
3 expr: tNUMBER
$default accept
$default reduce using rule 3 (expr)
State 4
3 expr: tNUMBER
2 program: $@1 expr
$default reduce using rule 3 (expr)
$default reduce using rule 2 (program)
State 5
2 program: $@1 expr
0 $accept: program "EOI"
$default reduce using rule 2 (program)
$default accept
STR
Expand Down

0 comments on commit 2e2aa78

Please sign in to comment.