Context-free Grammar for Standard Pascal - PDF

Document Sample
Context-free Grammar for Standard Pascal - PDF Powered By Docstoc
					                   Context-free Grammar for Standard Pascal

    In these productions, non-terminals are denoted in italic typewriter surrounded
by angle brackets ( ). Literal terminals are in typewriter. Generic terminals are in
boldface. The symbols ‘|’, ‘∅’ and ‘⇒’ are part of the BNF description language rather
than Pascal. Items in roman are verbal descriptions.
    A few features (bugs?) are omitted. These include the set data type, labels and the
goto statement, the with statement, and variant records.

 1. Program ⇒ ProgramHeader          Declarations     CompoundStatement .
 2. ProgramHeader ⇒ program id ;
                    | program id ( IdList ) ;
 3. Declarations ⇒ ConstantDefinitions TypeDefinitions
                      VariableDeclarations ProcedureDeclarations
 4. ConstantDefinitions ⇒ ∅
                          | const ConstantDefinitionList
 5. ConstantDefinitionList ⇒ ConstantDef
                             | ConstantDef ConstantDefinitionList
 6. ConstantDef ⇒ id = constant ;
 7. TypeDeclarations ⇒ ∅
                       | type TypeDefinitionList
 8. TypeDefinitionList ⇒ TypeDef
                         | TypeDef TypeDefinitionList
 9. TypeDef ⇒ id = TypeSpecifier ;
10. VariableDeclarations ⇒ ∅
                           | var VariableDeclarationList
11. VariableDeclarationList ⇒ VariableDec
                              | VariableDec VariableDeclarationList
12. VariableDec ⇒ IdList : TypeSpecifier ;
13. ProcedureDeclarations ⇒ ∅
                           | ProcedureDec ProcedureDeclarations
14. ProcedureDec ⇒ ProcedureHeader forward ;
                   | ProcedureHeader Declarations CompoundStatement ;
                   | FunctionHeader forward ;
                   | FunctionHeader Declarations CompoundStatement ;
15. ProcedureHeader ⇒ procedure id Arguments ;
16. FunctionHeader ⇒ function id Arguments : TypeSpecifier ;
17. Arguments ⇒ ∅
                | ( ArgumentList )
18. ArgumentList ⇒ Arg
                   | Arg ; ArgumentList

                                          1
19. Arg ⇒ IdList : TypeSpecifier
         | var IdList : TypeSpecifer
20. CompoundStatement ⇒ begin StatementList end
21. StatementList ⇒     Statement
                   |    Statement ; StatementList
22. Statement ⇒ ∅
               | CompoundStatement
               | AssignmentStatement
               | ProcedureCall
               | ForStatement
               | WhileStatement
               | IfStatement
               | CaseStatement
               | RepeatStatement
23. AssignmentStatement ⇒ Variable := Expression
24. ProcedureCall ⇒ id Actuals
25. ForStatement ⇒ for id := Expression to Expression do Statement
                  | for id := Expression downto Expression do Statement
26. WhileStatement ⇒ while Expression do Statement
27. IfStatement ⇒ if Expression then Statement
                 | if Expression then Statement else Statement
28. RepeatStatement ⇒ repeat StatementList until Expression
29. CaseStatement ⇒ case Expression of CaseList end
30. CaseList ⇒      Case
              |     Case ; CaseList
31. Case ⇒ ConstantList : Statement
32. ConstantList ⇒ constant
                  | constant , ConstantList
33. Expression ⇒     SimpleExpression
                |    SimpleExpression       = SimpleExpression
                |    SimpleExpression       <> SimpleExpression
                |    SimpleExpression       < SimpleExpression
                |    SimpleExpression       <= SimpleExpression
                |    SimpleExpression       > SimpleExpression
                |    SimpleExpression       >= SimpleExpression
34. SimpleExpression ⇒     Term
                      |    SimpleExpression + Term
                      |    SimpleExpression - Term
                      |    SimpleExpression or Term

                                        2
35. Term ⇒      Factor
          |     Term * Factor
          |     Term / Factor
          |     Term div Factor
          |     Term mod Factor
          |     Term and Factor
36. Factor ⇒     ( Expression )
            |    + Factor
            |    - Factor
            |    not Factor
            |     FunctionCall
            |    constant
            |     Variable
37. FunctionCall ⇒ id Actuals
38. Actuals ⇒ ∅
             | ( ExpressionList )
39. ExpressionList ⇒     Expresssion
                    |    Expression , ExpressionList
40. Variable ⇒ id
              | Variable . id
              | Variable ^
              | Variable [ ExpressionList ]
41. TypeSpecifier ⇒     id
                   |    ^ TypeSpecifier
                   |    ( IdList )
                   |    constant .. constant
                   |    array [ DimensionList ] of TypeSpecifier
                   |    record FieldList end
                   |    file of TypeSpecifier
42. DimensionList ⇒     Dimension
                   |    Dimension , DimensionList
43. Dimension ⇒ constant .. constant
               | id
44. FieldList ⇒     Field
               |    Field ; FieldList
45. Field ⇒ IdList : TypeSpecifier
46. IdList ⇒ id
            | id , IdList


                                      3