Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/yacc: write docs for custom error message #10888

Closed
minux opened this issue May 17, 2015 · 6 comments
Closed

cmd/yacc: write docs for custom error message #10888

minux opened this issue May 17, 2015 · 6 comments

Comments

@minux
Copy link
Member

minux commented May 17, 2015

And preferably, with examples.

Please also put the feature into doc/go1.5.txt.

@andrewchambers
Copy link

Russ wrote this before, there are some references, assuming this still uses the same methods - http://research.swtch.com/yyerror

@rsc rsc modified the milestones: Unplanned, Go1.5 Jul 21, 2015
@bradfitz
Copy link
Contributor

cmd/yacc no longer used and doesn't live in the main repo anymore (https://golang.org/doc/go1.8#tool_yacc), so closing this issue.

@purpleidea
Copy link

Are there any docs on how to get the custom error messages? (eg: why was this closed?) I found yyErrorMessages in current generated go tool yacc code. I was wondering if anyone has some current pointers into getting this all going. I'm looking at implementing this technique: https://research.swtch.com/yyerror -- Thanks!

@mdempsky
Copy link
Member

mdempsky commented Jul 5, 2017

I suppose it could be recategorized as x/tools/cmd/yacc instead.

You can look at go.y, before it was removed: https://github.com/golang/go/blob/f1b919574cf8022d09afdece1f76b2c2b2529004/src/cmd/compile/internal/gc/go.y

The syntax is %error, a sequence of tokens, a : character, and then the custom error string. Basically the same as the old go.errors format described in Russ's blog post.

@purpleidea
Copy link

@mdempsky This was really helpful thanks!

Looking into the source code, and the generated code it seems that there's a global var: yyErrorVerbose, which is set to false, and which unless true prevents the display of these fancy new error messages... How does one set it to true?

It looks to be hardcoded with no flags, and sed tricks aren't very pretty. It's here: https://github.com/golang/tools/blob/master/cmd/goyacc/yacc.go#L3265 maybe the processing function has some weird trick, but I haven't read through all that code yet. It's a bit confusing at first glance.

Secondly, any chance you could give me a few more tips about writing the token sequences? In particular, given a grammar like this:

resource_body:
	/* end of list */
	{
		$$.fields = []*StmtResField{}
	}
|	resource_body resource_field
	{
		$$.fields = append($1.fields, $2.field)
	}
;
resource_field:
	IDENTIFIER ROCKET STRING COMMA
	{
		$$.field = &StmtResField{
			field: $1.str,
			kind: reflect.String,
			value: $3.str,
		}
	}
|	IDENTIFIER ROCKET INTEGER COMMA
	{
		$$.field = &StmtResField{
			field: $1.str,
			kind: reflect.Int64,
			value: $3.int64, // int64
		}
	}
;

How do you express a token list for the %error pattern that know what context it's in? For example, a missing comma. I tried a plain: IDENTIFIER ROCKET STRING but it generates the wrong state table entry. (I've been reading a lot of generated code :P)

Thanks in advance!
James

@purpleidea
Copy link

FYI: for future readers, by adding this:

func init() {
	yyErrorVerbose = true // set the global that enables showing full errors
}

to the file somewhere will cause the global to get set on import, and the errors will get shown.

@golang golang locked and limited conversation to collaborators Jul 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants