⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simple.y

📁 domaci iz kompajlera. da domaci rad.kjhg a sdkjqwe laksdj qwekljasd qwekljh qwekjh
💻 Y
字号:
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tree.h" 

#define YYDEBUG 1

A_program theprogram ;
A_declarations thedecls;
A_commands thecommands;
A_commands else_block;
A_commands then_block;
A_commands loop_block;
A_exp while_exp;

int errors;
%}


%union semrec {
  int intval;
  char *id;
  struct A_exp_ *expr;
  struct A_commands_ *comm;
}
%start program
%token<intval> NUMBER
%token<id> IDENTIFIER
%token IF WHILE
%token SKIP THEN ELSE FI DO END
%token INTEGER READ WRITE LET IN
%token ASSGNOP
%type <expr>exp
%type <comm>commands
%type <comm>command
%left '-''+'
%left '*''/'
%right '^'
%%
program : LET { thedecls=0;}
           declarations  
          IN      { thecommands = 0;  }
             commands
          END   {
					theprogram = (A_program) malloc (sizeof(struct A_program_));	
					theprogram->decls = (A_declarations) malloc(sizeof(struct A_declarations_));
					theprogram->comms = (A_commands) malloc(sizeof(struct A_commands_));
					theprogram->decls = thedecls;
					theprogram->comms = thecommands;
					YYACCEPT;
				}
;

declarations :
    | INTEGER id_seq IDENTIFIER '.' { 
										struct Variable* new_var = (struct Variable*)malloc(sizeof(struct Variable));
										new_var->type = (char*)malloc(strlen("INTEGER")+1);
										new_var->id = (char*)malloc(strlen($3)+1);
										new_var->type = "INTEGER";
										strcpy (new_var->id, $3);
										
										A_declarations d = makeDecls(new_var);
										thedecls = append(thedecls, d);
										
									}
;
id_seq :
    | id_seq IDENTIFIER ',' { 
					
								struct Variable* new_var = (struct Variable*)malloc(sizeof(struct Variable));
								new_var->type = (char*)malloc(strlen("INTEGER")+1);
								new_var->id = (char*)malloc(strlen($2)+1);
								new_var->type = "INTEGER";
								strcpy (new_var->id, $2);
								
								A_declarations d = makeDecls(new_var);
								
								thedecls = append(thedecls, d);
								
							}
;

commands : { $$ = (A_commands) 0; }
    | commands command ';'  { 
								$$ = makeA_recCommand($1, $2);
								thecommands = $$;
							}
;
command : 
     READ IDENTIFIER 	{
						      
							$$ = makeA_readCommand($2);	
						}  
    | WRITE exp       	{ 
							$$ = makeA_writeCommand($2);	
						}
    | IDENTIFIER ASSGNOP exp { 
								$$ = makeA_assignCommand($1, $3);		
							 }
 
    | IF exp        { 	
						 
						
						then_block = (A_commands) malloc(sizeof(struct A_commands_));
						else_block = (A_commands) malloc(sizeof(struct A_commands_));

					}
      THEN commands   	{ 
						
							then_block = thecommands;
						}
					  
      ELSE          { 
							          
					}
           commands		{
							else_block = thecommands;
						}
      FI              	{ 

							$$ = makeA_ifCommand($2, then_block, else_block);
						
						}

    | WHILE  exp {   
						loop_block = (A_commands) malloc(sizeof(struct A_commands_));
						while_exp = (A_exp) malloc(sizeof(struct A_exp_));
						while_exp = $2;
					}
      DO 
          commands    
      END             { 
						loop_block = thecommands;
						
						$$ = makeA_whileCommand($2, loop_block);
					 }
;
exp : NUMBER          { $$ = makeA_numExp($1);   			}
     | IDENTIFIER     { $$ = makeA_idExp($1);			}
     | exp '<' exp    { $$ = makeA_opExp($1, A_lt, $3);        }
     | exp '=' exp    { $$ = makeA_opExp($1, A_eq, $3);        }
     | exp '>' exp    { $$ = makeA_opExp($1, A_gt, $3);        }
     | exp '+' exp    { $$ = makeA_opExp($1, A_plus, $3);     }
     | exp '-' exp    { $$ = makeA_opExp($1, A_minus, $3);    }
     | exp '*' exp    { $$ = makeA_opExp($1, A_times, $3);   }
     | exp '/' exp    { $$ = makeA_opExp($1, A_div, $3);      }
     | exp '^' exp    { $$ = makeA_opExp($1, A_power, $3);   }
     | '(' exp ')'    {/**/}
;

%%
main( int argc, char *argv[])
{ extern FILE *yyin;
  ++argv; --argc;
  yyin = fopen( argv[0], "r" );
  errors = 0;
  yyparse ();
  printf ( "Parse Completed\n" );
  if ( errors == 0 )
   { 
	 	printAST(theprogram, argv[1]);
    }
}

yyerror ( char *s )
{
  errors++;
  printf("%s, Leksima: %s, br. pocetne linije: %d, br. zavrsne linije: %d, br. pocetne kolone: %d, br. zavrsne kolone: %d\n", s, yylloc.first_line, yylloc.last_line, yylloc.first_column, yylloc.last_column);
  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -