Search code examples
grammarebnfoberon

"odd" expression in PL/0


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 ")".

Solution

  • 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;