I am trying to create a parser using golang's yacc tool. I found nex to simplify creating a lexer to give the parser. My problem now is that the generated parser has no method or field to give me access to the parsing result. I could just store the parse result in a global variable, but that seems wrong.
Currently I've added the following as an initial attempt to the top of my parser.y file:
type ResultParser interface {
yyParser // Generated parser interface
Result() s.Expr // s.Expr is an interface for the parsed result
}
func (p *yyParserImpl) Result() s.Expr {
return p.stack[1].expr
}
func NewResultParser() ResultParser {
return &yyParserImpl{}
}
Is there a recomended/better way of getting at the result from the parser?
(Since this feels like a bit of an abuse of the generator...)
$$.expr
. I want to get the root of that tree. The aboveResult
function seem to access the right result, but it feels a bit hacky. I'm not 100% sure thatp.stack[1]
will be where the root/parse result is always stored... – HawkeyedyyErrorMessages
variable :\ hmm.. – Hawkeyed