GATE / CS ARCHIVE

GATE CSE 1995 / QUESTION 10(b)

Distinguish Pascal lexical and syntax errors

Compiler Design · Lexical Analysis · Full GATE CSE 1995 paper

GATE 1995, Q10(b) Descriptive
Report issue

Lexical Analysis

Question 10(b), GATE 1995: Identify errors in: if A > 1, then B = 2.5A else read(C);
Tap to enlarge
Answer & solution

Answer: Remove the comma, use := for assignment, and replace the malformed/adjacent 2.5A with a valid expression such as 2.5*A if multiplication was intended. (no official key was published)

The comma after 1 is not allowed between an if condition and then. Delete it. The then branch needs a statement; B=... is a comparison expression, not Pascal assignment, so use B:=.... Neither comma nor = is an invalid token by itself: their placement/use is a syntactic error.

The character sequence 2.5A is not one valid Pascal numeric literal. A scanner may report an invalid numeric suffix, or tokenize it as the real literal 2.5 followed by identifier A, depending on its lexical rules. In the latter case the parser still rejects the adjacent operands because no operator connects them. Pascal has no implicit multiplication. If multiplication was intended, write 2.5*A; if the A was accidental, deleting it is a different possible repair.

Under the multiplication interpretation a corrected statement is “if A > 1 then B := 2.5*A else read(C);”. Whether assigning this real-valued expression to B is type-correct additionally depends on B’s declaration, which the question does not provide. Calling all three faults lexical is imprecise; the solution distinguishes token formation from grammar and typing.

Original paper, page 6