求解:编译器tiny+语法分析,一个语法不知道怎么推导

xiangxw 发布于 2010/11/19 21:50
阅读 314
收藏 1
EBNF如下,但bfactor不知道怎么推导,选(bool-exp)还是(comparison-exp)?

stmt-sequence -> statement { ; statement }

statement -> if-stmt | repeat-stmt | assign-stmt | read-stmt | write-stmt | while-stmt

while-stmt -> while bool-exp do stmt-sequence end

if-stmt -> if bool-exp then stmt-sequence [else stmt-sequence] end

repeat-stmt -> repeat stmt-sequence until bool-exp

assign-stmt -> identifier:=exp

read-stmt -> read identifier

write-stmt -> write exp

exp -> arithmetic-exp | bool-exp | string-exp| comparison-exp

comparison-exp -> arithmetic-exp comparison-op arithmetic-exp

comparison-op -> < | = | > | >= | <=

arithmetic-exp -> term { addop term }

addop -> + | -

term -> factor { mulop factor }

mulop -> * | /

factor -> (arithmetic-exp) | number | identifier

bool-exp -> bterm { or bterm }

bterm -> bfactor { and bfactor}

bfactor -> true | false | identifier | (bool-exp) | not bfactor | (comparison-exp)

string-exp -> string | identifier

加载中
0
J
JackPhone

bool-exp 是与、或运算,如:1 and 2

comparison-exp 是比较运算,如:1>=2

0
xiangxw
xiangxw

引用来自#2楼“JackPhone”的帖子

bool-exp 是与、或运算,如:1 and 2

comparison-exp 是比较运算,如:1>=2

根据文法,bool-exp也可以是1>=2,也可以是1>=2 and 2>=3

bool-exp -> bterm { or bterm }

comparison-exp -> arithmetic-exp comparison-op arithmetic-exp

我要写一个tiny+的编译器,语法分析用递归下降分析,你这样说还是不能解决选bool-exp 还是comparison-exp的问题

返回顶部
顶部