質問

Given the following EBNF grammar (found on wikipedia for PL/0), what is an expression preceded by the "ODD" keyword? I would like to implement the simple language as a small project but I can not seem to figure out what this means. Has anyone head of this before or able to interpret what an odd expression is so that I can write a compiler for the language correctly?

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 ")".
役に立ちましたか?

解決

The EBNF grammar indicates via quotation marks that ODD is a sequence of terminal symbols. This is confirmed by the examples following the grammar on the Wikipedia page. Semantically, it appears to be a predicate testing the parity of numeric values.

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;

他のヒント

I believe odd is analogous to a function that determines whether the value of the expression that follows it evaluates to an odd or even (integer) value, and returns 'true' when the value is odd. In C:

bool odd(int exp) { return (exp & 1) != 0; }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top