5

A game for learning C operator precedence

 1 year ago
source link: https://www.devever.net/~hl/cprec
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

A game for learning C operator precedence

Have you ever added parentheses to a C expression because you weren't quite sure of operator precedence?

This is a game to learn C operator precedence. You must add parentheses to each subexpression without changing the meaning of the expression.

What is a ? b : c, -d with parentheses?

Type input and press Enter, or press Enter to skip.

Expression size (caution):

(If you really get stuck, your web browser's developer console may give some hints.)

Attempts made Total completed Skipped without trying

How it works

This was absurdly easy to make thanks to the excellent Tree-sitter library, which can parse practically any language and which supports WebAssembly. The game works by using a very simple custom generator to generate random C expressions, then parsing that expression using Tree-sitter. The user's input is also parsed and the two ASTs are compared. Fortunately, Tree-sitter does include parentheses expressions in its AST despite them being semantic noise, so checking that parentheses have been added by the user was also easy. Source code: just view source.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK