From c641286baad7077953be039d31755b71366fc0c5 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sun, 20 Sep 2015 15:32:40 -0400 Subject: [PATCH] add docs ogrammar vs AST --- README.md | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 70fe94c..3054ff1 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,8 @@ So far I have a mostly working scanner/tokenizer. The main goal was to be able t Currently there are a few small differences between the output format, but the tokens being produced are the same. -### Parser -Next up is going to be writing the parser to be able to generate an AST which will match the form provided from: +### Grammar Parser +Next up is going to be writing the parser to be able to validate the source code grammar; which will match the form provided from: ```python import parser import pprint @@ -53,7 +53,7 @@ if __name__ == '__main__': ``` ```bash -python3 parse.py +python3 grammar.py ``` ```bash @@ -107,6 +107,32 @@ $ python3 parse.py test.py ['ENDMARKER', '']] ``` +### AST Parsing +AST parsing will take the validated source grammar and convert it into a valid AST. + +The goal is to get a similar AST output as the following: + +```python +import ast + + +def main(filename): + with open(filename, 'r') as fp: + contents = fp.read() + module = ast.parse(contents) + print(ast.dump(module)) + +if __name__ == '__main__': + import sys + main(sys.argv[1]) +``` + +```bash +$ echo "print('hello world')" > test.py +$ python3 parser.py test.py +Module(body=[Expr(value=Call(func=Name(id='print', ctx=Load()), args=[Str(s='hello world')], keywords=[]))]) +``` + ### Compiler The compiler will be up after the parser. The compiler will be responsible for converting the parsed AST into Python bytecode.