Created
October 7, 2012 05:31
-
-
Save kini/3847209 to your computer and use it in GitHub Desktop.
Comparing ASTs in Python to show that "elif" is desugared in the parser
This file contains 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
[1] fs-boone@zhenghe ~/classes/cs321/hw1 $ cat print-ast.py | |
#!/usr/bin/env python | |
import sys, ast | |
print ast.dump(ast.parse(sys.stdin.read())) | |
[1] fs-boone@zhenghe ~/classes/cs321/hw1 $ cat test1 | |
if x == 1: | |
print 1 | |
elif x == 2: | |
print 3 | |
else: | |
print 'Something else' | |
[1] fs-boone@zhenghe ~/classes/cs321/hw1 $ cat test2 | |
if x == 1: | |
print 1 | |
else: | |
if x == 2: | |
print 3 | |
else: | |
print 'Something else' | |
[1] fs-boone@zhenghe ~/classes/cs321/hw1 $ ./print-ast.py < test1 | |
Module(body=[If(test=Compare(left=Name(id='x', ctx=Load()), ops=[Eq()], comparators=[Num(n=1)]), body=[Print(dest=None, values=[Num(n=1)], nl=True)], orelse=[If(test=Compare(left=Name(id='x', ctx=Load()), ops=[Eq()], comparators=[Num(n=2)]), body=[Print(dest=None, values=[Num(n=3)], nl=True)], orelse=[Print(dest=None, values=[Str(s='Something else')], nl=True)])])]) | |
[1] fs-boone@zhenghe ~/classes/cs321/hw1 $ ./print-ast.py < test2 | |
Module(body=[If(test=Compare(left=Name(id='x', ctx=Load()), ops=[Eq()], comparators=[Num(n=1)]), body=[Print(dest=None, values=[Num(n=1)], nl=True)], orelse=[If(test=Compare(left=Name(id='x', ctx=Load()), ops=[Eq()], comparators=[Num(n=2)]), body=[Print(dest=None, values=[Num(n=3)], nl=True)], orelse=[Print(dest=None, values=[Str(s='Something else')], nl=True)])])]) | |
[1] fs-boone@zhenghe ~/classes/cs321/hw1 $ diff -u <(./print-ast.py < test1) <(./print-ast.py < test2) | |
[1] fs-boone@zhenghe ~/classes/cs321/hw1 $ echo $? | |
0 | |
[1] fs-boone@zhenghe ~/classes/cs321/hw1 $ python --version | |
Python 2.7.3rc2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment