Creating a syntax tree from tokens

大憨熊 提交于 2019-12-12 03:00:09

问题


I'm trying to create a tiny interpreter for TI-BASIC syntax.

This is a snippet of TI-BASIC I'm trying to interpret

A->(2+(3*3))

I've tokenized the code above into this sequence of tokens:

Token{type=VARIABLE, content='A'}
Token{type=ASSIGN, content='null'}
Token{type=L_PAREN, content='null'}
Token{type=NUM, content='2'}
Token{type=ADD, content='null'}
Token{type=L_PAREN, content='null'}
Token{type=NUM, content='3'}
Token{type=MULT, content='null'}
Token{type=NUM, content='3'}
Token{type=R_PAREN, content='null'}
Token{type=R_PAREN, content='null'}
Token{type=EOS, content='null'} (end of statement)
Token{type=EOF, content='null'} (end of file)

If I'm not mistaken, I think the next step from here is to represent these tokens as a tree of statements (Abstract Syntax Tree?)

 Assignment (->)
    / \
   /   \
  A    Add
       /\
      /  \
     2  Multiply
           /\
          /  \
         3    3

I'm wondering how I should go about creating this tree, or if that's even the correct thing to do. Thanks!


回答1:


The simple answer is that you need to write a parser for TI-Basic. You've already written your lexer (lexical analyzer) and now you need to write your syntax analyzer.

There are many ways of doing this, but the Wikipedia page on parsers is a good place to start: examples of parsers.



来源:https://stackoverflow.com/questions/24661870/creating-a-syntax-tree-from-tokens

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!