Browse Source

add docs ogrammar vs AST

master
Brett Langdon 10 years ago
parent
commit
c641286baa
1 changed files with 29 additions and 3 deletions
  1. +29
    -3
      README.md

+ 29
- 3
README.md View File

@ -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. 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 ```python
import parser import parser
import pprint import pprint
@ -53,7 +53,7 @@ if __name__ == '__main__':
``` ```
```bash ```bash
python3 parse.py <script.py>
python3 grammar.py <script.py>
``` ```
```bash ```bash
@ -107,6 +107,32 @@ $ python3 parse.py test.py
['ENDMARKER', '']] ['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 ### Compiler
The compiler will be up after the parser. The compiler will be responsible for converting the parsed AST into Python bytecode. The compiler will be up after the parser. The compiler will be responsible for converting the parsed AST into Python bytecode.


Loading…
Cancel
Save