grammarpascalebnf

How can Pascal have conditions when it doesn't have boolean type?


Based on formal Pascal EBNF definition (pg69-75), I saw that Pascal only supports 3 primitive types: Integer, Real and String.

In C, any values which are different from 0 can be interpreted as a true literal. Pascal doesn't work like C. How can Pascal deal with conditional expressions when it doesn't have Boolean type?


Solution

  • The Pascal standard clearly defines the syntax and semantics of a Boolean-type.

    Quoting from the document you link to:

    6.4.2.2 Required simple-types

    The following types shall exist:

    c. Boolean-type. The required type-identifier Boolean shall denote the Boolean-type. The Boolean-type shall be an ordinal-type. The values shall be the enumeration of truth values denoted by the required constant-identifiers false and true, such that false is the predecessor of true. (page 16)

    The values true and false correspond to the EBNF production:

    constant = [ sign ] (constant-identifier | number) | string
    

    which can produce:

    constant = constant-identifier
    

    (since [ sign ] is optional)

    A constant-identifier is just an identifier.

    Also:

    6.7.2.3 Boolean operators

     Boolean-expression = expression .
    

    A Boolean-expression shall be an expression that denotes a value of Boolean-type. (page 49)

    Table 6 (on the following page) defines the operand and result types of the comparison operators (==, <=, >=, <>, <, > and in). In all cases, the result type is "Boolean-type".

    Finally:

    6.8.3.4 If-statements

    If the Boolean-expression of the if-statement yields the value true, the statement of the if-statement shall be executed. If the Boolean-expression yields the value false, the statement of the if-statement shall not be executed, and the statement of the else-part, if any, shall be executed. (page 54)