Jump to content

User:J.saipratap

From Wikipedia, the free encyclopedia

The following code is an attempt to the ANTLR exercise



grammar ascend0.9.7_py2.6

definitions:

   /* empty */
   | definition+
   ;

definition:

   require_file
   | provide_module
   | import
   | add_method_def
   | replace_method_def
   | add_notes_def
   | constant_def
   | atom_def
   | model_def
   | definition_def
   | patch_def
   | units_def
   | global_def
   ;

global_def:

   GLOBAL ';' fstatements end ';'
   ;

require_file:

   REQUIRE DQUOTE ';'
   | REQUIRE name ';'
   | REQUIRE name
   ;

provide_module:

   PROVIDE DQUOTE ';'
   | PROVIDE name ';'
   | PROVIDE name
   ;

import :

   IMPORT IDENTIFIER FROM DQUOTE ';'
   ;

add_notes_def:

   add_notes_head notes_body end ';'
   ;

add_notes_head:

   ADD NOTES IN IDENTIFIER optional_method ';'
   ;

add_method_def:

   add_method_head proclist end ';'
   ;

add_method_head:

   ADD METHODS IN IDENTIFIER ';'
   | ADD METHODS IN DEFINITION MODEL ';'
   ;

replace_method_def:

   replace_method_head proclist end ';'
   ;

replace_method_head:

   REPLACE METHODS IN IDENTIFIER ';'
   | REPLACE METHODS IN DEFINITION MODEL ';'
   ;

atom_def:

   universal atom_head fstatements methods end ';'
   ;

atom_head:

   atom_id REFINES IDENTIFIER dims default_val ';'
   ;

atom_id:

   ATOM IDENTIFIER 
   ;

default_val:

   /* empty */
   | DEFAULT optional_sign number
   | DEFAULT FALSE
   | DEFAULT TRUE
   | DEFAULT SYMBOL
   ;

constant_def:

   universal constant_head
   ;

constant_head:

   CONSTANT IDENTIFIER REFINES IDENTIFIER dims constant_val
   optional_notes ';'
   ;

constant_val:

   /* empty */
   | CASSIGN optional_sign number
   | CASSIGN TRUE
   | CASSIGN FALSE
   | CASSIGN SYMBOL
   ;

model_def:

   universal model_head fstatements methods end ';'
   ;

model_head:

   model_id optional_model_parameters
   optional_parameter_wheres ';'
   | model_id optional_model_parameters optional_parameter_wheres
   REFINES IDENTIFIER optional_parameter_reduction ';'
   ;

model_id:

   MODEL IDENTIFIER 
   ;

optional_model_parameters:

   /* empty */
   |  '(' fstatements ')'
   ;

optional_parameter_wheres:

   | WHERE '(' fstatements ')'
   ;

optional_parameter_reduction:

   | '(' fstatements ')'
   ;

patch_def:

   patch_head fstatements methods end ';'
   ;

patch_head:

   PATCH IDENTIFIER FOR IDENTIFIER ';'
   ;

universal:

   /* empty */
   | UNIVERSAL
   ;

definition_def:

   definition_id fstatements methods end ';'
   ;

definition_id:

   DEFINITION IDENTIFIER
   ;


units_def:

   units_statement ';'
   ;

units_statement:

   UNITS unitdeflist end
   ;

unitdeflist:

   | unitdef+
   ;

unitdef:

   IDENTIFIER '=' BRACEDTEXT ';'
   ;


methods:

   /* empty */
   | METHODS
   proclist
   ;

proclist:

   proclistf
   ;

proclistf:

   | procedure+
   ;

procedure:

   procedure_id ';' fstatements end ';'
   ;

procedure_id:

   METHOD IDENTIFIER
   ;


fstatements:

   statements
   ;

statements:

   /* empty */
   |  statement+ ';'
   ;

statement:

   isa_statement
   | willbe_statement
   | aliases_statement
   | is_statement
   | isrefinedto_statement
   | arealike_statement
   | arethesame_statement
   | willbethesame_statement
   | willnotbethesame_statement
   | assignment_statement
   | relation_statement
   | glassbox_statement
   | blackbox_statement
   | call_statement
   | external_statement
   | for_statement
   | run_statement
   | fix_statement
   | free_statement
   | assert_statement
   | if_statement
   | while_statement
   | when_statement
   | use_statement
   | flow_statement
   | select_statement
   | switch_statement
   | conditional_statement
   | notes_statement
   | units_statement
   ;

isa_statement:

   fvarlist ISA type_identifier optional_of optional_with_value
   ;

willbe_statement:

   fvarlist WILLBE type_identifier optional_of optional_with_value
   ;

aliases_statement:

   fvarlist ALIASES fname
   | fvarlist ALIASES '(' fvarlist ')' WHERE fvarlist ISA
   IDENTIFIER OF IDENTIFIER optional_set_values
   ;

optional_set_values:

   /* empty */
   | WITH_VALUE_T '(' set ')'
   ;

is_statement:

   fvarlist IS IDENTIFIER optional_of
   ;

isrefinedto_statement:

   fvarlist ISREFINEDTO type_identifier
   ;

call_identifier:

   IDENTIFIER
   | IDENTIFIER '(' set ')'
   ;

type_identifier:

   IDENTIFIER
   | IDENTIFIER '(' set ')'
   ;

optional_method:

   /* empty */
   | METHOD IDENTIFIER
   ;

optional_of:

   /* empty */
   | OF IDENTIFIER
   ;

optional_with_value:

   | WITH_VALUE_T expr
   ;

arealike_statement:

   fvarlist AREALIKE
   ;

arethesame_statement:

   fvarlist ARETHESAME
   ;

willbethesame_statement:

   fvarlist WILLBETHESAME
   ;

willnotbethesame_statement:

   fvarlist WILLNOTBETHESAME
   ;

assignment_statement:

   fname ASSIGN expr
   | fname CASSIGN expr
   ;

relation_statement:

   relation
   | fname ':' relation
   ;

relation:

   expr
   | MINIMIZE expr
   | MAXIMIZE expr
   ;

blackbox_statement:

   fname ':' IDENTIFIER '(' input_args ';' output_args data_args ')'
   ;

input_args:

   fvarlist ':' INPUT
   ;

output_args:

   fvarlist ':' OUTPUT
   ;

data_args:

   /* empty */
   | ';' fname ':' DATA
   ;

glassbox_statement:

   fname ':' IDENTIFIER '(' fvarlist ';' INTEGER ')' optional_scope
   ;

optional_scope:

   /* empty */
   | IN fname
   ;

for_statement:

   FOR IDENTIFIER IN expr optional_direction forexprend 
   fstatements end
   ;

optional_direction:

   /* empty */
   | INCREASING
   | DECREASING
   ;

forexprend:

   CREATE
   | EXPECT
   | CHECK
   | DO
   ;

run_statement:

   RUN fname
   | RUN fname DBLCOLON fname
   ;

fix_statement: FIX fvarlist ;

free_statement: FREE fvarlist ;

external_statement:

   EXTERNAL IDENTIFIER '(' fvarlist ')'
   ;

call_statement:

   CALL call_identifier
   ;

assert_statement: ASSERT expr

   ;

if_statement:

   IF expr THEN fstatements optional_else end
   ;

while_statement:

   WHILE expr DO fstatements end
   ;

optional_else:

   | ELSE fstatements
   ;

when_statement:

   WHEN fvarlist whenlist end
   | fname ':' WHEN fvarlist whenlist end
   | WHEN '(' fvarlist ')' whenlist end
   | fname ':' WHEN '(' fvarlist ')' whenlist end
   ;

whenlist:

   whenlistf
   ;

whenlistf:

   (CASE set ':' fstatements)+
   | (OTHERWISE ':' fstatements)+
   ;

flow_statement:

   BREAK 
   | CONTINUE
   | FALLTHRU
   | RETURN
   | STOP optional_bracedtext
   ;

use_statement:

   USE fname
   ;

select_statement:

   SELECT fvarlist selectlist end
   | SELECT '(' fvarlist ')' selectlist end
   ;

selectlist:

   selectlistf
   ;

selectlistf:

  ( CASE set ':' fstatements)+
  | ( OTHERWISE ':' fstatements)+
   ;

switch_statement:

   SWITCH fvarlist switchlist end
   | SWITCH '(' fvarlist ')' switchlist end
   ;

