Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %baseclass-preinclude "semantics.h"
- %lsp-needed
- %token NATURAL;
- %token BOOLEAN;
- %token TRUE;
- %token FALSE;
- %token NUMBER;
- %token <szoveg> IDENT;
- %token ASSIGN;
- %right PLUS;
- %type <expr_d> expr;
- %type <expr_d> declaration;
- %type <instr_d> declarations;
- %type <expr_d> assignment;
- %type <instr_d> assignments;
- %union
- {
- std::string *szoveg;
- expr_data *expr_d;
- instr_data *instr_d;
- }
- %%
- start:
- declarations assignments
- {
- std::cout << std::string("") +
- "extern ki_elojeles_egesz\n" +
- "extern ki_logikai\n" +
- "global main\n" +
- "section .bss\n" +
- $1->code +
- "section .text\n" +
- "main:\n" +
- $2->code +
- "ret\n";
- delete $1;
- delete $2;
- }
- ;
- declarations:
- // ures
- {
- $$ = new instr_data(d_loc__.first_line, "");
- }
- |
- declaration declarations
- {
- std::string fun;
- fun = ($1->var_type == natural ? "ki_elojeles_egesz" : "ki_logikai");
- $$ = new instr_data( $1->decl_row,
- $1->code +
- $2->code
- );
- delete $1;
- delete $2;
- }
- ;
- declaration:
- NATURAL IDENT
- {
- if( szimbolumtabla.count(*$2) > 0 )
- {
- std::stringstream ss;
- ss << "Ujradefinialt valtozo: " << *$2 << std::endl
- << "Korabbi deklaracio sora: " << szimbolumtabla[*$2].decl_row;
- error( ss.str().c_str() );
- }
- szimbolumtabla[*$2] = var_data( d_loc__.first_line, natural );
- $$ = new expr_data(d_loc__.first_line, natural, *$2 + ": resd 1\n");
- delete $2;
- }
- |
- BOOLEAN IDENT
- {
- if( szimbolumtabla.count(*$2) > 0 )
- {
- std::stringstream ss;
- ss << "Ujradefinialt valtozo: " << *$2 << std::endl
- << "Korabbi deklaracio sora: " << szimbolumtabla[*$2].decl_row;
- error( ss.str().c_str() );
- }
- szimbolumtabla[*$2] = var_data( d_loc__.first_line, boolean );
- $$ = new expr_data(d_loc__.first_line, boolean, *$2 + ": resb 1\n");
- delete $2;
- }
- ;
- assignments:
- // ures
- {
- $$ = new instr_data(d_loc__.first_line, "");
- }
- |
- assignment assignments
- {
- $$ = new instr_data( $1->decl_row,
- $1->code +
- $2->code
- );
- }
- ;
- assignment:
- IDENT ASSIGN expr
- {
- if( szimbolumtabla.count(*$1) == 0 )
- {
- error( (std::string("Nem definialt valtozo: ") + *$1).c_str() );
- }
- if( szimbolumtabla[*$1].var_type != $3->var_type )
- {
- error( "Tipushibas ertekadas." );
- }
- $$ = new expr_data(d_loc__.first_line, szimbolumtabla[*$1].var_type,
- "mov eax, " +
- $3->code +
- "mov " +
- *$1 +
- ", eax\n"
- );
- delete $1;
- delete $3;
- }
- ;
- expr:
- IDENT
- {
- if( szimbolumtabla.count(*$1) == 0 )
- {
- error( (std::string("Nem definialt valtozo: ") + *$1).c_str() );
- }
- $$ = new expr_data(d_loc__.first_line, szimbolumtabla[*$1].var_type, "mov eax, " + *$1 + "\n");
- delete $1;
- }
- |
- NUMBER
- {
- $$ = new expr_data(d_loc__.first_line, natural, "mov eax, " + *$1 + "\n");
- }
- |
- TRUE
- {
- $$ = new expr_data(d_loc__.first_line, boolean, "mov eax, 1\n");
- }
- |
- FALSE
- {
- $$ = new expr_data(d_loc__.first_line, boolean, "mov eax, 0\n");
- }
- | expr PLUS expr
- {
- if( $1->var_type != natural || $3->var_type != natural ) {
- error( (std::string("Típushibás kifejezés")).c_str() );
- }
- $$ = new expr_data(d_loc__.first_line, natural, "mov eax, " + $3->code +
- "push eax\n" + "mov eax, " + $1->code +
- "pop ebx\n"
- "add eax, ebx\n"
- );
- delete $1;
- delete $3;
- }
- ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement