All pastes #476306 Raw Edit

Stuff

public text v1 · immutable
·published 2007-05-08 00:03 UTC
rendered paste body
%{
#include <iostream.h>
#include <cstdlib.h>
#include <string.h>
#include <unistd.h>
#include "chatzidimitriou_syntax.cpp.h"

void yyerror(char *);
extern int yylex();
extern char *yytext;
extern FILE *yyin;


%}

%token BYTE 1
%token DELETE 2
%token ELSE 3
%token FALSE 4
%token IF 5
%token INT 6
%token NEW 7
%token NULL 8
%token PROC 9
%token REFERENCE 10
%token RETURN 11
%token WHILE 12
%token WITH 13
%token TRUE 14
%token EQUAL 15
%token NOTEQUAL 16
%token GREQUAL 17
%token LEEQUAL 18
%token OR_OP 19
%token AND_OP 20
%token UINT_CONST 21
%token CHAR_CONST 22
%token STRING_CONST 23
%token IDENTIFIER 24
%token GENERIC_ERROR 25

%right '+'
%right '-'
%left '*' '/' '%'
%left '+' '-'
%nonassoc EQUAL NOTEQUAL '>' '<' GREQUAL LEEQUAL
%right '!'
%left AND_OP
%left OR_OP

%%

program:
	func-def;

func_def:
	identifier '(' func_par_list1 ')' ':' r_type local_defs comp_statement;

func_par_list1:
	 | func_par_list;

func_par_list:
	id ':' type | id ':' 'reference' type;

type:
	'int' | 'byte' | type '[' ']';

r-type:
	type | 'proc';
	
local_defs:
	 | local_defs local_def;

local_def:
	func-def | var-def;

var-def:
	id ':' type ';';

stmt:
	';' | lvalue ':=' expr ';' 
	| compound-stmt | func-call ';'
	| 'if' '(' cond ')' stmt else1 
	| 'while' '(' cond ')' stmt
	| 'return' expr ';'
	| 'new' lvalue 'with' expr ';'
	| 'delete' lvalue ';' ;

else1:
	 | 'else' stmt;

compound-stm:
	'{' stmt '}';

func-call:
	id '(' expr-list ')';

expr-list:
	expr | expr ',' expr-list;

expr:
	'null' | id | '(' expr ')' | func-call | unary_op expr | expr operand expr ;

lvalue:
	id | expr '[' expr ']';

cond:
	'true' | 'false' | '(' cond ')' | '!' cond
	| expr rel_op;

rel_op:
EQUAL | NOTEQUAL | '<' | '>' | GREQUAL | LEEQUAL;

operand:
'+'|'-'|'*'|'/';

unary_op:
'+' | '-';

%%

int main(int argc, char *argv[])
{
	if(yyparse() != 0){
		printf ("Error!");
		exit(1);
	} else {
		printf ("Successful!");
	}
	
	return EXIT_SUCCESS;
}