nlsandler / write_a_c_compiler

Test suite to help you write your own C compiler
https://norasandler.com/2017/11/29/Write-a-Compiler.html
MIT License
855 stars 97 forks source link

Stage 6 parser definition for conditional expressions looks incorrect. #4

Closed stevejb71 closed 5 years ago

stevejb71 commented 6 years ago
<exp> ::= <id> "=" <exp> | <conditional-exp>
<conditional-exp> ::= <logical-or-exp> "?" <exp> ":" <conditional-exp>

This looks wrong - it forces every expression to be a conditional. Something like the below would be better I think:

<conditional-exp> ::= <logical-or-exp> "?" <exp> ":" <conditional-exp> | <logical-or-exp>
nlsandler commented 5 years ago

Thanks for opening this issue! You're correct - this grammar rule would force every expression to be a conditional.

I updated the grammar rule in the blog post to fix this issue:

<conditional-exp> ::= <logical-or-exp> [ "?" <exp> ":" <conditional-exp> ]

Let me know if you'd like me to credit you in the post for catching that mistake!

stevejb71 commented 5 years ago

Yes, a small credit would be very nice. Thank you for offering and also for the great blog series.

nlsandler commented 5 years ago

Credit: https://norasandler.com/2018/02/25/Write-a-Compiler-6.html#fn5 Thanks again for catching that!