本脚本语言的上下无关文法如下:
Script -> function
| script function
Function -> SCRIPT stmt_sequence SCRIPT_END
Stmt_sequence -> stmt
| stmt stmt_stmtsequence
Stmt -> if_stmt
| assign_stmt
| decl_stmt
| loop_stmt
| cmd_stmt
If_stmt -> IF expression THEN stmt_sequence END
| IF expression THEN stmt_sequence ELSE stmt_sequence END
decl_stmt -> STRING param_sequence SEMI
| NUMBER param_sequence SEMI
| BOOL param_sequence SEMI
Cmd_stmt -> COMMAND QUOTE_LEFT param_sequence QUOTE_RIGHT SEMI
Loop_stmt -> LOOP stmt_sequence UNTIL expression SEMI
Assign_stmt -> factor ASSIGN expression SEMI
Param_sequence -> factor
| factor COMMA param_sequence
Expression -> factor op expression
| factor
Op -> =|<=|+|-|*|/
Factor -> ID|NUM|STR实现方式,词法分析跟语法树生成是同时进行的(因为没有回溯语法)递归下降法,其实实现起来真的很简单,只要能实现一个最简单的语法,摸清楚细节之后,其余的就是拼!未引入错误判断机制,请勿挑战语法错!.h文件,上面一截是词法分析的,用于getToken,下面一截用于语法树生成。忽略词法分析
posted on 2009-06-19 11:21 whitech 阅读(656) 评论(0) 编辑 收藏 引用
Powered by: C++博客 Copyright © whitech