Results 1 to 3 of 3

Thread: Getting a simple AST working in Python

  1. #1
    Join Date
    Dec 2015
    Beans
    13

    Getting a simple AST working in Python

    Hi all -

    For a very long time, I've had problems understanding abstract syntax trees and how to implement them for interpreters or compilers.
    I don't have a problem with lexing (creating tokens) but when it comes to arranging said tokens into an AST I hit a brick wall.

    I'm aware that there is this site here which looks at ASTs - https://ruslanspivak.com/lsbasi-part7/ - but the code there is under the MIT license and I want the code I put together to be "public domain" - this means that I can't use that code.

    Anyway - this is what I've come up with so far -
    Code:
    #  ast.py  
    
    #  This code is released to the public domain 
    #  under the Unlicense. 
    #  "Share and enjoy......"   :)  
    
    #  expr : term ((PLUS|MINUS) term)*
    #  term : factor ((MUL|DIV) factor)* 
    #  factor : INTEGER | LPAREN expr RPAREN 
    
    
    # Simple node-type for numbers and operators
    class node() 
      def __init__(self, ntype, nvalue): 
          self.ntype = ntype
          self.nvalue = nvalue 
          
      def __repr__(self): 
          print self.ntype, self.nvalue       
    
    
    # Binary node-type for number OP number.  
    class binnode()
      def __init__(self, ntype, op, left, right): 
          self.ntype = ntype
          self.op = op
          self.left = left
          self.right = right 
          
      def __repr__(self): 
          print self.ntype, self.op, self.left, self.right
    So, any help to develop this further is much appreciated!
    Many thanks - bye for now -
    - t

  2. #2
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,588
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Getting a simple AST working in Python

    For a program you need data structures and algorithm. You've made a start at some data structures but you are lacking an algorithm. Choose an algorithm https://en.wikipedia.org/wiki/Catego...ing_algorithms then have a go at coding it.

  3. #3
    Join Date
    Dec 2015
    Beans
    13

    Re: Getting a simple AST working in Python

    Quote Originally Posted by spjackson View Post
    For a program you need data structures and algorithm. You've made a start at some data structures but you are lacking an algorithm. Choose an algorithm https://en.wikipedia.org/wiki/Catego...ing_algorithms then have a go at coding it.
    Hi spjackson - thanks for your reply!

    Ok - I'll check out that link and will go from there.
    Thanks again, bye for now -

    - t.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •