Lab 6
This commit is contained in:
88
def.yy
88
def.yy
@@ -33,8 +33,10 @@ int memoryCounter = 0;
|
|||||||
std::stack<StackElement> expressionsStack;
|
std::stack<StackElement> expressionsStack;
|
||||||
FILE *tripletFile;
|
FILE *tripletFile;
|
||||||
int tempVarCounter = 0;
|
int tempVarCounter = 0;
|
||||||
|
int stringCounter = 0;
|
||||||
|
|
||||||
std::vector<std::string> asmCode;
|
std::vector<std::string> asmCode;
|
||||||
|
std::map<std::string, std::string> stringLiterals;
|
||||||
|
|
||||||
bool isNumber(const std::string& s) {
|
bool isNumber(const std::string& s) {
|
||||||
if (s.empty()) return false;
|
if (s.empty()) return false;
|
||||||
@@ -86,11 +88,49 @@ void generateAsm(const std::string& result, const std::string& arg1,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void generatePrintInt(const std::string& var) {
|
||||||
|
asmCode.push_back("# print_integer(" + var + ")");
|
||||||
|
asmCode.push_back(generateLoad("$a0", var));
|
||||||
|
asmCode.push_back("li $v0, 1");
|
||||||
|
asmCode.push_back("syscall");
|
||||||
|
}
|
||||||
|
|
||||||
|
void generatePrintFloat(const std::string& var) {
|
||||||
|
asmCode.push_back("# print_float(" + var + ")");
|
||||||
|
asmCode.push_back("lwc1 $f12, " + var);
|
||||||
|
asmCode.push_back("li $v0, 2");
|
||||||
|
asmCode.push_back("syscall");
|
||||||
|
}
|
||||||
|
|
||||||
|
void generatePrintString(const std::string& strLabel) {
|
||||||
|
asmCode.push_back("# print_string(" + strLabel + ")");
|
||||||
|
asmCode.push_back("la $a0, " + strLabel);
|
||||||
|
asmCode.push_back("li $v0, 4");
|
||||||
|
asmCode.push_back("syscall");
|
||||||
|
}
|
||||||
|
|
||||||
|
void generateReadInt(const std::string& var) {
|
||||||
|
asmCode.push_back("# read_integer -> " + var);
|
||||||
|
asmCode.push_back("li $v0, 5");
|
||||||
|
asmCode.push_back("syscall");
|
||||||
|
asmCode.push_back("sw $v0, " + var);
|
||||||
|
}
|
||||||
|
|
||||||
|
void generateReadFloat(const std::string& var) {
|
||||||
|
asmCode.push_back("# read_float -> " + var);
|
||||||
|
asmCode.push_back("li $v0, 6");
|
||||||
|
asmCode.push_back("syscall");
|
||||||
|
asmCode.push_back("swc1 $f0, " + var);
|
||||||
|
}
|
||||||
|
|
||||||
void writeDataSection(FILE* out) {
|
void writeDataSection(FILE* out) {
|
||||||
fprintf(out, ".data\n");
|
fprintf(out, ".data\n");
|
||||||
for (auto& pair : symbolTable) {
|
for (auto& pair : symbolTable) {
|
||||||
fprintf(out, "%s: .word 0\n", pair.first.c_str());
|
fprintf(out, "%s: .word 0\n", pair.first.c_str());
|
||||||
}
|
}
|
||||||
|
for (auto& pair : stringLiterals) {
|
||||||
|
fprintf(out, "%s: .asciiz %s\n", pair.first.c_str(), pair.second.c_str());
|
||||||
|
}
|
||||||
fprintf(out, "\n");
|
fprintf(out, "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,8 +179,9 @@ void writeTriplet(const std::string& result, const std::string& arg1,
|
|||||||
}
|
}
|
||||||
|
|
||||||
%token <text> ID
|
%token <text> ID
|
||||||
|
%token <text> STRING_LIT
|
||||||
%token <ival> INT_LIT
|
%token <ival> INT_LIT
|
||||||
%token LET
|
%token LET PRINT_INT PRINT_FLOAT PRINT_STRING READ_INT READ_FLOAT
|
||||||
%token INT_TYPE
|
%token INT_TYPE
|
||||||
%token SEMICOLON COLON
|
%token SEMICOLON COLON
|
||||||
|
|
||||||
@@ -163,6 +204,8 @@ statement_list
|
|||||||
statement
|
statement
|
||||||
: variable_declaration { printf("Deklaracja zmiennej\n"); }
|
: variable_declaration { printf("Deklaracja zmiennej\n"); }
|
||||||
| assignment { printf("Instrukcja przypisania\n"); }
|
| assignment { printf("Instrukcja przypisania\n"); }
|
||||||
|
| print_statement { printf("Instrukcja wypisania\n"); }
|
||||||
|
| read_statement { printf("Instrukcja odczytu\n"); }
|
||||||
| expression SEMICOLON {
|
| expression SEMICOLON {
|
||||||
printf("instrukcja\n");
|
printf("instrukcja\n");
|
||||||
if (!expressionsStack.empty()) {
|
if (!expressionsStack.empty()) {
|
||||||
@@ -196,6 +239,47 @@ variable_declaration
|
|||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
|
print_statement
|
||||||
|
: PRINT_INT '(' expression ')' SEMICOLON
|
||||||
|
{
|
||||||
|
printf("print_integer\n");
|
||||||
|
if (!expressionsStack.empty()) {
|
||||||
|
StackElement expr = expressionsStack.top();
|
||||||
|
expressionsStack.pop();
|
||||||
|
generatePrintInt(expr.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
| PRINT_FLOAT '(' expression ')' SEMICOLON
|
||||||
|
{
|
||||||
|
printf("print_float\n");
|
||||||
|
if (!expressionsStack.empty()) {
|
||||||
|
StackElement expr = expressionsStack.top();
|
||||||
|
expressionsStack.pop();
|
||||||
|
generatePrintFloat(expr.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
| PRINT_STRING '(' STRING_LIT ')' SEMICOLON
|
||||||
|
{
|
||||||
|
printf("print_string: %s\n", $3);
|
||||||
|
std::string label = "str" + std::to_string(stringCounter++);
|
||||||
|
stringLiterals[label] = std::string($3);
|
||||||
|
generatePrintString(label);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
read_statement
|
||||||
|
: READ_INT '(' ID ')' SEMICOLON
|
||||||
|
{
|
||||||
|
printf("read_integer -> %s\n", $3);
|
||||||
|
generateReadInt(std::string($3));
|
||||||
|
}
|
||||||
|
| READ_FLOAT '(' ID ')' SEMICOLON
|
||||||
|
{
|
||||||
|
printf("read_float -> %s\n", $3);
|
||||||
|
generateReadFloat(std::string($3));
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
expression
|
expression
|
||||||
: expression '+' expression {
|
: expression '+' expression {
|
||||||
printf("Wyrazenie z +\n");
|
printf("Wyrazenie z +\n");
|
||||||
@@ -275,7 +359,6 @@ int main(int argc, char *argv[]) {
|
|||||||
return OUTFILE_ERROR;
|
return OUTFILE_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otwórz plik wyjściowy na asembler
|
|
||||||
yyout = fopen("output.asm", "w");
|
yyout = fopen("output.asm", "w");
|
||||||
if (!yyout) {
|
if (!yyout) {
|
||||||
fprintf(stderr, "Blad output.asm\n");
|
fprintf(stderr, "Blad output.asm\n");
|
||||||
@@ -292,7 +375,6 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
fclose(yyout);
|
fclose(yyout);
|
||||||
|
|
||||||
// Zapisz tablicę symboli do symbols.txt
|
|
||||||
saveSymbolTable();
|
saveSymbolTable();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
23
in5.txt
23
in5.txt
@@ -1,2 +1,21 @@
|
|||||||
let x: Int = 5 + 3;
|
let x: Int = 42;
|
||||||
let y: Int = x - 1;
|
print_string("Integer: \n");
|
||||||
|
print_string("\n");
|
||||||
|
print_integer(x);
|
||||||
|
print_string("\n");
|
||||||
|
|
||||||
|
print_string("Podaj float: \n");
|
||||||
|
let f: Int = 0;
|
||||||
|
read_float(f);
|
||||||
|
print_string("Float: \n");
|
||||||
|
print_float(f);
|
||||||
|
print_string("\n");
|
||||||
|
|
||||||
|
print_string("Podaj integer: \n");
|
||||||
|
let y: Int = 0;
|
||||||
|
read_integer(y);
|
||||||
|
print_string("Wynik: \n");
|
||||||
|
print_integer(y);
|
||||||
|
print_string("\n");
|
||||||
|
|
||||||
|
print_string("Koniec testu\n");
|
||||||
|
|||||||
41
z5.l
41
z5.l
@@ -1,5 +1,4 @@
|
|||||||
%{
|
%{
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "def.tab.hh"
|
#include "def.tab.hh"
|
||||||
@@ -12,18 +11,28 @@ void yyerror(const char *msg, ...);
|
|||||||
|
|
||||||
%option noyywrap
|
%option noyywrap
|
||||||
%%
|
%%
|
||||||
"let" {fprintf(stdout, "LET\n"); return LET;}
|
"let" {fprintf(stdout, "LET\n"); return LET;}
|
||||||
"Int" {fprintf(stdout, "INT_TYPE\n"); return INT_TYPE;}
|
"Int" {fprintf(stdout, "INT_TYPE\n"); return INT_TYPE;}
|
||||||
":" {fprintf(stdout, ":\n"); return COLON;}
|
":" {fprintf(stdout, ":\n"); return COLON;}
|
||||||
";" {fprintf(stdout, ";\n"); return SEMICOLON;}
|
";" {fprintf(stdout, ";\n"); return SEMICOLON;}
|
||||||
\+ {fprintf(stdout, "+\n"); return '+';}
|
"print_integer" {fprintf(stdout, "PRINT_INT\n"); return PRINT_INT;}
|
||||||
\* {fprintf(stdout, "*\n"); return '*';}
|
"print_float" {fprintf(stdout, "PRINT_FLOAT\n"); return PRINT_FLOAT;}
|
||||||
\( {fprintf(stdout, "(\n"); return '(';}
|
"print_string" {fprintf(stdout, "PRINT_STRING\n"); return PRINT_STRING;}
|
||||||
\) {fprintf(stdout, ")\n"); return ')';}
|
"read_integer" {fprintf(stdout, "READ_INT\n"); return READ_INT;}
|
||||||
\/ {fprintf(stdout, "/\n"); return '/';}
|
"read_float" {fprintf(stdout, "READ_FLOAT\n"); return READ_FLOAT;}
|
||||||
\- {fprintf(stdout, "-\n"); return '-';}
|
\+ {fprintf(stdout, "+\n"); return '+';}
|
||||||
\= {fprintf(stdout, "=\n"); return '=';}
|
\* {fprintf(stdout, "*\n"); return '*';}
|
||||||
[0-9]+ {
|
\( {fprintf(stdout, "(\n"); return '(';}
|
||||||
|
\) {fprintf(stdout, ")\n"); return ')';}
|
||||||
|
\/ {fprintf(stdout, "/\n"); return '/';}
|
||||||
|
\- {fprintf(stdout, "-\n"); return '-';}
|
||||||
|
\= {fprintf(stdout, "=\n"); return '=';}
|
||||||
|
\"[^\"]*\" {
|
||||||
|
fprintf(stdout, "string literal: %s\n", yytext);
|
||||||
|
yylval.text = strdup(yytext);
|
||||||
|
return STRING_LIT;
|
||||||
|
}
|
||||||
|
[0-9]+ {
|
||||||
fprintf(stdout, "liczba: %s\n", yytext);
|
fprintf(stdout, "liczba: %s\n", yytext);
|
||||||
yylval.ival = atoi(yytext);
|
yylval.ival = atoi(yytext);
|
||||||
return INT_LIT;
|
return INT_LIT;
|
||||||
@@ -33,9 +42,9 @@ void yyerror(const char *msg, ...);
|
|||||||
yylval.text = strdup(yytext);
|
yylval.text = strdup(yytext);
|
||||||
return ID;
|
return ID;
|
||||||
}
|
}
|
||||||
[ \t]+ {;}
|
[ \t]+ {;}
|
||||||
\n {yylineno++;}
|
\n {yylineno++;}
|
||||||
. {yyerror("Blad leksykalny\n");}
|
. {yyerror("Blad leksykalny\n");}
|
||||||
%%
|
%%
|
||||||
|
|
||||||
void yyerror(const char *msg, ...)
|
void yyerror(const char *msg, ...)
|
||||||
|
|||||||
Reference in New Issue
Block a user