old-parser.y 13.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
/*****************************************************************************/
/*      								     */
/*   Projecto de Compiladores ---------------------------------- VSPL        */
/*		                                        	 Level 0     */
/*		                                        		     */
/*   Bison Parser Specification.					     */
/*		        						     */
/*						    Salvador Pinto Abreu     */
/*									     */
/*****************************************************************************/

/*****************************************************************************/
/*									     */
/*                           	     Prelude.   			     */
/*									     */
/*****************************************************************************/

%{

#include <stdio.h>
#include <ctype.h>
#include <strings.h>
#include <ctype.h>

#include "types.h"

static int guardNo = 0;

#define APT(pop, push) \
{ \
    if (pop) \
	printf ("[%s|_Tail] ==> [", pop); \
    else \
	printf ("_Tail ==> ["); \
    printf push; \
    printf ("|_Tail].\n"); \
}
%}

%union {
    UNION_DEFS();
}

/*****************************************************************************/
/*									     */
/*                            Terminal declarations.  			     */
/*									     */
/*****************************************************************************/

%token ID				/* An identifier */

%token INT				/* The INT reserved word */
%token REAL				/* The REAL reserved word */
%token BOOL				/* The BOOL reserved word */
%token VOID				/* The VOID reserved word */

%token MAP				/* The MAP reserved word */
%token CLASS				/* The CLASS reserved word */
%token OP				/* The OP reserved word */

%token INT_LIT				/* An INT literal */
%token REAL_LIT				/* A REAL literal */
%token BOOL_LIT				/* A BOOL literal */

%token RETURN				/* The RETURN reserved word */
%token BREAK				/* The BREAK reserved word */
%token SKIP				/* The SKIP reserved word */
%token RETRY				/* The RETRY reserved word */
%token ELSE				/* The ELSE reserved word */

%token COND				/* The COND reserved word  (?) */
%token WHILE				/* The WHILE reserved word (*) */

%token IDENTICAL			/* The "==" token */
%token ASSIGN				/* The ":=" token */
%token RETURNS				/* The "->" token */

%token AND				/* The AND operator */
%token OR				/* The OR operator */
%token NOT				/* The NOT operator */

%token LT				/* The "<" operator */
%token LE				/* The "<=" operator */
%token EQ				/* The "=" operator */
%token NE				/* The "~=" operator */
%token GE				/* The ">=" operator */
%token GT				/* The ">" operator */

/*****************************************************************************/
/*									     */
/*                            Operator Precedences.  			     */
/*									     */
/*****************************************************************************/

%nonassoc ':' ';'			/* ID/type separator */
%nonassoc RETURN ASSIGN RETURNS		/* Must do it because of <exp> */

%right    ','				/* Tuple operator */

%left     OR
%left     AND
%right    NOT

%nonassoc LT LE EQ NE GE GT

%left     '+' '-'
%left     '*' '/' '%'
%nonassoc UMINUS

%nonassoc '(' ')' '[' ']' '{' '}'	/* Well, not really, but... */
					/* Have to list these after "->" */
%left     '.'				/* Qualified name constructor */

/*****************************************************************************/
/*									     */
/*                      Grammar symbol type declarations.		     */
/*									     */
/*****************************************************************************/

%type <id>       ID

%type <lit.ival> INT_LIT
%type <lit.rval> REAL_LIT
%type <lit.bval> BOOL_LIT

/*****************************************************************************/
/*									     */
/*                                Start Symbol.  			     */
/*									     */
/*****************************************************************************/

%start program

%%

program :
          globals			/* Global declarations */
	;

/*****************************************************************************/
/*									     */
/*                                Declarations.  			     */
/*									     */
/*****************************************************************************/


