Ok so i know go automatically inserts a semicolon if the last token in a input line is an identifier,a basic literal such as a number or string constant, or one of the tokens : break continue fallthrough return ++ -- ) }. Is this implemented in lexers grammar or parsers grammar?(i know lexer breaks the input into tokens). I want to see the grammar where this is implemented but i can't find any links for the lexer grammar. The go specification (https://golang.org/ref/spec) i believe has mostly parsers grammar and code examples. i am implementing a small language using antlr and bits of java and go grammar and i would like to omit the use of semicolon, how would i go about doing this? thanks for the help.
评论:
dragonrider22:
sseth:On mobile, but here's some starting points. The go/scanner package is good to look at as well as the go/parser package. The scanner struct has a Boolean field to determine if a semicolon needs to be added. This is set based on the token. You can see this at the bottom of the https://github.com/golang/go/blob/master/src/go/scanner/scanner.go file.
hobbified:The trick is to have the lexer inject semicolons at the end of lines only after certain types of tokens. In go you inject if the line ends in an identifier, integer, or right paren, etc.
Maybe think of a few places where go omits the semicolon and think about what type of token is at the end of a line.
For lexing I'd highly recommend watching Rob Pike's presentation. Also take a look at the text/template package. You will see the technique he's talking about put into practice there. https://www.youtube.com/watch?v=HxaD_trXwRE
For lexing, you may or may not want to use a code generator. You can write a very nice lexer without one.
Grammar is what parsers do, not lexers.
