From: Jim Freeze Date: 2003-12-12T05:23:05+09:00 Subject: Re: problems with racc: $end token --h31gzZEtNLTqOjlF Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Friday, 12 December 2003 at 3:55:31 +0900, Luke A. Kanies wrote: Hi Luke I took your code and repeated the $end problem. I removed the problem by putting an optional return after your define { } block. See code attached: -- Jim Freeze ---------- Bubble Memory, n.: A derogatory term, usually referring to a person's intelligence. See also "vacuum tube". --h31gzZEtNLTqOjlF Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="gram.y" class CricketParser token DEFINE NAME STRING PARAM VALUE RETURN COMMENT WORD #INLINECOMMENT EOF rule start : objects ; objects: object { [val[0]] } | objects object { [val[0], val[1]].flatten } ; #object: DEFINE NAME '{' RETURN object: DEFINE WORD '{' RETURN vars '}' optreturns { Cricket.create( val[1],val[3]) } ; vars: var { [val[0]] } | vars var { [val[0], val[1]].flatten } ; #var: PARAM VALUE returns { [val[0],val[1]] } var: WORD WORD returns { [val[0],val[1]] } ; optreturns : /*none*/ | returns ; returns: return | returns return ; return: comment RETURN ; comment: # nothing | COMMENT ; # | INLINECOMMENT end ---- inner def parse(str) str.strip! str.gsub!(/[\r\f]/, "") @orig_lines = str.dup.split("\n") @line_no = 1 @q = [] until str.empty? do case str # # Remove comments. Leaves \n # when /\A[ \t]*[#;].*$/ str = $' # # Remove white space # when /\A[ \t]+/o #@q.push [:SPACE, " "] str = $' # # Tokenize new lines. Consolidate multiple NL into one. # when /\A\n+/o @line_no += $&.count("\n") unless @q.empty? @q.push [:RETURN, "\n"] unless [:RETURN].include?(@q.last[0]) end str = $' # keywords when /\A(define)\b/o id = $&.upcase.intern @q << [id, id] str = $' # WORD when /\A[a-zA-Z0-9_.@\-]+/o @q.push [:WORD, $&] str = $' # One character tokens else c = str[0,1] @q.push [c, c] str = str[1..-1] end#case puts "@q: #{@q.inspect}" end#until puts "@q: #{@q.inspect}" if $DEBUG do_parse end def next_token @q.shift end ---- footer if $0 == __FILE__ then file = <<-EOT # a comment define contact { contact_name vwf1607 ; inline comment alias Lawrence-Hubenak host_notification_period none host_notification_commands host-notify-by-email service_notification_period none service_notification_commands notify-by-email email lawrence.hubenak@hcahealthcare.com pager lawrence.hubenak@my2way.com } EOT class Cricket def initialize(a,b) @a,@b = a,b end def self.create(a,b) Cricket.new(a,b) end end#class Cricket puts 'parsing:' print file puts puts 'result:' p CricketParser.new.parse( file ) end --h31gzZEtNLTqOjlF--