Better HN
topnewbestaskshowjobs
Log inSign up

priceishere

1 karma · joined Apr 1, 2026 (7mo ago) · 3 submissions

Submissions

comment 7mo ago · link · parent

An even simpler way imo, is explicit functions instead of a precedence table, then the code pretty much has the same structure as EBNF.

Need to parse * before +? Begin at add, have it call parse_mul for its left and right sides, and so on.

  parse_mul() {
    left = parse_literal()
    while(is_mul_token()) { // left associative
      right = parse_literal()
      make_mul_node(left, right)
    }
  }

  parse_add() {
    left = parse_mul()
    while(is_add_token()) { // left associative
      right = parse_mul()
      make_add_node(left, right)
    }
  }
Then just add more functions as you climb up the precedence levels.

comment 7mo ago · link · parent

But does it even conserve memory? Copying a string when you have the length is 2 bytes of machine code on x86 (rep movsb).

Remember, code takes up memory too.

comment 7mo ago · link · parent

How do you drop nulls in the middle of a string without requiring O(N) extra space to restore the original characters?