All pastes #39093 Raw Edit

YAc

public text v1 · immutable
#39093 ·published 2006-01-30 03:47 UTC
rendered paste body
%{

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void yyerror( char * );
int yylex( );

extern FILE *yyin;
extern int lino;
%}
%union{
   int yint;
   char *str;
};
%token INT FLOAT VOID INTEGER EXP FLT ID
%left '+''-'
%left '*''/' 
%%
program: 
       declist funcs     { printf( " SUCCESS~~\n\n" ); }
       ;
declist:
       declist decl
       |
       ;
decl:  INT id_list ';'
       |FLOAT id_list ';'
       ;
id_list:
       id_list ',' ID
       |ID
       ;
funcs:
       type ID '(' paramlist ')' body
       ;
type:
       INT
       |VOID
       |FLOAT
       |
       ;
paramlist:
       paramlist ',' param
       |param
       |
       ;
param: 
       type ID
       ;
body:
       '{' stmtlist '}'
       ;
stmtlist:
       stmtlist stmt
       |stmt 
//       |error '\n'       { yyclearin; yyerrok; }
       ;
stmt:
       assign
       |decl
       |';'
       |error '\n'      { yyclearin; yyerrok; }
       |error ';'       { yyclearin; yyerrok; }
       |error '}'       { yyclearin; yyerrok; }
       ;
assign:
       ID '=' expr ';'
expr:
       ID
       |INTEGER
       |FLT
       |EXP
       |expr '*' expr
       |expr '/' expr
       |expr '+' expr
       |expr '-' expr
       ;
%%

void yyerror( char *s )
{
   printf("%s\t\t%d \n\n",s,lino);
}

int main()
{
   int i;
   yyin = fopen("inp.txt","r");
   yyparse();
   fclose(yyin);
   return 0; 
}