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

Your little float-literal bug #149

Open
chetmurthy opened this issue Jun 18, 2020 · 2 comments
Open

Your little float-literal bug #149

chetmurthy opened this issue Jun 18, 2020 · 2 comments

Comments

@chetmurthy
Copy link

I think the problem is that all three of the bits that precede ['e' 'e'] are optional. The typical way to deal with this problem is to force one of the second or third decimal bits to be non-empty. I don't have an example to hand of this sort of thing, but I think you can probably find one in the ocaml compiler sources. But there are lots of examples in lex specifications all over the place, I'm sure.

let float_literal =
  ['+' '-']? ['0'-'9']*
  ('.' ['0'-'9']* )?
  (['e' 'E'] ['+' '-']? ['0'-'9'] ['0'-'9']*)?
@droyo
Copy link

droyo commented Dec 30, 2020

I'm sure you've seen this @mransan, but https://developers.google.com/protocol-buffers/docs/reference/proto3-spec contains the EBNF for proto files. You can do a pretty mechanical translation from the BNF notation to lex and yacc rules. For example, here is the part for float literals:

decimals  = decimalDigit { decimalDigit }
exponent  = ( "e" | "E" ) [ "+" | "-" ] decimals
floatLit = ( decimals "." [ decimals ] [ exponent ] | decimals exponent | "."decimals [ exponent ] ) | "inf" | "nan"

This translates almost verbatim to

let decimals = ['0' - '9']+
let exp = ( 'e' | 'E' ) ( '+' | '-' )? decimals
let float_literal = decimals '.' decimals? exp? | decimals exp | '.' decimals exp? | "inf" | "nan"

Which doesn't match "E1" and IMO is easier to read. For a more surgical approach I think you can get away with changing * to + on your first digit match.

@mransan
Copy link
Owner

mransan commented Dec 30, 2020

Thanks, it's going to take me a bit of time to make the fix (laptop need setup) but I'll try soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants