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