decl :					/* Name declaration: */
       ids ':' type			/* Variable, Explicit type, Undef */
		{ APT ("T,I", ("decl_list(I, var, T)")); }
     | ID  ':' type			/* Variable, Explicit type, Undef */
		{ APT ("T",   ("decl(id('%s'), var, T)", $1)); }
     | ID  ':' type ASSIGN exp		/* Variable, Explicit type, Init */
		{ APT ("V,T", ("V,decl(id('%s'), var, T)", $1));
		  APT ("V",   ("assign(id('%s'), V)", $1)); }
     | ID  ':' type EQ exp		/* Constant, Explicit type */
		{ APT ("V,T", ("decl(id('%s'), const(V), T)", $1)); }

     | ID           EQ exp		/* Constant, Implicit type */
		{ APT ("V",   ("decl(id('%s'), const(V), T)", $1)); }
     ;



/******************************************************************************/

formals :				/* Formal declarations: */
		{ APT (NULL,  ("[]")); }
	| formal_list
		{ }
	;

formal_list :				/* List of formal declarations */
	  formal
		{ APT ("X",   ("[X]")); }
        | formal ';' formal_list
		{ APT ("T,H", ("[H|T]")); }
        | error
		{ yyerrok; yyclearin;
		  error ("expected s formal declaration.");
		  APT (NULL, ("error(formals)")); }
        ;

formal :				/* Formal declaration: */
	 ids				/* Implicit type */
		{ APT ("I",   ("decl_list(I, formal, T)")); }
       | ID				/* Implicit type */
		{ APT (NULL,  ("decl(id('%s'), formal, T)", $1)); }
       | ids ':' type			/* Explicit type */
		{ APT ("T,I", ("decl_list(I, formal, T)")); }
       | ID ':' type			/* Explicit type */
		{ APT ("T",   ("decl(id('%s'), formal, T)", $1)); }
       ;

/******************************************************************************/

fields :				/* Field declarations: */
		{ APT (NULL, ("[]")); }
	| field_list
		{ }
	;

field_list :				/* List of field declarations */
	     field
		{ APT ("H", ("[H]")); }
	   | field ';' field_list
		{ APT ("T,H", ("[H|T]")); }
	   | error
		{ yyerrok; yyclearin;
		  error ("expected s field declaration.");
		  APT (NULL, ("error(fields)")); }
	   ;

field :				/* Field declaration: */
	ids ':' type			/* Explicit type */
		{ APT ("T,I", ("field_list(I, T)")); }
      | ID ':' type			/* Explicit type */
		{ APT ("T",   ("field(id('%s'), T)", $1)); }
      ;

/******************************************************************************/

globals :				/* Global declarations: */
		{ APT (NULL, ("[]")); }
	| global_list
		{ }
	;

global_list :
	      global
		{ APT ("T",   ("[T]")); }
	    | global ';' global_list
		{ APT ("T,H", ("[H|T]")); }
	    | error
		{ yyerrok; yyclearin;
		  error ("expected a global declaration.");
		  APT (NULL, ("error(decls)")); }
	    ;

global :				/* Global declaration: */
	 ids ':' type
		{ APT ("T,I", ("decl_list(I, var, T)")); }
       | ID  ':' type
		{ APT ("T",   ("decl(id('%s'), var, T)", $1)); }
       | ID  ':' type ASSIGN exp
		{ APT ("V,T", ("V,decl(id('%s'), var, T)", $1));
		  APT ("V",   ("assign(id('%s'), V)", $1)); }
       | ID           ASSIGN exp
		{ APT ("V",   ("decl(id('%s'), const(V), T)", $1)); }
       | ID  ':' global_type EQ global_exp
		{ APT ("V,T", ("decl(id('%s'), const(V), T)", $1)); }
       | ID           EQ global_exp
		{ APT ("V",   ("decl(id('%s'), const(V), T)", $1)); }
       ;


/******************************************************************************/

ids : ID ',' ids_aux
		{ APT ("X", ("[id('%s')|X]", $1)); }
    ;

ids_aux : ID
		{ APT (NULL, ("[id('%s')]", $1)); }
	| ID ',' ids_aux
		{ APT ("T",  ("[id('%s')|T]", $1)); }
	;

