Skip to content

Instantly share code, notes, and snippets.

@tyler569
Last active September 22, 2018 01:05
Show Gist options
  • Save tyler569/233522b2a929fe2af21d68f9eef50a69 to your computer and use it in GitHub Desktop.
Save tyler569/233522b2a929fe2af21d68f9eef50a69 to your computer and use it in GitHub Desktop.

Revisions

  1. tyler569 revised this gist Sep 22, 2018. 1 changed file with 23 additions and 0 deletions.
    23 changes: 23 additions & 0 deletions with parens
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    tyler@xps:~/lcc$ python3 parse_test.py
    >
    >
    >
    > 2 - (2 - 2)
    [2, '-', '(', 2, '-', 2, ')']
    ('-', 2, ('-', 2, 2))
    2
    >
    > (2 - 2) - 2
    ['(', 2, '-', 2, ')', '-', 2]
    ('-', ('-', 2, 2), 2)
    -2
    >
    > (1 + 1) * 2
    ['(', 1, '+', 1, ')', '*', 2]
    ('*', ('+', 1, 1), 2)
    4
    > 1 + (1 * 2)
    [1, '+', '(', 1, '*', 2, ')']
    ('+', 1, ('*', 1, 2))
    3
    >
  2. tyler569 created this gist Sep 22, 2018.
    31 changes: 31 additions & 0 deletions infix parser
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    tyler@xps:~/lcc$ python3 parse_test.py
    >
    > 2+2
    [2, '+', 2]
    ('+', 2, 2)
    4
    > 2*2+2
    [2, '*', 2, '+', 2]
    ('+', ('*', 2, 2), 2)
    6
    > 2+2*2
    [2, '+', 2, '*', 2]
    ('+', 2, ('*', 2, 2))
    6
    > 2-2-2
    [2, '-', 2, '-', 2]
    ('-', ('-', 2, 2), 2)
    -2
    > 4/2-1
    [4, '/', 2, '-', 1]
    ('-', ('/', 4, 2), 1)
    1.0
    > 1-4/2
    [1, '-', 4, '/', 2]
    ('-', 1, ('/', 4, 2))
    -1.0
    > 2*2+3*4/5-3-4
    [2, '*', 2, '+', 3, '*', 4, '/', 5, '-', 3, '-', 4]
    ('-', ('-', ('+', ('*', 2, 2), ('*', 3, ('/', 4, 5))), 3), 4)
    -0.5999999999999996
    >