triplets - lab04
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -4,3 +4,4 @@ def.tab.o
|
|||||||
leks
|
leks
|
||||||
lex.yy.cc
|
lex.yy.cc
|
||||||
lex.yy.o
|
lex.yy.o
|
||||||
|
triplets.txt
|
||||||
|
|||||||
2
Makefile
2
Makefile
@@ -22,4 +22,4 @@ def.tab.cc: def.yy
|
|||||||
$(YACC) -d def.yy
|
$(YACC) -d def.yy
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm *.o cutie def.tab.cc def.tab.hh lex.yy.*
|
rm *.o cutie def.tab.cc def.tab.hh lex.yy.* triplets.txt
|
||||||
|
|||||||
101
def.yy
101
def.yy
@@ -2,6 +2,8 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stack>
|
||||||
|
#include <string>
|
||||||
#define INFILE_ERROR 1
|
#define INFILE_ERROR 1
|
||||||
#define OUTFILE_ERROR 2
|
#define OUTFILE_ERROR 2
|
||||||
|
|
||||||
@@ -10,6 +12,25 @@
|
|||||||
int yylex();
|
int yylex();
|
||||||
|
|
||||||
void yyerror(const char *msg, ...);
|
void yyerror(const char *msg, ...);
|
||||||
|
|
||||||
|
struct StackElement {
|
||||||
|
std::string value;
|
||||||
|
enum { INT, VAR } type;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::stack<StackElement> expressionsStack;
|
||||||
|
FILE *tripletFile;
|
||||||
|
int tempVarCounter = 0;
|
||||||
|
|
||||||
|
std::string generateTempVar() {
|
||||||
|
return "result" + std::to_string(tempVarCounter++);
|
||||||
|
}
|
||||||
|
|
||||||
|
void writeTriplet(const std::string& result, const std::string& arg1, const std::string& arg2, const std::string& op) {
|
||||||
|
fprintf(tripletFile, "%s= %s %s %s \n", result.c_str(), arg1.c_str(), arg2.c_str(), op.c_str());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
%}
|
%}
|
||||||
|
|
||||||
%union {
|
%union {
|
||||||
@@ -41,27 +62,91 @@ statement_list
|
|||||||
|
|
||||||
statement
|
statement
|
||||||
: variable_declaration { printf("Deklaracja zmiennej\n"); }
|
: variable_declaration { printf("Deklaracja zmiennej\n"); }
|
||||||
| expression SEMICOLON { printf("instrukcja\n"); }
|
| expression SEMICOLON {
|
||||||
|
printf("instrukcja\n");
|
||||||
|
if (!expressionsStack.empty()) {
|
||||||
|
expressionsStack.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
variable_declaration
|
variable_declaration
|
||||||
: LET ID COLON INT_TYPE '=' expression SEMICOLON
|
: LET ID COLON INT_TYPE '=' expression SEMICOLON
|
||||||
{ printf("Deklaracja zmiennej: %s\n", $2); }
|
{
|
||||||
|
printf("Deklaracja zmiennej: %s\n", $2);
|
||||||
|
if (!expressionsStack.empty()) {
|
||||||
|
StackElement expression = expressionsStack.top();
|
||||||
|
expressionsStack.pop();
|
||||||
|
writeTriplet(std::string($2), expression.value, "", "=");
|
||||||
|
}
|
||||||
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
expression
|
expression
|
||||||
: expression '+' expression { printf("Wyrazenie z +\n"); }
|
: expression '+' expression {
|
||||||
| expression '-' expression { printf("Wyrazenie z -\n"); }
|
printf("Wyrazenie z +\n");
|
||||||
| expression '*' expression { printf("Wyrazenie z *\n"); }
|
StackElement rhs = expressionsStack.top();
|
||||||
| expression '/' expression { printf("Wyrazenie z /\n"); }
|
expressionsStack.pop();
|
||||||
|
StackElement lhs = expressionsStack.top();
|
||||||
|
expressionsStack.pop();
|
||||||
|
std::string temp = generateTempVar();
|
||||||
|
writeTriplet(temp, lhs.value, rhs.value, "+");
|
||||||
|
}
|
||||||
|
| expression '-' expression {
|
||||||
|
printf("Wyrazenie z -\n");
|
||||||
|
StackElement rhs = expressionsStack.top();
|
||||||
|
expressionsStack.pop();
|
||||||
|
StackElement lhs = expressionsStack.top();
|
||||||
|
expressionsStack.pop();
|
||||||
|
std::string temp = generateTempVar();
|
||||||
|
writeTriplet(temp, lhs.value, rhs.value, "-");
|
||||||
|
}
|
||||||
|
| expression '*' expression {
|
||||||
|
printf("Wyrazenie z *\n");
|
||||||
|
StackElement rhs = expressionsStack.top();
|
||||||
|
expressionsStack.pop();
|
||||||
|
StackElement lhs = expressionsStack.top();
|
||||||
|
expressionsStack.pop();
|
||||||
|
std::string temp = generateTempVar();
|
||||||
|
writeTriplet(temp, lhs.value, rhs.value, "*");
|
||||||
|
}
|
||||||
|
| expression '/' expression {
|
||||||
|
printf("Wyrazenie z /\n");
|
||||||
|
StackElement rhs = expressionsStack.top();
|
||||||
|
expressionsStack.pop();
|
||||||
|
StackElement lhs = expressionsStack.top();
|
||||||
|
expressionsStack.pop();
|
||||||
|
std::string temp = generateTempVar();
|
||||||
|
writeTriplet(temp, lhs.value, rhs.value, "/");
|
||||||
|
}
|
||||||
| '(' expression ')' { printf("Wyrazenie w nawiasach\n"); }
|
| '(' expression ')' { printf("Wyrazenie w nawiasach\n"); }
|
||||||
| INT_LIT { printf("Literal calkowity: %d\n", $1); }
|
| INT_LIT {
|
||||||
| ID { printf("Identyfikator: %s\n", $1); }
|
printf("Literal calkowity: %d\n", $1);
|
||||||
|
StackElement element;
|
||||||
|
element.value = std::to_string($1);
|
||||||
|
element.type = StackElement::INT;
|
||||||
|
expressionsStack.push(element);
|
||||||
|
}
|
||||||
|
| ID {
|
||||||
|
printf("Identyfikator: %s\n", $1);
|
||||||
|
char buffer[128];
|
||||||
|
snprintf(buffer, sizeof(buffer), "%s", $1);
|
||||||
|
StackElement element;
|
||||||
|
element.value = std::string(buffer);
|
||||||
|
element.type = StackElement::VAR;
|
||||||
|
expressionsStack.push(element);
|
||||||
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
tripletFile = fopen("triplets.txt", "w");
|
||||||
|
if (!tripletFile) {
|
||||||
|
fprintf(stderr, "Blad\n");
|
||||||
|
return OUTFILE_ERROR;
|
||||||
|
}
|
||||||
yyparse();
|
yyparse();
|
||||||
|
fclose(tripletFile);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user