Skip to content

Instantly share code, notes, and snippets.

@lukewilson2002
Last active August 25, 2018 17:12
Show Gist options
  • Save lukewilson2002/7ee22a281da2d374b76b42f94fef0b72 to your computer and use it in GitHub Desktop.
Save lukewilson2002/7ee22a281da2d374b76b42f94fef0b72 to your computer and use it in GitHub Desktop.
expression = additive_expr ;
additive_expr = multiplicative_expr, { ("+" | "-"), additive_expr } ;
multiplicative_expr = factor, { ("*" | "/"), multiplicative_expr } ;
factor = "(", additive_expr, ")"
| number ;
an expression:
number(num)
binary-operation(op, expression, expression)
fn parse_expression(tokens: array of tokens) returns expression:
return parse_additive_expr(tokens)
fn parse_additive_expr(tokens: array of tokens) returns expression:
let expr = parse_multiplicative_expr(tokens)
if the next token is '+' or '-':
let operation = consume token
let right_expr = parse_additive_expr(tokens)
return binary-operation(operation, expr, right_expr)
else:
return expr
fn parse_multiplicative_expr(tokens: array of tokens) returns expression:
let expr = parse_factor(tokens)
if the next token is '*' or '/':
let operation = consume token
let right_expr = parse_multiplicative_expr(tokens)
return binary-operation(operation, expr, right_expr)
else:
return expr
fn parse_factor(tokens: array of tokens) returns expression:
if the next tokens is a left_parenthesis:
let expr = parse_additive_expr(tokens)
make sure the next token is the closing parenthesis
return expr
else if the next token is a constant number:
return number(num)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment