Last active
August 25, 2018 17:12
-
-
Save lukewilson2002/7ee22a281da2d374b76b42f94fef0b72 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
expression = additive_expr ; | |
additive_expr = multiplicative_expr, { ("+" | "-"), additive_expr } ; | |
multiplicative_expr = factor, { ("*" | "/"), multiplicative_expr } ; | |
factor = "(", additive_expr, ")" | |
| number ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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