Syntax: unaryOp infinite recursion

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Syntax: unaryOp infinite recursion

xq1d1xw5w
The syntax for term is

term             : integerConstant
                    | stringConstant
                    | keywordConstant
                    | varName
                    | varName '[* expression ']'
                    | subroutineCall
                    | '(' expression ')'
                    | unaryOp term

My concern is with the last line, doesn't this allow y + ~---~-~-~-~--~x and so on ad infinitum?
Is this intended? Usually you would require parenthesis around each unary operator, so shouldn't the syntax be

term             unaryOp?
                    ( integerConstant
                    | stringConstant
                    | keywordConstant
                    | varName
                    | varName '[* expression ']'
                    | subroutineCall
                    | '(' expression ')'
                    )

or even better

term             ( unaryOp? integerConstant
                    | '~'? stringConstant
                    | '~'? keywordConstant
                    | unaryOp? varName
                    | unaryOp? varName '[* expression ']'
                    | unaryOp? subroutineCall
                    | unaryOp? '(' expression ')'
                    )

if we want to stop taking negative strings and booleans at syntax check.
Did you correct this implicitly in your code or are we just lucky that the provided Jack code is not malformed?

New user here, thanks for any answers

Reply | Threaded
Open this post in threaded view
|

Re: Syntax: unaryOp infinite recursion

bupjae
Provided jack syntax will accept ~---~-~-~-~--~x as valid expression.
It will also accept expression like ((((((((((((((((x)))))))))))))))) or any heavily recursive expression.

I think if a JackCompiler implementation can correctly translate ~x and -x and ~(x|y) to VM code, it will translate ~---~-~-~-~--~x without difficulty.