switchlist:

   switchlistf
   ;

switchlistf:

  ( CASE set ':' fstatements)+;
   |( OTHERWISE ':' fstatements)+
   ;

conditional_statement:

   CONDITIONAL fstatements end
   ;

notes_statement:

   NOTES notes_body end
   ;

notes_body:

   (SYMBOL noteslist)+
 
   ;

noteslist:

   (fvarlist BRACEDTEXT)+
   ;

fvarlist:

   varlist
   ;

varlist:

   fname+
   | fname+ ',' fname
   ;

fname:

   name optional_notes
   ;

name:

   IDENTIFIER
   | IDENTIFIER+ '.' IDENTIFIER
   | IDENTIFIER+ '[' set ']'
   ;

end:

   END CONDITIONAL
   | END FOR
   | END IF
   | END INTERACTIVE
   | END METHODS
   | END NOTES
   | END SELECT
   | END SWITCH
   | END UNITS
   | END GLOBAL
   | END WHEN
   | END WHILE
   | END IDENTIFIER
   | END /* empty */
   ;

optional_bracedtext:

   /* empty */
   | BRACEDTEXT
   ;

optional_notes:

   /* empty */
   | DQUOTE
   ;

set:

   setexprlist
   | /* empty */
   ;

setexprlist:

   expr
   | expr DOTDOT expr
   setexprlist1
   ;
   

setexprlist1:

    setexprlist ',' expr
   | setexprlist ',' expr DOTDOT expr
   ;

number:

   INTEGER
   | realnumber
   ;

realnumber:

   REAL opunits
   | INTEGER BRACEDTEXT
   ;

opunits:

   /* empty */
   | BRACEDTEXT
   ;

dims:

   DIMENSION dimensions
   | DIMENSIONLESS
   | /* empty */
   ;

dimensions:

   '*'
   | dimexpr
   ;

dimexpr:

   IDENTIFIER
   | INTEGER
   dimexpr1;
   

dimexpr1:

   | dimexpr '/' dimexpr
   | dimexpr '*' dimexpr
   | dimexpr '^' fraction
   | '(' dimexpr ')'
   ;

fraction:

   optional_sign fractail
   ;

fractail:

   INTEGER
   | '(' INTEGER '/' INTEGER ')'
   ;

optional_sign:

   /* empty */
   | '+'
   | '-'
   ;

expr:

   INTEGER
   | MAXINTEGER
   | realnumber
   | MAXREAL
   | TRUE
   | FALSE
   | ANY
   | SYMBOL
   | fname
   | '[' set ']'
   | SATISFIED '(' fname ',' realnumber ')'
   | SATISFIED '(' fname ')'
   | SUM '(' set ')'
   | SUM '[' set ']'
   | PROD '(' set ')'
   | PROD '[' set ']'
   | UNION '(' set ')'
   | UNION '[' set ']'
   | INTERSECTION '(' set ')'
   | INTERSECTION '[' set ']'
   | CARD '(' set ')'
   | CARD '[' set ']'
   | CHOICE '(' set ')'
   | CHOICE '[' set ']'
   expr1
   ;

expr1 :

   | expr '+' expr
   | expr '-' expr
   | expr '*' expr
   | expr '/' expr
   | expr '^' expr
   | expr AND expr
   | expr OR expr
   | NOT expr
   | expr relop expr %prec NEQ
   | expr logrelop expr %prec BEQ
   | expr IN expr
   | expr '|' expr
   | expr SUCHTHAT expr
   | '+' expr %prec UPLUS
   | '-' expr %prec UMINUS
   | IDENTIFIER '(' expr ')'
   | '(' expr ')'
   ;

relop:

   '='
   | '<'
   | '>'
   | LEQ
   | GEQ 
   | NEQ 
   ;

logrelop:

   BEQ 
   | BNE
   ;
   

%prec :'precedes';


LEQ : '<=' ; GEQ :

       '>=';

NEQ :

       '<>';	

DOTDOT :

       '..';	

DBLCOLON:

       '::';	

ASSIGN :

       ':=';	

CASSIGN :

       ':==';	

BEQ  : '=='; BNE  : '!=';

