diff --git a/def.tab.cc b/def.tab.cc index 051004d..78e2b66 100644 --- a/def.tab.cc +++ b/def.tab.cc @@ -88,16 +88,19 @@ /* Copy the first part of user declarations. */ #line 1 "def.yy" -#include -#include -#include +#include +#include +#include +#include + #define INFILE_ERROR 1 #define OUTFILE_ERROR 2 -extern int yylex(void); -extern void yyerror(const char *s); -extern int yylineno; -extern FILE *yyin; +extern yyFlexLexer lexer; + +void yyerror(const char *s); +int yylex(); + /* Enabling traces. */ @@ -120,13 +123,13 @@ extern FILE *yyin; #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED typedef union YYSTYPE -#line 14 "def.yy" +#line 17 "def.yy" { char *text; int ival; } /* Line 193 of yacc.c. */ -#line 130 "def.tab.cc" +#line 133 "def.tab.cc" YYSTYPE; # define yystype YYSTYPE /* obsolescent; will be withdrawn */ # define YYSTYPE_IS_DECLARED 1 @@ -139,7 +142,7 @@ typedef union YYSTYPE /* Line 216 of yacc.c. */ -#line 143 "def.tab.cc" +#line 146 "def.tab.cc" #ifdef short # undef short @@ -426,8 +429,8 @@ static const yytype_int8 yyrhs[] = /* YYRLINE[YYN] -- source line where rule number YYN was defined. */ static const yytype_uint8 yyrline[] = { - 0, 33, 33, 37, 38, 42, 43, 47, 52, 53, - 54, 55, 56, 57, 58 + 0, 36, 36, 40, 41, 45, 46, 50, 55, 56, + 57, 58, 59, 60, 61 }; #endif @@ -1341,63 +1344,63 @@ yyreduce: switch (yyn) { case 2: -#line 33 "def.yy" +#line 36 "def.yy" { printf("koniec\n"); ;} break; case 5: -#line 42 "def.yy" +#line 45 "def.yy" { printf("Deklaracja zmiennej\n"); ;} break; case 6: -#line 43 "def.yy" +#line 46 "def.yy" { printf("instrukcja\n"); ;} break; case 7: -#line 48 "def.yy" - { printf("Deklaracja zmiennej: %s\n", (yyvsp[(2) - (7)].text)); ;} +#line 51 "def.yy" + { printf("Deklaracja zmiennej: %s\n", (yyvsp[(2) - (7)].text)); free((yyvsp[(2) - (7)].text)); ;} break; case 8: -#line 52 "def.yy" +#line 55 "def.yy" { printf("Wyrazenie z +\n"); ;} break; case 9: -#line 53 "def.yy" +#line 56 "def.yy" { printf("Wyrazenie z -\n"); ;} break; case 10: -#line 54 "def.yy" +#line 57 "def.yy" { printf("Wyrazenie z *\n"); ;} break; case 11: -#line 55 "def.yy" +#line 58 "def.yy" { printf("Wyrazenie z /\n"); ;} break; case 12: -#line 56 "def.yy" +#line 59 "def.yy" { printf("Wyrazenie w nawiasach\n"); ;} break; case 13: -#line 57 "def.yy" +#line 60 "def.yy" { printf("Literal calkowity: %d\n", (yyvsp[(1) - (1)].ival)); ;} break; case 14: -#line 58 "def.yy" - { printf("Identyfikator: %s\n", (yyvsp[(1) - (1)].text)); ;} +#line 61 "def.yy" + { printf("Identyfikator: %s\n", (yyvsp[(1) - (1)].text)); free((yyvsp[(1) - (1)].text)); ;} break; /* Line 1267 of yacc.c. */ -#line 1401 "def.tab.cc" +#line 1404 "def.tab.cc" default: break; } YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc); @@ -1611,9 +1614,19 @@ yyreturn: } -#line 61 "def.yy" +#line 64 "def.yy" +yyFlexLexer lexer; + +int yylex() { + return lexer.yylex(); +} + +void yyerror(const char *s) { + fprintf(stderr, "Blad: %s w linii %d\n", s, lexer.lineno()); +} + int main(int argc, char *argv[]) { yyparse(); return 0; diff --git a/def.tab.hh b/def.tab.hh index 16b43d6..b1df8c6 100644 --- a/def.tab.hh +++ b/def.tab.hh @@ -60,7 +60,7 @@ #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED typedef union YYSTYPE -#line 14 "def.yy" +#line 17 "def.yy" { char *text; int ival; diff --git a/def.tab.o b/def.tab.o index 204d516..a84f786 100644 Binary files a/def.tab.o and b/def.tab.o differ diff --git a/def.yy b/def.yy index e1f40b7..d7d45a1 100644 --- a/def.yy +++ b/def.yy @@ -1,14 +1,17 @@ %{ -#include -#include -#include +#include +#include +#include +#include + #define INFILE_ERROR 1 #define OUTFILE_ERROR 2 -extern int yylex(void); -extern void yyerror(const char *s); -extern int yylineno; -extern FILE *yyin; +extern yyFlexLexer lexer; + +void yyerror(const char *s); +int yylex(); + %} %union { @@ -45,7 +48,7 @@ statement variable_declaration : LET ID COLON INT_TYPE '=' expression SEMICOLON - { printf("Deklaracja zmiennej: %s\n", $2); } + { printf("Deklaracja zmiennej: %s\n", $2); free($2); } ; expression @@ -55,11 +58,21 @@ expression | 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); } + | ID { printf("Identyfikator: %s\n", $1); free($1); } ; %% +yyFlexLexer lexer; + +int yylex() { + return lexer.yylex(); +} + +void yyerror(const char *s) { + fprintf(stderr, "Blad: %s w linii %d\n", s, lexer.lineno()); +} + int main(int argc, char *argv[]) { yyparse(); return 0; diff --git a/leks b/leks new file mode 100755 index 0000000..5b33d0e Binary files /dev/null and b/leks differ diff --git a/lex.yy.cc b/lex.yy.cc index 734b1a0..0cd8b84 100644 --- a/lex.yy.cc +++ b/lex.yy.cc @@ -434,9 +434,10 @@ static const flex_int32_t yy_rule_can_match_eol[18] = #define YY_RESTORE_YY_MORE_OFFSET #line 1 "z5.l" #line 2 "z5.l" -#include -#include -#include +#include +#include +#include +#include #include "def.tab.hh" #define INFILE_ERROR 1 @@ -444,10 +445,8 @@ static const flex_int32_t yy_rule_can_match_eol[18] = #define YY_DECL int yyFlexLexer::yylex() -extern int yylineno; -void yyerror(char *msg, ...); +#line 448 "lex.yy.cc" #line 449 "lex.yy.cc" -#line 450 "lex.yy.cc" #define INITIAL 0 @@ -579,7 +578,8 @@ YY_DECL } { -#line 20 "z5.l" +#line 19 "z5.l" + #line 584 "lex.yy.cc" @@ -651,98 +651,101 @@ do_action: /* This label is used only to access EOF actions. */ case 1: YY_RULE_SETUP #line 21 "z5.l" -{fprintf(stdout, "LET\n"); return LET;} +{std::fprintf(stdout, "LET\n"); return LET;} YY_BREAK case 2: YY_RULE_SETUP #line 22 "z5.l" -{fprintf(stdout, "INT_TYPE\n"); return INT_TYPE;} +{std::fprintf(stdout, "INT_TYPE\n"); return INT_TYPE;} YY_BREAK case 3: YY_RULE_SETUP #line 23 "z5.l" -{fprintf(stdout, ":\n"); return COLON;} +{std::fprintf(stdout, ":\n"); return COLON;} YY_BREAK case 4: YY_RULE_SETUP #line 24 "z5.l" -{fprintf(stdout, ";\n"); return SEMICOLON;} +{std::fprintf(stdout, ";\n"); return SEMICOLON;} YY_BREAK case 5: YY_RULE_SETUP #line 25 "z5.l" -{fprintf(stdout, "+\n"); return '+';} +{std::fprintf(stdout, "+\n"); return '+';} YY_BREAK case 6: YY_RULE_SETUP #line 26 "z5.l" -{fprintf(stdout, "*\n"); return '*';} +{std::fprintf(stdout, "*\n"); return '*';} YY_BREAK case 7: YY_RULE_SETUP #line 27 "z5.l" -{fprintf(stdout, "(\n"); return '(';} +{std::fprintf(stdout, "(\n"); return '(';} YY_BREAK case 8: YY_RULE_SETUP #line 28 "z5.l" -{fprintf(stdout, ")\n"); return ')';} +{std::fprintf(stdout, ")\n"); return ')';} YY_BREAK case 9: YY_RULE_SETUP #line 29 "z5.l" -{fprintf(stdout, "/\n"); return '/';} +{std::fprintf(stdout, "/\n"); return '/';} YY_BREAK case 10: YY_RULE_SETUP #line 30 "z5.l" -{fprintf(stdout, "-\n"); return '-';} +{std::fprintf(stdout, "-\n"); return '-';} YY_BREAK case 11: YY_RULE_SETUP #line 31 "z5.l" -{fprintf(stdout, "=\n"); return '=';} +{std::fprintf(stdout, "=\n"); return '=';} YY_BREAK case 12: YY_RULE_SETUP -#line 32 "z5.l" +#line 33 "z5.l" { - fprintf(stdout, "liczba: %s\n", yytext); + std::fprintf(stdout, "liczba: %s\n", yytext); yylval.ival = atoi(yytext); return INT_LIT; } YY_BREAK case 13: YY_RULE_SETUP -#line 37 "z5.l" +#line 39 "z5.l" { - fprintf(stdout, "identyfikator: %s\n", yytext); + std::fprintf(stdout, "identyfikator: %s\n", yytext); yylval.text = strdup(yytext); return ID; } YY_BREAK case 14: YY_RULE_SETUP -#line 42 "z5.l" -{;} +#line 45 "z5.l" +{/* ignoruj białe znaki */} YY_BREAK case 15: /* rule 15 can match eol */ YY_RULE_SETUP -#line 43 "z5.l" -{yylineno++;} +#line 46 "z5.l" +{/* nowa linia */} YY_BREAK case 16: YY_RULE_SETUP -#line 44 "z5.l" -{yyerror("Blad leksykalny\n");} +#line 47 "z5.l" +{ + std::fprintf(stderr, "Blad leksykalny w linii %d\n", lineno()); + exit(1); + } YY_BREAK case 17: YY_RULE_SETUP -#line 45 "z5.l" +#line 52 "z5.l" ECHO; YY_BREAK -#line 745 "lex.yy.cc" +#line 748 "lex.yy.cc" case YY_STATE_EOF(INITIAL): yyterminate(); @@ -1714,11 +1717,6 @@ void yyfree (void * ptr ) #define YYTABLES_NAME "yytables" -#line 45 "z5.l" +#line 52 "z5.l" -void yyerror(char *msg, ...) -{ - printf("%d: %s", yylineno, msg); - exit(1); -} diff --git a/lex.yy.o b/lex.yy.o index 7f86c2f..21c373e 100644 Binary files a/lex.yy.o and b/lex.yy.o differ diff --git a/z5.l b/z5.l index bdc1930..f92965b 100644 --- a/z5.l +++ b/z5.l @@ -1,45 +1,52 @@ %{ -#include -#include -#include +#include +#include +#include +#include #include "def.tab.hh" #define INFILE_ERROR 1 #define OUTFILE_ERROR 2 -extern int yylineno; -void yyerror(char *msg, ...); +#define YY_DECL int yyFlexLexer::yylex() + %} %option noyywrap +%option c++ +%option yylineno + %% -"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 '=';} + +"let" {std::fprintf(stdout, "LET\n"); return LET;} +"Int" {std::fprintf(stdout, "INT_TYPE\n"); return INT_TYPE;} +":" {std::fprintf(stdout, ":\n"); return COLON;} +";" {std::fprintf(stdout, ";\n"); return SEMICOLON;} +\+ {std::fprintf(stdout, "+\n"); return '+';} +\* {std::fprintf(stdout, "*\n"); return '*';} +\( {std::fprintf(stdout, "(\n"); return '(';} +\) {std::fprintf(stdout, ")\n"); return ')';} +\/ {std::fprintf(stdout, "/\n"); return '/';} +\- {std::fprintf(stdout, "-\n"); return '-';} +\= {std::fprintf(stdout, "=\n"); return '=';} + [0-9]+ { - fprintf(stdout, "liczba: %s\n", yytext); + std::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); + std::fprintf(stdout, "identyfikator: %s\n", yytext); yylval.text = strdup(yytext); return ID; } -[ \t]+ {;} -\n {yylineno++;} -. {yyerror("Blad leksykalny\n");} + +[ \t]+ {/* ignoruj białe znaki */} +\n {/* nowa linia */} +. { + std::fprintf(stderr, "Blad leksykalny w linii %d\n", lineno()); + exit(1); + } + %% -void yyerror(char *msg, ...) -{ - printf("%d: %s", yylineno, msg); - exit(1); -}