Grammar CGPParser
ANTLR-generated HTML file from cgp/CGPParser.g

Terence Parr, MageLang Institute
ANTLR Version 2.7.0; 1989-1999

	
	package cgp;
	

/**
 * A conceptual graph language which embodies Guy Mineau's process formalism.
 * Copyright (C) 2000 David Benn
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Conceptual Graph Processes parser.
 *
 * David Benn, June-October 2000
 */
class CGPParser extends LLkParser


imaginaryTokenDefinitions
	:	OPTION_LIST TYPE_DECL VALUE_SELECTOR BLOCK IF_STATEMENT WHILE_STATEMENT FOREACH_STATEMENT CONDITION FUN_DEF ANON_FUN_DEF LAMBDA_DEF ACTOR_DEF PROCESS_DEF RULE_DEF PRE_DEF MATCH_EXPRESSION MUTATE_KB_EXPRESSION POST_DEF CALL ACTUAL_ARGS VAR_ASSIGN ATTR_ASSIGN LIST_ASSIGN MEMBER_FUNCALL ATTR_SELECTION LIST_SELECTION LIST CONCEPT_LITERAL GRAPH_LITERAL FILE_VALUE NEW_VALUE EMPTY_LIST TYPE_VALUE AND OR IS NOT 
	;


program
	:	( topLevelStatement )* 
	;


topLevelStatement
	:	( nodeKind IDENT ) => typeDecl 
	|	( "function" IDENT ) => funDef 
	|	( "lambda" IDENT ) => lambdaDef 
	|	( "actor" IDENT ) => actorDef 
	|	( "process" IDENT ) => processDef 
	|	optionList 
	|	statement 
	;


nodeKind
	:	"concept" 
	|	"relation" 
	;


typeDecl
	:	nodeKind IDENT ( GT IDENT )* SEMI 
	;


funDef
	:	"function" IDENT LPAREN 
		(	IDENT 
		|	
		) 
		( COMMA IDENT )* RPAREN block 
	;


lambdaDef
	:	"lambda" IDENT LPAREN IDENT ( COMMA IDENT )* RPAREN "is" expr 
	;


actorDef
	:	"actor" IDENT LPAREN IDENT ( COMMA IDENT )* RPAREN "is" expr 
	;


processDef
	:	"process" IDENT LPAREN 
		(	processParamKind IDENT 
		|	
		) 
		( COMMA processParamKind IDENT )* RPAREN 
		(	"initial" block 
		|	
		) 
		( ruleDef )* "end" 
	;


optionList
	:	"option" IDENT 
		(	GETS STRING 
		|	
		) 
		( COMMA IDENT 
			(	GETS STRING 
			|	
			) )* SEMI 
	;


statement
	:	intrinsicStatement 
	|	ifStatement 
	|	whileStatement 
	|	foreachStatement 
	|	( factor DOT IDENT actualArgs ) => memberFunCall SEMI 
	|	( factor actualArgs ) => call SEMI 
	|	assignment 
	|	SEMI 
	;


block
	:	( statement )* "end" 
	;


/** Expression productions **
  *
  * Precedence of operators (from lowest to highest)
  * -----------------------				Associativity
  *							-------------
  *	or							L	
  *	and							L
  *	> < >= <= == !=	is (is: type equivalence operator)	L
  *	+ -							L
  *	* div mod						L
  *	- not (unary negation and logical complement)		L
  *	[] . (list indexing and attribute/method selection operators)
  *
  *	Notes:  - The above prec/assoc table (except for rel ops) is a subset 
  *		  of Java's but some names have changed for readability's sake.
  *		- and & or are NOT short circuit operators.
  *
  */
expr
	:	andExpr ( "or" andExpr )* 
	;


actorParamKind
	:	"in" 
	|	"out" 
	;


processParamKind
	:	"in" 
	|	"out" 
	;


ruleDef
	:	"rule" IDENT 
		(	optionList 
		|	
		) 
		preDef postDef "end" 
	;


preDef
	:	"pre" 
		(	"action" block 
		|	
		) 
		( (	TILDE 
			|	
			) 
			matchExpression )* "end" 
	;


