# Snowdream

posts - 403, comments - 310, trackbacks - 0, articles - 7
BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理
1. 考虑表达式3 + 4的语法分析树，exp( exp(number (3)), op(+), exp(number (4)) )。

2. 简单的算术表达式的抽象语法树的数据类型
typedef enum {Plus, Minus, Times} OpKind;
typedef
enum {OpKind, ConstKind} ExpKind;
typedef
struct streenode
{
ExpKind kind;
OpKind op;

struct streenode *lchild, *rchild;

int val;
} STreeNode;
typedef STreeNode
*SyntaxTree;

3. 简单算术文法的二义性解决

34 - 3 = 31, 31 * 42
3 * 42 = 126, 34 - 126

exp -> exp addop exp | term
term -> term mulop term | factor
mulop -> *
factor -> (exp) | number

exp -> exp addop term | term
term -> term mulop factor | factor
mulop -> *
factor -> (exp) | number

exp -> term addop exp | term

4. else 悬挂的问题

statement -> if-stmt | other
if-stmt -> if (exp) statement | if (exp) statement else statement
exp -> 0 | 1

statement -> matched-stmt | unmatched-stmt
matched-stmt -> if (exp) matched-stmt else matched-stmt | other
unmatched-stmt -> if (exp) statement | if (exp) matched-stmt else unmatched-stmt
exp -> 0|1

if (0)  // unmatched-stmt
if (1) other else other  // matched-stmt

if (1) then
if (0) then other
else other
endif
endif

### 评论

guoshuai0020000@gmail.com

guoshuai0020000@gmail.com

 只有注册用户登录后才能发表评论。 网站导航: 相关文章: