Difference between revisions of "CoCoA:Grammar"

From ApCoCoAWiki
Line 2: Line 2:
  
 
A few rules to start the formal language definition.
 
A few rules to start the formal language definition.
(They will slowly get more readable as soon as I get confidence with the wiki)
 
  
  [[PROGRAM]] ::= <[[COMMAND]]> | <math>\Lambda</math>
+
  [[PROGRAM]] ::= {{flist|[[COMMAND]]}} | <math>\Lambda</math>
  
 
  [[COMMAND]] ::= [[HELP_QUERY]] ''newline''
 
  [[COMMAND]] ::= [[HELP_QUERY]] ''newline''
Line 11: Line 10:
 
  | [[STATEMENT]];
 
  | [[STATEMENT]];
 
 
  [[HELP_QUERY]] ::= '''?''' <math>\langle</math>*<math>\rangle</math>
+
  [[HELP_QUERY]] ::= '''?''' {{flist|*}}
  
  [[PACKAGE_DECL]] ::= Package [[PACKAGE_ID]] <[[FUNCTION_DECL]]> End[Package]
+
  [[PACKAGE_DECL]] ::= Package [[PACKAGE_ID]] {{flist|[[FUNCTION_DECL]]}} End{{fopt|Package}}
  
  [[FUNCTION_DECL]] ::= Define [[IDENTIFIER]]([[PARAMS]]) [[STATEMENTS]] End[Define]
+
  [[FUNCTION_DECL]] ::= Define [[IDENTIFIER]]'''(''' [[PARAMS]] ''')''' [[STATEMENTS]] End{{fopt|Define}}
  | Define [Internal/External] [[IDENTIFIER]]([[PARAMS]])
+
| Define Internal [[IDENTIFIER]]'''(''' [[PARAMS]] ''')''';
 +
  | Define External [[IDENTIFIER]]'''(''' [[PARAMS]] ''')''';
  
  [[PARAMS]] ::= <[[PARAM]] ,>
+
  [[PARAMS]] ::= {{flist|[[PARAM]] ,}}
  
  [[PARAM]] ::= [ [[TYPE]] ] [[IDENTIFIER]]
+
  [[PARAM]] ::= {{fopt|[[TYPE]]}} [[IDENTIFIER]]
 +
 
 +
[[STATEMENTS]] ::= {{flist|[[STATEMENT]] ;}}
  
 
  [[STATEMENT]] ::= <math>\Lambda</math>
 
  [[STATEMENT]] ::= <math>\Lambda</math>
Line 26: Line 28:
 
  | [[EXPRESSION]]
 
  | [[EXPRESSION]]
 
  | [[FUNCTION_CALL]]
 
  | [[FUNCTION_CALL]]
  | Return [ [[EXPRESSION]] ]
+
  | Return {{fopt|[[EXPRESSION]]}}
 
  | Print [[EXPRESSIONS]]
 
  | Print [[EXPRESSIONS]]
  | PrintLn [ [[EXPRESSIONS]] ]
+
  | PrintLn {{fopt|[[EXPRESSIONS]]}}
  | If [[EXPRESSION]] Then [[STATEMENTS]] [ [[ELSE_LIST]] ] End[If]
+
  | If [[EXPRESSION]] Then [[STATEMENTS]] {{fopt|[[ELSE_LIST]]}} End{{fopt|If}}
  | While [[EXPRESSION]] Do [[STATEMENTS]] End[While]
+
  | While [[EXPRESSION]] Do [[STATEMENTS]] End{{fopt|While}}
 
  | Repeat [[STATEMENTS]] Until [[EXPRESSION]]
 
  | Repeat [[STATEMENTS]] Until [[EXPRESSION]]
  | For [[IDENTIFIER]] := [[EXPRESSION]] To [[EXPRESSION]] [ Step [[EXPRESSION]] ]
+
  | For [[IDENTIFIER]] := [[EXPRESSION]] To [[EXPRESSION]] {{fopt|Step [[EXPRESSION]]}}
  Do [[STATEMENTS]] End[For]
+
  Do [[STATEMENTS]] End{{fopt|For}}
  | ForEach [[IDENTIFIER]] In [[EXPRESSION]] Do [[STATEMENTS]] End[ForEach]
+
  | ForEach [[IDENTIFIER]] In [[EXPRESSION]] Do [[STATEMENTS]] End{{fopt|ForEach}}
 
  | Break
 
  | Break
  | Block [[STATEMENTS]] End[Block]
+
  | Block [[STATEMENTS]] End{{fopt|Block}}
  | Declare [ [[TYPE]] ] <[[IDENTIFIER]] ,>
+
  | Declare {{fopt|[[TYPE]]}} {{flist|[[IDENTIFIER]] ,}}
  | Delete <[[IDENTIFIER]] ,>
+
  | Delete {{flist|[[IDENTIFIER]] ,}}
 
  | Quit
 
  | Quit
 
 
Line 45: Line 47:
 
  [[LEFT_EXPR]] ::= [[IDENTIFIER]]
 
  [[LEFT_EXPR]] ::= [[IDENTIFIER]]
 
  | [[LEFT_EXPR]].[[IDENTIFIER]]
 
  | [[LEFT_EXPR]].[[IDENTIFIER]]
  | [[LEFT_EXPR]].'''['''<[[EXPRESSION]] ,>''']'''
+
  | [[LEFT_EXPR]].'''['''{{flist|[[EXPRESSION]] ,}}''']'''
  
  [[FUNCTION_CALL]] ::= [[FUNCTION_ID]](<[[EXPRESSION]] ,>)
+
  [[FUNCTION_CALL]] ::= [[FUNCTION_ID]]'''(''' {{flist|[[EXPRESSION]] ,}} ''')'''
  
  [[FUNCTION_ID]] ::= [[IDENTIFIER]] | [[EXPRESSION]].[[IDENTIFIER]] | [[PACKAGE_ID.IDENTIFIER]]
+
  [[FUNCTION_ID]] ::= [[IDENTIFIER]] | [[EXPRESSION]]'''.'''[[IDENTIFIER]] | [[PACKAGE_ID]]'''.'''[[IDENTIFIER]]

Revision as of 10:09, 20 May 2006

Base grammar

A few rules to start the formal language definition.

PROGRAM ::= COMMAND | 
COMMAND ::= HELP_QUERY newline
	| PACKAGE_DECL
	| FUNCTION_DECL
	| STATEMENT;
HELP_QUERY ::= ? *
PACKAGE_DECL ::= Package PACKAGE_ID FUNCTION_DECL EndPackage
FUNCTION_DECL ::= Define IDENTIFIER( PARAMS ) STATEMENTS EndDefine
	| Define Internal IDENTIFIER( PARAMS );
	| Define External IDENTIFIER( PARAMS );
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