/*****************************************************************************/
/*									     */
/*                              Type signatures.  			     */
/*									     */
/*****************************************************************************/


types :					/* Type list */
	type				/* Single type (end of list) */
    		{ APT ("T", ("[T]")); }
      | type ',' types			/* Type tuple (list) */
		{ APT ("T,H", ("[H|T]")); }
      ;


type :					/* Type signature (type expression) */
       '(' types ')'			/* Type list */
		{ }
     | INT				/* Integer */
		{ APT (NULL, ("int")); }
     | REAL				/* Real */
		{ APT (NULL, ("real")); }
     | BOOL				/* Boolean */
		{ APT (NULL, ("bool")); }
     | '[' exp ']' type			/* Array type */
		{ APT ("T,B", ("array(T, B)")); }
     | '{' fields '}'			/* Record/Class type */
		{ APT ("F",   ("class(F)")); }
     | error
		{ yyerrok; yyclearin;
		  error ("expected a type.");
		  APT (NULL, ("error(type)")); }
     ;



/******************************************************************************/

global_type :				/* Type signature valid only at top */
	      '(' types ')'		/* Type list */
		{ }
	    | INT			/* Integer */
		{ APT (NULL, ("int")); }
	    | REAL			/* Real */
		{ APT (NULL, ("real")); }
	    | BOOL			/* Boolean */
		{ APT (NULL, ("bool")); }
	    | type RETURNS type		/* Function type */
		{ APT ("R,D", ("map(D, R)")); }
	    | '[' exp ']' type		/* Array type */
		{ APT ("T,B", ("array(T, B)")); }
	    | '{' fields '}'		/* Record/Class type */
		{ APT ("F",   ("class(F)")); }
	    | error
		{ yyerrok; yyclearin;
		  error ("expected a type.");
		  APT (NULL, ("error(type)")); }
	    ;

/*****************************************************************************/
/*									     */
/*                                  Expressions.   			     */
/*									     */
/*****************************************************************************/

exp :					/* Generic expression */
      sexp
		{ }
    | sexp ',' exp_list
		{ APT ("T,H", ("[H|T]")); }
    ;

exp_list :
	   sexp
		{ APT ("E", ("[E]")); }
	 | sexp ',' exp_list
		{ APT ("T,H", ("[H|T]")); }
	 ;

sexp :
      '(' exp ')'
		{ }
     | sexp OR sexp			/* Boolean operators */
		{ APT ("R,L", ("or(L, R)")); }
     | sexp AND sexp
		{ APT ("R,L", ("and(L, R)")); }
     | NOT sexp
		{ APT ("A",   ("not(A)")); }
     | sexp LT sexp			/* Relational operators */
		{ APT ("R,L", ("lt(L, R)")); }
     | sexp LE sexp
		{ APT ("R,L", ("le(L, R)")); }
     | sexp EQ sexp
		{ APT ("R,L", ("eq(L, R)")); }
     | sexp NE sexp
		{ APT ("R,L", ("ne(L, R)")); }
     | sexp GE sexp
		{ APT ("R,L", ("ge(L, R)")); }
     | sexp GT sexp
		{ APT ("R,L", ("gt(L, R)")); }
     | sexp '+' sexp			/* Arithmetic operators */
		{ APT ("R,L", ("add(L, R)")); }
     | sexp '-' sexp
		{ APT ("R,L", ("sub(L, R)")); }
     | sexp '*' sexp
		{ APT ("R,L", ("mul(L, R)")); }
     | sexp '/' sexp
		{ APT ("R,L", ("div(L, R)")); }
     | sexp '%' sexp
		{ APT ("R,L", ("mod(L, R)")); }
     | '-' sexp		%prec UMINUS
		{ APT ("A",   ("neg(A)")); }
     | sexp '.' ID			/* Qualified names */
		{ APT ("L", ("name(L, id('%s'))", $3)); }
     | sexp '[' exp ']'			/* Array references */
		{ APT ("R,L", ("array(L, R)")); }
     | sexp '(' exp ')'			/* Function references */
		{ APT ("R,L", ("map(L, R)")); }
     | ID				/* Simple names */
		{ APT (NULL,  ("id('%s')", $1)); }
     | INT_LIT				/* Integer constant */
		{ APT (NULL,  ("lit(int, %d)", $1)); }
     | REAL_LIT				/* Real constant */
		{ APT (NULL,  ("lit(real, %g)", $1)); }
     | BOOL_LIT				/* Boolean constant */
		{ APT (NULL,  ("lit(bool, %s)", $1? "true": "false")); }
     ;


