从令牌创建语法树 [英] Creating a syntax tree from tokens

查看:122
本文介绍了从令牌创建语法树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 TI-BASIC 语法创建一个微型解释器。 / p>

这是我要解释的TI-BASIC片段

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

我已经标记了代码

  Token {type = VARIABLE,content ='A'} 
Token {type = ASSIGN,content ='null'}
令牌{type = L_PAREN,content ='null'}
Token {type = NUM​​,content ='2'}
Token {type = ADD ,content ='null'}
令牌{type = L_PAREN,content ='null'}
令牌{type = NUM​​,content ='3'}
令牌{type = MULT,content ='null'}
令牌{type = NUM​​,内容='3'}
令牌{type = R_PAREN,内容='null'}
令牌{type = R_PAREN,content =' null'}
令牌{type = EOS,内容='null'}(语句结尾)
令牌{type = EOF,内容='null'}(文件结尾)

如果我没记错的话,我认为下一步是将这些表示为kens作为语句树(抽象语法树?)

 分配(->)
/ \
/ \
A加
/ \
/ \
2乘以
/ \
/ \
3 3

我想知道如何创建该树,或者这是否正确要做的事。谢谢!

解决方案

简单的答案是,您需要为TI-Basic编写一个解析器。您已经编写了词法分析器(词法分析器),现在需要编写语法分析器。



有很多方法可以执行此操作,但是解析器的Wikipedia页面是一个不错的起点:解析器示例


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!

解决方案

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.

这篇关于从令牌创建语法树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