鉴于以下EBNF语法(在维基百科上找到PL / 0),前面有“ODD”关键字的表达式是什么?我想将简单语言作为一个小项目来实现,但我似乎无法弄清楚这意味着什么。有没有人能够解释这个奇怪的表达式是什么,以便我可以正确地编写一个语言编译器?
program = block "." .
block = [ "const" ident "=" number {"," ident "=" number} ";"]
[ "var" ident {"," ident} ";"]
{ "procedure" ident ";" block ";" } statement .
statement = [ ident ":=" expression | "call" ident |
"begin" statement {";" statement } "end" |
"if" condition "then" statement |
"while" condition "do" statement ].
condition = "odd" expression |
expression ("="|"#"|"<"|"<="|">"|">=") expression .
expression = [ "+"|"-"] term { ("+"|"-") term}.
term = factor {("*"|"/") factor}.
factor = ident | number | "(" expression ")".
答案 0 :(得分:1)
EBNF语法指示 via 引号,ODD
是一系列终端符号。维基百科页面上的语法后面的示例证实了这一点。从语义上讲,它似乎是一个测试数值奇偶校验的谓词。
BEGIN
a := x;
b := y;
z := 0;
WHILE b > 0 DO BEGIN
IF ODD b THEN z := z + a;
a := 2 * a;
b := b / 2
END
END;
答案 1 :(得分:0)
我相信odd
类似于一个函数,它确定跟在它后面的表达式的值是否计算为奇数或偶数(整数)值,并在值为奇数时返回“true”。在C:
bool odd(int exp) { return (exp & 1) != 0; }