postDef
	:	"post" 
		(	"action" block 
		|	
		) 
		( mutateKBExpression 
			(	optionList 
			|	
			) )* "end" 
	;


matchExpression
	:	expr SEMI 
	;


mutateKBExpression
	:	expr SEMI 
	;


intrinsicStatement
	:	"print" expr SEMI 
	|	"println" expr SEMI 
	|	( "return" expr ) => "return" expr SEMI 
	|	"return" SEMI 
	|	( "exit" expr ) => "exit" expr SEMI 
	|	"exit" SEMI 
	|	"last" SEMI 
	|	"system" expr SEMI 
	|	"assert" expr SEMI 
	|	"retract" expr SEMI 
	|	"apply" expr expr SEMI 
	;


ifStatement
	:	"if" condition "then" block 
		(	"else" block 
		|	
		) 
		
	;


whileStatement
	:	"while" condition "do" block 
	;


foreachStatement
	:	"foreach" IDENT "in" expr "do" block 
	;


factor
	:	NUMBER 
	|	STRING 
	|	boolValue 
	|	listValue 
	|	graphValue 
	|	( "concept" GRAPH_STRING ) => conceptValue 
	|	( "file" factor ) => fileValue 
	|	( "function" LPAREN ) => anonFunDef 
	|	( "new" IDENT ) => newValue 
	|	typeValue 
	|	IDENT 
	|	LPAREN expr RPAREN 
	;


actualArgs
	:	LPAREN 
		(	expr 
		|	
		) 
		( COMMA expr )* RPAREN 
	;


memberFunCall
	:	factor DOT IDENT actualArgs 
	;


call
	:	factor actualArgs 
	;


assignment
	:	( IDENT GETS ) => IDENT GETS expr SEMI 
	|	( factor LBRACK ) => factor LBRACK expr RBRACK GETS expr SEMI 
	|	( factor DOT ) => factor DOT IDENT GETS expr SEMI 
	;


condition
	:	expr 
	;


andExpr
	:	relExpr ( "and" relExpr )* 
	;


relExpr
	:	addExpr 
		(	( GT addExpr ) 
		|	( LT addExpr ) 
		|	( GE addExpr ) 
		|	( LE addExpr ) 
		|	( EQ addExpr ) 
		|	( NE addExpr ) 
		|	( "is" addExpr ) 
		)* 
	;


addExpr
	:	mulExpr 
		(	( PLUS mulExpr ) 
		|	( MINUS mulExpr ) 
		)* 
	;


mulExpr
	:	unaryExpr 
		(	( MUL unaryExpr ) 
		|	( "div" unaryExpr ) 
		|	( "mod" unaryExpr ) 
		)* 
	;


unaryExpr
	:	MINUS selectionExpr 
	|	"not" selectionExpr 
	|	selectionExpr 
	;


selectionExpr
	:	callExpr 
		(	( DOT IDENT LPAREN ) => DOT IDENT actualArgs 
		|	( DOT IDENT ) 
		|	( LBRACK expr RBRACK ) 
		|	
		) 
	;


callExpr
	:	( "system" factor ) => "system" factor 
	|	( "activate" factor ) => "activate" factor 
	|	( "apply" factor ) => "apply" factor factor 
	|	( factor actualArgs ) => call 
	|	( factor ) => factor 
	;


boolValue
	:	"true" 
	|	"false" 
	;


listValue
	:	( LBRACE RBRACE ) => LBRACE RBRACE 
	|	LBRACE expr ( COMMA expr )* RBRACE 
	;


graphValue
	:	GRAPH_STRING 
	;


conceptValue
	:	"concept" GRAPH_STRING 
	;


fileValue
	:	"file" factor 
	;


anonFunDef
	:	"function" LPAREN 
		(	IDENT 
		|	
		) 
		( COMMA IDENT )* RPAREN block 
	;


newValue
	:	"new" factor 
	;


typeValue
	:	"number" 
	|	"string" 
	|	"boolean" 
	|	"concept" 
	|	"graph" 
	|	"list" 
	|	"function" 
	|	"lambda" 
	|	"actor" 
	|	"process" 
	|	"file" 
	|	"undefined" 
	;