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

[2-parsers] Subtraction Issue #7

Open
nidhijaju opened this issue Jan 30, 2019 · 5 comments
Open

[2-parsers] Subtraction Issue #7

nidhijaju opened this issue Jan 30, 2019 · 5 comments

Comments

@nidhijaju
Copy link

The parser gives a syntax error when inputting 5-3 as it lexes it as 5 and -3 instead of 5 MINUS 3. This happens because the lexer tries to take in as many tokens as possible. Hence, even though it recognises the minus sign, it sees that there is a number followed by it which is why it takes it in as a negative number as opposed to a minus sign in subtraction.

@johnwickerson
Copy link
Contributor

johnwickerson commented Jan 30, 2019

Thanks for spotting this.

You could fix this by adding a unary minus-operator and not allowing integer literals to be prefixed with a minus sign. Then an input like -3 would be parsed as UMINUS( LITERAL(3)) rather than LITERAL(-3).

Or, you could just accept that the user has to always provide a space after the minus-operator. Since the provided specification has this limitation built-in, I think it would be not be reasonable for me to test your code with input like 5-3. In other words, you can assume that the input will always look like 5- 3 or 5 - 3, and never like 5 -3 or 5-3.

@ES5017
Copy link

ES5017 commented Feb 4, 2019

Just so you know, one of the TODO's is:

/* TODO-3 : Add support for (x+6) and (10-y). You'll need to add production rules, and create an AddOperator...

which might be misleading

@Darrekt
Copy link

Darrekt commented Feb 8, 2019

On this note, another ambiguity occurs in the case of
-2^2

Where standard mathematical associativity should equate it to (I believe)
( 0 - ( 2^2 ) )
but due to the absence of the unary negation operator, our parser would do
( -2 ^ 2 )
I would just like to ensure that this is intended behaviour. The attached standards for mathematical associativity do not describe this case. I would appreciate if this ambiguity could be cleared!

@lhl2617
Copy link

lhl2617 commented Feb 8, 2019

@Darrekt I believe since unary operators are not defined, -2^2 won't be tested; instead, 0 - 2 ^ 2 is a possible input.

@johnwickerson
Copy link
Contributor

Right. -2^2 has to be lexed as Literal(-2), ^, Literal(2), and hence parsed as (-2)^2. Not as -(2^2), which would presuppose a unary minus operator.

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

5 participants