Difference between revisions of "CoCoA:Grammar"

From ApCoCoAWiki
Line 6: Line 6:
  
 
  [[COMMAND]] ::= [[HELP_QUERY]] ''newline''
 
  [[COMMAND]] ::= [[HELP_QUERY]] ''newline''
| [[PACKAGE_DECL]]
+
    | [[COMMENT]]
| [[FUNCTION_DECL]]
+
    | [[PACKAGE_DECL]]
| [[STATEMENT]];
+
    | [[FUNCTION_DECL]]
 +
    | [[STATEMENT]];
 
 
 
  [[HELP_QUERY]] ::= '''?''' {{flist|*}}
 
  [[HELP_QUERY]] ::= '''?''' {{flist|*}}
 +
 +
[[COMMENT]] ::= '''--''' {{flist|*}} ''newline''
 +
    | '''//''' {{flist|*}} ''newline''
 +
    | '''/*''' {{flist|*}} '''*/'''
  
 
  [[PACKAGE_DECL]] ::=
 
  [[PACKAGE_DECL]] ::=
    Package [[PACKAGE_ID]]
+
      Package [[PACKAGE_ID]]
      {{opt|{{flist|[[EXPORT_DECL]]}}}}
+
        {{fopt|{{flist|[[EXPORT_DECL]]}}}}
      {{opt|{{flist|[[VARIABLE_DECL]]}}}}
+
        {{fopt|{{flist|[[VARIABLE_DECL]]}}}}
      {{opt|{{flist|[[FUNCTION_DECL]]}}}}
+
        {{fopt|{{flist|[[FUNCTION_DECL]]}}}}
    EndPackage
+
      EndPackage
  
 
  [[FUNCTION_DECL]] ::=
 
  [[FUNCTION_DECL]] ::=
    Define [[IDENTIFIER]]'''(''' [[PARAMS]] ''')''' [[STATEMENTS]] EndDefine
+
      Define [[IDENTIFIER]]'''(''' [[PARAMS]] ''')''' [[STATEMENTS]] EndDefine
  | Define [[IDENTIFIER]]'''(''' [[PARAMS]] ''')''' BuiltIn
+
    | Define [[IDENTIFIER]]'''(''' [[PARAMS]] ''')''' BuiltIn
  | Define [[IDENTIFIER]]'''(''' [[PARAMS]] ''')''' PlugIn
+
    | Define [[IDENTIFIER]]'''(''' [[PARAMS]] ''')''' PlugIn
  
 
  [[PARAMS]] ::= {{flist|[[PARAM]] ,}}
 
  [[PARAMS]] ::= {{flist|[[PARAM]] ,}}
Line 31: Line 36:
  
 
  [[STATEMENT]] ::= <math>\Lambda</math>
 
  [[STATEMENT]] ::= <math>\Lambda</math>
| [[ASSIGNMENT]]
+
    | [[ASSIGNMENT]]
| [[EXPRESSION]]
+
    | [[EXPRESSION]]
| [[FUNCTION_CALL]]
+
    | [[FUNCTION_CALL]]
| Return {{fopt|[[EXPRESSION]]}}
+
    | Return {{fopt|[[EXPRESSION]]}}
| Print [[EXPRESSIONS]]
+
    | Print [[EXPRESSIONS]]
| PrintLn {{fopt|[[EXPRESSIONS]]}}
+
    | PrintLn {{fopt|[[EXPRESSIONS]]}}
| If [[EXPRESSION]] Then [[STATEMENTS]] {{fopt|[[ELSE_LIST]]}} EndIf
+
    | If [[EXPRESSION]] Then [[STATEMENTS]] {{fopt|[[ELSE_LIST]]}} EndIf
| While [[EXPRESSION]] Do [[STATEMENTS]] EndWhile
+
    | While [[EXPRESSION]] Do [[STATEMENTS]] EndWhile
| Repeat [[STATEMENTS]] Until [[EXPRESSION]]
+
    | Repeat [[STATEMENTS]] Until [[EXPRESSION]]
| For [[IDENTIFIER]] := [[EXPRESSION]] To [[EXPRESSION]] {{fopt|Step [[EXPRESSION]]}}
+
    | For [[IDENTIFIER]] := [[EXPRESSION]] To [[EXPRESSION]] {{fopt|Step [[EXPRESSION]]}}
Do [[STATEMENTS]] EndFor
+
    Do [[STATEMENTS]] EndFor
| ForEach [[IDENTIFIER]] In [[EXPRESSION]] Do [[STATEMENTS]] EndForEach
+
    | ForEach [[IDENTIFIER]] In [[EXPRESSION]] Do [[STATEMENTS]] EndForEach
| Break
+
    | Break
| Block [[STATEMENTS]] EndBlock
+
    | Block [[STATEMENTS]] EndBlock
| Declare {{fopt|[[TYPE]]}} {{flist|[[IDENTIFIER]] ,}}
+
    | Declare {{fopt|[[TYPE]]}} {{flist|[[IDENTIFIER]] ,}}
| Delete {{flist|[[IDENTIFIER]] ,}}
+
    | Delete {{flist|[[IDENTIFIER]] ,}}
| Quit
+
    | Quit
 
 
 
  [[ASSIGNMENT]] ::= [[LEFT_EXPRESSION]] ''':=''' [[EXPRESSION]]
 
  [[ASSIGNMENT]] ::= [[LEFT_EXPRESSION]] ''':=''' [[EXPRESSION]]

Revision as of 13:51, 6 June 2007

Base grammar

A few rules to start the formal language definition.

PROGRAM ::= COMMAND | 
COMMAND ::= HELP_QUERY newline
   | COMMENT
   | PACKAGE_DECL
   | FUNCTION_DECL
   | STATEMENT;
HELP_QUERY ::= ? *
COMMENT ::= -- * newline
   | // * newline
   | /* * */
PACKAGE_DECL ::=
     Package PACKAGE_ID
       EXPORT_DECL
       VARIABLE_DECL
       FUNCTION_DECL
     EndPackage
FUNCTION_DECL ::=
     Define IDENTIFIER( PARAMS ) STATEMENTS EndDefine
   | Define IDENTIFIER( PARAMS ) BuiltIn
   | Define IDENTIFIER( PARAMS ) PlugIn
PARAMS ::= PARAM ,
PARAM ::= TYPE IDENTIFIER
STATEMENTS ::= STATEMENT ;
STATEMENT ::= 
   | ASSIGNMENT
   | EXPRESSION
   | FUNCTION_CALL
   | Return EXPRESSION
   | Print EXPRESSIONS
   | PrintLn EXPRESSIONS
   | If EXPRESSION Then STATEMENTS ELSE_LIST EndIf
   | While EXPRESSION Do STATEMENTS EndWhile
   | Repeat STATEMENTS Until EXPRESSION
   | For IDENTIFIER := EXPRESSION To EXPRESSION Step EXPRESSION
   	Do STATEMENTS EndFor
   | ForEach IDENTIFIER In EXPRESSION Do STATEMENTS EndForEach
   | Break
   | Block STATEMENTS EndBlock
   | Declare TYPE IDENTIFIER ,
   | Delete IDENTIFIER ,
   | Quit
ASSIGNMENT ::= LEFT_EXPRESSION := EXPRESSION
LEFT_EXPR ::= IDENTIFIER
	| LEFT_EXPR.IDENTIFIER
	| LEFT_EXPR.[EXPRESSION ,]
FUNCTION_CALL ::= FUNCTION_ID( EXPRESSION , )
FUNCTION_ID ::= IDENTIFIER | EXPRESSION.IDENTIFIER | PACKAGE_ID.IDENTIFIER