您的位置:首页 > 其它

Tiny语言编译器:Tiny语法分析器模块

2010-12-23 09:57 176 查看
编译器的词法分析模块完成后就要进行语法分析模块的设计,实验的环境还是和上次词法分析模块的一样,在其基础上增加了语法分析的功能。

View Code

本次实验用的是自顶向下的方法来分析语法。  本次实验所使用到的数据结构如下:
节点类型:
typedef  enum {StmtK ,ExpK }NodeKind;
语句类型:
typedef  enum     { IfK,RepeatK,AssignK,ReadK,WriteK,WhileK,DeclareK }StmtKind;
相对于书中的例子多以一个DeclareK类型的语句。
表达式种类:
typedef  enum {OpK,ConstK,IdK,StringK}ExpKind;
用于类型检查的表打式类型:
typedef  enum {Void ,Integer,Boolean ,Strings}ExpType;
节点:
typedef struct treeNode
{
struct treeNode * child[MAXCHILDREN];
struct treeNode * sibling;
int lineno;
NodeKind nodekind;
union
{
StmtKind stmt;
ExpKind exp;
} kind;
union
{
TokenTypes op;
int val;
char * name;
} attr;
ExpType type;
}TreeNode;
进行语法分析所需要的语法分析的文法已经相对应的函数说明如下:

处理文法   declarations-> decl ; declarations |ε:
static TreeNode * declarations(void);
处理文法    decl -> type-specifier varlist:
static TreeNode * decl(void);
处理文法     type-specifier    -> int | bool | char
varlist    -> identifier { , identifier }
static TreeNode * varlist(ExpType type);

处理文法    stmt-sequence    -> statement { ; statement }
static TreeNode * stmt_sequence(void);
处理文法    statement    -> if-stmt | repeat-stmt | assign-stmt |                     read-stmt | write-stmt | while-stmt
static TreeNode * statement(void);
处理文法     if-stmt    -> if  bool-exp then stmt-sequence [else                         stmt-sequence] end
static TreeNode * if_stmt(void);
处理文法    10    repeat-stmt    -> repeat stmt-sequence until                         bool-exp
static TreeNode * repeat_stmt(void);
处理文法    11    assign-stmt    -> identifier:=exp
static TreeNode * assign_stmt(void);
处理文法    12 read-stmt -> read identifier
static TreeNode * read_stmt(void);
处理文法  13write-stmt    -> write exp
static TreeNode * write_stmt(void);
处理文法    8while-stmt -> while bool-exp do stmt-sequence end
static TreeNode * while_stmt(void);
处理文法    14    exp    -> arithmetic-exp | bool-exp | string-exp
static TreeNode * exp(void);
处理文法  15 arithmetic-exp    -> term { addop term }
static TreeNode * arithmetic_exp(void);
处理文法 20    bool-exp    -> bterm { or bterm }
static TreeNode * bool_exp(void);
处理文法  21    bterm    -> bfactor { and  bfactor}
static TreeNode * bterm(void);
处理文法 22    bfactor ->not bfactor| true |false| comparison-exp
static TreeNode * bfactor(void);
处理文法 23    comparison-exp -> arithmetic-exp comparison-op
arithmetic-exp
static TreeNode * comparison_exp(void);
处理文法    25string-exp-> string
static TreeNode * string_exp(void);
处理文法  17    term    -> factor { mulop factor }
static TreeNode * term(void);
处理文法 19    factor    ->  (arithmetic-exp) | number | identifier
static TreeNode * factor(void);


在写语法分析模块遇到比较困难的问题是:
一是declarations-> decl ; declarations |ε:文法产生的树节点的安排问题,我们是采用了相同类型的声明串用兄弟节点的身份串起来,例如:String a ,b, c;和声明串 int d ,e f ;所在的子树是兄弟节点的关系。然后声明串的内部 a, b,c又分别是兄弟节点,最总这三个节点由一个节点挂到刚才上面说所的声明串的孩子节点下。
二是,之前文法出现了点左公因子的问题,目前该程序由于时间问题,我们暂时不对其做处理,之前想过一些文法来处理,但总归是比理想,希望能在下次试验解决这个问题。

2010-12-23 09:56:22
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: