The Grammar ----------- The grammar is given in Extended Backus-Naur Form (EBNF). The meaning of the meta-symbols are as follows MetaSymbol Meaning ---------- ------- ::= is defined to be | alternatively [X] 0 or 1 instance of X {X} 0 or more instances of X (X | Y) a grouping: either X or Y "(" ")" "[" "]" The corresponding terminal XYZ The token corresponding to the terminal symbol xyz MetaIdentifier The non-terminal symbol MetaIdentifier In addition, we use ID, INT, and STR as the tokens retuened by the scanner for identifier, integer, and string, repsectively. The grammar is as follows: Program ::= PROGRAM ID ; [TypeDefinitions] [VariableDeclarations] [SubprogramDeclarations] CompoundStatement . TypeDefinitions ::= TYPE TypeDefinition ; {TypeDefinition ; } VariableDeclarations ::= VAR VariableDeclaration ; {VariableDeclaration ; } SubprogramDeclarations ::= {(ProcedureDeclaration | FunctionDeclaration) ; } TypeDefinition ::= ID = Type VariableDeclaration ::= IdentifierList : Type ProcedureDeclaration ::= PROCEDURE ID "(" FormalParameterList ")" ; ( Block | forward ) FunctionDeclaration ::= FUNCTION ID "(" FormalParameterList ")" : ResultType ; ( Block | FORWARD ) FormalParameterList ::= [IdentifierList : Type { ; IdentifierList : Type } ] Block ::= [VariableDeclarations] CompoundStatement CompoundStatement ::= BEGIN StatementSequence END StatementSequence ::= Statement { ; Statement } Statement ::= SimpleStatement | StructuredStatement SimpleStatement ::= [ (AssignmentStatement | ProcedureStatement) ] AssignmentStatement ::= Variable := Expression ProcedureStatement ::= ID "(" ActualParameterList ")" StructuredStatement ::= CompoundStatement | IF Expression THEN Statement [ ELSE Statement ] | WHILE Expression DO Statement | FOR ID := Expression TO Expression DO Statement Type ::= Basictype | ID | ARRAY "[" constant .. constant "]" of Type | RECORD FieldList END ResultType ::= Basictype | ID Basictype ::= INTEGER | STRING | BOOLEAN Fieldlist ::= [ IdentifierList : Type { ; IdentifierList : Type } ] Constant ::= [ Sign ] INT Expression ::= SimpleExpression [ RelationalOp SimpleExpression ] RelationalOp ::= < | <= | > | >= | <> | = SimpleExpression ::= [ Sign ] Term { AddOp Term } AddOp ::= + | - | OR Term ::= Factor { MulOp Factor } MulOp ::= * | DIV | MOD | AND Factor ::= INT | STR | Variable | FunctionReference | NOT Factor | "(" Expression ")" FunctionReference ::= ID "(" ActualParameterList ")" Variable ::= ID ComponentSelection ComponentSelection ::= [ ( . ID ComponentSelection | "[" Expression "]" ComponentSelection ) ] ActualParameterList ::= [ Expression { , Expression } ] IdentifierList ::= ID { , ID } Sign ::= + | - Note: In a conditional, an else is associated with the nearest if...then.