Skip to content
Browse files

Going home for the day. No major changes besides putting off dumping/…

…loading until there is a clear need for it.
  • Loading branch information...
1 parent 7aecddd commit 3fe7548b48be3c8db0342bc1466985f352639eea chris.wailes@gmail.com committed Mar 28, 2011
Showing with 114 additions and 121 deletions.
  1. +3 −3 LICENSE
  2. +1 −3 TODO
  3. +89 −94 lib/rltk/parser.rb
  4. +21 −21 test/test.rb
View
6 LICENSE
@@ -14,9 +14,9 @@ furnished to do so, subject to the following conditions:
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimers in the
documentation and/or other materials provided with the distribution.
- 3. The names of the contributors to this project may not be used to endorse
- or promote products derived from this Software without specific prior
- written permission.
+ 3. Neither the names of the RLTK development team, nor the names of its
+ contributors may be used to endorse or promote products derived from this
+ Software without specific prior written permission.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
View
4 TODO
@@ -1,7 +1,5 @@
parser.rb:
- Track down bug in ABParser with input "AAAAB".
- Parsing errors that can be caught by the user's code.
- Saving and loading the parse table.
+ Error productions.
Implement some unit testing.
Add documentation.
View
183 lib/rltk/parser.rb
@@ -61,6 +61,8 @@ def initialize
@production_precs = Array.new
@token_precs = Hash.new
+ @did_load = false
+
@grammar.callback do |r, type, num|
@procs[r.id] =
if type == :*
@@ -100,7 +102,7 @@ def add_state(state)
end
def build_finalize_opts(opts)
- opts[:explain] = self.get_io(opts[:explain])
+ opts[:explain] = self.get_io(opts[:explain])
{
:explain => false,
@@ -510,13 +512,6 @@ def parse(tokens, opts = {})
# next token until every stack is done with the
# current one.
tokens.each do |token|
-
- #~puts "Processing:"
- #~pp processing
- #~
- #~puts "Moving on:"
- #~pp moving_on
-
# Check to make sure this token was seen in the
# grammar definition.
if not @symbols.include?(token.type)
@@ -746,92 +741,6 @@ def right(*symbols)
def start(symbol)
@grammar.start symbol
end
-
- class State
- attr_accessor :id
- attr_reader :items
- attr_reader :actions
-
- def initialize(tokens, items = [])
- @id = nil
- @items = items
- @actions = tokens.inject(Hash.new) { |h, t| h[t] = Array.new; h }
- end
-
- def ==(other)
- self.items == other.items
- end
-
- def append(item)
- if item.is_a?(CFG::Item) and not @items.include?(item) then @items << item end
- end
-
- alias :<< :append
-
- def clean
- @items = nil
- end
-
- def close(productions)
- self.each do |item|
- if (next_symbol = item.next_symbol) and CFG::is_nonterminal?(next_symbol)
- productions[next_symbol].each { |p| self << p.to_item }
- end
- end
- end
-
- def each
- @items.each {|item| yield item}
- end
-
- def on(symbol, action)
- if @actions.key?(symbol)
- @actions[symbol] << action
- else
- raise ParserConstructionError, "Attempting to set action for token (#{symbol}) not seen in grammar definition."
- end
- end
-
- def on_any(action)
- @actions.each { |k, v| if CFG::is_terminal?(k) then v << action end }
- end
-
- def on?(symbol)
- @actions[symbol].clone
- end
- end
-
- class Action
- attr_reader :id
-
- def initialize(id = nil)
- @id = id
- end
- end
-
- class Accept < Action
- def to_s
- "Accept"
- end
- end
-
- class GoTo < Action
- def to_s
- "GoTo #{self.id}"
- end
- end
-
- class Reduce < Action
- def to_s
- "Reduce by Production #{self.id}"
- end
- end
-
- class Shift < Action
- def to_s
- "Shift to State #{self.id}"
- end
- end
end
class ParseStack
@@ -913,5 +822,91 @@ def tree
tree += "}"
end
end
+
+ class State
+ attr_accessor :id
+ attr_reader :items
+ attr_reader :actions
+
+ def initialize(tokens, items = [])
+ @id = nil
+ @items = items
+ @actions = tokens.inject(Hash.new) { |h, t| h[t] = Array.new; h }
+ end
+
+ def ==(other)
+ self.items == other.items
+ end
+
+ def append(item)
+ if item.is_a?(CFG::Item) and not @items.include?(item) then @items << item end
+ end
+
+ alias :<< :append
+
+ def clean
+ @items = nil
+ end
+
+ def close(productions)
+ self.each do |item|
+ if (next_symbol = item.next_symbol) and CFG::is_nonterminal?(next_symbol)
+ productions[next_symbol].each { |p| self << p.to_item }
+ end
+ end
+ end
+
+ def each
+ @items.each {|item| yield item}
+ end
+
+ def on(symbol, action)
+ if @actions.key?(symbol)
+ @actions[symbol] << action
+ else
+ raise ParserConstructionError, "Attempting to set action for token (#{symbol}) not seen in grammar definition."
+ end
+ end
+
+ def on_any(action)
+ @actions.each { |k, v| if CFG::is_terminal?(k) then v << action end }
+ end
+
+ def on?(symbol)
+ @actions[symbol].clone
+ end
+ end
+
+ class Action
+ attr_reader :id
+
+ def initialize(id = nil)
+ @id = id
+ end
+ end
+
+ class Accept < Action
+ def to_s
+ "Accept"
+ end
+ end
+
+ class GoTo < Action
+ def to_s
+ "GoTo #{self.id}"
+ end
+ end
+
+ class Reduce < Action
+ def to_s
+ "Reduce by Production #{self.id}"
+ end
+ end
+
+ class Shift < Action
+ def to_s
+ "Shift to State #{self.id}"
+ end
+ end
end
end
View
42 test/test.rb
@@ -5,28 +5,28 @@
require 'rltk/lexer'
require 'rltk/parser'
-require 'rltk/lexers/calculator'
-require 'rltk/parsers/calculator'
+#~require 'rltk/lexers/calculator'
+#~require 'rltk/parsers/calculator'
+#~
+#~lexer = RLTK::Lexers::Calc.new
+#~parser = RLTK::Parsers::Calc.new
-lexer = RLTK::Lexers::Calc.new
-parser = RLTK::Parsers::Calc.new
+class ABLexer < RLTK::Lexer
+ rule(/A/) { [:A, 1] }
+ rule(/B/) { [:B, 2] }
+
+ rule(/\s/)
+end
-#~class ABLexer < RLTK::Lexer
- #~rule(/A/) { [:A, 1] }
- #~rule(/B/) { [:B, 2] }
- #~
- #~rule(/\s/)
-#~end
-#~
-#~class ABParser < RLTK::Parser
- #~
- #~production(:a, "A* B") {|a, b| "Accepted with #{a.length} A(s)" }
- #~
- #~finalize
-#~end
-#~
-#~lexer = ABLexer.new
-#~parser = ABParser.new
+class ABParser < RLTK::Parser
+
+ production(:a, "A* B") {|a, b| "Accepted with #{a.length} A(s)" }
+
+ finalize
+end
+
+lexer = ABLexer.new
+parser = ABParser.new
#~class LALexer < RLTK::Lexer
#~rule(/A/) { [:A, 1] }
@@ -57,4 +57,4 @@
#~lexer = LALexer.new
#~parser = LAParser.new
-puts parser.parse(lexer.lex(ARGV[0]), {:parse_tree => 'calc.dot', :verbose => if ARGV[1] == 'true' then true else ARGV[1] end})
+puts parser.parse(lexer.lex(ARGV[0]), {:parse_tree => 'abs.dot', :verbose => if ARGV[1] == 'true' then true else ARGV[1] end})

0 comments on commit 3fe7548

Please sign in to comment.
Something went wrong with that request. Please try again.