Context Free Grammar for mPascal


Given below is a context free grammar for mPascal.  We make changes to this grammar from year to year, and it is possible that some minor errors are introduced in the process that go undetected until the grammar is used in an actual parser.  If any modifications are necessary, we will change the grammar and post a message on our Announcements page. 

Note that names in blue below are names of tokens.  You can look back at the ebnf to find the regular expressions defining these tokens.  The regular expressions are not included here because they are technically not part of a context free grammars (one never includes rules with tokens on the left, because that would mean that they are nonterminals rather than tokens.


 

SystemGoal      --> Program $             
Program         --> ProgramHeading ";" Block "." 

ProgramHeading  --> "program" ProgramIdentifier 

Block           --> VariableDeclarationPart ProcedureAndFunctionDeclarationPart StatementPart             

 


 

VariableDeclarationPart  --> "var" VariableDeclaration ";" VariableDeclarationTail 
                         --> l 

VariableDeclarationTail  --> VariableDeclaration ";" VariableDeclarationTail 
                         --> l             
VariableDeclaration      --> Identifierlist ":" Type
Type                     --> "Integer" 
                         --> "Float"             

 


 

ProcedureAndFunctionDeclarationPart --> ProcedureDeclaration ProcedureAndFunctionDeclarationPart 
                                    --> FunctionDeclaration ProcedureAndFunctionDeclarationPart 
                                    --> l             
ProcedureDeclaration                --> ProcedureHeading ";" Block ";" 

FunctionDeclaration                 --> FunctionHeading ";" Block ";"             
ProcedureHeading                    --> "procedure" procedureIdentifier OptionalFormalParameterList 

FunctionHeading                     --> "function" functionIdentifier OptionalFormalParameterList Type

OptionalFormalParameterList         --> "(" FormalParameterSection FormalParameterSectionTail ")" 
                                    --> l             
FormalParameterSectionTail          --> ";" FormalParameterSection FormalParameterSectionTail 
                                    --> l 

FormalParameterSection              --> ValueParameterSection 
                                    --> VariableParameterSection             
ValueParameterSection               --> IdentifierList ":" Type

VariableParameterSection            --> "var" IdentifierList ":" Type            

 


 

StatementPart      --> CompoundStatement             
CompoundStatement  --> "begin" StatementSequence "end" 

StatementSequence  --> Statement StatementTail 

StatementTail      --> ";" Statement StatementTail 
                   --> l             

 


 

Statement           --> EmptyStatement 
                    --> CompoundStatement 
                    --> ReadStatement 
                    --> WriteStatement 
                    --> AssignmentStatement 
                    --> IfStatement 
                    --> WhileStatement 
                    --> RepeatStatement 
                    --> ForStatement 
                    --> ProcedureStatement             
EmptyStatement      --> l             
ReadStatement       --> "read" "(" ReadParameter ReadParameterTail ")" 

ReadParameterTail   --> "," ReadParameter ReadParameterTail 
                    --> l 

ReadParameter       --> VariableIdentifier             
WriteStatement      --> "write" "(" WriteParameter WriteParameterTail ")" 

WriteParameterTail  --> "," WriteParameter WriteParameterTail 
                    --> l 

WriteParameter      --> OrdinalExpression             
AssignmentStatement --> VariableIdentifier ":=" Expression 
                    --> FunctionIdentifier ":=" Expression             
IfStatement         --> "if" BooleanExpression "then" Statement OptionalElsePart 

OptionalElsePart    --> "else" Statement 
                    --> l             
RepeatStatement     --> "repeat" StatementSequence "until" BooleanExpression             
WhileStatement      --> "while" BooleanExpression "do" Statement             
ForStatement        --> "for" ControlVariable ":=" InitialValue StepValue FinalValue "do" Statement 

ControlVariable     --> VariableIdentifier 

InitialValue        --> OrdinalExpression 

StepValue           --> "to" 
                    --> "downto" 

FinalValue          --> OrdinalExpression             
ProcedureStatement  --> ProcedureIdentifier OptionalActualParameterList 

OptionalActualParameterList --> "(" ActualParameter ActualParameterTail ")" 
                    --> l 

ActualParameterTail --> "," ActualParameter ActualParameterTail 
                    --> l 

ActualParameter     --> OrdinalExpression             

 


 

Expression              --> SimpleExpression OptionalRelationalPart 

OptionalRelationalPart  --> RelationalOperator SimpleExpression 
                        --> l 

RelationalOperator      --> "=" 
                        --> "<" 
                        --> ">" 
                        --> "<=" 
                        --> ">=" 
                        --> "<>"             
SimpleExpression        --> OptionalSign Term TermTail 

TermTail                --> AddingOperator Term TermTail 
                        --> l 

OptionalSign            --> "+" 
                        --> "-" 
                        --> l 

AddingOperator          --> "+" 
                        --> "-" 
                        --> "or"             
Term                    --> Factor FactorTail             
FactorTail              --> MultiplyingOperator Factor FactorTail 
                        --> l             
MultiplyingOperator     --> "*" 
                        --> "div" 
                        --> "mod" 
                        --> "and"             
Factor                  --> UnsignedInteger 
                        --> VariableIdentifier 
                        --> "not" Factor 
                        --> "(" Expression ")" 
                        --> FunctionIdentifier OptionalActualParameterList             

 


 

ProgramIdentifier    --> Identifier 

VariableIdentifier   --> Identifier 

ProcedureIdentifier  --> Identifier 

FunctionIdentifier   --> Identifier             
BooleanExpression    --> Expression 

OrdinalExpression    --> Expression             
IdentifierList       --> Identifier IdentifierTail 

IdentifierTail       --> "," Identifier IdentifierTail 
                     --> l