L
Luke A. Kanies
Hello,
I'm trying to write a simple parser using racc, and I'm apparently
retarded. I've looked at all of the example code I can find, including
rdtool, and I cannot seem to resolve this problem. I am convinced there
is something small I am missing, because it's apparently small enough that
I can't see the difference between my grammar/parser and everyone else's.
I always get the following error when I try to run my parser:
parse error on token '$end' => 'false'
The basic text is mine, but the error is kicked out by racc. For some
reason, racc is considering the $end token to be a parse error, rather
than considering it the end of parsing. I've tried understanding all the
code involved, including racc's parser.rb, the racc script itself, the
generated parser.rb file, and a good bit more. I just can't get it.
Here are the pertinent portions of my grammar file:
class Cricket:arser
token DEFINE NAME STRING PARAM LCURLY RCURLY VALUE
rule
file: objects
;
objects: object { [val[0]] }
| objects object { [val[0], val[1]].flatten }
;
object: DEFINE NAME LCURLY vars RCURLY {
Cricket::Object.create(val[1],val[3]) }
;
vars: var
| vars var
;
var: PARAM VALUE { [val[0],val[1]] }
;
end
----inner
def parse(src)
#puts "src is " + BLUE + src + RESET
@src = src
$invar = false
$inobject = false
$done = false
begin
do_parse
rescue SyntaxError
$stderr.print "Got a syntax error: " + $! + "\n"
exit
end
end
def next_token
....
if @src.length == 0
puts "returning end"
#return [false, 0]
#return [false, '$']
return [false, false]
end
....
end
As you can see, I've tried returning different types things, to no affect
(although the value of the $end token changes, the error still gets kicked
up).
The really strange thing is that the parser I wrote earlier had the same
problem _unless_ i passed a file (not a string) to yylex. This seems to
imply that the EOF from the file somehow avoids this error. I'm not using
yylex in this case (as my tokens are quite easy), and I'd really like to
just understand what the problem is.
Any pointers would be greatly appreciated, but apparently pointing me to
further example code is not helpful, unless you can point out how this
sample code avoids this error. Again, I expect it's something small, but
it's small enough that I've missed it twice now, both times having written
the grammar from scratch.
Thanks,
Luke
--
First they came for the hackers. But I never did anything
illegal with my computer, so I didn't speak up.
Then they came for the pornographers. But I thought there was
too much smut on the Internet anyway, so I didn't speak up.
Then they came for the anonymous remailers. But a lot of nasty
stuff gets sent from anon.penet.fi, so I didn't speak up.
Then they came for the encryption users. But I could never
figure out how to work PGP anyway, so I didn't speak up.
Then they came for me. And by that time there was no one left
to speak up.
-- Alara Rogers, Aleph Press
I'm trying to write a simple parser using racc, and I'm apparently
retarded. I've looked at all of the example code I can find, including
rdtool, and I cannot seem to resolve this problem. I am convinced there
is something small I am missing, because it's apparently small enough that
I can't see the difference between my grammar/parser and everyone else's.
I always get the following error when I try to run my parser:
parse error on token '$end' => 'false'
The basic text is mine, but the error is kicked out by racc. For some
reason, racc is considering the $end token to be a parse error, rather
than considering it the end of parsing. I've tried understanding all the
code involved, including racc's parser.rb, the racc script itself, the
generated parser.rb file, and a good bit more. I just can't get it.
Here are the pertinent portions of my grammar file:
class Cricket:arser
token DEFINE NAME STRING PARAM LCURLY RCURLY VALUE
rule
file: objects
;
objects: object { [val[0]] }
| objects object { [val[0], val[1]].flatten }
;
object: DEFINE NAME LCURLY vars RCURLY {
Cricket::Object.create(val[1],val[3]) }
;
vars: var
| vars var
;
var: PARAM VALUE { [val[0],val[1]] }
;
end
----inner
def parse(src)
#puts "src is " + BLUE + src + RESET
@src = src
$invar = false
$inobject = false
$done = false
begin
do_parse
rescue SyntaxError
$stderr.print "Got a syntax error: " + $! + "\n"
exit
end
end
def next_token
....
if @src.length == 0
puts "returning end"
#return [false, 0]
#return [false, '$']
return [false, false]
end
....
end
As you can see, I've tried returning different types things, to no affect
(although the value of the $end token changes, the error still gets kicked
up).
The really strange thing is that the parser I wrote earlier had the same
problem _unless_ i passed a file (not a string) to yylex. This seems to
imply that the EOF from the file somehow avoids this error. I'm not using
yylex in this case (as my tokens are quite easy), and I'd really like to
just understand what the problem is.
Any pointers would be greatly appreciated, but apparently pointing me to
further example code is not helpful, unless you can point out how this
sample code avoids this error. Again, I expect it's something small, but
it's small enough that I've missed it twice now, both times having written
the grammar from scratch.
Thanks,
Luke
--
First they came for the hackers. But I never did anything
illegal with my computer, so I didn't speak up.
Then they came for the pornographers. But I thought there was
too much smut on the Internet anyway, so I didn't speak up.
Then they came for the anonymous remailers. But a lot of nasty
stuff gets sent from anon.penet.fi, so I didn't speak up.
Then they came for the encryption users. But I could never
figure out how to work PGP anyway, so I didn't speak up.
Then they came for me. And by that time there was no one left
to speak up.
-- Alara Rogers, Aleph Press