/******************************************************************************/

global_exp :
	      sexp			/* An ordinary expression */
		{ }
	    | MAP '(' formals ')'	/* Function literal */
	      '[' stats ']'
		{ APT ("S,F", ("lit(map(TF,TR), map(F,S,Symbols))")); }
	    | MAP '(' formals ')' RETURNS type /* Function literal */
	      '[' stats ']'
		{ APT ("S,TR,F", ("lit(map(TF,TR), map(F,S,Symbols))")); }
	    ;


/******************************************************************************/

primary :				/* Restricted expression */
	  prim
		{ }
	| prim ',' prim_list
		{ APT ("T,H", ("[H|T]")); }
	;

prim_list :
	    prim
		{ APT ("P", ("[P]")); }
	  | prim ',' prim_list
		{ APT ("T,H", ("[H|T]")); }
	  ;


prim :
       '(' primary ')'
		{ }
     | ID
		{ APT (NULL,  ("id('%s')", $1)); }
     | prim '.' prim
		{ APT ("R,L", ("name(L, R)")); }
     | prim '[' exp ']'
		{ APT ("R,L", ("array(L, R)")); }
     ;

/*****************************************************************************/
/*									     */
/*                                  Statements.   			     */
/*									     */
/*****************************************************************************/

stats :	/* EMPTY */
		{ APT (NULL, ("[]")); }
      | stat_list
		{ }
      ;

stat_list :
	    stat
		{ APT ("S", ("[S]")); }
	  | stat ';' stat_list
		{ APT ("Ss,S", ("[S|Ss]")); }
	  | error
		{ yyerrok; yyclearin;
		  error ("expected a valid statement.");
		  APT (NULL, ("error(stats)")); }
	  ;

stat :					/* A statement: */
       decl				/* Declaration */
		{ }
     | action				/* Assignment or call */
		{ }
     | RETURN exp
		{ APT ("E", ("return(E)")); }
     | BREAK
		{ APT (NULL, ("break(S)")); }
     | SKIP
		{ APT (NULL, ("skip(S)")); }
     | RETRY
		{ APT (NULL, ("retry(S)")); }
     | COND
		{ APT (NULL, ("%d", ++guardNo)); }
       '[' clauses ']'
		{ APT ("B,G", ("cond(B, G)")); }
     | WHILE
		{ APT (NULL, ("%d", ++guardNo)); }
       '[' clauses ']'
		{ APT ("B,G", ("while(B, G)")); }
     | '[' stats ']'
		{ }
     ;


clauses :
	  exp RETURNS stats
		{ APT ("S,E", ("[clause(E, S)]")); }
	| exp RETURNS stats '|' clauses
		{ APT ("Cs,S,E", ("[clause(E, S)|Cs]")); }
	| exp RETURNS stats '|' ELSE RETURNS stats
		{ APT ("ES,S,E", ("[clause(E, S),else(ES)]")); }
	| error ']'
		{ yyerrok; yyclearin;
		  error ("syntax error in guarded clause.");
		  APT (NULL, ("error(clause)")); }
	;

action :
         primary ASSIGN exp		/* Assignment */
		{ APT ("V,P", ("assign(P, V)")); }
       | primary '(' exp ')'	/* Function call */
		{ APT ("R,L", ("map(L, R)")); }
       ;

%%

yyerror(s)
    char *s;
{
    if (strcmp(s, "parse error")) {
	error (s);
    }
}