Worked example 1
For the following line of Pascal code, , describe the output of the Lexical Analysis stage and draw a simplified representation of the parse tree created during Syntax Analysis.
Show solution outline
Mark Scheme Breakdown:
1. Lexical Analysis (2 marks):
- The lexical analyser removes whitespace and breaks the code into a sequence of tokens. (1 mark for process)
- The token stream would be: IDENTIFIER('Score'), , IDENTIFIER('Score'), , , SEMICOLON(';'). (1 mark for correct token sequence)
2. Syntax Analysis (Parse Tree) (2 marks):
- The syntax analyser uses the token stream to build a parse tree to check grammatical structure. (1 mark for purpose)
- The tree would represent the assignment. A simplified text representation is:
ASSIGN (:=)
/ \
IDENTIFIER(Score) EXPRESSION (+)
/ \
IDENTIFIER(Score) INTEGER(10)
(1 mark for a correctly structured tree showing the assignment and the addition as a sub-tree).