This commit is contained in:
2025-12-03 23:59:07 +01:00
commit 0474558835
11 changed files with 5411 additions and 0 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

25
Makefile Normal file
View File

@@ -0,0 +1,25 @@
CC=gcc
CXX=g++
LEX=flex
YACC=bison
LD=gcc
all: leks
leks: def.tab.o lex.yy.o
$(CXX) lex.yy.o def.tab.o -o leks -ll
lex.yy.o: lex.yy.cc
$(CXX) -c lex.yy.cc
lex.yy.cc: z5.l
$(LEX) -c++ -o lex.yy.cc z5.l
def.tab.o: def.tab.cc
$(CXX) -c def.tab.cc
def.tab.cc: def.yy
$(YACC) -d def.yy
clean:
rm *.o cutie def.tab.cc def.tab.hh def.yy.cc

1621
def.tab.cc Normal file

File diff suppressed because it is too large Load Diff

77
def.tab.hh Normal file
View File

@@ -0,0 +1,77 @@
/* A Bison parser, made by GNU Bison 2.3. */
/* Skeleton interface for Bison's Yacc-like parsers in C
Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005, 2006
Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA. */
/* As a special exception, you may create a larger work that contains
part or all of the Bison parser skeleton and distribute that work
under terms of your choice, so long as that work isn't itself a
parser generator using the skeleton or a modified version thereof
as a parser skeleton. Alternatively, if you modify or redistribute
the parser skeleton itself, you may (at your option) remove this
special exception, which will cause the skeleton and the resulting
Bison output files to be licensed under the GNU General Public
License without this special exception.
This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */
/* Tokens. */
#ifndef YYTOKENTYPE
# define YYTOKENTYPE
/* Put the tokens into the symbol table, so that GDB and other debuggers
know about them. */
enum yytokentype {
ID = 258,
INT_LIT = 259,
LET = 260,
INT_TYPE = 261,
SEMICOLON = 262,
COLON = 263
};
#endif
/* Tokens. */
#define ID 258
#define INT_LIT 259
#define LET 260
#define INT_TYPE 261
#define SEMICOLON 262
#define COLON 263
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef union YYSTYPE
#line 14 "def.yy"
{
char *text;
int ival;
}
/* Line 1529 of yacc.c. */
#line 70 "def.tab.hh"
YYSTYPE;
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_DECLARED 1
# define YYSTYPE_IS_TRIVIAL 1
#endif
extern YYSTYPE yylval;

BIN
def.tab.o Normal file

Binary file not shown.

66
def.yy Normal file
View File

@@ -0,0 +1,66 @@
%{
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define INFILE_ERROR 1
#define OUTFILE_ERROR 2
extern int yylex(void);
extern void yyerror(const char *s);
extern int yylineno;
extern FILE *yyin;
%}
%union {
char *text;
int ival;
}
%token <text> ID
%token <ival> INT_LIT
%token LET
%token INT_TYPE
%token SEMICOLON COLON
%left '+' '-'
%left '*' '/'
%start program
%%
program
: statement_list { printf("koniec\n"); }
;
statement_list
: statement
| statement_list statement
;
statement
: variable_declaration { printf("Deklaracja zmiennej\n"); }
| expression SEMICOLON { printf("instrukcja\n"); }
;
variable_declaration
: LET ID COLON INT_TYPE '=' expression SEMICOLON
{ printf("Deklaracja zmiennej: %s\n", $2); }
;
expression
: expression '+' expression { printf("Wyrazenie z +\n"); }
| expression '-' expression { printf("Wyrazenie z -\n"); }
| expression '*' expression { printf("Wyrazenie z *\n"); }
| expression '/' expression { printf("Wyrazenie z /\n"); }
| '(' expression ')' { printf("Wyrazenie w nawiasach\n"); }
| INT_LIT { printf("Literal calkowity: %d\n", $1); }
| ID { printf("Identyfikator: %s\n", $1); }
;
%%
int main(int argc, char *argv[]) {
yyparse();
return 0;
}

2
in5.txt Normal file
View File

@@ -0,0 +1,2 @@
let x: Int = 5 + 3;
let y: Int = x - 1;

1851
lex.yy.c Normal file

File diff suppressed because it is too large Load Diff

1724
lex.yy.cc Normal file

File diff suppressed because it is too large Load Diff

BIN
lex.yy.o Normal file

Binary file not shown.

45
z5.l Normal file
View File

@@ -0,0 +1,45 @@
%{
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "def.tab.hh"
#define INFILE_ERROR 1
#define OUTFILE_ERROR 2
extern int yylineno;
void yyerror(char *msg, ...);
%}
%option noyywrap
%%
"let" {fprintf(stdout, "LET\n"); return LET;}
"Int" {fprintf(stdout, "INT_TYPE\n"); return INT_TYPE;}
":" {fprintf(stdout, ":\n"); return COLON;}
";" {fprintf(stdout, ";\n"); return SEMICOLON;}
\+ {fprintf(stdout, "+\n"); return '+';}
\* {fprintf(stdout, "*\n"); return '*';}
\( {fprintf(stdout, "(\n"); return '(';}
\) {fprintf(stdout, ")\n"); return ')';}
\/ {fprintf(stdout, "/\n"); return '/';}
\- {fprintf(stdout, "-\n"); return '-';}
\= {fprintf(stdout, "=\n"); return '=';}
[0-9]+ {
fprintf(stdout, "liczba: %s\n", yytext);
yylval.ival = atoi(yytext);
return INT_LIT;
}
[A-Za-z_][A-Za-z0-9_]* {
fprintf(stdout, "identyfikator: %s\n", yytext);
yylval.text = strdup(yytext);
return ID;
}
[ \t]+ {;}
\n {yylineno++;}
. {yyerror("Blad leksykalny\n");}
%%
void yyerror(char *msg, ...)
{
printf("%d: %s", yylineno, msg);
exit(1);
}