%{ /*------------------------------------------------------------------- ###### # # ## ##### #### ###### ##### # # # # # # # # # # ###### # # # # #### ##### # # # ###### ##### # # ##### # # # # # # # # # # # # # # # #### ###### # # Versao <$Rsvision$$Date$> -------------------------------------------------------------------*/ int _accao=1; int in_sequence = 0; char CONTEXTO= 'd'; #include "version" char straux [30], command [50]; char option; int jjDEBUG =1; #include "sexp.h" #include "met.h" void erro(); struct t_tipo { char ti[5]; struct t_tipo *arg1, *arg2, *next; }; typedef struct t_tipo TIPO; typedef struct t_tipo *APTIPO; %} %union { char *str; } %{ /* *** prioridades e associatividades: *** */ %} %nonassoc minima %nonassoc ';' %left ',' %nonassoc FUNCAO %nonassoc LAMBDA %nonassoc baixa %nonassoc LET OTHER SETA IF THEN RELBEGIN RELEND PRE ESTADO %nonassoc PERTENCE ELSE RETUR ALL EXIST EXIST1 %nonassoc '>' %left IMPLICA %left AND %left OR %left NOT %nonassoc EQ NEQ LEQ GEQ '<' GT %left STRCAT %left ADD SUB %left MUL DIV %nonassoc NOTIN IN %left UNION %left INTER %left '-' %nonassoc '#' FSETA alta %left '^' %left PLUS %left '/' %left '\\ ' %left '.' %left '[' %left '(' %nonassoc IS ':' ORIO QUOTE %right maxima %nonassoc maximam %{ /* *** fim de definicao de prioridades *** */ /* *** Simbolos Terminais *** */ %} %token SIMB CONSTANTE INTEGER STRING BOOL %token SET LIST TYPE ENDTYPE P4 NIL RELACAO INCLUDE INCLUDEM DEBUGON %token DEBUGOFF QUIT MODEL ENDMODEL FSETA2 %{ /* *** fim de definicao de terminais *** */ /* *** Simbolos Nao Terminais *** */ %} %% especificacao : especificacao typesMod | especificacao bloco | especificacao ENDMODEL | especificacao MODEL SIMB | ; /*--( Definicao de tipos )-------------------------------------------*/ defTipo : idTipo '=' defTp ';' { } | idTipo P4 defTp ';' { } | error ';' { yyerrok; } ; idTipo: SIMB { } ; defTp: simpleType { } | alternativa { } | tuplo { } ; simpleType : SIMB { } | NIL { } | relacao { } | funcfin { } | lista { } | conjunto { } | segmentoInicial { } | '(' simpleType ')' { } ; tuplo : itemtuplo rtuplo { } ; rtuplo: itemtuplo rtuplo { } | { } ; itemtuplo : SIMB ':' simpleType %prec minima { } ; funcfin : simpleType SETA simpleType { } ; relacao : simpleType RELACAO simpleType { } ; lista : SIMB LIST { } | '(' simpleType ')' LIST { } | segmentoInicial LIST { } ; conjunto : SIMB SET { } | '(' simpleType ')' SET { } | segmentoInicial SET { } ; segmentoInicial : INTEGER { } ; alternativa : '[' simpleType ']' %prec minima { } | simpleType '|' simpleType %prec minima { } | alternativa '|' '[' simpleType ']' %prec minima { } | alternativa '|' simpleType %prec minima { } ; /*--( Definicao de tipos nos modulos )-------------------------------*/ typesMod : TYPE {CONTEXTO='t';} defTipo { } restDefTypesMod ENDTYPE {CONTEXTO='d';} ; restDefTypesMod : defTipo { } restDefTypesMod | ; /*--( Bloco = (deffuncao+include+teste)* )---------------------------*/ bloco : bloco elemento | elemento ; elemento : exp ';' { } | include { } | ESTADO SIMB ':' tipo ';' { } | directiva { } | deffuncao ';' { } | error ';' {yyerrok;} elemento | QUIT { return(0);} ; /*--( directivas de debug/outras )-----------------------------------*/ directiva : DEBUGON {jjDEBUG = 1;} | DEBUGOFF {jjDEBUG = 0;} ; /*--( Includes )-----------------------------------------------------*/ include : INCLUDE SIMB '.' SIMB { } | INCLUDE STRING { } | INCLUDE SIMB { } | INCLUDEM STRING { } | INCLUDEM SIMB '.' SIMB { } | INCLUDEM SIMB { } ; /*--( definicao de funcoes )-----------------------------------------*/ deffuncao : SIMB '(' explist ')' '=' exp { geratag($1); } | FUNCAO SIMB assinatura precond /* mensagem */ poscondicao { geratag($2); } ; poscondicao : estado retorno { } | retorno estado { } | retorno { } | estado { } | LET '(' deflist ')' IN poscondicao { } ; retorno : RETUR exp { } ; estado : ESTADO exp { } ; assinatura : '(' explist2 ')' resultado { } ; resultado : ':' resultado1 { } | { } ; resultado1 : resultado1 tipo { } | { } ; tipo : defTp { } ; explist2 : SIMB ':' tipo ',' explist2 { } | SIMB ':' tipo { } | SIMB ',' explist2 { } | SIMB { } | '(' SIMB ')' ',' explist2 { } | '(' SIMB ')' { } | { } ; precond : PRE exp { } | PRE exp SETA exp { } | { } ; /*--( definicao de variaveis de Estado )-----------------------------*/ defobj : SIMB atrib exp %prec baixa { } | SIMB '.' SIMB atrib exp %prec baixa { } ; atrib : PERTENCE ; /*--( Expressoes )---------------------------------------------------*/ exp : SIMB { } | '(' exp ')' { } | CONSTANTE { } | exp '(' explist ')' { } /* | '(' exp ')' '(' explist ')' { #ifdef NYAG $$=mklista(cons($2,$5)); #endif #ifdef PRETTY #endif } */ | FUNCAO assinatura precond /* mensagem */ poscondicao { } | LAMBDA '(' explist ')' '.' exp %prec PERTENCE { } | SIMB '(' explist ')' %prec maxima { gera_tag_pair($1); } | QUOTE '(' explist ')' { } | op_orio ORIO '(' exp ',' exp ')' { } | NIL { } | defobj { } | strexp { } | intexp { } | ffexp { } | relexp { } | seqexp { } | setexp { } | letexp { } | ifexp { } | boolexp { } | exp '\\ ' exp { } | exp '/' exp { } | exp '[' exp ']' %prec maxima { } | SIMB '.' SIMB { } ; strexp : STRING { } | exp STRCAT exp { } ; op_orio : SIMB | ADD | STRCAT | MUL | OR /* | '^' {$$=mkatconst("append");} */ | PLUS ; /*--( Expressoes Inteiras )----------------------------------------------*/ intexp : INTEGER { } | '#' exp { } | exp ADD exp { } | exp SUB exp { } | exp DIV exp { } | exp MUL exp { } ; /*--( Expressoes Bool )----------------------------------------------*/ boolexp : exp IN exp { } | exp NOTIN exp { } | exp EQ exp { } | exp AND exp { } | exp IMPLICA exp { } | exp OR exp { } | NOT exp { } | exp '<' exp { } | exp GT exp { } | exp '>' exp { } | exp LEQ exp { } | exp GEQ exp { } | exp NEQ exp { } | IS defTp '(' exp ')' { } | ALL '(' all ')' { } | EXIST '(' exist ')' { } | EXIST1 '(' exist1 ')' { } | BOOL { } | FunctExp ; FunctExp : exp SET { } | exp LIST { } | exp FSETA { } | FSETA2 exp { } /* | LAMBDA '(' explist ')' '.' exp %prec PERTENCE { #ifdef NYAG $$=lambdaexp($3,$6); #endif } */ ; all : SIMB PERTENCE exp ',' all { } | SIMB PERTENCE exp ':' exp { } ; exist : SIMB PERTENCE exp ',' exist { } | SIMB PERTENCE exp ':' exp { } ; exist1 : SIMB PERTENCE exp ',' exist1 { } | SIMB PERTENCE exp ':' exp { } ; /*--( Set e Seq )----------------------------------------------------*/ setexp : '{' compreensao '}' { } | '{' extensao '}' { } | '{' '}' { } | exp INTER exp { } | exp UNION exp { } | exp '-' exp { } /* | SIMB SET '(' exp ')' { #ifdef NYAG $$=_set( mkpair(mkatomo($1), mkatconst("_x_")), _from(mkatconst("_x_"),$4)); #endif } */ ; compreensao : exp '|' fromlist { } ; compreensao2 : exp '|' fromlist2 { } ; extensao : extensao ',' exp { } | exp { } seqexp2 : compreensao2 { } | extensao ',' exp '>' { } | exp '>' { } | '>' { } | exp ':' exp '>' { } ; seqexp : '<' {in_sequence++ ;} seqexp2 { } | exp '^' exp { } /* | SIMB LIST '(' exp ')' { #ifdef NYAG $$=funbin("seq", mkpair(mkatomo($1), mkatconst("_y_")), _from(mkatconst("_y_"),$4)); #endif } */ ; fromlist : from ',' fromlist { } | from { } ; fromlist2 : from ',' fromlist2 { } | from2 { } ; from : SIMB PERTENCE exp { } | SIMB PERTENCE exp ':' exp { } ; from2 : SIMB PERTENCE exp '>' { } | SIMB PERTENCE exp ':' exp '>' { } ; /*--( Funcoes finitas e relacoes binarias )--------------------------*/ ffexp : '[' ffcompreensao ']' { } | '[' exppairlist ']' { } | exp PLUS exp { } /* | SIMB FSETA '(' exp ')' { #ifdef NYAG $$=_ff1( _list2( mkpair( mkatomo($1),mkatconst("_x_")), _ap(copia($4),mkatconst("_x_"))), _from(mkatconst("_x_"),_dom($4))); #endif } | INTER SETA SIMB '(' exp ')' { #ifdef NYAG $$=_ff1( _list2(mkatconst("_x_"), mkpair( mkatomo($3), _ap(copia($5),mkatconst("_x_")))), _from(mkatconst("_x_"),_dom($5))); #endif } */ /* | exp SETA exp '(' exp ')' %prec maximam { #ifdef NYAG $$=_ff1( _list2( mkpair( $1,mkatconst("_x_")), mkpair( $3, _ap(copia($5),mkatconst("_x_")))), _from(mkatconst("_x_"),_dom($5))); #endif } */ ; ffcompreensao : exp SETA exp '|' fromlist { } ; exppairlist : exppairlist ',' exppair { } | exppair { } | { } ; exppair : exp SETA exp { } ; relexp : RELBEGIN ffcompreensao RELEND { } | RELBEGIN exppairlist RELEND { } ; explist : exp ',' explist { } | exp { } | { } ; /*--( Expressoes condicionais )--------------------------------------*/ letexp : LET '(' deflist ')' IN exp %prec LET { } ; deflist : def ',' deflist { } | def { } | comdef ',' deflist { } | comdef { } ; comdef : '<' SIMB ',' SIMB '>' '=' exp { } ; def : SIMB '=' exp { } | '=' exp { } ; ifexp : IF exp THEN exp { } | IF '(' expcaselist { } | IF exp THEN exp ELSE exp { } ; expcaselist : casepair ',' expcaselist { } | casepair ')' { } | ELSE SETA exp ')' { } | casepair ')' OTHER exp { } ; casepair : exp SETA exp { } | is_exp ; is_exp : exp IS '<' SIMB ':' SIMB '>' SETA exp { } | exp IS '<' '>' SETA exp { } | exp IS '{' SIMB ':' SIMB '}' SETA exp { } | exp IS '{' '}' SETA exp { } | exp IS '[' SIMB SETA SIMB ':' SIMB ']' SETA exp { } | exp IS '[' ']' SETA exp { } ; %% /* ---(Analisador lexico )----------------------------------------------*/ #include #include static int queres_comentarios=1; #include extern jmp_buf xljmpbuf ; #include "lex.yy.c" /* ---(Formata output )-------------------------------------------------*/ #if INTERACTIVE || FUNC #include "iformata.c" #else #include "formata.c" #endif /* ---(Funcoes auxiliares)----------------------------------------------*/ void met(s) char s []; { strcat(strcpy(straux,s),".met"); } char *aspas(s) char *s; {char *aux; aux=(char *) malloc(strlen(s)+3); return(strcat(strcat(strcpy(aux,"\""),s),"\"")); } #ifdef __TURBOC__ #define NAOBUF #else #define NAOBUF setvbuf(yyout,buff,_IOLBF,BUFSIZ) #endif __TURBOC__ /* ---(main )-----------------------------------------------------------*/ FILE *ftags; char fichin[100]; char buff[BUFSIZ]; int _ERRO=0; char *FILENAME=""; #ifdef FUNC char * S_GLOBI; char * S_GLOBO; int seca(S,S2) char * S; char * S2; { line_read=(char*)NULL; S_GLOBI=S; S_GLOBO=S2; yyparse(); return(_ERRO); } /* ---(Tratamento de erros em FUNC)-------------------------------------*/ void erro(s) char *s; { _ERRO=1;} void yyerror() {_ERRO=1;} #else /*FUNC*/ extern int neverprompt; main (argc,argv) int argc; char *argv[]; { #ifdef INTERACTIVE int i; char S[200]; #ifdef PIPE neverprompt=1; #endif /*PIPE*/ #ifndef PIPE printversion(); printf("camila version %s (%s)\n",cam_version, cam_date); #endif /*PIPE*/ libxminit(0,NULL); /* inicia xmetoo */ sprintf(S,"(load \"%s/.metoorc\")",getenv("HOME")); #ifndef PIPE puts(S+7); #endif /*PIPE*/ libxmevalstr(S); libxmevalstr("(def nload lambda (f) (progn (sh (strcat \"seca \" f \" _tmp\"))(load \"_tmp\"))) "); if((argc ==3) && (!strcmp(argv[1],"-n"))) { #ifdef PIPE yyin=fopen(argv[2],"r"); #else /*PIPE*/ freopen(argv[2],"r",stdin); #endif /*PIPE*/ FILENAME = strdup(argv[2]); yyout = stdout; queres_comentarios=0; NAOBUF; } else for(i=1; i< argc ; i++) { puts(argv[i]); sprintf(S,"(nload \"%s\" ) ",argv[i]); libxmevalstr(S);} yyparse(); #else if((argc ==3) && (!strcmp(argv[1],"-n"))) { yyin = fopen(argv[2],"r"); FILENAME = strdup(argv[2]); fprintf(stderr,"DEBUG1: %s %s %s\n",argv[0],argv[1], argv[2]); yyout = stdout; queres_comentarios=0; NAOBUF; yyparse(); } else if((argc ==2) && (!strcmp(argv[1],"-n"))) { yyin = stdin; FILENAME = strdup(""); fprintf(stderr,"DEBUG2: %s -n\n",argv[0]); yyout = stdout; queres_comentarios=0; NAOBUF; yyparse(); } /* tags : seca -t file */ else if((argc ==3) && (!strcmp(argv[1],"-t"))) { _accao = 2; yyin = fopen(argv[2],"r"); FILENAME = strdup(argv[2]); if(yyin) { ftags=fopen("tags.aux","a"); queres_comentarios=0; strcpy(fichin,argv[2]); yyparse(); } else fprintf(stderr,".can't open file '%s'\n",argv[3]); } /* prettyp : seca -p infile outfile */ else if((argc == 4) && (!strcmp(argv[1],"-p"))) { _accao = 3; yyin = fopen(argv[2],"r"); FILENAME = strdup(argv[2]); if(yyin) { yyout = fopen(argv[3],"w+"); yyparse();} else fprintf(stderr,"..can't open file '%s'\n",argv[2]); } /* nyag : seca filein fileout */ else if(argc == 3) { _accao = 1; yyin = fopen(argv[1],"r"); FILENAME = strdup(argv[1]); if(yyin) { yyout = fopen(argv[2],"w+"); yyparse();} else fprintf(stderr,"...can't open file '%s'\n",argv[1]); } /* nyag : seca file */ else if(argc ==2) { _accao = 1; yyin = fopen(argv[1],"r"); FILENAME = strdup(argv[1]); if(yyin) yyparse(); else fprintf(stderr,"....can't open file '%s'\n",argv[1]); } else if(argc == 1) yyparse(); else fprintf(stderr,"Use:\n%s [filein [fileout]]\n -p filein fileout\n -t filein\n",argv[0]); return _ERRO; #endif } /* ---(Tratamento de erros )--------------------------------------------*/ void erro(s) char *s; { _ERRO=1;fprintf(stderr,"%s %d : semantic ERROR: %s\n",FILENAME, yylineno,s);} void yyerror() { _ERRO=1;fprintf(stderr,"%s %d : syntatic ERROR detected in '%s'\n", FILENAME, yylineno,yytext);} #endif /*FUNC */ yywrap() { return (1);} /* ---(Tratamento de tipos )--------------------------------------------*/ #include "secatipo.c" /*---(Gerador de TAGS )------------------------------------------------ */ static char func_name[30]; void gera_tag_pair(nome) char *nome; { printf("%s\n",nome); } void geratag(nome) char *nome; { fprintf(ftags,"%s %s /FUNC\\ [ ]\\ *\\ <%s\\ >/\n",nome,fichin,nome); printf(" %s\n\n",nome); }