'='  : '='; '>'  : '>'; '<'  : '<'; ','  : ','; '.'  : '.';  : ';'; ':'  : ':'; '['  : '['; ']'  : ']'; '('  : '('; ')'  : ')'; '+'  : '+'; '-'  : '-'; '*'  : '*'; '/'  : '/'; '^'  : '^'; '|'  : '|';

ADD : 'ADD'; ALIASES : 'ALIASES'; AND : 'AND'; ANY : 'ANY'; AREALIKE : 'AREALIKE'; ARETHESAME  : 'ARETHESAME'; ARRAY : 'ARRAY'; ATOM : 'ATOM'; BREAK : 'BREAK'; CALL : 'CALL'; CARD : 'CARD'; CASE : 'CASE'; CHECK : 'CHECK'; CHOICE : 'CHOICE'; CONDITIONAL : 'CONDITIONAL'; CONSTANT : 'CONSTANT'; CONTINUE : 'CONTINUE'; CREATE : 'CREATE'; DATA : 'DATA'; DECREASING : 'DECREASING'; DEFAULT : 'DEFAULT'; DEFINITION : 'DEFINITION'; DIMENSION : 'DIMENSION'; DIMENSIONLESS : 'DIMENSIONLESS'; DO : 'DO'; ELSE : 'ELSE'; END : 'END'; EXPECT : 'EXPECT'; EXTERNAL : 'EXTERNAL'; FALSE : 'FALSE'; FALLTHRU : 'FALLTHROUGH'; FIX  : 'FIX'; FREE  : 'FREE'; FOR : 'FOR'; FROM : 'FROM'; GLOBAL : 'GLOBAL'; ASSERT : 'ASSERT'; IF : 'IF'; IMPORT : 'IMPORT'; IN : 'IN'; INCREASING : 'INCREASING'; INPUT : 'INPUT'; INTERSECTION : 'INTERSECTION'; IS : 'IS'; ISA : 'ISA'; ISREFINEDTO : 'ISREFINEDTO'; MAXIMIZE : 'MAXIMIZE'; MAXINTEGER : 'MAXINTEGER'; MAXREAL : 'MAXREAL'; METHOD : 'METHOD'; METHODS : 'METHODS'; MINIMIZE : 'MINIMIZE'; MODEL : 'MODEL'; NOT : 'NOT'; NOTES : 'NOTES'; OF : 'OF'; OR : 'OR'; OTHERWISE : 'OTHERWISE'; OUTPUT : 'OUTPUT'; PATCH : 'PATCH'; PROD : 'PROD'; PROVIDE : 'PROVIDE'; REFINES : 'REFINES'; REPLACE : 'REPLACE'; REQUIRE : 'REQUIRE'; RUN : 'RUN'; SATISFIED : 'SATISFIED'; SELECT : 'SELECT'; SUCHTHAT : 'SUCHTHAT'; SUM : 'SUM'; SIZE : 'SIZE'; SWITCH : 'SWITCH'; STOP : 'STOP'; THEN : 'THEN'; TRUE : 'TRUE'; UNION : 'UNION'; UNITS : 'UNITS'; UNIVERSAL : 'UNIVERSAL'; USE : 'USE'; VALUE : 'VALUE'; WHEN : 'WHEN'; WHERE : 'WHERE'; WHILE : 'WHILE'; WILLBE : 'WILLBE'; WILLBETHESAME : 'WILLBETHESAME'; WILLNOTBETHESAME: 'WILLNOTBETHESAME'; WITH : 'WITH'; WITH_VALUE_T : 'WITHVALUE';

SYMBOL

    :   '\ | ~('\n')*';

IDENTIFIER  : ('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;

INTEGER :   '0'..'9'+;

REAL
    :   ('0'..'9')+ '.' ('0'..'9')* EXPONENT?
    |   '.' ('0'..'9')+ EXPONENT?
    |   ('0'..'9')+ EXPONENT
    ;

BRACEDTEXT : '{' ( '('+ ')'+)| ('{'+ '}'+) '}';


EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;

DQUOTE
    :  '"' 
    ;

UPLUS : '+' ; UMINUS : '-' ; RETURN : 'RETURN' ; INTERACTIVE : 'INTERACTIVE';