diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8e1418e --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +CXX := g++ +CXXFLAGS := -std=c++17 -Wall -Wextra -O2 + +all: mycompiler + +parser.cpp parser.hpp: parser.y + bison -d -o parser.cpp parser.y + +lexer.cpp: lexer.l parser.hpp + flex -o lexer.cpp lexer.l + +mycompiler: parser.cpp lexer.cpp main.cpp + $(CXX) $(CXXFLAGS) -o $@ parser.cpp lexer.cpp main.cpp -lfl + +clean: + rm -f parser.cpp parser.hpp lexer.cpp mycompiler diff --git a/README.md b/README.md deleted file mode 100644 index d0e2dec..0000000 --- a/README.md +++ /dev/null @@ -1 +0,0 @@ -This is repo with compiler writen using C++ && Flex && Bison parser diff --git a/ast.h b/ast.h deleted file mode 100644 index c9599cf..0000000 --- a/ast.h +++ /dev/null @@ -1,84 +0,0 @@ -#ifndef AST_H -#define AST_H - -#include -#include -#include -#include - -using namespace std; - -struct ASTNode { - virtual ~ASTNode() = default; - virtual void print(int indent=0) const = 0; -}; - -using ASTNodePtr = unique_ptr; - -static string indentStr(int n) { return string(n, ' '); } - -struct ProgramNode : ASTNode { - vector decls; - void print(int indent=0) const override { - cout << indentStr(indent) << "Program\n"; - for (auto &d : decls) d->print(indent+2); - } -}; - -struct ClassNode : ASTNode { - string name; - string extendsName; - vector members; - ClassNode(const string &n="") : name(n) {} - void print(int indent=0) const override { - cout << indentStr(indent) << "Class: " << name; - if (!extendsName.empty()) cout << " extends " << extendsName; - cout << "\n"; - for (auto &m : members) m->print(indent+2); - } -}; - -struct VarNode : ASTNode { - string name; - string type; - VarNode(const string &n="", const string &t="") : name(n), type(t) {} - void print(int indent=0) const override { - cout << indentStr(indent) << "Var: " << name; - if (!type.empty()) cout << " : " << type; - cout << "\n"; - } -}; - -struct MethodNode : ASTNode { - string name; - vector params; - vector body; - MethodNode(const string &n="") : name(n) {} - void print(int indent=0) const override { - cout << indentStr(indent) << "Method: " << name << "\n"; - for (auto &b : body) b->print(indent+2); - } -}; - -extern unique_ptr g_program; - -struct SimpleToken { - string kind; - string text; - int line; - int startCol; - int endCol; - - SimpleToken() = default; - SimpleToken(const string& k, const string& t, int l, int sc, int ec) - : kind(k), text(t), line(l), startCol(sc), endCol(ec) {} -}; -extern std::vector simpleTokens; - -extern size_t parserTokIndex; - -void yyerror(const char *s); -int yylex(); -void printAST(); - -#endif // AST_H diff --git a/ast.hpp b/ast.hpp new file mode 100644 index 0000000..dba43ba --- /dev/null +++ b/ast.hpp @@ -0,0 +1,178 @@ +#pragma once +#include +#include +#include +#include + +namespace AST { + +struct Node { + virtual ~Node() = default; + virtual void print(std::ostream& os, int indent = 0) const = 0; +}; + +inline void doIndent(std::ostream& os, int n) { + for (int i = 0; i < n; ++i) os << " "; +} + +struct Expr : Node { }; + +struct IntLiteral : Expr { + std::int64_t value; + explicit IntLiteral(std::int64_t v) : value(v) {} + void print(std::ostream& os, int indent) const override { + doIndent(os, indent); os << "Int(" << value << ")\n"; + } +}; + +struct BoolLiteral : Expr { + bool value; + explicit BoolLiteral(bool v) : value(v) {} + void print(std::ostream& os, int indent) const override { + doIndent(os, indent); os << "Bool(" << (value ? "true" : "false") << ")\n"; + } +}; + +struct Identifier : Expr { + std::string name; + explicit Identifier(std::string n) : name(std::move(n)) {} + void print(std::ostream& os, int indent) const override { + doIndent(os, indent); os << "Id(" << name << ")\n"; + } +}; + +enum class BinOp { Add, Sub, Mul, Div, Assign }; + +struct Binary : Expr { + BinOp op; + Expr* lhs; + Expr* rhs; + Binary(BinOp o, Expr* l, Expr* r) : op(o), lhs(l), rhs(r) {} + ~Binary() { delete lhs; delete rhs; } + static const char* opToStr(BinOp o) { + switch (o) { + case BinOp::Add: return "+"; + case BinOp::Sub: return "-"; + case BinOp::Mul: return "*"; + case BinOp::Div: return "/"; + case BinOp::Assign: return "="; + } + return "?"; + } + void print(std::ostream& os, int indent) const override { + doIndent(os, indent); + os << "BinOp(" << opToStr(op) << ")\n"; + lhs->print(os, indent + 1); + rhs->print(os, indent + 1); + } +}; + +struct Unary : Expr { + enum class Op { Neg }; + Op op; + Expr* rhs; + Unary(Op o, Expr* e) : op(o), rhs(e) {} + ~Unary() { delete rhs; } + void print(std::ostream& os, int indent) const override { + doIndent(os, indent); + os << "Unary(-)\n"; + rhs->print(os, indent + 1); + } +}; + +struct Stmt : Node { }; + +struct ReturnStmt : Stmt { + Expr* value; + explicit ReturnStmt(Expr* v) : value(v) {} + ~ReturnStmt() { delete value; } + void print(std::ostream& os, int indent) const override { + doIndent(os, indent); os << "return\n"; + value->print(os, indent + 1); + } +}; + +struct IfStmt : Stmt { + Expr* cond; + Stmt* thenS; + Stmt* elseS; + IfStmt(Expr* c, Stmt* t, Stmt* e) : cond(c), thenS(t), elseS(e) {} + ~IfStmt() { delete cond; delete thenS; delete elseS; } + void print(std::ostream& os, int indent) const override { + doIndent(os, indent); os << "if\n"; + cond->print(os, indent + 1); + doIndent(os, indent); os << "then\n"; + thenS->print(os, indent + 1); + doIndent(os, indent); os << "else\n"; + elseS->print(os, indent + 1); + } +}; + +struct VarDecl : Node { + std::string name; + std::string typeName; + Expr* init; + VarDecl(std::string n, std::string t, Expr* i) + : name(std::move(n)), typeName(std::move(t)), init(i) {} + ~VarDecl() { delete init; } + void print(std::ostream& os, int indent) const override { + doIndent(os, indent); + os << "var " << name << " : " << typeName; + if (init) { + os << " =\n"; + init->print(os, indent + 1); + } else { + os << "\n"; + } + } +}; + +struct Param { + std::string name; + std::string typeName; + Param(std::string n, std::string t) : name(std::move(n)), typeName(std::move(t)) {} +}; + +struct MethodDecl : Node { + std::string name; + std::vector params; + std::string returnType; + Stmt* body; + MethodDecl(std::string n, std::string rt, Stmt* b) + : name(std::move(n)), returnType(std::move(rt)), body(b) {} + ~MethodDecl() { for (auto* p : params) delete p; delete body; } + void print(std::ostream& os, int indent) const override { + doIndent(os, indent); + os << "method " << name << "("; + for (size_t i = 0; i < params.size(); ++i) { + os << params[i]->name << " : " << params[i]->typeName; + if (i + 1 < params.size()) os << ", "; + } + os << ") : " << returnType << "\n"; + if (body) body->print(os, indent + 1); + } +}; + +struct ClassDecl : Node { + std::string name; + std::vector fields; + std::vector methods; + explicit ClassDecl(std::string n) : name(std::move(n)) {} + ~ClassDecl() { for (auto* v : fields) delete v; for (auto* m : methods) delete m; } + void print(std::ostream& os, int indent) const override { + doIndent(os, indent); + os << "Class: " << name << "\n"; + for (auto* v : fields) v->print(os, indent + 1); + for (auto* m : methods) m->print(os, indent + 1); + } +}; + +struct Program : Node { + std::vector classes; + ~Program() { for (auto* c : classes) delete c; } + void print(std::ostream& os, int indent = 0) const override { + for (auto* c : classes) c->print(os, indent); + } +}; + +} diff --git a/lex.yy.cc b/lex.yy.cc deleted file mode 100644 index aee928e..0000000 --- a/lex.yy.cc +++ /dev/null @@ -1,1794 +0,0 @@ -#line 2 "lex.yy.cc" - -#line 4 "lex.yy.cc" - -#define YY_INT_ALIGNED short int - -/* A lexical scanner generated by flex */ - -#define FLEX_SCANNER -#define YY_FLEX_MAJOR_VERSION 2 -#define YY_FLEX_MINOR_VERSION 6 -#define YY_FLEX_SUBMINOR_VERSION 4 -#if YY_FLEX_SUBMINOR_VERSION > 0 -#define FLEX_BETA -#endif - - /* The c++ scanner is a mess. The FlexLexer.h header file relies on the - * following macro. This is required in order to pass the c++-multiple-scanners - * test in the regression suite. We get reports that it breaks inheritance. - * We will address this in a future release of flex, or omit the C++ scanner - * altogether. - */ - #define yyFlexLexer yyFlexLexer - -/* First, we deal with platform-specific or compiler-specific issues. */ - -/* begin standard C headers. */ - -/* end standard C headers. */ - -/* flex integer type definitions */ - -#ifndef FLEXINT_H -#define FLEXINT_H - -/* C99 systems have . Non-C99 systems may or may not. */ - -#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L - -/* C99 says to define __STDC_LIMIT_MACROS before including stdint.h, - * if you want the limit (max/min) macros for int types. - */ -#ifndef __STDC_LIMIT_MACROS -#define __STDC_LIMIT_MACROS 1 -#endif - -#include -typedef int8_t flex_int8_t; -typedef uint8_t flex_uint8_t; -typedef int16_t flex_int16_t; -typedef uint16_t flex_uint16_t; -typedef int32_t flex_int32_t; -typedef uint32_t flex_uint32_t; -#else -typedef signed char flex_int8_t; -typedef short int flex_int16_t; -typedef int flex_int32_t; -typedef unsigned char flex_uint8_t; -typedef unsigned short int flex_uint16_t; -typedef unsigned int flex_uint32_t; - -/* Limits of integral types. */ -#ifndef INT8_MIN -#define INT8_MIN (-128) -#endif -#ifndef INT16_MIN -#define INT16_MIN (-32767-1) -#endif -#ifndef INT32_MIN -#define INT32_MIN (-2147483647-1) -#endif -#ifndef INT8_MAX -#define INT8_MAX (127) -#endif -#ifndef INT16_MAX -#define INT16_MAX (32767) -#endif -#ifndef INT32_MAX -#define INT32_MAX (2147483647) -#endif -#ifndef UINT8_MAX -#define UINT8_MAX (255U) -#endif -#ifndef UINT16_MAX -#define UINT16_MAX (65535U) -#endif -#ifndef UINT32_MAX -#define UINT32_MAX (4294967295U) -#endif - -#ifndef SIZE_MAX -#define SIZE_MAX (~(size_t)0) -#endif - -#endif /* ! C99 */ - -#endif /* ! FLEXINT_H */ - -/* begin standard C++ headers. */ -#include -#include -#include -#include -#include -/* end standard C++ headers. */ - -/* TODO: this is always defined, so inline it */ -#define yyconst const - -#if defined(__GNUC__) && __GNUC__ >= 3 -#define yynoreturn __attribute__((__noreturn__)) -#else -#define yynoreturn -#endif - -/* Returned upon end-of-file. */ -#define YY_NULL 0 - -/* Promotes a possibly negative, possibly signed char to an - * integer in range [0..255] for use as an array index. - */ -#define YY_SC_TO_UI(c) ((YY_CHAR) (c)) - -/* Enter a start condition. This macro really ought to take a parameter, - * but we do it the disgusting crufty way forced on us by the ()-less - * definition of BEGIN. - */ -#define BEGIN (yy_start) = 1 + 2 * -/* Translate the current start state into a value that can be later handed - * to BEGIN to return to the state. The YYSTATE alias is for lex - * compatibility. - */ -#define YY_START (((yy_start) - 1) / 2) -#define YYSTATE YY_START -/* Action number for EOF rule of a given start state. */ -#define YY_STATE_EOF(state) (YY_END_OF_BUFFER + state + 1) -/* Special action meaning "start processing a new file". */ -#define YY_NEW_FILE yyrestart( yyin ) -#define YY_END_OF_BUFFER_CHAR 0 - -/* Size of default input buffer. */ -#ifndef YY_BUF_SIZE -#ifdef __ia64__ -/* On IA-64, the buffer size is 16k, not 8k. - * Moreover, YY_BUF_SIZE is 2*YY_READ_BUF_SIZE in the general case. - * Ditto for the __ia64__ case accordingly. - */ -#define YY_BUF_SIZE 32768 -#else -#define YY_BUF_SIZE 16384 -#endif /* __ia64__ */ -#endif - -/* The state buf must be large enough to hold one state per character in the main buffer. - */ -#define YY_STATE_BUF_SIZE ((YY_BUF_SIZE + 2) * sizeof(yy_state_type)) - -#ifndef YY_TYPEDEF_YY_BUFFER_STATE -#define YY_TYPEDEF_YY_BUFFER_STATE -typedef struct yy_buffer_state *YY_BUFFER_STATE; -#endif - -#ifndef YY_TYPEDEF_YY_SIZE_T -#define YY_TYPEDEF_YY_SIZE_T -typedef size_t yy_size_t; -#endif - -extern int yyleng; - -#define EOB_ACT_CONTINUE_SCAN 0 -#define EOB_ACT_END_OF_FILE 1 -#define EOB_ACT_LAST_MATCH 2 - - #define YY_LESS_LINENO(n) - #define YY_LINENO_REWIND_TO(ptr) - -/* Return all but the first "n" matched characters back to the input stream. */ -#define yyless(n) \ - do \ - { \ - /* Undo effects of setting up yytext. */ \ - int yyless_macro_arg = (n); \ - YY_LESS_LINENO(yyless_macro_arg);\ - *yy_cp = (yy_hold_char); \ - YY_RESTORE_YY_MORE_OFFSET \ - (yy_c_buf_p) = yy_cp = yy_bp + yyless_macro_arg - YY_MORE_ADJ; \ - YY_DO_BEFORE_ACTION; /* set up yytext again */ \ - } \ - while ( 0 ) -#define unput(c) yyunput( c, (yytext_ptr) ) - -#ifndef YY_STRUCT_YY_BUFFER_STATE -#define YY_STRUCT_YY_BUFFER_STATE -struct yy_buffer_state - { - - std::streambuf* yy_input_file; - - char *yy_ch_buf; /* input buffer */ - char *yy_buf_pos; /* current position in input buffer */ - - /* Size of input buffer in bytes, not including room for EOB - * characters. - */ - int yy_buf_size; - - /* Number of characters read into yy_ch_buf, not including EOB - * characters. - */ - int yy_n_chars; - - /* Whether we "own" the buffer - i.e., we know we created it, - * and can realloc() it to grow it, and should free() it to - * delete it. - */ - int yy_is_our_buffer; - - /* Whether this is an "interactive" input source; if so, and - * if we're using stdio for input, then we want to use getc() - * instead of fread(), to make sure we stop fetching input after - * each newline. - */ - int yy_is_interactive; - - /* Whether we're considered to be at the beginning of a line. - * If so, '^' rules will be active on the next match, otherwise - * not. - */ - int yy_at_bol; - - int yy_bs_lineno; /**< The line count. */ - int yy_bs_column; /**< The column count. */ - - /* Whether to try to fill the input buffer when we reach the - * end of it. - */ - int yy_fill_buffer; - - int yy_buffer_status; - -#define YY_BUFFER_NEW 0 -#define YY_BUFFER_NORMAL 1 - /* When an EOF's been seen but there's still some text to process - * then we mark the buffer as YY_EOF_PENDING, to indicate that we - * shouldn't try reading from the input source any more. We might - * still have a bunch of tokens to match, though, because of - * possible backing-up. - * - * When we actually see the EOF, we change the status to "new" - * (via yyrestart()), so that the user can continue scanning by - * just pointing yyin at a new input file. - */ -#define YY_BUFFER_EOF_PENDING 2 - - }; -#endif /* !YY_STRUCT_YY_BUFFER_STATE */ - -/* We provide macros for accessing buffer states in case in the - * future we want to put the buffer states in a more general - * "scanner state". - * - * Returns the top of the stack, or NULL. - */ -#define YY_CURRENT_BUFFER ( (yy_buffer_stack) \ - ? (yy_buffer_stack)[(yy_buffer_stack_top)] \ - : NULL) -/* Same as previous macro, but useful when we know that the buffer stack is not - * NULL or when we need an lvalue. For internal use only. - */ -#define YY_CURRENT_BUFFER_LVALUE (yy_buffer_stack)[(yy_buffer_stack_top)] - -void *yyalloc ( yy_size_t ); -void *yyrealloc ( void *, yy_size_t ); -void yyfree ( void * ); - -#define yy_new_buffer yy_create_buffer -#define yy_set_interactive(is_interactive) \ - { \ - if ( ! YY_CURRENT_BUFFER ){ \ - yyensure_buffer_stack (); \ - YY_CURRENT_BUFFER_LVALUE = \ - yy_create_buffer( yyin, YY_BUF_SIZE ); \ - } \ - YY_CURRENT_BUFFER_LVALUE->yy_is_interactive = is_interactive; \ - } -#define yy_set_bol(at_bol) \ - { \ - if ( ! YY_CURRENT_BUFFER ){\ - yyensure_buffer_stack (); \ - YY_CURRENT_BUFFER_LVALUE = \ - yy_create_buffer( yyin, YY_BUF_SIZE ); \ - } \ - YY_CURRENT_BUFFER_LVALUE->yy_at_bol = at_bol; \ - } -#define YY_AT_BOL() (YY_CURRENT_BUFFER_LVALUE->yy_at_bol) - -/* Begin user sect3 */ -#define YY_SKIP_YYWRAP -typedef flex_uint8_t YY_CHAR; - -#define yytext_ptr yytext -#define YY_INTERACTIVE - -#include - -int yyFlexLexer::yywrap() { return 1; } - -/* Done after the current pattern has been matched and before the - * corresponding action - sets up yytext. - */ -#define YY_DO_BEFORE_ACTION \ - (yytext_ptr) = yy_bp; \ - yyleng = (int) (yy_cp - yy_bp); \ - (yy_hold_char) = *yy_cp; \ - *yy_cp = '\0'; \ - (yy_c_buf_p) = yy_cp; -#define YY_NUM_RULES 10 -#define YY_END_OF_BUFFER 11 -/* This struct is not used in this scanner, - but its presence is necessary. */ -struct yy_trans_info - { - flex_int32_t yy_verify; - flex_int32_t yy_nxt; - }; -static const flex_int16_t yy_accept[44] = - { 0, - 0, 0, 11, 9, 1, 2, 9, 9, 8, 5, - 6, 6, 6, 6, 6, 6, 6, 1, 2, 0, - 7, 0, 0, 5, 6, 6, 6, 6, 3, 6, - 6, 6, 4, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 0 - } ; - -static const YY_CHAR yy_ec[256] = - { 0, - 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, - 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 2, 1, 5, 1, 1, 1, 1, 1, 6, - 6, 6, 6, 6, 6, 7, 6, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 1, 6, 6, - 6, 6, 1, 1, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 1, 10, 1, 1, 9, 1, 11, 9, 9, 9, - - 12, 13, 9, 14, 15, 9, 9, 16, 9, 17, - 18, 9, 9, 19, 20, 21, 22, 9, 23, 9, - 9, 9, 6, 1, 6, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1 - } ; - -static const YY_CHAR yy_meta[24] = - { 0, - 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2 - } ; - -static const flex_int16_t yy_base[46] = - { 0, - 0, 0, 63, 64, 60, 64, 58, 19, 64, 18, - 0, 44, 16, 46, 46, 38, 42, 53, 64, 23, - 64, 51, 45, 23, 0, 32, 35, 31, 0, 28, - 26, 32, 38, 33, 24, 21, 30, 25, 28, 20, - 26, 20, 64, 34, 30 - } ; - -static const flex_int16_t yy_def[46] = - { 0, - 43, 1, 43, 43, 43, 43, 43, 44, 43, 43, - 45, 45, 45, 45, 45, 45, 45, 43, 43, 44, - 43, 44, 43, 43, 45, 45, 45, 45, 45, 45, - 45, 45, 43, 45, 45, 45, 45, 45, 45, 45, - 45, 45, 0, 43, 43 - } ; - -static const flex_int16_t yy_nxt[88] = - { 0, - 4, 5, 6, 7, 8, 9, 4, 10, 11, 4, - 11, 12, 13, 11, 14, 11, 11, 11, 15, 11, - 16, 11, 17, 21, 23, 24, 27, 21, 22, 23, - 24, 25, 22, 28, 20, 20, 29, 29, 42, 29, - 41, 29, 40, 39, 29, 33, 38, 37, 36, 29, - 35, 34, 33, 43, 18, 32, 31, 30, 29, 26, - 19, 18, 43, 3, 43, 43, 43, 43, 43, 43, - 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, - 43, 43, 43, 43, 43, 43, 43 - } ; - -static const flex_int16_t yy_chk[88] = - { 0, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 8, 10, 10, 13, 20, 8, 24, - 24, 45, 20, 13, 44, 44, 42, 41, 40, 39, - 38, 37, 36, 35, 34, 33, 32, 31, 30, 28, - 27, 26, 23, 22, 18, 17, 16, 15, 14, 12, - 7, 5, 3, 43, 43, 43, 43, 43, 43, 43, - 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, - 43, 43, 43, 43, 43, 43, 43 - } ; - -/* The intent behind this definition is that it'll catch - * any uses of REJECT which flex missed. - */ -#define REJECT reject_used_but_not_detected -#define yymore() yymore_used_but_not_detected -#define YY_MORE_ADJ 0 -#define YY_RESTORE_YY_MORE_OFFSET -#line 1 "lexer.l" -#line 4 "lexer.l" -#include -#include -#include -#include -#include -#include -#include - -using namespace std; - -class Token { -public: - int line; - int startCol; - int endCol; - - Token(int l, int s, int e) : line(l), startCol(s), endCol(e) {} - virtual ~Token() = default; - virtual void print() const = 0; -}; - -class KeywordToken : public Token { -public: - string value; - KeywordToken(const string& val, int l, int s, int e) : Token(l,s,e), value(val) {} - void print() const override { cout << "KEYWORD(" << value << ")\n"; } -}; - -class IdentifierToken : public Token { -public: - string name; - IdentifierToken(const string& n, int l, int s, int e) : Token(l,s,e), name(n) {} - void print() const override { cout << "IDENTIFIER(" << name << ")\n"; } -}; - -class IntegerToken : public Token { -public: - int value; - IntegerToken(int v, int l, int s, int e) : Token(l,s,e), value(v) {} - void print() const override { cout << "INTEGER(" << value << ")\n"; } -}; - -class RealToken : public Token { -public: - double value; - RealToken(double v, int l, int s, int e) : Token(l,s,e), value(v) {} - void print() const override { cout << "REAL(" << value << ")\n"; } -}; - -class BooleanToken : public Token { -public: - string value; - BooleanToken(const string& v, int l, int s, int e) : Token(l,s,e), value(v) {} - void print() const override { cout << "BOOLEAN(" << value << ")\n"; } -}; - -class StringToken : public Token { -public: - string value; - StringToken(const string& v, int l, int s, int e) : Token(l,s,e), value(v) {} - void print() const override { cout << "STRING(\"" << value << "\")\n"; } -}; - -class SymbolToken : public Token { -public: - string symbol; - SymbolToken(const string& s, int l, int sc, int ec) : Token(l,sc,ec), symbol(s) {} - void print() const override { cout << "SYMBOL(" << symbol << ")\n"; } -}; - -class UnknownToken : public Token { -public: - string text; - UnknownToken(const string& t, int l, int s, int e) : Token(l,s,e), text(t) {} - void print() const override { cout << "UNKNOWN(" << text << ")\n"; } -}; - -vector> tokens; -int currentLine = 1; -int currentCol = 1; - -struct SimpleToken { - string kind; - string text; - int line; - int startCol; - int endCol; -}; -vector simpleTokens; - -static inline void addToken(unique_ptr t) { - SimpleToken st; - st.line = t->line; - st.startCol = t->startCol; - st.endCol = t->endCol; - - if (auto *p = dynamic_cast(t.get())) { - st.kind = "KEYWORD"; st.text = p->value; - } else if (auto *p = dynamic_cast(t.get())) { - st.kind = "IDENTIFIER"; st.text = p->name; - } else if (auto *p = dynamic_cast(t.get())) { - st.kind = "INTEGER"; st.text = to_string(p->value); - } else if (auto *p = dynamic_cast(t.get())) { - st.kind = "REAL"; st.text = to_string(p->value); - } else if (auto *p = dynamic_cast(t.get())) { - st.kind = "BOOLEAN"; st.text = p->value; - } else if (auto *p = dynamic_cast(t.get())) { - st.kind = "STRING"; st.text = p->value; - } else if (auto *p = dynamic_cast(t.get())) { - st.kind = "SYMBOL"; st.text = p->symbol; - } else if (auto *p = dynamic_cast(t.get())) { - st.kind = "UNKNOWN"; st.text = p->text; - } - - simpleTokens.push_back(st); - tokens.push_back(move(t)); -} -#line 545 "lex.yy.cc" -#line 546 "lex.yy.cc" - -#define INITIAL 0 - -#ifndef YY_NO_UNISTD_H -/* Special case for "unistd.h", since it is non-ANSI. We include it way - * down here because we want the user's section 1 to have been scanned first. - * The user has a chance to override it with an option. - */ -#include -#endif - -#ifndef YY_EXTRA_TYPE -#define YY_EXTRA_TYPE void * -#endif - -#ifndef yytext_ptr -static void yy_flex_strncpy ( char *, const char *, int ); -#endif - -#ifdef YY_NEED_STRLEN -static int yy_flex_strlen ( const char * ); -#endif - -#ifndef YY_NO_INPUT - -#endif - -/* Amount of stuff to slurp up with each read. */ -#ifndef YY_READ_BUF_SIZE -#ifdef __ia64__ -/* On IA-64, the buffer size is 16k, not 8k */ -#define YY_READ_BUF_SIZE 16384 -#else -#define YY_READ_BUF_SIZE 8192 -#endif /* __ia64__ */ -#endif - -/* Copy whatever the last rule matched to the standard output. */ -#ifndef ECHO -#define ECHO LexerOutput( yytext, yyleng ) -#endif - -/* Gets input and stuffs it into "buf". number of characters read, or YY_NULL, - * is returned in "result". - */ -#ifndef YY_INPUT -#define YY_INPUT(buf,result,max_size) \ -\ - if ( (int)(result = LexerInput( (char *) buf, max_size )) < 0 ) \ - YY_FATAL_ERROR( "input in flex scanner failed" ); - -#endif - -/* No semi-colon after return; correct usage is to write "yyterminate();" - - * we don't want an extra ';' after the "return" because that will cause - * some compilers to complain about unreachable statements. - */ -#ifndef yyterminate -#define yyterminate() return YY_NULL -#endif - -/* Number of entries by which start-condition stack grows. */ -#ifndef YY_START_STACK_INCR -#define YY_START_STACK_INCR 25 -#endif - -/* Report a fatal error. */ -#ifndef YY_FATAL_ERROR -#define YY_FATAL_ERROR(msg) LexerError( msg ) -#endif - -/* end tables serialization structures and prototypes */ - -/* Default declaration of generated scanner - a define so the user can - * easily add parameters. - */ -#ifndef YY_DECL -#define YY_DECL_IS_OURS 1 -#define YY_DECL int yyFlexLexer::yylex() -#endif /* !YY_DECL */ - -/* Code executed at the beginning of each rule, after yytext and yyleng - * have been set up. - */ -#ifndef YY_USER_ACTION -#define YY_USER_ACTION -#endif - -/* Code executed at the end of each rule. */ -#ifndef YY_BREAK -#define YY_BREAK /*LINTED*/break; -#endif - -#define YY_RULE_SETUP \ - YY_USER_ACTION - -/** The main scanner function which does all the work. - */ -YY_DECL -{ - yy_state_type yy_current_state; - char *yy_cp, *yy_bp; - int yy_act; - - if ( !(yy_init) ) - { - (yy_init) = 1; - -#ifdef YY_USER_INIT - YY_USER_INIT; -#endif - - if ( ! (yy_start) ) - (yy_start) = 1; /* first start state */ - - if ( ! yyin ) - yyin.rdbuf(std::cin.rdbuf()); - - if ( ! yyout ) - yyout.rdbuf(std::cout.rdbuf()); - - if ( ! YY_CURRENT_BUFFER ) { - yyensure_buffer_stack (); - YY_CURRENT_BUFFER_LVALUE = - yy_create_buffer( yyin, YY_BUF_SIZE ); - } - - yy_load_buffer_state( ); - } - - { -#line 123 "lexer.l" - - -#line 681 "lex.yy.cc" - - while ( /*CONSTCOND*/1 ) /* loops until end-of-file is reached */ - { - yy_cp = (yy_c_buf_p); - - /* Support of yytext. */ - *yy_cp = (yy_hold_char); - - /* yy_bp points to the position in yy_ch_buf of the start of - * the current run. - */ - yy_bp = yy_cp; - - yy_current_state = (yy_start); -yy_match: - do - { - YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)] ; - if ( yy_accept[yy_current_state] ) - { - (yy_last_accepting_state) = yy_current_state; - (yy_last_accepting_cpos) = yy_cp; - } - while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) - { - yy_current_state = (int) yy_def[yy_current_state]; - if ( yy_current_state >= 44 ) - yy_c = yy_meta[yy_c]; - } - yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c]; - ++yy_cp; - } - while ( yy_base[yy_current_state] != 64 ); - -yy_find_action: - yy_act = yy_accept[yy_current_state]; - if ( yy_act == 0 ) - { /* have to back up */ - yy_cp = (yy_last_accepting_cpos); - yy_current_state = (yy_last_accepting_state); - yy_act = yy_accept[yy_current_state]; - } - - YY_DO_BEFORE_ACTION; - -do_action: /* This label is used only to access EOF actions. */ - - switch ( yy_act ) - { /* beginning of action switch */ - case 0: /* must back up */ - /* undo the effects of YY_DO_BEFORE_ACTION */ - *yy_cp = (yy_hold_char); - yy_cp = (yy_last_accepting_cpos); - yy_current_state = (yy_last_accepting_state); - goto yy_find_action; - -case 1: -YY_RULE_SETUP -#line 125 "lexer.l" -{ currentCol += yyleng; } - YY_BREAK -case 2: -/* rule 2 can match eol */ -YY_RULE_SETUP -#line 126 "lexer.l" -{ currentLine++; currentCol = 1; } - YY_BREAK -case 3: -YY_RULE_SETUP -#line 128 "lexer.l" -{ - addToken(make_unique(yytext, currentLine, currentCol, currentCol + yyleng - 1)); - currentCol += yyleng; -} - YY_BREAK -case 4: -YY_RULE_SETUP -#line 133 "lexer.l" -{ - addToken(make_unique(stod(yytext), currentLine, currentCol, currentCol + yyleng - 1)); - currentCol += yyleng; -} - YY_BREAK -case 5: -YY_RULE_SETUP -#line 138 "lexer.l" -{ - addToken(make_unique(stoi(yytext), currentLine, currentCol, currentCol + yyleng - 1)); - currentCol += yyleng; -} - YY_BREAK -case 6: -YY_RULE_SETUP -#line 143 "lexer.l" -{ - addToken(make_unique(yytext, currentLine, currentCol, currentCol + yyleng - 1)); - currentCol += yyleng; -} - YY_BREAK -case 7: -/* rule 7 can match eol */ -YY_RULE_SETUP -#line 148 "lexer.l" -{ - string val(yytext + 1, yyleng - 2); - addToken(make_unique(val, currentLine, currentCol, currentCol + yyleng - 1)); - currentCol += yyleng; -} - YY_BREAK -case 8: -YY_RULE_SETUP -#line 154 "lexer.l" -{ - addToken(make_unique(yytext, currentLine, currentCol, currentCol + yyleng - 1)); - currentCol += yyleng; -} - YY_BREAK -case 9: -YY_RULE_SETUP -#line 159 "lexer.l" -{ - addToken(make_unique(yytext, currentLine, currentCol, currentCol + yyleng - 1)); - currentCol += yyleng; -} - YY_BREAK -case 10: -YY_RULE_SETUP -#line 164 "lexer.l" -ECHO; - YY_BREAK -#line 812 "lex.yy.cc" -case YY_STATE_EOF(INITIAL): - yyterminate(); - - case YY_END_OF_BUFFER: - { - /* Amount of text matched not including the EOB char. */ - int yy_amount_of_matched_text = (int) (yy_cp - (yytext_ptr)) - 1; - - /* Undo the effects of YY_DO_BEFORE_ACTION. */ - *yy_cp = (yy_hold_char); - YY_RESTORE_YY_MORE_OFFSET - - if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_NEW ) - { - /* We're scanning a new file or input source. It's - * possible that this happened because the user - * just pointed yyin at a new source and called - * yylex(). If so, then we have to assure - * consistency between YY_CURRENT_BUFFER and our - * globals. Here is the right place to do so, because - * this is the first action (other than possibly a - * back-up) that will match for the new input source. - */ - (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; - YY_CURRENT_BUFFER_LVALUE->yy_input_file = yyin.rdbuf(); - YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = YY_BUFFER_NORMAL; - } - - /* Note that here we test for yy_c_buf_p "<=" to the position - * of the first EOB in the buffer, since yy_c_buf_p will - * already have been incremented past the NUL character - * (since all states make transitions on EOB to the - * end-of-buffer state). Contrast this with the test - * in input(). - */ - if ( (yy_c_buf_p) <= &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] ) - { /* This was really a NUL. */ - yy_state_type yy_next_state; - - (yy_c_buf_p) = (yytext_ptr) + yy_amount_of_matched_text; - - yy_current_state = yy_get_previous_state( ); - - /* Okay, we're now positioned to make the NUL - * transition. We couldn't have - * yy_get_previous_state() go ahead and do it - * for us because it doesn't know how to deal - * with the possibility of jamming (and we don't - * want to build jamming into it because then it - * will run more slowly). - */ - - yy_next_state = yy_try_NUL_trans( yy_current_state ); - - yy_bp = (yytext_ptr) + YY_MORE_ADJ; - - if ( yy_next_state ) - { - /* Consume the NUL. */ - yy_cp = ++(yy_c_buf_p); - yy_current_state = yy_next_state; - goto yy_match; - } - - else - { - yy_cp = (yy_c_buf_p); - goto yy_find_action; - } - } - - else switch ( yy_get_next_buffer( ) ) - { - case EOB_ACT_END_OF_FILE: - { - (yy_did_buffer_switch_on_eof) = 0; - - if ( yywrap( ) ) - { - /* Note: because we've taken care in - * yy_get_next_buffer() to have set up - * yytext, we can now set up - * yy_c_buf_p so that if some total - * hoser (like flex itself) wants to - * call the scanner after we return the - * YY_NULL, it'll still work - another - * YY_NULL will get returned. - */ - (yy_c_buf_p) = (yytext_ptr) + YY_MORE_ADJ; - - yy_act = YY_STATE_EOF(YY_START); - goto do_action; - } - - else - { - if ( ! (yy_did_buffer_switch_on_eof) ) - YY_NEW_FILE; - } - break; - } - - case EOB_ACT_CONTINUE_SCAN: - (yy_c_buf_p) = - (yytext_ptr) + yy_amount_of_matched_text; - - yy_current_state = yy_get_previous_state( ); - - yy_cp = (yy_c_buf_p); - yy_bp = (yytext_ptr) + YY_MORE_ADJ; - goto yy_match; - - case EOB_ACT_LAST_MATCH: - (yy_c_buf_p) = - &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)]; - - yy_current_state = yy_get_previous_state( ); - - yy_cp = (yy_c_buf_p); - yy_bp = (yytext_ptr) + YY_MORE_ADJ; - goto yy_find_action; - } - break; - } - - default: - YY_FATAL_ERROR( - "fatal flex scanner internal error--no action found" ); - } /* end of action switch */ - } /* end of scanning one token */ - } /* end of user's declarations */ -} /* end of yylex */ - -/* The contents of this function are C++ specific, so the () macro is not used. - * This constructor simply maintains backward compatibility. - * DEPRECATED - */ -yyFlexLexer::yyFlexLexer( std::istream* arg_yyin, std::ostream* arg_yyout ): - yyin(arg_yyin ? arg_yyin->rdbuf() : std::cin.rdbuf()), - yyout(arg_yyout ? arg_yyout->rdbuf() : std::cout.rdbuf()) -{ - ctor_common(); -} - -/* The contents of this function are C++ specific, so the () macro is not used. - */ -yyFlexLexer::yyFlexLexer( std::istream& arg_yyin, std::ostream& arg_yyout ): - yyin(arg_yyin.rdbuf()), - yyout(arg_yyout.rdbuf()) -{ - ctor_common(); -} - -/* The contents of this function are C++ specific, so the () macro is not used. - */ -void yyFlexLexer::ctor_common() -{ - yy_c_buf_p = 0; - yy_init = 0; - yy_start = 0; - yy_flex_debug = 0; - yylineno = 1; // this will only get updated if %option yylineno - - yy_did_buffer_switch_on_eof = 0; - - yy_looking_for_trail_begin = 0; - yy_more_flag = 0; - yy_more_len = 0; - yy_more_offset = yy_prev_more_offset = 0; - - yy_start_stack_ptr = yy_start_stack_depth = 0; - yy_start_stack = NULL; - - yy_buffer_stack = NULL; - yy_buffer_stack_top = 0; - yy_buffer_stack_max = 0; - - yy_state_buf = 0; - -} - -/* The contents of this function are C++ specific, so the () macro is not used. - */ -yyFlexLexer::~yyFlexLexer() -{ - delete [] yy_state_buf; - yyfree( yy_start_stack ); - yy_delete_buffer( YY_CURRENT_BUFFER ); - yyfree( yy_buffer_stack ); -} - -/* The contents of this function are C++ specific, so the () macro is not used. - */ -void yyFlexLexer::switch_streams( std::istream& new_in, std::ostream& new_out ) -{ - // was if( new_in ) - yy_delete_buffer( YY_CURRENT_BUFFER ); - yy_switch_to_buffer( yy_create_buffer( new_in, YY_BUF_SIZE ) ); - - // was if( new_out ) - yyout.rdbuf(new_out.rdbuf()); -} - -/* The contents of this function are C++ specific, so the () macro is not used. - */ -void yyFlexLexer::switch_streams( std::istream* new_in, std::ostream* new_out ) -{ - if( ! new_in ) { - new_in = &yyin; - } - - if ( ! new_out ) { - new_out = &yyout; - } - - switch_streams(*new_in, *new_out); -} - -#ifdef YY_INTERACTIVE -int yyFlexLexer::LexerInput( char* buf, int /* max_size */ ) -#else -int yyFlexLexer::LexerInput( char* buf, int max_size ) -#endif -{ - if ( yyin.eof() || yyin.fail() ) - return 0; - -#ifdef YY_INTERACTIVE - yyin.get( buf[0] ); - - if ( yyin.eof() ) - return 0; - - if ( yyin.bad() ) - return -1; - - return 1; - -#else - (void) yyin.read( buf, max_size ); - - if ( yyin.bad() ) - return -1; - else - return yyin.gcount(); -#endif -} - -void yyFlexLexer::LexerOutput( const char* buf, int size ) -{ - (void) yyout.write( buf, size ); -} - -/* yy_get_next_buffer - try to read in a new buffer - * - * Returns a code representing an action: - * EOB_ACT_LAST_MATCH - - * EOB_ACT_CONTINUE_SCAN - continue scanning from current position - * EOB_ACT_END_OF_FILE - end of file - */ -int yyFlexLexer::yy_get_next_buffer() -{ - char *dest = YY_CURRENT_BUFFER_LVALUE->yy_ch_buf; - char *source = (yytext_ptr); - int number_to_move, i; - int ret_val; - - if ( (yy_c_buf_p) > &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] ) - YY_FATAL_ERROR( - "fatal flex scanner internal error--end of buffer missed" ); - - if ( YY_CURRENT_BUFFER_LVALUE->yy_fill_buffer == 0 ) - { /* Don't try to fill the buffer, so this is an EOF. */ - if ( (yy_c_buf_p) - (yytext_ptr) - YY_MORE_ADJ == 1 ) - { - /* We matched a single character, the EOB, so - * treat this as a final EOF. - */ - return EOB_ACT_END_OF_FILE; - } - - else - { - /* We matched some text prior to the EOB, first - * process it. - */ - return EOB_ACT_LAST_MATCH; - } - } - - /* Try to read more data. */ - - /* First move last chars to start of buffer. */ - number_to_move = (int) ((yy_c_buf_p) - (yytext_ptr) - 1); - - for ( i = 0; i < number_to_move; ++i ) - *(dest++) = *(source++); - - if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_EOF_PENDING ) - /* don't do the read, it's not guaranteed to return an EOF, - * just force an EOF - */ - YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars) = 0; - - else - { - int num_to_read = - YY_CURRENT_BUFFER_LVALUE->yy_buf_size - number_to_move - 1; - - while ( num_to_read <= 0 ) - { /* Not enough room in the buffer - grow it. */ - - /* just a shorter name for the current buffer */ - YY_BUFFER_STATE b = YY_CURRENT_BUFFER_LVALUE; - - int yy_c_buf_p_offset = - (int) ((yy_c_buf_p) - b->yy_ch_buf); - - if ( b->yy_is_our_buffer ) - { - int new_size = b->yy_buf_size * 2; - - if ( new_size <= 0 ) - b->yy_buf_size += b->yy_buf_size / 8; - else - b->yy_buf_size *= 2; - - b->yy_ch_buf = (char *) - /* Include room in for 2 EOB chars. */ - yyrealloc( (void *) b->yy_ch_buf, - (yy_size_t) (b->yy_buf_size + 2) ); - } - else - /* Can't grow it, we don't own it. */ - b->yy_ch_buf = NULL; - - if ( ! b->yy_ch_buf ) - YY_FATAL_ERROR( - "fatal error - scanner input buffer overflow" ); - - (yy_c_buf_p) = &b->yy_ch_buf[yy_c_buf_p_offset]; - - num_to_read = YY_CURRENT_BUFFER_LVALUE->yy_buf_size - - number_to_move - 1; - - } - - if ( num_to_read > YY_READ_BUF_SIZE ) - num_to_read = YY_READ_BUF_SIZE; - - /* Read in more data. */ - YY_INPUT( (&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]), - (yy_n_chars), num_to_read ); - - YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); - } - - if ( (yy_n_chars) == 0 ) - { - if ( number_to_move == YY_MORE_ADJ ) - { - ret_val = EOB_ACT_END_OF_FILE; - yyrestart( yyin ); - } - - else - { - ret_val = EOB_ACT_LAST_MATCH; - YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = - YY_BUFFER_EOF_PENDING; - } - } - - else - ret_val = EOB_ACT_CONTINUE_SCAN; - - if (((yy_n_chars) + number_to_move) > YY_CURRENT_BUFFER_LVALUE->yy_buf_size) { - /* Extend the array by 50%, plus the number we really need. */ - int new_size = (yy_n_chars) + number_to_move + ((yy_n_chars) >> 1); - YY_CURRENT_BUFFER_LVALUE->yy_ch_buf = (char *) yyrealloc( - (void *) YY_CURRENT_BUFFER_LVALUE->yy_ch_buf, (yy_size_t) new_size ); - if ( ! YY_CURRENT_BUFFER_LVALUE->yy_ch_buf ) - YY_FATAL_ERROR( "out of dynamic memory in yy_get_next_buffer()" ); - /* "- 2" to take care of EOB's */ - YY_CURRENT_BUFFER_LVALUE->yy_buf_size = (int) (new_size - 2); - } - - (yy_n_chars) += number_to_move; - YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] = YY_END_OF_BUFFER_CHAR; - YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] = YY_END_OF_BUFFER_CHAR; - - (yytext_ptr) = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[0]; - - return ret_val; -} - -/* yy_get_previous_state - get the state just before the EOB char was reached */ - - yy_state_type yyFlexLexer::yy_get_previous_state() -{ - yy_state_type yy_current_state; - char *yy_cp; - - yy_current_state = (yy_start); - - for ( yy_cp = (yytext_ptr) + YY_MORE_ADJ; yy_cp < (yy_c_buf_p); ++yy_cp ) - { - YY_CHAR yy_c = (*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : 1); - if ( yy_accept[yy_current_state] ) - { - (yy_last_accepting_state) = yy_current_state; - (yy_last_accepting_cpos) = yy_cp; - } - while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) - { - yy_current_state = (int) yy_def[yy_current_state]; - if ( yy_current_state >= 44 ) - yy_c = yy_meta[yy_c]; - } - yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c]; - } - - return yy_current_state; -} - -/* yy_try_NUL_trans - try to make a transition on the NUL character - * - * synopsis - * next_state = yy_try_NUL_trans( current_state ); - */ - yy_state_type yyFlexLexer::yy_try_NUL_trans( yy_state_type yy_current_state ) -{ - int yy_is_jam; - char *yy_cp = (yy_c_buf_p); - - YY_CHAR yy_c = 1; - if ( yy_accept[yy_current_state] ) - { - (yy_last_accepting_state) = yy_current_state; - (yy_last_accepting_cpos) = yy_cp; - } - while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) - { - yy_current_state = (int) yy_def[yy_current_state]; - if ( yy_current_state >= 44 ) - yy_c = yy_meta[yy_c]; - } - yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c]; - yy_is_jam = (yy_current_state == 43); - - return yy_is_jam ? 0 : yy_current_state; -} - -#ifndef YY_NO_UNPUT - void yyFlexLexer::yyunput( int c, char* yy_bp) -{ - char *yy_cp; - - yy_cp = (yy_c_buf_p); - - /* undo effects of setting up yytext */ - *yy_cp = (yy_hold_char); - - if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 ) - { /* need to shift things up to make room */ - /* +2 for EOB chars. */ - int number_to_move = (yy_n_chars) + 2; - char *dest = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[ - YY_CURRENT_BUFFER_LVALUE->yy_buf_size + 2]; - char *source = - &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]; - - while ( source > YY_CURRENT_BUFFER_LVALUE->yy_ch_buf ) - *--dest = *--source; - - yy_cp += (int) (dest - source); - yy_bp += (int) (dest - source); - YY_CURRENT_BUFFER_LVALUE->yy_n_chars = - (yy_n_chars) = (int) YY_CURRENT_BUFFER_LVALUE->yy_buf_size; - - if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 ) - YY_FATAL_ERROR( "flex scanner push-back overflow" ); - } - - *--yy_cp = (char) c; - - (yytext_ptr) = yy_bp; - (yy_hold_char) = *yy_cp; - (yy_c_buf_p) = yy_cp; -} -#endif - - int yyFlexLexer::yyinput() -{ - int c; - - *(yy_c_buf_p) = (yy_hold_char); - - if ( *(yy_c_buf_p) == YY_END_OF_BUFFER_CHAR ) - { - /* yy_c_buf_p now points to the character we want to return. - * If this occurs *before* the EOB characters, then it's a - * valid NUL; if not, then we've hit the end of the buffer. - */ - if ( (yy_c_buf_p) < &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] ) - /* This was really a NUL. */ - *(yy_c_buf_p) = '\0'; - - else - { /* need more input */ - int offset = (int) ((yy_c_buf_p) - (yytext_ptr)); - ++(yy_c_buf_p); - - switch ( yy_get_next_buffer( ) ) - { - case EOB_ACT_LAST_MATCH: - /* This happens because yy_g_n_b() - * sees that we've accumulated a - * token and flags that we need to - * try matching the token before - * proceeding. But for input(), - * there's no matching to consider. - * So convert the EOB_ACT_LAST_MATCH - * to EOB_ACT_END_OF_FILE. - */ - - /* Reset buffer status. */ - yyrestart( yyin ); - - /*FALLTHROUGH*/ - - case EOB_ACT_END_OF_FILE: - { - if ( yywrap( ) ) - return 0; - - if ( ! (yy_did_buffer_switch_on_eof) ) - YY_NEW_FILE; -#ifdef __cplusplus - return yyinput(); -#else - return input(); -#endif - } - - case EOB_ACT_CONTINUE_SCAN: - (yy_c_buf_p) = (yytext_ptr) + offset; - break; - } - } - } - - c = *(unsigned char *) (yy_c_buf_p); /* cast for 8-bit char's */ - *(yy_c_buf_p) = '\0'; /* preserve yytext */ - (yy_hold_char) = *++(yy_c_buf_p); - - return c; -} - -/** Immediately switch to a different input stream. - * @param input_file A readable stream. - * - * @note This function does not reset the start condition to @c INITIAL . - */ - void yyFlexLexer::yyrestart( std::istream& input_file ) -{ - - if ( ! YY_CURRENT_BUFFER ){ - yyensure_buffer_stack (); - YY_CURRENT_BUFFER_LVALUE = - yy_create_buffer( yyin, YY_BUF_SIZE ); - } - - yy_init_buffer( YY_CURRENT_BUFFER, input_file ); - yy_load_buffer_state( ); -} - -/** Delegate to the new version that takes an istream reference. - * @param input_file A readable stream. - * - * @note This function does not reset the start condition to @c INITIAL . - */ -void yyFlexLexer::yyrestart( std::istream* input_file ) -{ - if( ! input_file ) { - input_file = &yyin; - } - yyrestart( *input_file ); -} - -/** Switch to a different input buffer. - * @param new_buffer The new input buffer. - * - */ - void yyFlexLexer::yy_switch_to_buffer( YY_BUFFER_STATE new_buffer ) -{ - - /* TODO. We should be able to replace this entire function body - * with - * yypop_buffer_state(); - * yypush_buffer_state(new_buffer); - */ - yyensure_buffer_stack (); - if ( YY_CURRENT_BUFFER == new_buffer ) - return; - - if ( YY_CURRENT_BUFFER ) - { - /* Flush out information for old buffer. */ - *(yy_c_buf_p) = (yy_hold_char); - YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p); - YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); - } - - YY_CURRENT_BUFFER_LVALUE = new_buffer; - yy_load_buffer_state( ); - - /* We don't actually know whether we did this switch during - * EOF (yywrap()) processing, but the only time this flag - * is looked at is after yywrap() is called, so it's safe - * to go ahead and always set it. - */ - (yy_did_buffer_switch_on_eof) = 1; -} - - void yyFlexLexer::yy_load_buffer_state() -{ - (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; - (yytext_ptr) = (yy_c_buf_p) = YY_CURRENT_BUFFER_LVALUE->yy_buf_pos; - yyin.rdbuf(YY_CURRENT_BUFFER_LVALUE->yy_input_file); - (yy_hold_char) = *(yy_c_buf_p); -} - -/** Allocate and initialize an input buffer state. - * @param file A readable stream. - * @param size The character buffer size in bytes. When in doubt, use @c YY_BUF_SIZE. - * - * @return the allocated buffer state. - */ - YY_BUFFER_STATE yyFlexLexer::yy_create_buffer( std::istream& file, int size ) -{ - YY_BUFFER_STATE b; - - b = (YY_BUFFER_STATE) yyalloc( sizeof( struct yy_buffer_state ) ); - if ( ! b ) - YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); - - b->yy_buf_size = size; - - /* yy_ch_buf has to be 2 characters longer than the size given because - * we need to put in 2 end-of-buffer characters. - */ - b->yy_ch_buf = (char *) yyalloc( (yy_size_t) (b->yy_buf_size + 2) ); - if ( ! b->yy_ch_buf ) - YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); - - b->yy_is_our_buffer = 1; - - yy_init_buffer( b, file ); - - return b; -} - -/** Delegate creation of buffers to the new version that takes an istream reference. - * @param file A readable stream. - * @param size The character buffer size in bytes. When in doubt, use @c YY_BUF_SIZE. - * - * @return the allocated buffer state. - */ - YY_BUFFER_STATE yyFlexLexer::yy_create_buffer( std::istream* file, int size ) -{ - return yy_create_buffer( *file, size ); -} - -/** Destroy the buffer. - * @param b a buffer created with yy_create_buffer() - * - */ - void yyFlexLexer::yy_delete_buffer( YY_BUFFER_STATE b ) -{ - - if ( ! b ) - return; - - if ( b == YY_CURRENT_BUFFER ) /* Not sure if we should pop here. */ - YY_CURRENT_BUFFER_LVALUE = (YY_BUFFER_STATE) 0; - - if ( b->yy_is_our_buffer ) - yyfree( (void *) b->yy_ch_buf ); - - yyfree( (void *) b ); -} - -/* Initializes or reinitializes a buffer. - * This function is sometimes called more than once on the same buffer, - * such as during a yyrestart() or at EOF. - */ - void yyFlexLexer::yy_init_buffer( YY_BUFFER_STATE b, std::istream& file ) - -{ - int oerrno = errno; - - yy_flush_buffer( b ); - - b->yy_input_file = file.rdbuf(); - b->yy_fill_buffer = 1; - - /* If b is the current buffer, then yy_init_buffer was _probably_ - * called from yyrestart() or through yy_get_next_buffer. - * In that case, we don't want to reset the lineno or column. - */ - if (b != YY_CURRENT_BUFFER){ - b->yy_bs_lineno = 1; - b->yy_bs_column = 0; - } - - b->yy_is_interactive = 0; - errno = oerrno; -} - -/** Discard all buffered characters. On the next scan, YY_INPUT will be called. - * @param b the buffer state to be flushed, usually @c YY_CURRENT_BUFFER. - * - */ - void yyFlexLexer::yy_flush_buffer( YY_BUFFER_STATE b ) -{ - if ( ! b ) - return; - - b->yy_n_chars = 0; - - /* We always need two end-of-buffer characters. The first causes - * a transition to the end-of-buffer state. The second causes - * a jam in that state. - */ - b->yy_ch_buf[0] = YY_END_OF_BUFFER_CHAR; - b->yy_ch_buf[1] = YY_END_OF_BUFFER_CHAR; - - b->yy_buf_pos = &b->yy_ch_buf[0]; - - b->yy_at_bol = 1; - b->yy_buffer_status = YY_BUFFER_NEW; - - if ( b == YY_CURRENT_BUFFER ) - yy_load_buffer_state( ); -} - -/** Pushes the new state onto the stack. The new state becomes - * the current state. This function will allocate the stack - * if necessary. - * @param new_buffer The new state. - * - */ -void yyFlexLexer::yypush_buffer_state (YY_BUFFER_STATE new_buffer) -{ - if (new_buffer == NULL) - return; - - yyensure_buffer_stack(); - - /* This block is copied from yy_switch_to_buffer. */ - if ( YY_CURRENT_BUFFER ) - { - /* Flush out information for old buffer. */ - *(yy_c_buf_p) = (yy_hold_char); - YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p); - YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); - } - - /* Only push if top exists. Otherwise, replace top. */ - if (YY_CURRENT_BUFFER) - (yy_buffer_stack_top)++; - YY_CURRENT_BUFFER_LVALUE = new_buffer; - - /* copied from yy_switch_to_buffer. */ - yy_load_buffer_state( ); - (yy_did_buffer_switch_on_eof) = 1; -} - -/** Removes and deletes the top of the stack, if present. - * The next element becomes the new top. - * - */ -void yyFlexLexer::yypop_buffer_state (void) -{ - if (!YY_CURRENT_BUFFER) - return; - - yy_delete_buffer(YY_CURRENT_BUFFER ); - YY_CURRENT_BUFFER_LVALUE = NULL; - if ((yy_buffer_stack_top) > 0) - --(yy_buffer_stack_top); - - if (YY_CURRENT_BUFFER) { - yy_load_buffer_state( ); - (yy_did_buffer_switch_on_eof) = 1; - } -} - -/* Allocates the stack if it does not exist. - * Guarantees space for at least one push. - */ -void yyFlexLexer::yyensure_buffer_stack(void) -{ - yy_size_t num_to_alloc; - - if (!(yy_buffer_stack)) { - - /* First allocation is just for 2 elements, since we don't know if this - * scanner will even need a stack. We use 2 instead of 1 to avoid an - * immediate realloc on the next call. - */ - num_to_alloc = 1; /* After all that talk, this was set to 1 anyways... */ - (yy_buffer_stack) = (struct yy_buffer_state**)yyalloc - (num_to_alloc * sizeof(struct yy_buffer_state*) - ); - if ( ! (yy_buffer_stack) ) - YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" ); - - memset((yy_buffer_stack), 0, num_to_alloc * sizeof(struct yy_buffer_state*)); - - (yy_buffer_stack_max) = num_to_alloc; - (yy_buffer_stack_top) = 0; - return; - } - - if ((yy_buffer_stack_top) >= ((yy_buffer_stack_max)) - 1){ - - /* Increase the buffer to prepare for a possible push. */ - yy_size_t grow_size = 8 /* arbitrary grow size */; - - num_to_alloc = (yy_buffer_stack_max) + grow_size; - (yy_buffer_stack) = (struct yy_buffer_state**)yyrealloc - ((yy_buffer_stack), - num_to_alloc * sizeof(struct yy_buffer_state*) - ); - if ( ! (yy_buffer_stack) ) - YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" ); - - /* zero only the new slots.*/ - memset((yy_buffer_stack) + (yy_buffer_stack_max), 0, grow_size * sizeof(struct yy_buffer_state*)); - (yy_buffer_stack_max) = num_to_alloc; - } -} - - void yyFlexLexer::yy_push_state( int _new_state ) -{ - if ( (yy_start_stack_ptr) >= (yy_start_stack_depth) ) - { - yy_size_t new_size; - - (yy_start_stack_depth) += YY_START_STACK_INCR; - new_size = (yy_size_t) (yy_start_stack_depth) * sizeof( int ); - - if ( ! (yy_start_stack) ) - (yy_start_stack) = (int *) yyalloc( new_size ); - - else - (yy_start_stack) = (int *) yyrealloc( - (void *) (yy_start_stack), new_size ); - - if ( ! (yy_start_stack) ) - YY_FATAL_ERROR( "out of memory expanding start-condition stack" ); - } - - (yy_start_stack)[(yy_start_stack_ptr)++] = YY_START; - - BEGIN(_new_state); -} - - void yyFlexLexer::yy_pop_state() -{ - if ( --(yy_start_stack_ptr) < 0 ) - YY_FATAL_ERROR( "start-condition stack underflow" ); - - BEGIN((yy_start_stack)[(yy_start_stack_ptr)]); -} - - int yyFlexLexer::yy_top_state() -{ - return (yy_start_stack)[(yy_start_stack_ptr) - 1]; -} - -#ifndef YY_EXIT_FAILURE -#define YY_EXIT_FAILURE 2 -#endif - -void yyFlexLexer::LexerError( const char* msg ) -{ - std::cerr << msg << std::endl; - exit( YY_EXIT_FAILURE ); -} - -/* Redefine yyless() so it works in section 3 code. */ - -#undef yyless -#define yyless(n) \ - do \ - { \ - /* Undo effects of setting up yytext. */ \ - int yyless_macro_arg = (n); \ - YY_LESS_LINENO(yyless_macro_arg);\ - yytext[yyleng] = (yy_hold_char); \ - (yy_c_buf_p) = yytext + yyless_macro_arg; \ - (yy_hold_char) = *(yy_c_buf_p); \ - *(yy_c_buf_p) = '\0'; \ - yyleng = yyless_macro_arg; \ - } \ - while ( 0 ) - -/* Accessor methods (get/set functions) to struct members. */ - -/* - * Internal utility routines. - */ - -#ifndef yytext_ptr -static void yy_flex_strncpy (char* s1, const char * s2, int n ) -{ - - int i; - for ( i = 0; i < n; ++i ) - s1[i] = s2[i]; -} -#endif - -#ifdef YY_NEED_STRLEN -static int yy_flex_strlen (const char * s ) -{ - int n; - for ( n = 0; s[n]; ++n ) - ; - - return n; -} -#endif - -void *yyalloc (yy_size_t size ) -{ - return malloc(size); -} - -void *yyrealloc (void * ptr, yy_size_t size ) -{ - - /* The cast to (char *) in the following accommodates both - * implementations that use char* generic pointers, and those - * that use void* generic pointers. It works with the latter - * because both ANSI C and C++ allow castless assignment from - * any pointer type to void*, and deal with argument conversions - * as though doing an assignment. - */ - return realloc(ptr, size); -} - -void yyfree (void * ptr ) -{ - free( (char *) ptr ); /* see yyrealloc() for (char *) cast */ -} - -#define YYTABLES_NAME "yytables" - -#line 164 "lexer.l" - - -#ifndef NO_LEXER_MAIN -int main(int argc, char **argv) { - yyFlexLexer lexer; - ifstream file; - if (argc > 1) { - file.open(argv[1]); - if (!file.is_open()) { - perror("File opening failed"); - return 1; - } - lexer.switch_streams(&file, &cout); - } - while (lexer.yylex() != 0) {} - for (auto &t : tokens) t->print(); - return 0; -} -#endif - diff --git a/lexer b/lexer deleted file mode 100644 index 4256eb7..0000000 Binary files a/lexer and /dev/null differ diff --git a/lexer.cpp b/lexer.cpp deleted file mode 100644 index 0a1a392..0000000 --- a/lexer.cpp +++ /dev/null @@ -1,1974 +0,0 @@ -#line 2 "lexer.cpp" - -#line 4 "lexer.cpp" - -#define YY_INT_ALIGNED short int - -/* A lexical scanner generated by flex */ - -#define FLEX_SCANNER -#define YY_FLEX_MAJOR_VERSION 2 -#define YY_FLEX_MINOR_VERSION 6 -#define YY_FLEX_SUBMINOR_VERSION 4 -#if YY_FLEX_SUBMINOR_VERSION > 0 -#define FLEX_BETA -#endif - -/* First, we deal with platform-specific or compiler-specific issues. */ - -/* begin standard C headers. */ -#include -#include -#include -#include - -/* end standard C headers. */ - -/* flex integer type definitions */ - -#ifndef FLEXINT_H -#define FLEXINT_H - -/* C99 systems have . Non-C99 systems may or may not. */ - -#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L - -/* C99 says to define __STDC_LIMIT_MACROS before including stdint.h, - * if you want the limit (max/min) macros for int types. - */ -#ifndef __STDC_LIMIT_MACROS -#define __STDC_LIMIT_MACROS 1 -#endif - -#include -typedef int8_t flex_int8_t; -typedef uint8_t flex_uint8_t; -typedef int16_t flex_int16_t; -typedef uint16_t flex_uint16_t; -typedef int32_t flex_int32_t; -typedef uint32_t flex_uint32_t; -#else -typedef signed char flex_int8_t; -typedef short int flex_int16_t; -typedef int flex_int32_t; -typedef unsigned char flex_uint8_t; -typedef unsigned short int flex_uint16_t; -typedef unsigned int flex_uint32_t; - -/* Limits of integral types. */ -#ifndef INT8_MIN -#define INT8_MIN (-128) -#endif -#ifndef INT16_MIN -#define INT16_MIN (-32767-1) -#endif -#ifndef INT32_MIN -#define INT32_MIN (-2147483647-1) -#endif -#ifndef INT8_MAX -#define INT8_MAX (127) -#endif -#ifndef INT16_MAX -#define INT16_MAX (32767) -#endif -#ifndef INT32_MAX -#define INT32_MAX (2147483647) -#endif -#ifndef UINT8_MAX -#define UINT8_MAX (255U) -#endif -#ifndef UINT16_MAX -#define UINT16_MAX (65535U) -#endif -#ifndef UINT32_MAX -#define UINT32_MAX (4294967295U) -#endif - -#ifndef SIZE_MAX -#define SIZE_MAX (~(size_t)0) -#endif - -#endif /* ! C99 */ - -#endif /* ! FLEXINT_H */ - -/* begin standard C++ headers. */ - -/* TODO: this is always defined, so inline it */ -#define yyconst const - -#if defined(__GNUC__) && __GNUC__ >= 3 -#define yynoreturn __attribute__((__noreturn__)) -#else -#define yynoreturn -#endif - -/* Returned upon end-of-file. */ -#define YY_NULL 0 - -/* Promotes a possibly negative, possibly signed char to an - * integer in range [0..255] for use as an array index. - */ -#define YY_SC_TO_UI(c) ((YY_CHAR) (c)) - -/* Enter a start condition. This macro really ought to take a parameter, - * but we do it the disgusting crufty way forced on us by the ()-less - * definition of BEGIN. - */ -#define BEGIN (yy_start) = 1 + 2 * -/* Translate the current start state into a value that can be later handed - * to BEGIN to return to the state. The YYSTATE alias is for lex - * compatibility. - */ -#define YY_START (((yy_start) - 1) / 2) -#define YYSTATE YY_START -/* Action number for EOF rule of a given start state. */ -#define YY_STATE_EOF(state) (YY_END_OF_BUFFER + state + 1) -/* Special action meaning "start processing a new file". */ -#define YY_NEW_FILE yyrestart( yyin ) -#define YY_END_OF_BUFFER_CHAR 0 - -/* Size of default input buffer. */ -#ifndef YY_BUF_SIZE -#ifdef __ia64__ -/* On IA-64, the buffer size is 16k, not 8k. - * Moreover, YY_BUF_SIZE is 2*YY_READ_BUF_SIZE in the general case. - * Ditto for the __ia64__ case accordingly. - */ -#define YY_BUF_SIZE 32768 -#else -#define YY_BUF_SIZE 16384 -#endif /* __ia64__ */ -#endif - -/* The state buf must be large enough to hold one state per character in the main buffer. - */ -#define YY_STATE_BUF_SIZE ((YY_BUF_SIZE + 2) * sizeof(yy_state_type)) - -#ifndef YY_TYPEDEF_YY_BUFFER_STATE -#define YY_TYPEDEF_YY_BUFFER_STATE -typedef struct yy_buffer_state *YY_BUFFER_STATE; -#endif - -#ifndef YY_TYPEDEF_YY_SIZE_T -#define YY_TYPEDEF_YY_SIZE_T -typedef size_t yy_size_t; -#endif - -extern int yyleng; - -extern FILE *yyin, *yyout; - -#define EOB_ACT_CONTINUE_SCAN 0 -#define EOB_ACT_END_OF_FILE 1 -#define EOB_ACT_LAST_MATCH 2 - - #define YY_LESS_LINENO(n) - #define YY_LINENO_REWIND_TO(ptr) - -/* Return all but the first "n" matched characters back to the input stream. */ -#define yyless(n) \ - do \ - { \ - /* Undo effects of setting up yytext. */ \ - int yyless_macro_arg = (n); \ - YY_LESS_LINENO(yyless_macro_arg);\ - *yy_cp = (yy_hold_char); \ - YY_RESTORE_YY_MORE_OFFSET \ - (yy_c_buf_p) = yy_cp = yy_bp + yyless_macro_arg - YY_MORE_ADJ; \ - YY_DO_BEFORE_ACTION; /* set up yytext again */ \ - } \ - while ( 0 ) -#define unput(c) yyunput( c, (yytext_ptr) ) - -#ifndef YY_STRUCT_YY_BUFFER_STATE -#define YY_STRUCT_YY_BUFFER_STATE -struct yy_buffer_state - { - FILE *yy_input_file; - - char *yy_ch_buf; /* input buffer */ - char *yy_buf_pos; /* current position in input buffer */ - - /* Size of input buffer in bytes, not including room for EOB - * characters. - */ - int yy_buf_size; - - /* Number of characters read into yy_ch_buf, not including EOB - * characters. - */ - int yy_n_chars; - - /* Whether we "own" the buffer - i.e., we know we created it, - * and can realloc() it to grow it, and should free() it to - * delete it. - */ - int yy_is_our_buffer; - - /* Whether this is an "interactive" input source; if so, and - * if we're using stdio for input, then we want to use getc() - * instead of fread(), to make sure we stop fetching input after - * each newline. - */ - int yy_is_interactive; - - /* Whether we're considered to be at the beginning of a line. - * If so, '^' rules will be active on the next match, otherwise - * not. - */ - int yy_at_bol; - - int yy_bs_lineno; /**< The line count. */ - int yy_bs_column; /**< The column count. */ - - /* Whether to try to fill the input buffer when we reach the - * end of it. - */ - int yy_fill_buffer; - - int yy_buffer_status; - -#define YY_BUFFER_NEW 0 -#define YY_BUFFER_NORMAL 1 - /* When an EOF's been seen but there's still some text to process - * then we mark the buffer as YY_EOF_PENDING, to indicate that we - * shouldn't try reading from the input source any more. We might - * still have a bunch of tokens to match, though, because of - * possible backing-up. - * - * When we actually see the EOF, we change the status to "new" - * (via yyrestart()), so that the user can continue scanning by - * just pointing yyin at a new input file. - */ -#define YY_BUFFER_EOF_PENDING 2 - - }; -#endif /* !YY_STRUCT_YY_BUFFER_STATE */ - -/* Stack of input buffers. */ -static size_t yy_buffer_stack_top = 0; /**< index of top of stack. */ -static size_t yy_buffer_stack_max = 0; /**< capacity of stack. */ -static YY_BUFFER_STATE * yy_buffer_stack = NULL; /**< Stack as an array. */ - -/* We provide macros for accessing buffer states in case in the - * future we want to put the buffer states in a more general - * "scanner state". - * - * Returns the top of the stack, or NULL. - */ -#define YY_CURRENT_BUFFER ( (yy_buffer_stack) \ - ? (yy_buffer_stack)[(yy_buffer_stack_top)] \ - : NULL) -/* Same as previous macro, but useful when we know that the buffer stack is not - * NULL or when we need an lvalue. For internal use only. - */ -#define YY_CURRENT_BUFFER_LVALUE (yy_buffer_stack)[(yy_buffer_stack_top)] - -/* yy_hold_char holds the character lost when yytext is formed. */ -static char yy_hold_char; -static int yy_n_chars; /* number of characters read into yy_ch_buf */ -int yyleng; - -/* Points to current character in buffer. */ -static char *yy_c_buf_p = NULL; -static int yy_init = 0; /* whether we need to initialize */ -static int yy_start = 0; /* start state number */ - -/* Flag which is used to allow yywrap()'s to do buffer switches - * instead of setting up a fresh yyin. A bit of a hack ... - */ -static int yy_did_buffer_switch_on_eof; - -void yyrestart ( FILE *input_file ); -void yy_switch_to_buffer ( YY_BUFFER_STATE new_buffer ); -YY_BUFFER_STATE yy_create_buffer ( FILE *file, int size ); -void yy_delete_buffer ( YY_BUFFER_STATE b ); -void yy_flush_buffer ( YY_BUFFER_STATE b ); -void yypush_buffer_state ( YY_BUFFER_STATE new_buffer ); -void yypop_buffer_state ( void ); - -static void yyensure_buffer_stack ( void ); -static void yy_load_buffer_state ( void ); -static void yy_init_buffer ( YY_BUFFER_STATE b, FILE *file ); -#define YY_FLUSH_BUFFER yy_flush_buffer( YY_CURRENT_BUFFER ) - -YY_BUFFER_STATE yy_scan_buffer ( char *base, yy_size_t size ); -YY_BUFFER_STATE yy_scan_string ( const char *yy_str ); -YY_BUFFER_STATE yy_scan_bytes ( const char *bytes, int len ); - -void *yyalloc ( yy_size_t ); -void *yyrealloc ( void *, yy_size_t ); -void yyfree ( void * ); - -#define yy_new_buffer yy_create_buffer -#define yy_set_interactive(is_interactive) \ - { \ - if ( ! YY_CURRENT_BUFFER ){ \ - yyensure_buffer_stack (); \ - YY_CURRENT_BUFFER_LVALUE = \ - yy_create_buffer( yyin, YY_BUF_SIZE ); \ - } \ - YY_CURRENT_BUFFER_LVALUE->yy_is_interactive = is_interactive; \ - } -#define yy_set_bol(at_bol) \ - { \ - if ( ! YY_CURRENT_BUFFER ){\ - yyensure_buffer_stack (); \ - YY_CURRENT_BUFFER_LVALUE = \ - yy_create_buffer( yyin, YY_BUF_SIZE ); \ - } \ - YY_CURRENT_BUFFER_LVALUE->yy_at_bol = at_bol; \ - } -#define YY_AT_BOL() (YY_CURRENT_BUFFER_LVALUE->yy_at_bol) - -/* Begin user sect3 */ - -#define yywrap() (/*CONSTCOND*/1) -#define YY_SKIP_YYWRAP -typedef flex_uint8_t YY_CHAR; - -FILE *yyin = NULL, *yyout = NULL; - -typedef int yy_state_type; - -extern int yylineno; -int yylineno = 1; - -extern char *yytext; -#ifdef yytext_ptr -#undef yytext_ptr -#endif -#define yytext_ptr yytext - -static yy_state_type yy_get_previous_state ( void ); -static yy_state_type yy_try_NUL_trans ( yy_state_type current_state ); -static int yy_get_next_buffer ( void ); -static void yynoreturn yy_fatal_error ( const char* msg ); - -/* Done after the current pattern has been matched and before the - * corresponding action - sets up yytext. - */ -#define YY_DO_BEFORE_ACTION \ - (yytext_ptr) = yy_bp; \ - yyleng = (int) (yy_cp - yy_bp); \ - (yy_hold_char) = *yy_cp; \ - *yy_cp = '\0'; \ - (yy_c_buf_p) = yy_cp; -#define YY_NUM_RULES 10 -#define YY_END_OF_BUFFER 11 -/* This struct is not used in this scanner, - but its presence is necessary. */ -struct yy_trans_info - { - flex_int32_t yy_verify; - flex_int32_t yy_nxt; - }; -static const flex_int16_t yy_accept[70] = - { 0, - 0, 0, 11, 9, 1, 2, 9, 9, 8, 5, - 8, 8, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 1, 2, 0, 7, 0, 0, 5, - 8, 6, 6, 6, 6, 6, 6, 6, 3, 6, - 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 0 - } ; - -static const YY_CHAR yy_ec[256] = - { 0, - 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, - 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 2, 1, 5, 1, 1, 1, 1, 1, 6, - 6, 6, 6, 6, 6, 7, 6, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 9, 6, 6, - 10, 11, 1, 1, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, - 6, 13, 6, 1, 12, 1, 14, 12, 15, 16, - - 17, 18, 12, 19, 20, 12, 12, 21, 22, 23, - 24, 25, 12, 26, 27, 28, 29, 30, 31, 32, - 12, 12, 6, 1, 6, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1 - } ; - -static const YY_CHAR yy_meta[33] = - { 0, - 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, - 1, 2, 1, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2 - } ; - -static const flex_int16_t yy_base[72] = - { 0, - 0, 0, 102, 103, 99, 103, 97, 28, 103, 27, - 89, 87, 0, 76, 15, 25, 19, 72, 78, 77, - 24, 79, 73, 89, 103, 35, 103, 87, 81, 37, - 103, 0, 74, 60, 70, 57, 63, 57, 0, 58, - 53, 52, 34, 50, 52, 57, 68, 48, 57, 56, - 45, 46, 51, 40, 45, 40, 49, 44, 37, 40, - 45, 37, 34, 42, 42, 41, 30, 25, 103, 54, - 40 - } ; - -static const flex_int16_t yy_def[72] = - { 0, - 69, 1, 69, 69, 69, 69, 69, 70, 69, 69, - 69, 69, 71, 71, 71, 71, 71, 71, 71, 71, - 71, 71, 71, 69, 69, 70, 69, 70, 69, 69, - 69, 71, 71, 71, 71, 71, 71, 71, 71, 71, - 71, 71, 71, 71, 71, 71, 69, 71, 71, 71, - 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, - 71, 71, 71, 71, 71, 71, 71, 71, 0, 69, - 69 - } ; - -static const flex_int16_t yy_nxt[136] = - { 0, - 4, 5, 6, 7, 8, 9, 4, 10, 11, 12, - 9, 13, 4, 13, 14, 13, 15, 16, 13, 17, - 18, 19, 13, 13, 13, 20, 13, 21, 13, 22, - 23, 13, 27, 29, 30, 34, 39, 35, 37, 27, - 28, 32, 43, 29, 30, 39, 36, 28, 38, 44, - 55, 39, 39, 56, 26, 26, 39, 68, 39, 67, - 66, 39, 65, 39, 64, 39, 39, 39, 63, 62, - 39, 61, 60, 39, 59, 47, 58, 39, 57, 54, - 53, 52, 39, 51, 50, 39, 49, 48, 47, 69, - 24, 46, 45, 42, 41, 40, 33, 31, 31, 25, - - 24, 69, 3, 69, 69, 69, 69, 69, 69, 69, - 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, - 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, - 69, 69, 69, 69, 69 - } ; - -static const flex_int16_t yy_chk[136] = - { 0, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 8, 10, 10, 15, 17, 15, 16, 26, - 8, 71, 21, 30, 30, 17, 15, 26, 16, 21, - 43, 68, 67, 43, 70, 70, 66, 65, 64, 63, - 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, - 52, 51, 50, 49, 48, 47, 46, 45, 44, 42, - 41, 40, 38, 37, 36, 35, 34, 33, 29, 28, - 24, 23, 22, 20, 19, 18, 14, 12, 11, 7, - - 5, 3, 69, 69, 69, 69, 69, 69, 69, 69, - 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, - 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, - 69, 69, 69, 69, 69 - } ; - -static yy_state_type yy_last_accepting_state; -static char *yy_last_accepting_cpos; - -extern int yy_flex_debug; -int yy_flex_debug = 0; - -/* The intent behind this definition is that it'll catch - * any uses of REJECT which flex missed. - */ -#define REJECT reject_used_but_not_detected -#define yymore() yymore_used_but_not_detected -#define YY_MORE_ADJ 0 -#define YY_RESTORE_YY_MORE_OFFSET -char *yytext; -#line 1 "lexer.l" -#line 4 "lexer.l" -#include -#include -#include -#include -#include -#include -#include "parser.hpp" -#include "ast.h" -#undef yyFlexLexer -extern YYSTYPE yylval; -using namespace std; - -class Token { -public: - int line; - int startCol; - int endCol; - - Token(int l, int s, int e) : line(l), startCol(s), endCol(e) {} - virtual ~Token() = default; - virtual void print() const = 0; -}; - -class KeywordToken : public Token { -public: - string value; - KeywordToken(const string& val, int l, int s, int e) : Token(l,s,e), value(val) {} - void print() const override { cout << "KEYWORD(" << value << ")\n"; } -}; - -class IdentifierToken : public Token { -public: - string name; - IdentifierToken(const string& n, int l, int s, int e) : Token(l,s,e), name(n) {} - void print() const override { cout << "IDENTIFIER(" << name << ")\n"; } -}; - -class IntegerToken : public Token { -public: - int value; - IntegerToken(int v, int l, int s, int e) : Token(l,s,e), value(v) {} - void print() const override { cout << "INTEGER(" << value << ")\n"; } -}; - -class RealToken : public Token { -public: - double value; - RealToken(double v, int l, int s, int e) : Token(l,s,e), value(v) {} - void print() const override { cout << "REAL(" << value << ")\n"; } -}; - -class BooleanToken : public Token { -public: - string value; - BooleanToken(const string& v, int l, int s, int e) : Token(l,s,e), value(v) {} - void print() const override { cout << "BOOLEAN(" << value << ")\n"; } -}; - -class StringToken : public Token { -public: - string value; - StringToken(const string& v, int l, int s, int e) : Token(l,s,e), value(v) {} - void print() const override { cout << "STRING(\"" << value << "\")\n"; } -}; - -class SymbolToken : public Token { -public: - string symbol; - SymbolToken(const string& s, int l, int sc, int ec) : Token(l,sc,ec), symbol(s) {} - void print() const override { cout << "SYMBOL(" << symbol << ")\n"; } -}; - -class UnknownToken : public Token { -public: - string text; - UnknownToken(const string& t, int l, int s, int e) : Token(l,s,e), text(t) {} - void print() const override { cout << "UNKNOWN(" << text << ")\n"; } -}; - -int currentLine = 1; -int currentCol = 1; - -#line 579 "lexer.cpp" -#line 580 "lexer.cpp" - -#define INITIAL 0 - -#ifndef YY_NO_UNISTD_H -/* Special case for "unistd.h", since it is non-ANSI. We include it way - * down here because we want the user's section 1 to have been scanned first. - * The user has a chance to override it with an option. - */ -#include -#endif - -#ifndef YY_EXTRA_TYPE -#define YY_EXTRA_TYPE void * -#endif - -static int yy_init_globals ( void ); - -/* Accessor methods to globals. - These are made visible to non-reentrant scanners for convenience. */ - -int yylex_destroy ( void ); - -int yyget_debug ( void ); - -void yyset_debug ( int debug_flag ); - -YY_EXTRA_TYPE yyget_extra ( void ); - -void yyset_extra ( YY_EXTRA_TYPE user_defined ); - -FILE *yyget_in ( void ); - -void yyset_in ( FILE * _in_str ); - -FILE *yyget_out ( void ); - -void yyset_out ( FILE * _out_str ); - - int yyget_leng ( void ); - -char *yyget_text ( void ); - -int yyget_lineno ( void ); - -void yyset_lineno ( int _line_number ); - -/* Macros after this point can all be overridden by user definitions in - * section 1. - */ - -#ifndef YY_SKIP_YYWRAP -#ifdef __cplusplus -extern "C" int yywrap ( void ); -#else -extern int yywrap ( void ); -#endif -#endif - -#ifndef YY_NO_UNPUT - - static void yyunput ( int c, char *buf_ptr ); - -#endif - -#ifndef yytext_ptr -static void yy_flex_strncpy ( char *, const char *, int ); -#endif - -#ifdef YY_NEED_STRLEN -static int yy_flex_strlen ( const char * ); -#endif - -#ifndef YY_NO_INPUT -#ifdef __cplusplus -static int yyinput ( void ); -#else -static int input ( void ); -#endif - -#endif - -/* Amount of stuff to slurp up with each read. */ -#ifndef YY_READ_BUF_SIZE -#ifdef __ia64__ -/* On IA-64, the buffer size is 16k, not 8k */ -#define YY_READ_BUF_SIZE 16384 -#else -#define YY_READ_BUF_SIZE 8192 -#endif /* __ia64__ */ -#endif - -/* Copy whatever the last rule matched to the standard output. */ -#ifndef ECHO -/* This used to be an fputs(), but since the string might contain NUL's, - * we now use fwrite(). - */ -#define ECHO do { if (fwrite( yytext, (size_t) yyleng, 1, yyout )) {} } while (0) -#endif - -/* Gets input and stuffs it into "buf". number of characters read, or YY_NULL, - * is returned in "result". - */ -#ifndef YY_INPUT -#define YY_INPUT(buf,result,max_size) \ - if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \ - { \ - int c = '*'; \ - int n; \ - for ( n = 0; n < max_size && \ - (c = getc( yyin )) != EOF && c != '\n'; ++n ) \ - buf[n] = (char) c; \ - if ( c == '\n' ) \ - buf[n++] = (char) c; \ - if ( c == EOF && ferror( yyin ) ) \ - YY_FATAL_ERROR( "input in flex scanner failed" ); \ - result = n; \ - } \ - else \ - { \ - errno=0; \ - while ( (result = (int) fread(buf, 1, (yy_size_t) max_size, yyin)) == 0 && ferror(yyin)) \ - { \ - if( errno != EINTR) \ - { \ - YY_FATAL_ERROR( "input in flex scanner failed" ); \ - break; \ - } \ - errno=0; \ - clearerr(yyin); \ - } \ - }\ -\ - -#endif - -/* No semi-colon after return; correct usage is to write "yyterminate();" - - * we don't want an extra ';' after the "return" because that will cause - * some compilers to complain about unreachable statements. - */ -#ifndef yyterminate -#define yyterminate() return YY_NULL -#endif - -/* Number of entries by which start-condition stack grows. */ -#ifndef YY_START_STACK_INCR -#define YY_START_STACK_INCR 25 -#endif - -/* Report a fatal error. */ -#ifndef YY_FATAL_ERROR -#define YY_FATAL_ERROR(msg) yy_fatal_error( msg ) -#endif - -/* end tables serialization structures and prototypes */ - -/* Default declaration of generated scanner - a define so the user can - * easily add parameters. - */ -#ifndef YY_DECL -#define YY_DECL_IS_OURS 1 - -extern int yylex (void); - -#define YY_DECL int yylex (void) -#endif /* !YY_DECL */ - -/* Code executed at the beginning of each rule, after yytext and yyleng - * have been set up. - */ -#ifndef YY_USER_ACTION -#define YY_USER_ACTION -#endif - -/* Code executed at the end of each rule. */ -#ifndef YY_BREAK -#define YY_BREAK /*LINTED*/break; -#endif - -#define YY_RULE_SETUP \ - YY_USER_ACTION - -/** The main scanner function which does all the work. - */ -YY_DECL -{ - yy_state_type yy_current_state; - char *yy_cp, *yy_bp; - int yy_act; - - if ( !(yy_init) ) - { - (yy_init) = 1; - -#ifdef YY_USER_INIT - YY_USER_INIT; -#endif - - if ( ! (yy_start) ) - (yy_start) = 1; /* first start state */ - - if ( ! yyin ) - yyin = stdin; - - if ( ! yyout ) - yyout = stdout; - - if ( ! YY_CURRENT_BUFFER ) { - yyensure_buffer_stack (); - YY_CURRENT_BUFFER_LVALUE = - yy_create_buffer( yyin, YY_BUF_SIZE ); - } - - yy_load_buffer_state( ); - } - - { -#line 88 "lexer.l" - - -#line 800 "lexer.cpp" - - while ( /*CONSTCOND*/1 ) /* loops until end-of-file is reached */ - { - yy_cp = (yy_c_buf_p); - - /* Support of yytext. */ - *yy_cp = (yy_hold_char); - - /* yy_bp points to the position in yy_ch_buf of the start of - * the current run. - */ - yy_bp = yy_cp; - - yy_current_state = (yy_start); -yy_match: - do - { - YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)] ; - if ( yy_accept[yy_current_state] ) - { - (yy_last_accepting_state) = yy_current_state; - (yy_last_accepting_cpos) = yy_cp; - } - while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) - { - yy_current_state = (int) yy_def[yy_current_state]; - if ( yy_current_state >= 70 ) - yy_c = yy_meta[yy_c]; - } - yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c]; - ++yy_cp; - } - while ( yy_base[yy_current_state] != 103 ); - -yy_find_action: - yy_act = yy_accept[yy_current_state]; - if ( yy_act == 0 ) - { /* have to back up */ - yy_cp = (yy_last_accepting_cpos); - yy_current_state = (yy_last_accepting_state); - yy_act = yy_accept[yy_current_state]; - } - - YY_DO_BEFORE_ACTION; - -do_action: /* This label is used only to access EOF actions. */ - - switch ( yy_act ) - { /* beginning of action switch */ - case 0: /* must back up */ - /* undo the effects of YY_DO_BEFORE_ACTION */ - *yy_cp = (yy_hold_char); - yy_cp = (yy_last_accepting_cpos); - yy_current_state = (yy_last_accepting_state); - goto yy_find_action; - -case 1: -YY_RULE_SETUP -#line 90 "lexer.l" -{ currentCol += yyleng; /* игнорируем */ } - YY_BREAK -case 2: -/* rule 2 can match eol */ -YY_RULE_SETUP -#line 91 "lexer.l" -{ currentLine++; currentCol = 1; } - YY_BREAK -case 3: -YY_RULE_SETUP -#line 93 "lexer.l" -{ - yylval.str = strdup(yytext); - int startCol = currentCol; - currentCol += yyleng; - if (strcmp(yytext, "if") == 0) return IF; - if (strcmp(yytext, "else") == 0) return ELSE; - if (strcmp(yytext, "while") == 0) return WHILE; - if (strcmp(yytext, "for") == 0) return LOOP; // если надо FOR - добавь - if (strcmp(yytext, "return") == 0) return RETURN; - if (strcmp(yytext, "true") == 0) return TRUE; - if (strcmp(yytext, "false") == 0) return FALSE; - if (strcmp(yytext, "class") == 0) return CLASS; - if (strcmp(yytext, "extends") == 0) return EXTENDS; - if (strcmp(yytext, "is") == 0) return IS; - if (strcmp(yytext, "end") == 0) return END; - if (strcmp(yytext, "var") == 0) return VAR; - if (strcmp(yytext, "method") == 0) return METHOD; - if (strcmp(yytext, "this") == 0) return THIS; - if (strcmp(yytext, "then") == 0) return THEN; - if (strcmp(yytext, "loop") == 0) return LOOP; - return IDENTIFIER; // если не keyword — идентификатор -} - YY_BREAK -case 4: -YY_RULE_SETUP -#line 116 "lexer.l" -{ - yylval.str = strdup(yytext); - int startCol = currentCol; - currentCol += yyleng; - return REAL; -} - YY_BREAK -case 5: -YY_RULE_SETUP -#line 123 "lexer.l" -{ - yylval.str = strdup(yytext); - int startCol = currentCol; - currentCol += yyleng; - return INTEGER; -} - YY_BREAK -case 6: -YY_RULE_SETUP -#line 130 "lexer.l" -{ - yylval.str = strdup(yytext); - int startCol = currentCol; - currentCol += yyleng; - return IDENTIFIER; -} - YY_BREAK -case 7: -/* rule 7 can match eol */ -YY_RULE_SETUP -#line 137 "lexer.l" -{ - string val(yytext + 1, yyleng - 2); - yylval.str = strdup(val.c_str()); - int startCol = currentCol; - currentCol += yyleng; - return STRING; -} - YY_BREAK -case 8: -YY_RULE_SETUP -#line 145 "lexer.l" -{ - yylval.str = strdup(yytext); - int startCol = currentCol; - currentCol += yyleng; - if (strcmp(yytext, ":") == 0) return COLON; - if (strcmp(yytext, ";") == 0) return SEMI; - if (strcmp(yytext, "(") == 0) return LPAREN; - if (strcmp(yytext, ")") == 0) return RPAREN; - if (strcmp(yytext, ",") == 0) return COMMA; - if (strcmp(yytext, ".") == 0) return DOT; - if (strcmp(yytext, ":=") == 0) return ASSIGN; - if (strcmp(yytext, "=>") == 0) return ARROW; - return SYMBOL; -} - YY_BREAK -case 9: -YY_RULE_SETUP -#line 160 "lexer.l" -{ - yylval.str = strdup(yytext); - int startCol = currentCol; - currentCol += yyleng; - return UNKNOWN; -} - YY_BREAK -case 10: -YY_RULE_SETUP -#line 167 "lexer.l" -ECHO; - YY_BREAK -#line 969 "lexer.cpp" -case YY_STATE_EOF(INITIAL): - yyterminate(); - - case YY_END_OF_BUFFER: - { - /* Amount of text matched not including the EOB char. */ - int yy_amount_of_matched_text = (int) (yy_cp - (yytext_ptr)) - 1; - - /* Undo the effects of YY_DO_BEFORE_ACTION. */ - *yy_cp = (yy_hold_char); - YY_RESTORE_YY_MORE_OFFSET - - if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_NEW ) - { - /* We're scanning a new file or input source. It's - * possible that this happened because the user - * just pointed yyin at a new source and called - * yylex(). If so, then we have to assure - * consistency between YY_CURRENT_BUFFER and our - * globals. Here is the right place to do so, because - * this is the first action (other than possibly a - * back-up) that will match for the new input source. - */ - (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; - YY_CURRENT_BUFFER_LVALUE->yy_input_file = yyin; - YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = YY_BUFFER_NORMAL; - } - - /* Note that here we test for yy_c_buf_p "<=" to the position - * of the first EOB in the buffer, since yy_c_buf_p will - * already have been incremented past the NUL character - * (since all states make transitions on EOB to the - * end-of-buffer state). Contrast this with the test - * in input(). - */ - if ( (yy_c_buf_p) <= &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] ) - { /* This was really a NUL. */ - yy_state_type yy_next_state; - - (yy_c_buf_p) = (yytext_ptr) + yy_amount_of_matched_text; - - yy_current_state = yy_get_previous_state( ); - - /* Okay, we're now positioned to make the NUL - * transition. We couldn't have - * yy_get_previous_state() go ahead and do it - * for us because it doesn't know how to deal - * with the possibility of jamming (and we don't - * want to build jamming into it because then it - * will run more slowly). - */ - - yy_next_state = yy_try_NUL_trans( yy_current_state ); - - yy_bp = (yytext_ptr) + YY_MORE_ADJ; - - if ( yy_next_state ) - { - /* Consume the NUL. */ - yy_cp = ++(yy_c_buf_p); - yy_current_state = yy_next_state; - goto yy_match; - } - - else - { - yy_cp = (yy_c_buf_p); - goto yy_find_action; - } - } - - else switch ( yy_get_next_buffer( ) ) - { - case EOB_ACT_END_OF_FILE: - { - (yy_did_buffer_switch_on_eof) = 0; - - if ( yywrap( ) ) - { - /* Note: because we've taken care in - * yy_get_next_buffer() to have set up - * yytext, we can now set up - * yy_c_buf_p so that if some total - * hoser (like flex itself) wants to - * call the scanner after we return the - * YY_NULL, it'll still work - another - * YY_NULL will get returned. - */ - (yy_c_buf_p) = (yytext_ptr) + YY_MORE_ADJ; - - yy_act = YY_STATE_EOF(YY_START); - goto do_action; - } - - else - { - if ( ! (yy_did_buffer_switch_on_eof) ) - YY_NEW_FILE; - } - break; - } - - case EOB_ACT_CONTINUE_SCAN: - (yy_c_buf_p) = - (yytext_ptr) + yy_amount_of_matched_text; - - yy_current_state = yy_get_previous_state( ); - - yy_cp = (yy_c_buf_p); - yy_bp = (yytext_ptr) + YY_MORE_ADJ; - goto yy_match; - - case EOB_ACT_LAST_MATCH: - (yy_c_buf_p) = - &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)]; - - yy_current_state = yy_get_previous_state( ); - - yy_cp = (yy_c_buf_p); - yy_bp = (yytext_ptr) + YY_MORE_ADJ; - goto yy_find_action; - } - break; - } - - default: - YY_FATAL_ERROR( - "fatal flex scanner internal error--no action found" ); - } /* end of action switch */ - } /* end of scanning one token */ - } /* end of user's declarations */ -} /* end of yylex */ - -/* yy_get_next_buffer - try to read in a new buffer - * - * Returns a code representing an action: - * EOB_ACT_LAST_MATCH - - * EOB_ACT_CONTINUE_SCAN - continue scanning from current position - * EOB_ACT_END_OF_FILE - end of file - */ -static int yy_get_next_buffer (void) -{ - char *dest = YY_CURRENT_BUFFER_LVALUE->yy_ch_buf; - char *source = (yytext_ptr); - int number_to_move, i; - int ret_val; - - if ( (yy_c_buf_p) > &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] ) - YY_FATAL_ERROR( - "fatal flex scanner internal error--end of buffer missed" ); - - if ( YY_CURRENT_BUFFER_LVALUE->yy_fill_buffer == 0 ) - { /* Don't try to fill the buffer, so this is an EOF. */ - if ( (yy_c_buf_p) - (yytext_ptr) - YY_MORE_ADJ == 1 ) - { - /* We matched a single character, the EOB, so - * treat this as a final EOF. - */ - return EOB_ACT_END_OF_FILE; - } - - else - { - /* We matched some text prior to the EOB, first - * process it. - */ - return EOB_ACT_LAST_MATCH; - } - } - - /* Try to read more data. */ - - /* First move last chars to start of buffer. */ - number_to_move = (int) ((yy_c_buf_p) - (yytext_ptr) - 1); - - for ( i = 0; i < number_to_move; ++i ) - *(dest++) = *(source++); - - if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_EOF_PENDING ) - /* don't do the read, it's not guaranteed to return an EOF, - * just force an EOF - */ - YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars) = 0; - - else - { - int num_to_read = - YY_CURRENT_BUFFER_LVALUE->yy_buf_size - number_to_move - 1; - - while ( num_to_read <= 0 ) - { /* Not enough room in the buffer - grow it. */ - - /* just a shorter name for the current buffer */ - YY_BUFFER_STATE b = YY_CURRENT_BUFFER_LVALUE; - - int yy_c_buf_p_offset = - (int) ((yy_c_buf_p) - b->yy_ch_buf); - - if ( b->yy_is_our_buffer ) - { - int new_size = b->yy_buf_size * 2; - - if ( new_size <= 0 ) - b->yy_buf_size += b->yy_buf_size / 8; - else - b->yy_buf_size *= 2; - - b->yy_ch_buf = (char *) - /* Include room in for 2 EOB chars. */ - yyrealloc( (void *) b->yy_ch_buf, - (yy_size_t) (b->yy_buf_size + 2) ); - } - else - /* Can't grow it, we don't own it. */ - b->yy_ch_buf = NULL; - - if ( ! b->yy_ch_buf ) - YY_FATAL_ERROR( - "fatal error - scanner input buffer overflow" ); - - (yy_c_buf_p) = &b->yy_ch_buf[yy_c_buf_p_offset]; - - num_to_read = YY_CURRENT_BUFFER_LVALUE->yy_buf_size - - number_to_move - 1; - - } - - if ( num_to_read > YY_READ_BUF_SIZE ) - num_to_read = YY_READ_BUF_SIZE; - - /* Read in more data. */ - YY_INPUT( (&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]), - (yy_n_chars), num_to_read ); - - YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); - } - - if ( (yy_n_chars) == 0 ) - { - if ( number_to_move == YY_MORE_ADJ ) - { - ret_val = EOB_ACT_END_OF_FILE; - yyrestart( yyin ); - } - - else - { - ret_val = EOB_ACT_LAST_MATCH; - YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = - YY_BUFFER_EOF_PENDING; - } - } - - else - ret_val = EOB_ACT_CONTINUE_SCAN; - - if (((yy_n_chars) + number_to_move) > YY_CURRENT_BUFFER_LVALUE->yy_buf_size) { - /* Extend the array by 50%, plus the number we really need. */ - int new_size = (yy_n_chars) + number_to_move + ((yy_n_chars) >> 1); - YY_CURRENT_BUFFER_LVALUE->yy_ch_buf = (char *) yyrealloc( - (void *) YY_CURRENT_BUFFER_LVALUE->yy_ch_buf, (yy_size_t) new_size ); - if ( ! YY_CURRENT_BUFFER_LVALUE->yy_ch_buf ) - YY_FATAL_ERROR( "out of dynamic memory in yy_get_next_buffer()" ); - /* "- 2" to take care of EOB's */ - YY_CURRENT_BUFFER_LVALUE->yy_buf_size = (int) (new_size - 2); - } - - (yy_n_chars) += number_to_move; - YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] = YY_END_OF_BUFFER_CHAR; - YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] = YY_END_OF_BUFFER_CHAR; - - (yytext_ptr) = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[0]; - - return ret_val; -} - -/* yy_get_previous_state - get the state just before the EOB char was reached */ - - static yy_state_type yy_get_previous_state (void) -{ - yy_state_type yy_current_state; - char *yy_cp; - - yy_current_state = (yy_start); - - for ( yy_cp = (yytext_ptr) + YY_MORE_ADJ; yy_cp < (yy_c_buf_p); ++yy_cp ) - { - YY_CHAR yy_c = (*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : 1); - if ( yy_accept[yy_current_state] ) - { - (yy_last_accepting_state) = yy_current_state; - (yy_last_accepting_cpos) = yy_cp; - } - while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) - { - yy_current_state = (int) yy_def[yy_current_state]; - if ( yy_current_state >= 70 ) - yy_c = yy_meta[yy_c]; - } - yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c]; - } - - return yy_current_state; -} - -/* yy_try_NUL_trans - try to make a transition on the NUL character - * - * synopsis - * next_state = yy_try_NUL_trans( current_state ); - */ - static yy_state_type yy_try_NUL_trans (yy_state_type yy_current_state ) -{ - int yy_is_jam; - char *yy_cp = (yy_c_buf_p); - - YY_CHAR yy_c = 1; - if ( yy_accept[yy_current_state] ) - { - (yy_last_accepting_state) = yy_current_state; - (yy_last_accepting_cpos) = yy_cp; - } - while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) - { - yy_current_state = (int) yy_def[yy_current_state]; - if ( yy_current_state >= 70 ) - yy_c = yy_meta[yy_c]; - } - yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c]; - yy_is_jam = (yy_current_state == 69); - - return yy_is_jam ? 0 : yy_current_state; -} - -#ifndef YY_NO_UNPUT - - static void yyunput (int c, char * yy_bp ) -{ - char *yy_cp; - - yy_cp = (yy_c_buf_p); - - /* undo effects of setting up yytext */ - *yy_cp = (yy_hold_char); - - if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 ) - { /* need to shift things up to make room */ - /* +2 for EOB chars. */ - int number_to_move = (yy_n_chars) + 2; - char *dest = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[ - YY_CURRENT_BUFFER_LVALUE->yy_buf_size + 2]; - char *source = - &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]; - - while ( source > YY_CURRENT_BUFFER_LVALUE->yy_ch_buf ) - *--dest = *--source; - - yy_cp += (int) (dest - source); - yy_bp += (int) (dest - source); - YY_CURRENT_BUFFER_LVALUE->yy_n_chars = - (yy_n_chars) = (int) YY_CURRENT_BUFFER_LVALUE->yy_buf_size; - - if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 ) - YY_FATAL_ERROR( "flex scanner push-back overflow" ); - } - - *--yy_cp = (char) c; - - (yytext_ptr) = yy_bp; - (yy_hold_char) = *yy_cp; - (yy_c_buf_p) = yy_cp; -} - -#endif - -#ifndef YY_NO_INPUT -#ifdef __cplusplus - static int yyinput (void) -#else - static int input (void) -#endif - -{ - int c; - - *(yy_c_buf_p) = (yy_hold_char); - - if ( *(yy_c_buf_p) == YY_END_OF_BUFFER_CHAR ) - { - /* yy_c_buf_p now points to the character we want to return. - * If this occurs *before* the EOB characters, then it's a - * valid NUL; if not, then we've hit the end of the buffer. - */ - if ( (yy_c_buf_p) < &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] ) - /* This was really a NUL. */ - *(yy_c_buf_p) = '\0'; - - else - { /* need more input */ - int offset = (int) ((yy_c_buf_p) - (yytext_ptr)); - ++(yy_c_buf_p); - - switch ( yy_get_next_buffer( ) ) - { - case EOB_ACT_LAST_MATCH: - /* This happens because yy_g_n_b() - * sees that we've accumulated a - * token and flags that we need to - * try matching the token before - * proceeding. But for input(), - * there's no matching to consider. - * So convert the EOB_ACT_LAST_MATCH - * to EOB_ACT_END_OF_FILE. - */ - - /* Reset buffer status. */ - yyrestart( yyin ); - - /*FALLTHROUGH*/ - - case EOB_ACT_END_OF_FILE: - { - if ( yywrap( ) ) - return 0; - - if ( ! (yy_did_buffer_switch_on_eof) ) - YY_NEW_FILE; -#ifdef __cplusplus - return yyinput(); -#else - return input(); -#endif - } - - case EOB_ACT_CONTINUE_SCAN: - (yy_c_buf_p) = (yytext_ptr) + offset; - break; - } - } - } - - c = *(unsigned char *) (yy_c_buf_p); /* cast for 8-bit char's */ - *(yy_c_buf_p) = '\0'; /* preserve yytext */ - (yy_hold_char) = *++(yy_c_buf_p); - - return c; -} -#endif /* ifndef YY_NO_INPUT */ - -/** Immediately switch to a different input stream. - * @param input_file A readable stream. - * - * @note This function does not reset the start condition to @c INITIAL . - */ - void yyrestart (FILE * input_file ) -{ - - if ( ! YY_CURRENT_BUFFER ){ - yyensure_buffer_stack (); - YY_CURRENT_BUFFER_LVALUE = - yy_create_buffer( yyin, YY_BUF_SIZE ); - } - - yy_init_buffer( YY_CURRENT_BUFFER, input_file ); - yy_load_buffer_state( ); -} - -/** Switch to a different input buffer. - * @param new_buffer The new input buffer. - * - */ - void yy_switch_to_buffer (YY_BUFFER_STATE new_buffer ) -{ - - /* TODO. We should be able to replace this entire function body - * with - * yypop_buffer_state(); - * yypush_buffer_state(new_buffer); - */ - yyensure_buffer_stack (); - if ( YY_CURRENT_BUFFER == new_buffer ) - return; - - if ( YY_CURRENT_BUFFER ) - { - /* Flush out information for old buffer. */ - *(yy_c_buf_p) = (yy_hold_char); - YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p); - YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); - } - - YY_CURRENT_BUFFER_LVALUE = new_buffer; - yy_load_buffer_state( ); - - /* We don't actually know whether we did this switch during - * EOF (yywrap()) processing, but the only time this flag - * is looked at is after yywrap() is called, so it's safe - * to go ahead and always set it. - */ - (yy_did_buffer_switch_on_eof) = 1; -} - -static void yy_load_buffer_state (void) -{ - (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; - (yytext_ptr) = (yy_c_buf_p) = YY_CURRENT_BUFFER_LVALUE->yy_buf_pos; - yyin = YY_CURRENT_BUFFER_LVALUE->yy_input_file; - (yy_hold_char) = *(yy_c_buf_p); -} - -/** Allocate and initialize an input buffer state. - * @param file A readable stream. - * @param size The character buffer size in bytes. When in doubt, use @c YY_BUF_SIZE. - * - * @return the allocated buffer state. - */ - YY_BUFFER_STATE yy_create_buffer (FILE * file, int size ) -{ - YY_BUFFER_STATE b; - - b = (YY_BUFFER_STATE) yyalloc( sizeof( struct yy_buffer_state ) ); - if ( ! b ) - YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); - - b->yy_buf_size = size; - - /* yy_ch_buf has to be 2 characters longer than the size given because - * we need to put in 2 end-of-buffer characters. - */ - b->yy_ch_buf = (char *) yyalloc( (yy_size_t) (b->yy_buf_size + 2) ); - if ( ! b->yy_ch_buf ) - YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); - - b->yy_is_our_buffer = 1; - - yy_init_buffer( b, file ); - - return b; -} - -/** Destroy the buffer. - * @param b a buffer created with yy_create_buffer() - * - */ - void yy_delete_buffer (YY_BUFFER_STATE b ) -{ - - if ( ! b ) - return; - - if ( b == YY_CURRENT_BUFFER ) /* Not sure if we should pop here. */ - YY_CURRENT_BUFFER_LVALUE = (YY_BUFFER_STATE) 0; - - if ( b->yy_is_our_buffer ) - yyfree( (void *) b->yy_ch_buf ); - - yyfree( (void *) b ); -} - -/* Initializes or reinitializes a buffer. - * This function is sometimes called more than once on the same buffer, - * such as during a yyrestart() or at EOF. - */ - static void yy_init_buffer (YY_BUFFER_STATE b, FILE * file ) - -{ - int oerrno = errno; - - yy_flush_buffer( b ); - - b->yy_input_file = file; - b->yy_fill_buffer = 1; - - /* If b is the current buffer, then yy_init_buffer was _probably_ - * called from yyrestart() or through yy_get_next_buffer. - * In that case, we don't want to reset the lineno or column. - */ - if (b != YY_CURRENT_BUFFER){ - b->yy_bs_lineno = 1; - b->yy_bs_column = 0; - } - - b->yy_is_interactive = file ? (isatty( fileno(file) ) > 0) : 0; - - errno = oerrno; -} - -/** Discard all buffered characters. On the next scan, YY_INPUT will be called. - * @param b the buffer state to be flushed, usually @c YY_CURRENT_BUFFER. - * - */ - void yy_flush_buffer (YY_BUFFER_STATE b ) -{ - if ( ! b ) - return; - - b->yy_n_chars = 0; - - /* We always need two end-of-buffer characters. The first causes - * a transition to the end-of-buffer state. The second causes - * a jam in that state. - */ - b->yy_ch_buf[0] = YY_END_OF_BUFFER_CHAR; - b->yy_ch_buf[1] = YY_END_OF_BUFFER_CHAR; - - b->yy_buf_pos = &b->yy_ch_buf[0]; - - b->yy_at_bol = 1; - b->yy_buffer_status = YY_BUFFER_NEW; - - if ( b == YY_CURRENT_BUFFER ) - yy_load_buffer_state( ); -} - -/** Pushes the new state onto the stack. The new state becomes - * the current state. This function will allocate the stack - * if necessary. - * @param new_buffer The new state. - * - */ -void yypush_buffer_state (YY_BUFFER_STATE new_buffer ) -{ - if (new_buffer == NULL) - return; - - yyensure_buffer_stack(); - - /* This block is copied from yy_switch_to_buffer. */ - if ( YY_CURRENT_BUFFER ) - { - /* Flush out information for old buffer. */ - *(yy_c_buf_p) = (yy_hold_char); - YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p); - YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); - } - - /* Only push if top exists. Otherwise, replace top. */ - if (YY_CURRENT_BUFFER) - (yy_buffer_stack_top)++; - YY_CURRENT_BUFFER_LVALUE = new_buffer; - - /* copied from yy_switch_to_buffer. */ - yy_load_buffer_state( ); - (yy_did_buffer_switch_on_eof) = 1; -} - -/** Removes and deletes the top of the stack, if present. - * The next element becomes the new top. - * - */ -void yypop_buffer_state (void) -{ - if (!YY_CURRENT_BUFFER) - return; - - yy_delete_buffer(YY_CURRENT_BUFFER ); - YY_CURRENT_BUFFER_LVALUE = NULL; - if ((yy_buffer_stack_top) > 0) - --(yy_buffer_stack_top); - - if (YY_CURRENT_BUFFER) { - yy_load_buffer_state( ); - (yy_did_buffer_switch_on_eof) = 1; - } -} - -/* Allocates the stack if it does not exist. - * Guarantees space for at least one push. - */ -static void yyensure_buffer_stack (void) -{ - yy_size_t num_to_alloc; - - if (!(yy_buffer_stack)) { - - /* First allocation is just for 2 elements, since we don't know if this - * scanner will even need a stack. We use 2 instead of 1 to avoid an - * immediate realloc on the next call. - */ - num_to_alloc = 1; /* After all that talk, this was set to 1 anyways... */ - (yy_buffer_stack) = (struct yy_buffer_state**)yyalloc - (num_to_alloc * sizeof(struct yy_buffer_state*) - ); - if ( ! (yy_buffer_stack) ) - YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" ); - - memset((yy_buffer_stack), 0, num_to_alloc * sizeof(struct yy_buffer_state*)); - - (yy_buffer_stack_max) = num_to_alloc; - (yy_buffer_stack_top) = 0; - return; - } - - if ((yy_buffer_stack_top) >= ((yy_buffer_stack_max)) - 1){ - - /* Increase the buffer to prepare for a possible push. */ - yy_size_t grow_size = 8 /* arbitrary grow size */; - - num_to_alloc = (yy_buffer_stack_max) + grow_size; - (yy_buffer_stack) = (struct yy_buffer_state**)yyrealloc - ((yy_buffer_stack), - num_to_alloc * sizeof(struct yy_buffer_state*) - ); - if ( ! (yy_buffer_stack) ) - YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" ); - - /* zero only the new slots.*/ - memset((yy_buffer_stack) + (yy_buffer_stack_max), 0, grow_size * sizeof(struct yy_buffer_state*)); - (yy_buffer_stack_max) = num_to_alloc; - } -} - -/** Setup the input buffer state to scan directly from a user-specified character buffer. - * @param base the character buffer - * @param size the size in bytes of the character buffer - * - * @return the newly allocated buffer state object. - */ -YY_BUFFER_STATE yy_scan_buffer (char * base, yy_size_t size ) -{ - YY_BUFFER_STATE b; - - if ( size < 2 || - base[size-2] != YY_END_OF_BUFFER_CHAR || - base[size-1] != YY_END_OF_BUFFER_CHAR ) - /* They forgot to leave room for the EOB's. */ - return NULL; - - b = (YY_BUFFER_STATE) yyalloc( sizeof( struct yy_buffer_state ) ); - if ( ! b ) - YY_FATAL_ERROR( "out of dynamic memory in yy_scan_buffer()" ); - - b->yy_buf_size = (int) (size - 2); /* "- 2" to take care of EOB's */ - b->yy_buf_pos = b->yy_ch_buf = base; - b->yy_is_our_buffer = 0; - b->yy_input_file = NULL; - b->yy_n_chars = b->yy_buf_size; - b->yy_is_interactive = 0; - b->yy_at_bol = 1; - b->yy_fill_buffer = 0; - b->yy_buffer_status = YY_BUFFER_NEW; - - yy_switch_to_buffer( b ); - - return b; -} - -/** Setup the input buffer state to scan a string. The next call to yylex() will - * scan from a @e copy of @a str. - * @param yystr a NUL-terminated string to scan - * - * @return the newly allocated buffer state object. - * @note If you want to scan bytes that may contain NUL values, then use - * yy_scan_bytes() instead. - */ -YY_BUFFER_STATE yy_scan_string (const char * yystr ) -{ - - return yy_scan_bytes( yystr, (int) strlen(yystr) ); -} - -/** Setup the input buffer state to scan the given bytes. The next call to yylex() will - * scan from a @e copy of @a bytes. - * @param yybytes the byte buffer to scan - * @param _yybytes_len the number of bytes in the buffer pointed to by @a bytes. - * - * @return the newly allocated buffer state object. - */ -YY_BUFFER_STATE yy_scan_bytes (const char * yybytes, int _yybytes_len ) -{ - YY_BUFFER_STATE b; - char *buf; - yy_size_t n; - int i; - - /* Get memory for full buffer, including space for trailing EOB's. */ - n = (yy_size_t) (_yybytes_len + 2); - buf = (char *) yyalloc( n ); - if ( ! buf ) - YY_FATAL_ERROR( "out of dynamic memory in yy_scan_bytes()" ); - - for ( i = 0; i < _yybytes_len; ++i ) - buf[i] = yybytes[i]; - - buf[_yybytes_len] = buf[_yybytes_len+1] = YY_END_OF_BUFFER_CHAR; - - b = yy_scan_buffer( buf, n ); - if ( ! b ) - YY_FATAL_ERROR( "bad buffer in yy_scan_bytes()" ); - - /* It's okay to grow etc. this buffer, and we should throw it - * away when we're done. - */ - b->yy_is_our_buffer = 1; - - return b; -} - -#ifndef YY_EXIT_FAILURE -#define YY_EXIT_FAILURE 2 -#endif - -static void yynoreturn yy_fatal_error (const char* msg ) -{ - fprintf( stderr, "%s\n", msg ); - exit( YY_EXIT_FAILURE ); -} - -/* Redefine yyless() so it works in section 3 code. */ - -#undef yyless -#define yyless(n) \ - do \ - { \ - /* Undo effects of setting up yytext. */ \ - int yyless_macro_arg = (n); \ - YY_LESS_LINENO(yyless_macro_arg);\ - yytext[yyleng] = (yy_hold_char); \ - (yy_c_buf_p) = yytext + yyless_macro_arg; \ - (yy_hold_char) = *(yy_c_buf_p); \ - *(yy_c_buf_p) = '\0'; \ - yyleng = yyless_macro_arg; \ - } \ - while ( 0 ) - -/* Accessor methods (get/set functions) to struct members. */ - -/** Get the current line number. - * - */ -int yyget_lineno (void) -{ - - return yylineno; -} - -/** Get the input stream. - * - */ -FILE *yyget_in (void) -{ - return yyin; -} - -/** Get the output stream. - * - */ -FILE *yyget_out (void) -{ - return yyout; -} - -/** Get the length of the current token. - * - */ -int yyget_leng (void) -{ - return yyleng; -} - -/** Get the current token. - * - */ - -char *yyget_text (void) -{ - return yytext; -} - -/** Set the current line number. - * @param _line_number line number - * - */ -void yyset_lineno (int _line_number ) -{ - - yylineno = _line_number; -} - -/** Set the input stream. This does not discard the current - * input buffer. - * @param _in_str A readable stream. - * - * @see yy_switch_to_buffer - */ -void yyset_in (FILE * _in_str ) -{ - yyin = _in_str ; -} - -void yyset_out (FILE * _out_str ) -{ - yyout = _out_str ; -} - -int yyget_debug (void) -{ - return yy_flex_debug; -} - -void yyset_debug (int _bdebug ) -{ - yy_flex_debug = _bdebug ; -} - -static int yy_init_globals (void) -{ - /* Initialization is the same as for the non-reentrant scanner. - * This function is called from yylex_destroy(), so don't allocate here. - */ - - (yy_buffer_stack) = NULL; - (yy_buffer_stack_top) = 0; - (yy_buffer_stack_max) = 0; - (yy_c_buf_p) = NULL; - (yy_init) = 0; - (yy_start) = 0; - -/* Defined in main.c */ -#ifdef YY_STDINIT - yyin = stdin; - yyout = stdout; -#else - yyin = NULL; - yyout = NULL; -#endif - - /* For future reference: Set errno on error, since we are called by - * yylex_init() - */ - return 0; -} - -/* yylex_destroy is for both reentrant and non-reentrant scanners. */ -int yylex_destroy (void) -{ - - /* Pop the buffer stack, destroying each element. */ - while(YY_CURRENT_BUFFER){ - yy_delete_buffer( YY_CURRENT_BUFFER ); - YY_CURRENT_BUFFER_LVALUE = NULL; - yypop_buffer_state(); - } - - /* Destroy the stack itself. */ - yyfree((yy_buffer_stack) ); - (yy_buffer_stack) = NULL; - - /* Reset the globals. This is important in a non-reentrant scanner so the next time - * yylex() is called, initialization will occur. */ - yy_init_globals( ); - - return 0; -} - -/* - * Internal utility routines. - */ - -#ifndef yytext_ptr -static void yy_flex_strncpy (char* s1, const char * s2, int n ) -{ - - int i; - for ( i = 0; i < n; ++i ) - s1[i] = s2[i]; -} -#endif - -#ifdef YY_NEED_STRLEN -static int yy_flex_strlen (const char * s ) -{ - int n; - for ( n = 0; s[n]; ++n ) - ; - - return n; -} -#endif - -void *yyalloc (yy_size_t size ) -{ - return malloc(size); -} - -void *yyrealloc (void * ptr, yy_size_t size ) -{ - - /* The cast to (char *) in the following accommodates both - * implementations that use char* generic pointers, and those - * that use void* generic pointers. It works with the latter - * because both ANSI C and C++ allow castless assignment from - * any pointer type to void*, and deal with argument conversions - * as though doing an assignment. - */ - return realloc(ptr, size); -} - -void yyfree (void * ptr ) -{ - free( (char *) ptr ); /* see yyrealloc() for (char *) cast */ -} - -#define YYTABLES_NAME "yytables" - -#line 167 "lexer.l" - diff --git a/lexer.l b/lexer.l index e624b65..4655ae5 100644 --- a/lexer.l +++ b/lexer.l @@ -1,167 +1,82 @@ -%option noyywrap - %{ -#include +#include +#include +#include #include -#include -#include -#include -#include #include "parser.hpp" -#include "ast.h" -#undef yyFlexLexer -extern YYSTYPE yylval; -using namespace std; - -class Token { -public: - int line; - int startCol; - int endCol; - - Token(int l, int s, int e) : line(l), startCol(s), endCol(e) {} - virtual ~Token() = default; - virtual void print() const = 0; -}; - -class KeywordToken : public Token { -public: - string value; - KeywordToken(const string& val, int l, int s, int e) : Token(l,s,e), value(val) {} - void print() const override { cout << "KEYWORD(" << value << ")\n"; } -}; - -class IdentifierToken : public Token { -public: - string name; - IdentifierToken(const string& n, int l, int s, int e) : Token(l,s,e), name(n) {} - void print() const override { cout << "IDENTIFIER(" << name << ")\n"; } -}; - -class IntegerToken : public Token { -public: - int value; - IntegerToken(int v, int l, int s, int e) : Token(l,s,e), value(v) {} - void print() const override { cout << "INTEGER(" << value << ")\n"; } -}; - -class RealToken : public Token { -public: - double value; - RealToken(double v, int l, int s, int e) : Token(l,s,e), value(v) {} - void print() const override { cout << "REAL(" << value << ")\n"; } -}; - -class BooleanToken : public Token { -public: - string value; - BooleanToken(const string& v, int l, int s, int e) : Token(l,s,e), value(v) {} - void print() const override { cout << "BOOLEAN(" << value << ")\n"; } -}; - -class StringToken : public Token { -public: - string value; - StringToken(const string& v, int l, int s, int e) : Token(l,s,e), value(v) {} - void print() const override { cout << "STRING(\"" << value << "\")\n"; } -}; - -class SymbolToken : public Token { -public: - string symbol; - SymbolToken(const string& s, int l, int sc, int ec) : Token(l,sc,ec), symbol(s) {} - void print() const override { cout << "SYMBOL(" << symbol << ")\n"; } -}; - -class UnknownToken : public Token { -public: - string text; - UnknownToken(const string& t, int l, int s, int e) : Token(l,s,e), text(t) {} - void print() const override { cout << "UNKNOWN(" << text << ")\n"; } -}; - -int currentLine = 1; -int currentCol = 1; - -%} - -%% +#include "tokens.hpp" -[ \t]+ { currentCol += yyleng; /* игнорируем */ } -\r?\n { currentLine++; currentCol = 1; } +int yycolumn = 1; +extern int yylineno; -"if"|"else"|"while"|"for"|"return"|"true"|"false"|"class"|"extends"|"is"|"end"|"var"|"method"|"this"|"then"|"loop" { - yylval.str = strdup(yytext); - int startCol = currentCol; - currentCol += yyleng; - if (strcmp(yytext, "if") == 0) return IF; - if (strcmp(yytext, "else") == 0) return ELSE; - if (strcmp(yytext, "while") == 0) return WHILE; - if (strcmp(yytext, "for") == 0) return LOOP; // если надо FOR - добавь - if (strcmp(yytext, "return") == 0) return RETURN; - if (strcmp(yytext, "true") == 0) return TRUE; - if (strcmp(yytext, "false") == 0) return FALSE; - if (strcmp(yytext, "class") == 0) return CLASS; - if (strcmp(yytext, "extends") == 0) return EXTENDS; - if (strcmp(yytext, "is") == 0) return IS; - if (strcmp(yytext, "end") == 0) return END; - if (strcmp(yytext, "var") == 0) return VAR; - if (strcmp(yytext, "method") == 0) return METHOD; - if (strcmp(yytext, "this") == 0) return THIS; - if (strcmp(yytext, "then") == 0) return THEN; - if (strcmp(yytext, "loop") == 0) return LOOP; - return IDENTIFIER; // если не keyword — идентификатор +static inline void push_kw(TokenKind k, const char* text) { + EmitToken(std::make_unique(k, text, yylineno, yycolumn)); + yycolumn += yyleng; } - -[0-9]+\.[0-9]+ { - yylval.str = strdup(yytext); - int startCol = currentCol; - currentCol += yyleng; - return REAL; -} - -[0-9]+ { - yylval.str = strdup(yytext); - int startCol = currentCol; - currentCol += yyleng; - return INTEGER; +static inline void push_sym(TokenKind k, const char* text) { + EmitToken(std::make_unique(k, text, yylineno, yycolumn)); + yycolumn += yyleng; } +%} -[A-Za-z_][A-Za-z0-9_]* { - yylval.str = strdup(yytext); - int startCol = currentCol; - currentCol += yyleng; - return IDENTIFIER; -} +%option noyywrap +%option nodefault +%option yylineno -\"([^\\\"]|\\.)*\" { - string val(yytext + 1, yyleng - 2); - yylval.str = strdup(val.c_str()); - int startCol = currentCol; - currentCol += yyleng; - return STRING; -} +ID [A-Za-z_][A-Za-z0-9_]* +INT [0-9]+ +WS [ \t\r]+ -(:=|=>|[+\-*/=(){};,<>:\[\]]) { - yylval.str = strdup(yytext); - int startCol = currentCol; - currentCol += yyleng; - if (strcmp(yytext, ":") == 0) return COLON; - if (strcmp(yytext, ";") == 0) return SEMI; - if (strcmp(yytext, "(") == 0) return LPAREN; - if (strcmp(yytext, ")") == 0) return RPAREN; - if (strcmp(yytext, ",") == 0) return COMMA; - if (strcmp(yytext, ".") == 0) return DOT; - if (strcmp(yytext, ":=") == 0) return ASSIGN; - if (strcmp(yytext, "=>") == 0) return ARROW; - return SYMBOL; -} +%% -. { - yylval.str = strdup(yytext); - int startCol = currentCol; - currentCol += yyleng; - return UNKNOWN; -} +"class" { push_kw(TokenKind::CLASS, yytext); return CLASS; } +"var" { push_kw(TokenKind::VAR, yytext); return VAR; } +"is" { push_kw(TokenKind::IS, yytext); return IS; } +"end" { push_kw(TokenKind::END, yytext); return END; } + +"method" { push_kw(TokenKind::METHOD, yytext); return METHOD; } +"return" { push_kw(TokenKind::RETURN, yytext); return RETURN; } +"if" { push_kw(TokenKind::IF, yytext); return IF; } +"then" { push_kw(TokenKind::THEN, yytext); return THEN; } +"else" { push_kw(TokenKind::ELSE, yytext); return ELSE; } +"true" { push_kw(TokenKind::TRUEKW, yytext); return TRUE; } +"false" { push_kw(TokenKind::FALSEKW, yytext); return FALSE; } + +"Int" { EmitToken(std::make_unique(yytext, yylineno, yycolumn)); + yylval.cstr = strdup(yytext); yycolumn += yyleng; return TYPE_NAME; } +"String" { EmitToken(std::make_unique(yytext, yylineno, yycolumn)); + yylval.cstr = strdup(yytext); yycolumn += yyleng; return TYPE_NAME; } +"Bool" { EmitToken(std::make_unique(yytext, yylineno, yycolumn)); + yylval.cstr = strdup(yytext); yycolumn += yyleng; return TYPE_NAME; } +"Float" { EmitToken(std::make_unique(yytext, yylineno, yycolumn)); + yylval.cstr = strdup(yytext); yycolumn += yyleng; return TYPE_NAME; } + +{ID} { EmitToken(std::make_unique(yytext, yylineno, yycolumn)); + yylval.cstr = strdup(yytext); yycolumn += yyleng; return IDENTIFIER; } + +{INT} { long long v = atoll(yytext); + EmitToken(std::make_unique(yytext, v, yylineno, yycolumn)); + yylval.ival = v; yycolumn += yyleng; return INT_LITERAL; } + +"=>" { push_sym(TokenKind::ARROW, yytext); return ARROW; } +":" { push_sym(TokenKind::COLON, yytext); return COLON; } +";" { push_sym(TokenKind::SEMICOLON, yytext); return SEMICOLON; } +"," { push_sym(TokenKind::COMMA, yytext); return COMMA; } +"(" { push_sym(TokenKind::LPAREN, yytext); return LPAREN; } +")" { push_sym(TokenKind::RPAREN, yytext); return RPAREN; } +"{" { push_sym(TokenKind::LBRACE, yytext); return LBRACE; } +"}" { push_sym(TokenKind::RBRACE, yytext); return RBRACE; } +"=" { push_sym(TokenKind::ASSIGN, yytext); return ASSIGN; } +"+" { push_sym(TokenKind::PLUS, yytext); return PLUS; } +"-" { push_sym(TokenKind::MINUS, yytext); return MINUS; } +"*" { push_sym(TokenKind::STAR, yytext); return STAR; } +"/" { push_sym(TokenKind::SLASH, yytext); return SLASH; } + +"//".* { yycolumn += yyleng; } +{WS} { yycolumn += yyleng; } +\n { yycolumn = 1; } + +. { std::fprintf(stderr, "Unknown char '%s' at %d:%d\n", yytext, yylineno, yycolumn); + yycolumn += yyleng; } -%% \ No newline at end of file +%% diff --git a/main.cpp b/main.cpp index a45cb6b..0e358f5 100644 --- a/main.cpp +++ b/main.cpp @@ -1,25 +1,35 @@ #include #include +#include "ast.hpp" +#include "tokens.hpp" + +extern int yyparse(void); extern FILE* yyin; -extern int yyparse(); -extern void printAST(); +extern AST::Program* g_program; -int main(int argc, char **argv) { +int main(int argc, char** argv) { if (argc < 2) { - std::cerr << "Usage: " << argv[0] << " \n"; + std::cerr << "Usage: " << argv[0] << " \n"; return 1; } - yyin = fopen(argv[1], "r"); + yyin = std::fopen(argv[1], "r"); if (!yyin) { - std::cerr << "Failed to open input file\n"; + std::perror("fopen"); return 1; } - int res = yyparse(); - if (res == 0) { - printAST(); + std::cout << "=== LEXER TOKENS ===\n"; + int rc = yyparse(); + std::fclose(yyin); + if (rc == 0) { + std::cout << "\n=== AST ===\n"; + if (g_program) { + g_program->print(std::cout); + delete g_program; + g_program = nullptr; + } + return 0; } else { - std::cerr << "Parsing failed\n"; + std::cerr << "Parse failed.\n"; + return 2; } - fclose(yyin); - return res; } diff --git a/mycompiler b/mycompiler deleted file mode 100644 index 5e30f90..0000000 Binary files a/mycompiler and /dev/null differ diff --git a/parser.cpp b/parser.cpp deleted file mode 100644 index f003747..0000000 --- a/parser.cpp +++ /dev/null @@ -1,1799 +0,0 @@ -/* A Bison parser, made by GNU Bison 3.8.2. */ - -/* Bison implementation for Yacc-like parsers in C - - Copyright (C) 1984, 1989-1990, 2000-2015, 2018-2021 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 3 of the License, 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, see . */ - -/* 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. */ - -/* C LALR(1) parser skeleton written by Richard Stallman, by - simplifying the original so-called "semantic" parser. */ - -/* DO NOT RELY ON FEATURES THAT ARE NOT DOCUMENTED in the manual, - especially those whose name start with YY_ or yy_. They are - private implementation details that can be changed or removed. */ - -/* All symbols defined below should begin with yy or YY, to avoid - infringing on user name space. This should be done even for local - variables, as they might otherwise be expanded by user macros. - There are some unavoidable exceptions within include files to - define necessary library symbols; they are noted "INFRINGES ON - USER NAME SPACE" below. */ - -/* Identify Bison output, and Bison version. */ -#define YYBISON 30802 - -/* Bison version string. */ -#define YYBISON_VERSION "3.8.2" - -/* Skeleton name. */ -#define YYSKELETON_NAME "yacc.c" - -/* Pure parsers. */ -#define YYPURE 0 - -/* Push parsers. */ -#define YYPUSH 0 - -/* Pull parsers. */ -#define YYPULL 1 - - - - -/* First part of user prologue. */ -#line 3 "parser.y" - -#include -#include -#include -#include -#include -#include "ast.h" -#include - -using namespace std; - -unique_ptr g_program = nullptr; -size_t parserTokIndex = 0; -vector simpleTokens; - -void yyerror(const char *s) { - cerr << "Parse error: " << s << "\n"; -} - -void printAST() { - if (g_program) { - g_program->print(0); - } else { - cout << "No AST generated.\n"; - } -} - -#line 99 "parser.cpp" - -# ifndef YY_CAST -# ifdef __cplusplus -# define YY_CAST(Type, Val) static_cast (Val) -# define YY_REINTERPRET_CAST(Type, Val) reinterpret_cast (Val) -# else -# define YY_CAST(Type, Val) ((Type) (Val)) -# define YY_REINTERPRET_CAST(Type, Val) ((Type) (Val)) -# endif -# endif -# ifndef YY_NULLPTR -# if defined __cplusplus -# if 201103L <= __cplusplus -# define YY_NULLPTR nullptr -# else -# define YY_NULLPTR 0 -# endif -# else -# define YY_NULLPTR ((void*)0) -# endif -# endif - -#include "parser.hpp" -/* Symbol kind. */ -enum yysymbol_kind_t -{ - YYSYMBOL_YYEMPTY = -2, - YYSYMBOL_YYEOF = 0, /* "end of file" */ - YYSYMBOL_YYerror = 1, /* error */ - YYSYMBOL_YYUNDEF = 2, /* "invalid token" */ - YYSYMBOL_CLASS = 3, /* CLASS */ - YYSYMBOL_EXTENDS = 4, /* EXTENDS */ - YYSYMBOL_IS = 5, /* IS */ - YYSYMBOL_END = 6, /* END */ - YYSYMBOL_VAR = 7, /* VAR */ - YYSYMBOL_METHOD = 8, /* METHOD */ - YYSYMBOL_THIS = 9, /* THIS */ - YYSYMBOL_RETURN = 10, /* RETURN */ - YYSYMBOL_IF = 11, /* IF */ - YYSYMBOL_THEN = 12, /* THEN */ - YYSYMBOL_ELSE = 13, /* ELSE */ - YYSYMBOL_WHILE = 14, /* WHILE */ - YYSYMBOL_LOOP = 15, /* LOOP */ - YYSYMBOL_TRUE = 16, /* TRUE */ - YYSYMBOL_FALSE = 17, /* FALSE */ - YYSYMBOL_IDENTIFIER = 18, /* IDENTIFIER */ - YYSYMBOL_INTEGER = 19, /* INTEGER */ - YYSYMBOL_REAL = 20, /* REAL */ - YYSYMBOL_STRING = 21, /* STRING */ - YYSYMBOL_SYMBOL = 22, /* SYMBOL */ - YYSYMBOL_UNKNOWN = 23, /* UNKNOWN */ - YYSYMBOL_COLON = 24, /* COLON */ - YYSYMBOL_SEMI = 25, /* SEMI */ - YYSYMBOL_LPAREN = 26, /* LPAREN */ - YYSYMBOL_RPAREN = 27, /* RPAREN */ - YYSYMBOL_COMMA = 28, /* COMMA */ - YYSYMBOL_DOT = 29, /* DOT */ - YYSYMBOL_ASSIGN = 30, /* ASSIGN */ - YYSYMBOL_ARROW = 31, /* ARROW */ - YYSYMBOL_YYACCEPT = 32, /* $accept */ - YYSYMBOL_program = 33, /* program */ - YYSYMBOL_top_list = 34, /* top_list */ - YYSYMBOL_top_item = 35, /* top_item */ - YYSYMBOL_class_decl = 36, /* class_decl */ - YYSYMBOL_class_opt_ext = 37, /* class_opt_ext */ - YYSYMBOL_class_body = 38, /* class_body */ - YYSYMBOL_class_member = 39, /* class_member */ - YYSYMBOL_var_decl = 40, /* var_decl */ - YYSYMBOL_maybe_type = 41, /* maybe_type */ - YYSYMBOL_method_decl = 42, /* method_decl */ - YYSYMBOL_method_body = 43, /* method_body */ - YYSYMBOL_method_member = 44 /* method_member */ -}; -typedef enum yysymbol_kind_t yysymbol_kind_t; - - - - -#ifdef short -# undef short -#endif - -/* On compilers that do not define __PTRDIFF_MAX__ etc., make sure - and (if available) are included - so that the code can choose integer types of a good width. */ - -#ifndef __PTRDIFF_MAX__ -# include /* INFRINGES ON USER NAME SPACE */ -# if defined __STDC_VERSION__ && 199901 <= __STDC_VERSION__ -# include /* INFRINGES ON USER NAME SPACE */ -# define YY_STDINT_H -# endif -#endif - -/* Narrow types that promote to a signed type and that can represent a - signed or unsigned integer of at least N bits. In tables they can - save space and decrease cache pressure. Promoting to a signed type - helps avoid bugs in integer arithmetic. */ - -#ifdef __INT_LEAST8_MAX__ -typedef __INT_LEAST8_TYPE__ yytype_int8; -#elif defined YY_STDINT_H -typedef int_least8_t yytype_int8; -#else -typedef signed char yytype_int8; -#endif - -#ifdef __INT_LEAST16_MAX__ -typedef __INT_LEAST16_TYPE__ yytype_int16; -#elif defined YY_STDINT_H -typedef int_least16_t yytype_int16; -#else -typedef short yytype_int16; -#endif - -/* Work around bug in HP-UX 11.23, which defines these macros - incorrectly for preprocessor constants. This workaround can likely - be removed in 2023, as HPE has promised support for HP-UX 11.23 - (aka HP-UX 11i v2) only through the end of 2022; see Table 2 of - . */ -#ifdef __hpux -# undef UINT_LEAST8_MAX -# undef UINT_LEAST16_MAX -# define UINT_LEAST8_MAX 255 -# define UINT_LEAST16_MAX 65535 -#endif - -#if defined __UINT_LEAST8_MAX__ && __UINT_LEAST8_MAX__ <= __INT_MAX__ -typedef __UINT_LEAST8_TYPE__ yytype_uint8; -#elif (!defined __UINT_LEAST8_MAX__ && defined YY_STDINT_H \ - && UINT_LEAST8_MAX <= INT_MAX) -typedef uint_least8_t yytype_uint8; -#elif !defined __UINT_LEAST8_MAX__ && UCHAR_MAX <= INT_MAX -typedef unsigned char yytype_uint8; -#else -typedef short yytype_uint8; -#endif - -#if defined __UINT_LEAST16_MAX__ && __UINT_LEAST16_MAX__ <= __INT_MAX__ -typedef __UINT_LEAST16_TYPE__ yytype_uint16; -#elif (!defined __UINT_LEAST16_MAX__ && defined YY_STDINT_H \ - && UINT_LEAST16_MAX <= INT_MAX) -typedef uint_least16_t yytype_uint16; -#elif !defined __UINT_LEAST16_MAX__ && USHRT_MAX <= INT_MAX -typedef unsigned short yytype_uint16; -#else -typedef int yytype_uint16; -#endif - -#ifndef YYPTRDIFF_T -# if defined __PTRDIFF_TYPE__ && defined __PTRDIFF_MAX__ -# define YYPTRDIFF_T __PTRDIFF_TYPE__ -# define YYPTRDIFF_MAXIMUM __PTRDIFF_MAX__ -# elif defined PTRDIFF_MAX -# ifndef ptrdiff_t -# include /* INFRINGES ON USER NAME SPACE */ -# endif -# define YYPTRDIFF_T ptrdiff_t -# define YYPTRDIFF_MAXIMUM PTRDIFF_MAX -# else -# define YYPTRDIFF_T long -# define YYPTRDIFF_MAXIMUM LONG_MAX -# endif -#endif - -#ifndef YYSIZE_T -# ifdef __SIZE_TYPE__ -# define YYSIZE_T __SIZE_TYPE__ -# elif defined size_t -# define YYSIZE_T size_t -# elif defined __STDC_VERSION__ && 199901 <= __STDC_VERSION__ -# include /* INFRINGES ON USER NAME SPACE */ -# define YYSIZE_T size_t -# else -# define YYSIZE_T unsigned -# endif -#endif - -#define YYSIZE_MAXIMUM \ - YY_CAST (YYPTRDIFF_T, \ - (YYPTRDIFF_MAXIMUM < YY_CAST (YYSIZE_T, -1) \ - ? YYPTRDIFF_MAXIMUM \ - : YY_CAST (YYSIZE_T, -1))) - -#define YYSIZEOF(X) YY_CAST (YYPTRDIFF_T, sizeof (X)) - - -/* Stored state numbers (used for stacks). */ -typedef yytype_int8 yy_state_t; - -/* State numbers in computations. */ -typedef int yy_state_fast_t; - -#ifndef YY_ -# if defined YYENABLE_NLS && YYENABLE_NLS -# if ENABLE_NLS -# include /* INFRINGES ON USER NAME SPACE */ -# define YY_(Msgid) dgettext ("bison-runtime", Msgid) -# endif -# endif -# ifndef YY_ -# define YY_(Msgid) Msgid -# endif -#endif - - -#ifndef YY_ATTRIBUTE_PURE -# if defined __GNUC__ && 2 < __GNUC__ + (96 <= __GNUC_MINOR__) -# define YY_ATTRIBUTE_PURE __attribute__ ((__pure__)) -# else -# define YY_ATTRIBUTE_PURE -# endif -#endif - -#ifndef YY_ATTRIBUTE_UNUSED -# if defined __GNUC__ && 2 < __GNUC__ + (7 <= __GNUC_MINOR__) -# define YY_ATTRIBUTE_UNUSED __attribute__ ((__unused__)) -# else -# define YY_ATTRIBUTE_UNUSED -# endif -#endif - -/* Suppress unused-variable warnings by "using" E. */ -#if ! defined lint || defined __GNUC__ -# define YY_USE(E) ((void) (E)) -#else -# define YY_USE(E) /* empty */ -#endif - -/* Suppress an incorrect diagnostic about yylval being uninitialized. */ -#if defined __GNUC__ && ! defined __ICC && 406 <= __GNUC__ * 100 + __GNUC_MINOR__ -# if __GNUC__ * 100 + __GNUC_MINOR__ < 407 -# define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN \ - _Pragma ("GCC diagnostic push") \ - _Pragma ("GCC diagnostic ignored \"-Wuninitialized\"") -# else -# define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN \ - _Pragma ("GCC diagnostic push") \ - _Pragma ("GCC diagnostic ignored \"-Wuninitialized\"") \ - _Pragma ("GCC diagnostic ignored \"-Wmaybe-uninitialized\"") -# endif -# define YY_IGNORE_MAYBE_UNINITIALIZED_END \ - _Pragma ("GCC diagnostic pop") -#else -# define YY_INITIAL_VALUE(Value) Value -#endif -#ifndef YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN -# define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN -# define YY_IGNORE_MAYBE_UNINITIALIZED_END -#endif -#ifndef YY_INITIAL_VALUE -# define YY_INITIAL_VALUE(Value) /* Nothing. */ -#endif - -#if defined __cplusplus && defined __GNUC__ && ! defined __ICC && 6 <= __GNUC__ -# define YY_IGNORE_USELESS_CAST_BEGIN \ - _Pragma ("GCC diagnostic push") \ - _Pragma ("GCC diagnostic ignored \"-Wuseless-cast\"") -# define YY_IGNORE_USELESS_CAST_END \ - _Pragma ("GCC diagnostic pop") -#endif -#ifndef YY_IGNORE_USELESS_CAST_BEGIN -# define YY_IGNORE_USELESS_CAST_BEGIN -# define YY_IGNORE_USELESS_CAST_END -#endif - - -#define YY_ASSERT(E) ((void) (0 && (E))) - -#if 1 - -/* The parser invokes alloca or malloc; define the necessary symbols. */ - -# ifdef YYSTACK_USE_ALLOCA -# if YYSTACK_USE_ALLOCA -# ifdef __GNUC__ -# define YYSTACK_ALLOC __builtin_alloca -# elif defined __BUILTIN_VA_ARG_INCR -# include /* INFRINGES ON USER NAME SPACE */ -# elif defined _AIX -# define YYSTACK_ALLOC __alloca -# elif defined _MSC_VER -# include /* INFRINGES ON USER NAME SPACE */ -# define alloca _alloca -# else -# define YYSTACK_ALLOC alloca -# if ! defined _ALLOCA_H && ! defined EXIT_SUCCESS -# include /* INFRINGES ON USER NAME SPACE */ - /* Use EXIT_SUCCESS as a witness for stdlib.h. */ -# ifndef EXIT_SUCCESS -# define EXIT_SUCCESS 0 -# endif -# endif -# endif -# endif -# endif - -# ifdef YYSTACK_ALLOC - /* Pacify GCC's 'empty if-body' warning. */ -# define YYSTACK_FREE(Ptr) do { /* empty */; } while (0) -# ifndef YYSTACK_ALLOC_MAXIMUM - /* The OS might guarantee only one guard page at the bottom of the stack, - and a page size can be as small as 4096 bytes. So we cannot safely - invoke alloca (N) if N exceeds 4096. Use a slightly smaller number - to allow for a few compiler-allocated temporary stack slots. */ -# define YYSTACK_ALLOC_MAXIMUM 4032 /* reasonable circa 2006 */ -# endif -# else -# define YYSTACK_ALLOC YYMALLOC -# define YYSTACK_FREE YYFREE -# ifndef YYSTACK_ALLOC_MAXIMUM -# define YYSTACK_ALLOC_MAXIMUM YYSIZE_MAXIMUM -# endif -# if (defined __cplusplus && ! defined EXIT_SUCCESS \ - && ! ((defined YYMALLOC || defined malloc) \ - && (defined YYFREE || defined free))) -# include /* INFRINGES ON USER NAME SPACE */ -# ifndef EXIT_SUCCESS -# define EXIT_SUCCESS 0 -# endif -# endif -# ifndef YYMALLOC -# define YYMALLOC malloc -# if ! defined malloc && ! defined EXIT_SUCCESS -void *malloc (YYSIZE_T); /* INFRINGES ON USER NAME SPACE */ -# endif -# endif -# ifndef YYFREE -# define YYFREE free -# if ! defined free && ! defined EXIT_SUCCESS -void free (void *); /* INFRINGES ON USER NAME SPACE */ -# endif -# endif -# endif -#endif /* 1 */ - -#if (! defined yyoverflow \ - && (! defined __cplusplus \ - || (defined YYSTYPE_IS_TRIVIAL && YYSTYPE_IS_TRIVIAL))) - -/* A type that is properly aligned for any stack member. */ -union yyalloc -{ - yy_state_t yyss_alloc; - YYSTYPE yyvs_alloc; -}; - -/* The size of the maximum gap between one aligned stack and the next. */ -# define YYSTACK_GAP_MAXIMUM (YYSIZEOF (union yyalloc) - 1) - -/* The size of an array large to enough to hold all stacks, each with - N elements. */ -# define YYSTACK_BYTES(N) \ - ((N) * (YYSIZEOF (yy_state_t) + YYSIZEOF (YYSTYPE)) \ - + YYSTACK_GAP_MAXIMUM) - -# define YYCOPY_NEEDED 1 - -/* Relocate STACK from its old location to the new one. The - local variables YYSIZE and YYSTACKSIZE give the old and new number of - elements in the stack, and YYPTR gives the new location of the - stack. Advance YYPTR to a properly aligned location for the next - stack. */ -# define YYSTACK_RELOCATE(Stack_alloc, Stack) \ - do \ - { \ - YYPTRDIFF_T yynewbytes; \ - YYCOPY (&yyptr->Stack_alloc, Stack, yysize); \ - Stack = &yyptr->Stack_alloc; \ - yynewbytes = yystacksize * YYSIZEOF (*Stack) + YYSTACK_GAP_MAXIMUM; \ - yyptr += yynewbytes / YYSIZEOF (*yyptr); \ - } \ - while (0) - -#endif - -#if defined YYCOPY_NEEDED && YYCOPY_NEEDED -/* Copy COUNT objects from SRC to DST. The source and destination do - not overlap. */ -# ifndef YYCOPY -# if defined __GNUC__ && 1 < __GNUC__ -# define YYCOPY(Dst, Src, Count) \ - __builtin_memcpy (Dst, Src, YY_CAST (YYSIZE_T, (Count)) * sizeof (*(Src))) -# else -# define YYCOPY(Dst, Src, Count) \ - do \ - { \ - YYPTRDIFF_T yyi; \ - for (yyi = 0; yyi < (Count); yyi++) \ - (Dst)[yyi] = (Src)[yyi]; \ - } \ - while (0) -# endif -# endif -#endif /* !YYCOPY_NEEDED */ - -/* YYFINAL -- State number of the termination state. */ -#define YYFINAL 3 -/* YYLAST -- Last index in YYTABLE. */ -#define YYLAST 26 - -/* YYNTOKENS -- Number of terminals. */ -#define YYNTOKENS 32 -/* YYNNTS -- Number of nonterminals. */ -#define YYNNTS 13 -/* YYNRULES -- Number of rules. */ -#define YYNRULES 21 -/* YYNSTATES -- Number of states. */ -#define YYNSTATES 34 - -/* YYMAXUTOK -- Last valid token kind. */ -#define YYMAXUTOK 286 - - -/* YYTRANSLATE(TOKEN-NUM) -- Symbol number corresponding to TOKEN-NUM - as returned by yylex, with out-of-bounds checking. */ -#define YYTRANSLATE(YYX) \ - (0 <= (YYX) && (YYX) <= YYMAXUTOK \ - ? YY_CAST (yysymbol_kind_t, yytranslate[YYX]) \ - : YYSYMBOL_YYUNDEF) - -/* YYTRANSLATE[TOKEN-NUM] -- Symbol number corresponding to TOKEN-NUM - as returned by yylex. */ -static const yytype_int8 yytranslate[] = -{ - 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 1, 2, 3, 4, - 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, - 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, - 25, 26, 27, 28, 29, 30, 31 -}; - -#if YYDEBUG -/* YYRLINE[YYN] -- Source line where rule number YYN was defined. */ -static const yytype_uint8 yyrline[] = -{ - 0, 55, 55, 67, 68, 75, 76, 77, 81, 95, - 96, 100, 101, 108, 109, 113, 122, 123, 127, 139, - 140, 147 -}; -#endif - -/** Accessing symbol of state STATE. */ -#define YY_ACCESSING_SYMBOL(State) YY_CAST (yysymbol_kind_t, yystos[State]) - -#if 1 -/* The user-facing name of the symbol whose (internal) number is - YYSYMBOL. No bounds checking. */ -static const char *yysymbol_name (yysymbol_kind_t yysymbol) YY_ATTRIBUTE_UNUSED; - -/* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM. - First, the terminals, then, starting at YYNTOKENS, nonterminals. */ -static const char *const yytname[] = -{ - "\"end of file\"", "error", "\"invalid token\"", "CLASS", "EXTENDS", - "IS", "END", "VAR", "METHOD", "THIS", "RETURN", "IF", "THEN", "ELSE", - "WHILE", "LOOP", "TRUE", "FALSE", "IDENTIFIER", "INTEGER", "REAL", - "STRING", "SYMBOL", "UNKNOWN", "COLON", "SEMI", "LPAREN", "RPAREN", - "COMMA", "DOT", "ASSIGN", "ARROW", "$accept", "program", "top_list", - "top_item", "class_decl", "class_opt_ext", "class_body", "class_member", - "var_decl", "maybe_type", "method_decl", "method_body", "method_member", YY_NULLPTR -}; - -static const char * -yysymbol_name (yysymbol_kind_t yysymbol) -{ - return yytname[yysymbol]; -} -#endif - -#define YYPACT_NINF (-25) - -#define yypact_value_is_default(Yyn) \ - ((Yyn) == YYPACT_NINF) - -#define YYTABLE_NINF (-1) - -#define yytable_value_is_error(Yyn) \ - 0 - -/* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing - STATE-NUM. */ -static const yytype_int8 yypact[] = -{ - -25, 4, 0, -25, -13, -6, -4, -25, -25, -25, - -25, 9, -9, -10, -1, 13, 1, -3, -7, -25, - -25, -25, -25, 16, 3, -25, -25, -25, -25, -25, - -5, -25, -25, -25 -}; - -/* YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM. - Performed when YYTABLE does not specify something else to do. Zero - means the default is an error. */ -static const yytype_int8 yydefact[] = -{ - 3, 0, 2, 1, 0, 0, 0, 4, 5, 6, - 7, 9, 16, 0, 0, 0, 0, 0, 0, 10, - 11, 17, 15, 0, 0, 19, 8, 12, 13, 14, - 0, 18, 21, 20 -}; - -/* YYPGOTO[NTERM-NUM]. */ -static const yytype_int8 yypgoto[] = -{ - -25, -25, -25, -25, -25, -25, -25, -25, -24, -25, - 2, -25, -25 -}; - -/* YYDEFGOTO[NTERM-NUM]. */ -static const yytype_int8 yydefgoto[] = -{ - 0, 1, 2, 7, 8, 15, 24, 27, 9, 17, - 10, 30, 33 -}; - -/* YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM. If - positive, shift that token. If negative, reduce the rule whose - number is the opposite. If YYTABLE_NINF, syntax error. */ -static const yytype_int8 yytable[] = -{ - 28, 31, 5, 4, 3, 11, 32, 5, 6, 26, - 5, 6, 12, 14, 13, 16, 18, 19, 20, 21, - 23, 25, 22, 0, 0, 0, 29 -}; - -static const yytype_int8 yycheck[] = -{ - 24, 6, 7, 3, 0, 18, 30, 7, 8, 6, - 7, 8, 18, 4, 18, 24, 26, 18, 5, 18, - 27, 5, 25, -1, -1, -1, 24 -}; - -/* YYSTOS[STATE-NUM] -- The symbol kind of the accessing symbol of - state STATE-NUM. */ -static const yytype_int8 yystos[] = -{ - 0, 33, 34, 0, 3, 7, 8, 35, 36, 40, - 42, 18, 18, 18, 4, 37, 24, 41, 26, 18, - 5, 18, 25, 27, 38, 5, 6, 39, 40, 42, - 43, 6, 40, 44 -}; - -/* YYR1[RULE-NUM] -- Symbol kind of the left-hand side of rule RULE-NUM. */ -static const yytype_int8 yyr1[] = -{ - 0, 32, 33, 34, 34, 35, 35, 35, 36, 37, - 37, 38, 38, 39, 39, 40, 41, 41, 42, 43, - 43, 44 -}; - -/* YYR2[RULE-NUM] -- Number of symbols on the right-hand side of rule RULE-NUM. */ -static const yytype_int8 yyr2[] = -{ - 0, 2, 1, 0, 2, 1, 1, 1, 6, 0, - 2, 0, 2, 1, 1, 4, 0, 2, 7, 0, - 2, 1 -}; - - -enum { YYENOMEM = -2 }; - -#define yyerrok (yyerrstatus = 0) -#define yyclearin (yychar = YYEMPTY) - -#define YYACCEPT goto yyacceptlab -#define YYABORT goto yyabortlab -#define YYERROR goto yyerrorlab -#define YYNOMEM goto yyexhaustedlab - - -#define YYRECOVERING() (!!yyerrstatus) - -#define YYBACKUP(Token, Value) \ - do \ - if (yychar == YYEMPTY) \ - { \ - yychar = (Token); \ - yylval = (Value); \ - YYPOPSTACK (yylen); \ - yystate = *yyssp; \ - goto yybackup; \ - } \ - else \ - { \ - yyerror (YY_("syntax error: cannot back up")); \ - YYERROR; \ - } \ - while (0) - -/* Backward compatibility with an undocumented macro. - Use YYerror or YYUNDEF. */ -#define YYERRCODE YYUNDEF - - -/* Enable debugging if requested. */ -#if YYDEBUG - -# ifndef YYFPRINTF -# include /* INFRINGES ON USER NAME SPACE */ -# define YYFPRINTF fprintf -# endif - -# define YYDPRINTF(Args) \ -do { \ - if (yydebug) \ - YYFPRINTF Args; \ -} while (0) - - - - -# define YY_SYMBOL_PRINT(Title, Kind, Value, Location) \ -do { \ - if (yydebug) \ - { \ - YYFPRINTF (stderr, "%s ", Title); \ - yy_symbol_print (stderr, \ - Kind, Value); \ - YYFPRINTF (stderr, "\n"); \ - } \ -} while (0) - - -/*-----------------------------------. -| Print this symbol's value on YYO. | -`-----------------------------------*/ - -static void -yy_symbol_value_print (FILE *yyo, - yysymbol_kind_t yykind, YYSTYPE const * const yyvaluep) -{ - FILE *yyoutput = yyo; - YY_USE (yyoutput); - if (!yyvaluep) - return; - YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN - YY_USE (yykind); - YY_IGNORE_MAYBE_UNINITIALIZED_END -} - - -/*---------------------------. -| Print this symbol on YYO. | -`---------------------------*/ - -static void -yy_symbol_print (FILE *yyo, - yysymbol_kind_t yykind, YYSTYPE const * const yyvaluep) -{ - YYFPRINTF (yyo, "%s %s (", - yykind < YYNTOKENS ? "token" : "nterm", yysymbol_name (yykind)); - - yy_symbol_value_print (yyo, yykind, yyvaluep); - YYFPRINTF (yyo, ")"); -} - -/*------------------------------------------------------------------. -| yy_stack_print -- Print the state stack from its BOTTOM up to its | -| TOP (included). | -`------------------------------------------------------------------*/ - -static void -yy_stack_print (yy_state_t *yybottom, yy_state_t *yytop) -{ - YYFPRINTF (stderr, "Stack now"); - for (; yybottom <= yytop; yybottom++) - { - int yybot = *yybottom; - YYFPRINTF (stderr, " %d", yybot); - } - YYFPRINTF (stderr, "\n"); -} - -# define YY_STACK_PRINT(Bottom, Top) \ -do { \ - if (yydebug) \ - yy_stack_print ((Bottom), (Top)); \ -} while (0) - - -/*------------------------------------------------. -| Report that the YYRULE is going to be reduced. | -`------------------------------------------------*/ - -static void -yy_reduce_print (yy_state_t *yyssp, YYSTYPE *yyvsp, - int yyrule) -{ - int yylno = yyrline[yyrule]; - int yynrhs = yyr2[yyrule]; - int yyi; - YYFPRINTF (stderr, "Reducing stack by rule %d (line %d):\n", - yyrule - 1, yylno); - /* The symbols being reduced. */ - for (yyi = 0; yyi < yynrhs; yyi++) - { - YYFPRINTF (stderr, " $%d = ", yyi + 1); - yy_symbol_print (stderr, - YY_ACCESSING_SYMBOL (+yyssp[yyi + 1 - yynrhs]), - &yyvsp[(yyi + 1) - (yynrhs)]); - YYFPRINTF (stderr, "\n"); - } -} - -# define YY_REDUCE_PRINT(Rule) \ -do { \ - if (yydebug) \ - yy_reduce_print (yyssp, yyvsp, Rule); \ -} while (0) - -/* Nonzero means print parse trace. It is left uninitialized so that - multiple parsers can coexist. */ -int yydebug; -#else /* !YYDEBUG */ -# define YYDPRINTF(Args) ((void) 0) -# define YY_SYMBOL_PRINT(Title, Kind, Value, Location) -# define YY_STACK_PRINT(Bottom, Top) -# define YY_REDUCE_PRINT(Rule) -#endif /* !YYDEBUG */ - - -/* YYINITDEPTH -- initial size of the parser's stacks. */ -#ifndef YYINITDEPTH -# define YYINITDEPTH 200 -#endif - -/* YYMAXDEPTH -- maximum size the stacks can grow to (effective only - if the built-in stack extension method is used). - - Do not make this value too large; the results are undefined if - YYSTACK_ALLOC_MAXIMUM < YYSTACK_BYTES (YYMAXDEPTH) - evaluated with infinite-precision integer arithmetic. */ - -#ifndef YYMAXDEPTH -# define YYMAXDEPTH 10000 -#endif - - -/* Context of a parse error. */ -typedef struct -{ - yy_state_t *yyssp; - yysymbol_kind_t yytoken; -} yypcontext_t; - -/* Put in YYARG at most YYARGN of the expected tokens given the - current YYCTX, and return the number of tokens stored in YYARG. If - YYARG is null, return the number of expected tokens (guaranteed to - be less than YYNTOKENS). Return YYENOMEM on memory exhaustion. - Return 0 if there are more than YYARGN expected tokens, yet fill - YYARG up to YYARGN. */ -static int -yypcontext_expected_tokens (const yypcontext_t *yyctx, - yysymbol_kind_t yyarg[], int yyargn) -{ - /* Actual size of YYARG. */ - int yycount = 0; - int yyn = yypact[+*yyctx->yyssp]; - if (!yypact_value_is_default (yyn)) - { - /* Start YYX at -YYN if negative to avoid negative indexes in - YYCHECK. In other words, skip the first -YYN actions for - this state because they are default actions. */ - int yyxbegin = yyn < 0 ? -yyn : 0; - /* Stay within bounds of both yycheck and yytname. */ - int yychecklim = YYLAST - yyn + 1; - int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS; - int yyx; - for (yyx = yyxbegin; yyx < yyxend; ++yyx) - if (yycheck[yyx + yyn] == yyx && yyx != YYSYMBOL_YYerror - && !yytable_value_is_error (yytable[yyx + yyn])) - { - if (!yyarg) - ++yycount; - else if (yycount == yyargn) - return 0; - else - yyarg[yycount++] = YY_CAST (yysymbol_kind_t, yyx); - } - } - if (yyarg && yycount == 0 && 0 < yyargn) - yyarg[0] = YYSYMBOL_YYEMPTY; - return yycount; -} - - - - -#ifndef yystrlen -# if defined __GLIBC__ && defined _STRING_H -# define yystrlen(S) (YY_CAST (YYPTRDIFF_T, strlen (S))) -# else -/* Return the length of YYSTR. */ -static YYPTRDIFF_T -yystrlen (const char *yystr) -{ - YYPTRDIFF_T yylen; - for (yylen = 0; yystr[yylen]; yylen++) - continue; - return yylen; -} -# endif -#endif - -#ifndef yystpcpy -# if defined __GLIBC__ && defined _STRING_H && defined _GNU_SOURCE -# define yystpcpy stpcpy -# else -/* Copy YYSRC to YYDEST, returning the address of the terminating '\0' in - YYDEST. */ -static char * -yystpcpy (char *yydest, const char *yysrc) -{ - char *yyd = yydest; - const char *yys = yysrc; - - while ((*yyd++ = *yys++) != '\0') - continue; - - return yyd - 1; -} -# endif -#endif - -#ifndef yytnamerr -/* Copy to YYRES the contents of YYSTR after stripping away unnecessary - quotes and backslashes, so that it's suitable for yyerror. The - heuristic is that double-quoting is unnecessary unless the string - contains an apostrophe, a comma, or backslash (other than - backslash-backslash). YYSTR is taken from yytname. If YYRES is - null, do not copy; instead, return the length of what the result - would have been. */ -static YYPTRDIFF_T -yytnamerr (char *yyres, const char *yystr) -{ - if (*yystr == '"') - { - YYPTRDIFF_T yyn = 0; - char const *yyp = yystr; - for (;;) - switch (*++yyp) - { - case '\'': - case ',': - goto do_not_strip_quotes; - - case '\\': - if (*++yyp != '\\') - goto do_not_strip_quotes; - else - goto append; - - append: - default: - if (yyres) - yyres[yyn] = *yyp; - yyn++; - break; - - case '"': - if (yyres) - yyres[yyn] = '\0'; - return yyn; - } - do_not_strip_quotes: ; - } - - if (yyres) - return yystpcpy (yyres, yystr) - yyres; - else - return yystrlen (yystr); -} -#endif - - -static int -yy_syntax_error_arguments (const yypcontext_t *yyctx, - yysymbol_kind_t yyarg[], int yyargn) -{ - /* Actual size of YYARG. */ - int yycount = 0; - /* There are many possibilities here to consider: - - If this state is a consistent state with a default action, then - the only way this function was invoked is if the default action - is an error action. In that case, don't check for expected - tokens because there are none. - - The only way there can be no lookahead present (in yychar) is if - this state is a consistent state with a default action. Thus, - detecting the absence of a lookahead is sufficient to determine - that there is no unexpected or expected token to report. In that - case, just report a simple "syntax error". - - Don't assume there isn't a lookahead just because this state is a - consistent state with a default action. There might have been a - previous inconsistent state, consistent state with a non-default - action, or user semantic action that manipulated yychar. - - Of course, the expected token list depends on states to have - correct lookahead information, and it depends on the parser not - to perform extra reductions after fetching a lookahead from the - scanner and before detecting a syntax error. Thus, state merging - (from LALR or IELR) and default reductions corrupt the expected - token list. However, the list is correct for canonical LR with - one exception: it will still contain any token that will not be - accepted due to an error action in a later state. - */ - if (yyctx->yytoken != YYSYMBOL_YYEMPTY) - { - int yyn; - if (yyarg) - yyarg[yycount] = yyctx->yytoken; - ++yycount; - yyn = yypcontext_expected_tokens (yyctx, - yyarg ? yyarg + 1 : yyarg, yyargn - 1); - if (yyn == YYENOMEM) - return YYENOMEM; - else - yycount += yyn; - } - return yycount; -} - -/* Copy into *YYMSG, which is of size *YYMSG_ALLOC, an error message - about the unexpected token YYTOKEN for the state stack whose top is - YYSSP. - - Return 0 if *YYMSG was successfully written. Return -1 if *YYMSG is - not large enough to hold the message. In that case, also set - *YYMSG_ALLOC to the required number of bytes. Return YYENOMEM if the - required number of bytes is too large to store. */ -static int -yysyntax_error (YYPTRDIFF_T *yymsg_alloc, char **yymsg, - const yypcontext_t *yyctx) -{ - enum { YYARGS_MAX = 5 }; - /* Internationalized format string. */ - const char *yyformat = YY_NULLPTR; - /* Arguments of yyformat: reported tokens (one for the "unexpected", - one per "expected"). */ - yysymbol_kind_t yyarg[YYARGS_MAX]; - /* Cumulated lengths of YYARG. */ - YYPTRDIFF_T yysize = 0; - - /* Actual size of YYARG. */ - int yycount = yy_syntax_error_arguments (yyctx, yyarg, YYARGS_MAX); - if (yycount == YYENOMEM) - return YYENOMEM; - - switch (yycount) - { -#define YYCASE_(N, S) \ - case N: \ - yyformat = S; \ - break - default: /* Avoid compiler warnings. */ - YYCASE_(0, YY_("syntax error")); - YYCASE_(1, YY_("syntax error, unexpected %s")); - YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s")); - YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s")); - YYCASE_(4, YY_("syntax error, unexpected %s, expecting %s or %s or %s")); - YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s")); -#undef YYCASE_ - } - - /* Compute error message size. Don't count the "%s"s, but reserve - room for the terminator. */ - yysize = yystrlen (yyformat) - 2 * yycount + 1; - { - int yyi; - for (yyi = 0; yyi < yycount; ++yyi) - { - YYPTRDIFF_T yysize1 - = yysize + yytnamerr (YY_NULLPTR, yytname[yyarg[yyi]]); - if (yysize <= yysize1 && yysize1 <= YYSTACK_ALLOC_MAXIMUM) - yysize = yysize1; - else - return YYENOMEM; - } - } - - if (*yymsg_alloc < yysize) - { - *yymsg_alloc = 2 * yysize; - if (! (yysize <= *yymsg_alloc - && *yymsg_alloc <= YYSTACK_ALLOC_MAXIMUM)) - *yymsg_alloc = YYSTACK_ALLOC_MAXIMUM; - return -1; - } - - /* Avoid sprintf, as that infringes on the user's name space. - Don't have undefined behavior even if the translation - produced a string with the wrong number of "%s"s. */ - { - char *yyp = *yymsg; - int yyi = 0; - while ((*yyp = *yyformat) != '\0') - if (*yyp == '%' && yyformat[1] == 's' && yyi < yycount) - { - yyp += yytnamerr (yyp, yytname[yyarg[yyi++]]); - yyformat += 2; - } - else - { - ++yyp; - ++yyformat; - } - } - return 0; -} - - -/*-----------------------------------------------. -| Release the memory associated to this symbol. | -`-----------------------------------------------*/ - -static void -yydestruct (const char *yymsg, - yysymbol_kind_t yykind, YYSTYPE *yyvaluep) -{ - YY_USE (yyvaluep); - if (!yymsg) - yymsg = "Deleting"; - YY_SYMBOL_PRINT (yymsg, yykind, yyvaluep, yylocationp); - - YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN - YY_USE (yykind); - YY_IGNORE_MAYBE_UNINITIALIZED_END -} - - -/* Lookahead token kind. */ -int yychar; - -/* The semantic value of the lookahead symbol. */ -YYSTYPE yylval; -/* Number of syntax errors so far. */ -int yynerrs; - - - - -/*----------. -| yyparse. | -`----------*/ - -int -yyparse (void) -{ - yy_state_fast_t yystate = 0; - /* Number of tokens to shift before error messages enabled. */ - int yyerrstatus = 0; - - /* Refer to the stacks through separate pointers, to allow yyoverflow - to reallocate them elsewhere. */ - - /* Their size. */ - YYPTRDIFF_T yystacksize = YYINITDEPTH; - - /* The state stack: array, bottom, top. */ - yy_state_t yyssa[YYINITDEPTH]; - yy_state_t *yyss = yyssa; - yy_state_t *yyssp = yyss; - - /* The semantic value stack: array, bottom, top. */ - YYSTYPE yyvsa[YYINITDEPTH]; - YYSTYPE *yyvs = yyvsa; - YYSTYPE *yyvsp = yyvs; - - int yyn; - /* The return value of yyparse. */ - int yyresult; - /* Lookahead symbol kind. */ - yysymbol_kind_t yytoken = YYSYMBOL_YYEMPTY; - /* The variables used to return semantic value and location from the - action routines. */ - YYSTYPE yyval; - - /* Buffer for error messages, and its allocated size. */ - char yymsgbuf[128]; - char *yymsg = yymsgbuf; - YYPTRDIFF_T yymsg_alloc = sizeof yymsgbuf; - -#define YYPOPSTACK(N) (yyvsp -= (N), yyssp -= (N)) - - /* The number of symbols on the RHS of the reduced rule. - Keep to zero when no symbol should be popped. */ - int yylen = 0; - - YYDPRINTF ((stderr, "Starting parse\n")); - - yychar = YYEMPTY; /* Cause a token to be read. */ - - goto yysetstate; - - -/*------------------------------------------------------------. -| yynewstate -- push a new state, which is found in yystate. | -`------------------------------------------------------------*/ -yynewstate: - /* In all cases, when you get here, the value and location stacks - have just been pushed. So pushing a state here evens the stacks. */ - yyssp++; - - -/*--------------------------------------------------------------------. -| yysetstate -- set current state (the top of the stack) to yystate. | -`--------------------------------------------------------------------*/ -yysetstate: - YYDPRINTF ((stderr, "Entering state %d\n", yystate)); - YY_ASSERT (0 <= yystate && yystate < YYNSTATES); - YY_IGNORE_USELESS_CAST_BEGIN - *yyssp = YY_CAST (yy_state_t, yystate); - YY_IGNORE_USELESS_CAST_END - YY_STACK_PRINT (yyss, yyssp); - - if (yyss + yystacksize - 1 <= yyssp) -#if !defined yyoverflow && !defined YYSTACK_RELOCATE - YYNOMEM; -#else - { - /* Get the current used size of the three stacks, in elements. */ - YYPTRDIFF_T yysize = yyssp - yyss + 1; - -# if defined yyoverflow - { - /* Give user a chance to reallocate the stack. Use copies of - these so that the &'s don't force the real ones into - memory. */ - yy_state_t *yyss1 = yyss; - YYSTYPE *yyvs1 = yyvs; - - /* Each stack pointer address is followed by the size of the - data in use in that stack, in bytes. This used to be a - conditional around just the two extra args, but that might - be undefined if yyoverflow is a macro. */ - yyoverflow (YY_("memory exhausted"), - &yyss1, yysize * YYSIZEOF (*yyssp), - &yyvs1, yysize * YYSIZEOF (*yyvsp), - &yystacksize); - yyss = yyss1; - yyvs = yyvs1; - } -# else /* defined YYSTACK_RELOCATE */ - /* Extend the stack our own way. */ - if (YYMAXDEPTH <= yystacksize) - YYNOMEM; - yystacksize *= 2; - if (YYMAXDEPTH < yystacksize) - yystacksize = YYMAXDEPTH; - - { - yy_state_t *yyss1 = yyss; - union yyalloc *yyptr = - YY_CAST (union yyalloc *, - YYSTACK_ALLOC (YY_CAST (YYSIZE_T, YYSTACK_BYTES (yystacksize)))); - if (! yyptr) - YYNOMEM; - YYSTACK_RELOCATE (yyss_alloc, yyss); - YYSTACK_RELOCATE (yyvs_alloc, yyvs); -# undef YYSTACK_RELOCATE - if (yyss1 != yyssa) - YYSTACK_FREE (yyss1); - } -# endif - - yyssp = yyss + yysize - 1; - yyvsp = yyvs + yysize - 1; - - YY_IGNORE_USELESS_CAST_BEGIN - YYDPRINTF ((stderr, "Stack size increased to %ld\n", - YY_CAST (long, yystacksize))); - YY_IGNORE_USELESS_CAST_END - - if (yyss + yystacksize - 1 <= yyssp) - YYABORT; - } -#endif /* !defined yyoverflow && !defined YYSTACK_RELOCATE */ - - - if (yystate == YYFINAL) - YYACCEPT; - - goto yybackup; - - -/*-----------. -| yybackup. | -`-----------*/ -yybackup: - /* Do appropriate processing given the current state. Read a - lookahead token if we need one and don't already have one. */ - - /* First try to decide what to do without reference to lookahead token. */ - yyn = yypact[yystate]; - if (yypact_value_is_default (yyn)) - goto yydefault; - - /* Not known => get a lookahead token if don't already have one. */ - - /* YYCHAR is either empty, or end-of-input, or a valid lookahead. */ - if (yychar == YYEMPTY) - { - YYDPRINTF ((stderr, "Reading a token\n")); - yychar = yylex (); - } - - if (yychar <= YYEOF) - { - yychar = YYEOF; - yytoken = YYSYMBOL_YYEOF; - YYDPRINTF ((stderr, "Now at end of input.\n")); - } - else if (yychar == YYerror) - { - /* The scanner already issued an error message, process directly - to error recovery. But do not keep the error token as - lookahead, it is too special and may lead us to an endless - loop in error recovery. */ - yychar = YYUNDEF; - yytoken = YYSYMBOL_YYerror; - goto yyerrlab1; - } - else - { - yytoken = YYTRANSLATE (yychar); - YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc); - } - - /* If the proper action on seeing token YYTOKEN is to reduce or to - detect an error, take that action. */ - yyn += yytoken; - if (yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken) - goto yydefault; - yyn = yytable[yyn]; - if (yyn <= 0) - { - if (yytable_value_is_error (yyn)) - goto yyerrlab; - yyn = -yyn; - goto yyreduce; - } - - /* Count tokens shifted since error; after three, turn off error - status. */ - if (yyerrstatus) - yyerrstatus--; - - /* Shift the lookahead token. */ - YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc); - yystate = yyn; - YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN - *++yyvsp = yylval; - YY_IGNORE_MAYBE_UNINITIALIZED_END - - /* Discard the shifted token. */ - yychar = YYEMPTY; - goto yynewstate; - - -/*-----------------------------------------------------------. -| yydefault -- do the default action for the current state. | -`-----------------------------------------------------------*/ -yydefault: - yyn = yydefact[yystate]; - if (yyn == 0) - goto yyerrlab; - goto yyreduce; - - -/*-----------------------------. -| yyreduce -- do a reduction. | -`-----------------------------*/ -yyreduce: - /* yyn is the number of a rule to reduce with. */ - yylen = yyr2[yyn]; - - /* If YYLEN is nonzero, implement the default value of the action: - '$$ = $1'. - - Otherwise, the following line sets YYVAL to garbage. - This behavior is undocumented and Bison - users should not rely upon it. Assigning to YYVAL - unconditionally makes the parser a bit smaller, and it avoids a - GCC warning that YYVAL may be used uninitialized. */ - yyval = yyvsp[1-yylen]; - - - YY_REDUCE_PRINT (yyn); - switch (yyn) - { - case 2: /* program: top_list */ -#line 55 "parser.y" - { - g_program = make_unique(); - if ((yyvsp[0].vec)) { - for (ASTNode* p : *(yyvsp[0].vec)) { - g_program->decls.emplace_back(unique_ptr(p)); - } - delete (yyvsp[0].vec); - } - } -#line 1423 "parser.cpp" - break; - - case 3: /* top_list: %empty */ -#line 67 "parser.y" - { (yyval.vec) = new vector(); } -#line 1429 "parser.cpp" - break; - - case 4: /* top_list: top_list top_item */ -#line 68 "parser.y" - { - (yyval.vec) = (yyvsp[-1].vec); - if ((yyvsp[0].node)) (yyval.vec)->push_back((yyvsp[0].node)); - } -#line 1438 "parser.cpp" - break; - - case 5: /* top_item: class_decl */ -#line 75 "parser.y" - { (yyval.node) = (yyvsp[0].node); } -#line 1444 "parser.cpp" - break; - - case 6: /* top_item: var_decl */ -#line 76 "parser.y" - { (yyval.node) = (yyvsp[0].node); } -#line 1450 "parser.cpp" - break; - - case 7: /* top_item: method_decl */ -#line 77 "parser.y" - { (yyval.node) = (yyvsp[0].node); } -#line 1456 "parser.cpp" - break; - - case 8: /* class_decl: CLASS IDENTIFIER class_opt_ext IS class_body END */ -#line 81 "parser.y" - { - auto cn = new ClassNode((yyvsp[-4].str) ? string((yyvsp[-4].str)) : string()); - if ((yyvsp[-3].str)) cn->extendsName = string((yyvsp[-3].str)); - if ((yyvsp[-1].vec)) { - for (ASTNode* m : *(yyvsp[-1].vec)) cn->members.emplace_back(unique_ptr(m)); - delete (yyvsp[-1].vec); - } - if ((yyvsp[-4].str)) free((yyvsp[-4].str)); - if ((yyvsp[-3].str)) free((yyvsp[-3].str)); - (yyval.node) = cn; - } -#line 1472 "parser.cpp" - break; - - case 9: /* class_opt_ext: %empty */ -#line 95 "parser.y" - { (yyval.str) = nullptr; } -#line 1478 "parser.cpp" - break; - - case 10: /* class_opt_ext: EXTENDS IDENTIFIER */ -#line 96 "parser.y" - { (yyval.str) = (yyvsp[0].str); } -#line 1484 "parser.cpp" - break; - - case 11: /* class_body: %empty */ -#line 100 "parser.y" - { (yyval.vec) = new vector(); } -#line 1490 "parser.cpp" - break; - - case 12: /* class_body: class_body class_member */ -#line 101 "parser.y" - { - (yyval.vec) = (yyvsp[-1].vec); - if ((yyvsp[0].node)) (yyval.vec)->push_back((yyvsp[0].node)); - } -#line 1499 "parser.cpp" - break; - - case 13: /* class_member: var_decl */ -#line 108 "parser.y" - { (yyval.node) = (yyvsp[0].node); } -#line 1505 "parser.cpp" - break; - - case 14: /* class_member: method_decl */ -#line 109 "parser.y" - { (yyval.node) = (yyvsp[0].node); } -#line 1511 "parser.cpp" - break; - - case 15: /* var_decl: VAR IDENTIFIER maybe_type SEMI */ -#line 113 "parser.y" - { - auto vn = new VarNode((yyvsp[-2].str) ? string((yyvsp[-2].str)) : string(), (yyvsp[-1].str) ? string((yyvsp[-1].str)) : string()); - if ((yyvsp[-2].str)) free((yyvsp[-2].str)); - if ((yyvsp[-1].str)) free((yyvsp[-1].str)); - (yyval.node) = vn; - } -#line 1522 "parser.cpp" - break; - - case 16: /* maybe_type: %empty */ -#line 122 "parser.y" - { (yyval.str) = nullptr; } -#line 1528 "parser.cpp" - break; - - case 17: /* maybe_type: COLON IDENTIFIER */ -#line 123 "parser.y" - { (yyval.str) = (yyvsp[0].str); } -#line 1534 "parser.cpp" - break; - - case 18: /* method_decl: METHOD IDENTIFIER LPAREN RPAREN IS method_body END */ -#line 127 "parser.y" - { - auto mn = new MethodNode((yyvsp[-5].str) ? string((yyvsp[-5].str)) : string()); - if ((yyvsp[-1].vec)) { - for (ASTNode* b : *(yyvsp[-1].vec)) mn->body.emplace_back(unique_ptr(b)); - delete (yyvsp[-1].vec); - } - if ((yyvsp[-5].str)) free((yyvsp[-5].str)); - (yyval.node) = mn; - } -#line 1548 "parser.cpp" - break; - - case 19: /* method_body: %empty */ -#line 139 "parser.y" - { (yyval.vec) = new vector(); } -#line 1554 "parser.cpp" - break; - - case 20: /* method_body: method_body method_member */ -#line 140 "parser.y" - { - (yyval.vec) = (yyvsp[-1].vec); - if ((yyvsp[0].node)) (yyval.vec)->push_back((yyvsp[0].node)); - } -#line 1563 "parser.cpp" - break; - - case 21: /* method_member: var_decl */ -#line 147 "parser.y" - { (yyval.node) = (yyvsp[0].node); } -#line 1569 "parser.cpp" - break; - - -#line 1573 "parser.cpp" - - default: break; - } - /* User semantic actions sometimes alter yychar, and that requires - that yytoken be updated with the new translation. We take the - approach of translating immediately before every use of yytoken. - One alternative is translating here after every semantic action, - but that translation would be missed if the semantic action invokes - YYABORT, YYACCEPT, or YYERROR immediately after altering yychar or - if it invokes YYBACKUP. In the case of YYABORT or YYACCEPT, an - incorrect destructor might then be invoked immediately. In the - case of YYERROR or YYBACKUP, subsequent parser actions might lead - to an incorrect destructor call or verbose syntax error message - before the lookahead is translated. */ - YY_SYMBOL_PRINT ("-> $$ =", YY_CAST (yysymbol_kind_t, yyr1[yyn]), &yyval, &yyloc); - - YYPOPSTACK (yylen); - yylen = 0; - - *++yyvsp = yyval; - - /* Now 'shift' the result of the reduction. Determine what state - that goes to, based on the state we popped back to and the rule - number reduced by. */ - { - const int yylhs = yyr1[yyn] - YYNTOKENS; - const int yyi = yypgoto[yylhs] + *yyssp; - yystate = (0 <= yyi && yyi <= YYLAST && yycheck[yyi] == *yyssp - ? yytable[yyi] - : yydefgoto[yylhs]); - } - - goto yynewstate; - - -/*--------------------------------------. -| yyerrlab -- here on detecting error. | -`--------------------------------------*/ -yyerrlab: - /* Make sure we have latest lookahead translation. See comments at - user semantic actions for why this is necessary. */ - yytoken = yychar == YYEMPTY ? YYSYMBOL_YYEMPTY : YYTRANSLATE (yychar); - /* If not already recovering from an error, report this error. */ - if (!yyerrstatus) - { - ++yynerrs; - { - yypcontext_t yyctx - = {yyssp, yytoken}; - char const *yymsgp = YY_("syntax error"); - int yysyntax_error_status; - yysyntax_error_status = yysyntax_error (&yymsg_alloc, &yymsg, &yyctx); - if (yysyntax_error_status == 0) - yymsgp = yymsg; - else if (yysyntax_error_status == -1) - { - if (yymsg != yymsgbuf) - YYSTACK_FREE (yymsg); - yymsg = YY_CAST (char *, - YYSTACK_ALLOC (YY_CAST (YYSIZE_T, yymsg_alloc))); - if (yymsg) - { - yysyntax_error_status - = yysyntax_error (&yymsg_alloc, &yymsg, &yyctx); - yymsgp = yymsg; - } - else - { - yymsg = yymsgbuf; - yymsg_alloc = sizeof yymsgbuf; - yysyntax_error_status = YYENOMEM; - } - } - yyerror (yymsgp); - if (yysyntax_error_status == YYENOMEM) - YYNOMEM; - } - } - - if (yyerrstatus == 3) - { - /* If just tried and failed to reuse lookahead token after an - error, discard it. */ - - if (yychar <= YYEOF) - { - /* Return failure if at end of input. */ - if (yychar == YYEOF) - YYABORT; - } - else - { - yydestruct ("Error: discarding", - yytoken, &yylval); - yychar = YYEMPTY; - } - } - - /* Else will try to reuse lookahead token after shifting the error - token. */ - goto yyerrlab1; - - -/*---------------------------------------------------. -| yyerrorlab -- error raised explicitly by YYERROR. | -`---------------------------------------------------*/ -yyerrorlab: - /* Pacify compilers when the user code never invokes YYERROR and the - label yyerrorlab therefore never appears in user code. */ - if (0) - YYERROR; - ++yynerrs; - - /* Do not reclaim the symbols of the rule whose action triggered - this YYERROR. */ - YYPOPSTACK (yylen); - yylen = 0; - YY_STACK_PRINT (yyss, yyssp); - yystate = *yyssp; - goto yyerrlab1; - - -/*-------------------------------------------------------------. -| yyerrlab1 -- common code for both syntax error and YYERROR. | -`-------------------------------------------------------------*/ -yyerrlab1: - yyerrstatus = 3; /* Each real token shifted decrements this. */ - - /* Pop stack until we find a state that shifts the error token. */ - for (;;) - { - yyn = yypact[yystate]; - if (!yypact_value_is_default (yyn)) - { - yyn += YYSYMBOL_YYerror; - if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYSYMBOL_YYerror) - { - yyn = yytable[yyn]; - if (0 < yyn) - break; - } - } - - /* Pop the current state because it cannot handle the error token. */ - if (yyssp == yyss) - YYABORT; - - - yydestruct ("Error: popping", - YY_ACCESSING_SYMBOL (yystate), yyvsp); - YYPOPSTACK (1); - yystate = *yyssp; - YY_STACK_PRINT (yyss, yyssp); - } - - YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN - *++yyvsp = yylval; - YY_IGNORE_MAYBE_UNINITIALIZED_END - - - /* Shift the error token. */ - YY_SYMBOL_PRINT ("Shifting", YY_ACCESSING_SYMBOL (yyn), yyvsp, yylsp); - - yystate = yyn; - goto yynewstate; - - -/*-------------------------------------. -| yyacceptlab -- YYACCEPT comes here. | -`-------------------------------------*/ -yyacceptlab: - yyresult = 0; - goto yyreturnlab; - - -/*-----------------------------------. -| yyabortlab -- YYABORT comes here. | -`-----------------------------------*/ -yyabortlab: - yyresult = 1; - goto yyreturnlab; - - -/*-----------------------------------------------------------. -| yyexhaustedlab -- YYNOMEM (memory exhaustion) comes here. | -`-----------------------------------------------------------*/ -yyexhaustedlab: - yyerror (YY_("memory exhausted")); - yyresult = 2; - goto yyreturnlab; - - -/*----------------------------------------------------------. -| yyreturnlab -- parsing is finished, clean up and return. | -`----------------------------------------------------------*/ -yyreturnlab: - if (yychar != YYEMPTY) - { - /* Make sure we have latest lookahead translation. See comments at - user semantic actions for why this is necessary. */ - yytoken = YYTRANSLATE (yychar); - yydestruct ("Cleanup: discarding lookahead", - yytoken, &yylval); - } - /* Do not reclaim the symbols of the rule whose action triggered - this YYABORT or YYACCEPT. */ - YYPOPSTACK (yylen); - YY_STACK_PRINT (yyss, yyssp); - while (yyssp != yyss) - { - yydestruct ("Cleanup: popping", - YY_ACCESSING_SYMBOL (+*yyssp), yyvsp); - YYPOPSTACK (1); - } -#ifndef yyoverflow - if (yyss != yyssa) - YYSTACK_FREE (yyss); -#endif - if (yymsg != yymsgbuf) - YYSTACK_FREE (yymsg); - return yyresult; -} - -#line 150 "parser.y" - - -int yylex(void); diff --git a/parser.hpp b/parser.hpp deleted file mode 100644 index ef1c428..0000000 --- a/parser.hpp +++ /dev/null @@ -1,122 +0,0 @@ -/* A Bison parser, made by GNU Bison 3.8.2. */ - -/* Bison interface for Yacc-like parsers in C - - Copyright (C) 1984, 1989-1990, 2000-2015, 2018-2021 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 3 of the License, 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, see . */ - -/* 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. */ - -/* DO NOT RELY ON FEATURES THAT ARE NOT DOCUMENTED in the manual, - especially those whose name start with YY_ or yy_. They are - private implementation details that can be changed or removed. */ - -#ifndef YY_YY_PARSER_HPP_INCLUDED -# define YY_YY_PARSER_HPP_INCLUDED -/* Debug traces. */ -#ifndef YYDEBUG -# define YYDEBUG 0 -#endif -#if YYDEBUG -extern int yydebug; -#endif -/* "%code requires" blocks. */ -#line 31 "parser.y" - - #include - class ASTNode; // forward declaration - -#line 54 "parser.hpp" - -/* Token kinds. */ -#ifndef YYTOKENTYPE -# define YYTOKENTYPE - enum yytokentype - { - YYEMPTY = -2, - YYEOF = 0, /* "end of file" */ - YYerror = 256, /* error */ - YYUNDEF = 257, /* "invalid token" */ - CLASS = 258, /* CLASS */ - EXTENDS = 259, /* EXTENDS */ - IS = 260, /* IS */ - END = 261, /* END */ - VAR = 262, /* VAR */ - METHOD = 263, /* METHOD */ - THIS = 264, /* THIS */ - RETURN = 265, /* RETURN */ - IF = 266, /* IF */ - THEN = 267, /* THEN */ - ELSE = 268, /* ELSE */ - WHILE = 269, /* WHILE */ - LOOP = 270, /* LOOP */ - TRUE = 271, /* TRUE */ - FALSE = 272, /* FALSE */ - IDENTIFIER = 273, /* IDENTIFIER */ - INTEGER = 274, /* INTEGER */ - REAL = 275, /* REAL */ - STRING = 276, /* STRING */ - SYMBOL = 277, /* SYMBOL */ - UNKNOWN = 278, /* UNKNOWN */ - COLON = 279, /* COLON */ - SEMI = 280, /* SEMI */ - LPAREN = 281, /* LPAREN */ - RPAREN = 282, /* RPAREN */ - COMMA = 283, /* COMMA */ - DOT = 284, /* DOT */ - ASSIGN = 285, /* ASSIGN */ - ARROW = 286 /* ARROW */ - }; - typedef enum yytokentype yytoken_kind_t; -#endif - -/* Value type. */ -#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED -union YYSTYPE -{ -#line 36 "parser.y" - - char* str; - ASTNode* node; - std::vector* vec; - -#line 108 "parser.hpp" - -}; -typedef union YYSTYPE YYSTYPE; -# define YYSTYPE_IS_TRIVIAL 1 -# define YYSTYPE_IS_DECLARED 1 -#endif - - -extern YYSTYPE yylval; - - -int yyparse (void); - - -#endif /* !YY_YY_PARSER_HPP_INCLUDED */ diff --git a/parser.tab.cc b/parser.tab.cc deleted file mode 100644 index 8fa0537..0000000 --- a/parser.tab.cc +++ /dev/null @@ -1,1279 +0,0 @@ -// A Bison parser, made by GNU Bison 3.8.2. - -// Skeleton implementation for Bison LALR(1) parsers in C++ - -// Copyright (C) 2002-2015, 2018-2021 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 3 of the License, 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, see . - -// 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. - -// DO NOT RELY ON FEATURES THAT ARE NOT DOCUMENTED in the manual, -// especially those whose name start with YY_ or yy_. They are -// private implementation details that can be changed or removed. - - - -// First part of user prologue. -#line 7 "parser.y" - -/* C++ includes and AST definitions */ -#include -#include -#include -#include -#include /* strdup, free */ -using namespace std; - -/* --- AST classes (C++ style) --- */ -struct ASTNode { - virtual ~ASTNode() = default; - virtual void print(int indent=0) const = 0; -}; - -using ASTNodePtr = unique_ptr; - -static string indentStr(int n) { return string(n, ' '); } - -struct ProgramNode : ASTNode { - vector decls; - void print(int indent=0) const override { - cout << indentStr(indent) << "Program\n"; - for (auto &d : decls) d->print(indent+2); - } -}; - -struct ClassNode : ASTNode { - string name; - string extendsName; - vector members; - ClassNode(const string &n="") : name(n) {} - void print(int indent=0) const override { - cout << indentStr(indent) << "Class: " << name; - if (!extendsName.empty()) cout << " extends " << extendsName; - cout << "\n"; - for (auto &m : members) m->print(indent+2); - } -}; - -struct VarNode : ASTNode { - string name; - string type; - VarNode(const string &n="", const string &t="") : name(n), type(t) {} - void print(int indent=0) const override { - cout << indentStr(indent) << "Var: " << name; - if (!type.empty()) cout << " : " << type; - cout << "\n"; - } -}; - -struct MethodNode : ASTNode { - string name; - vector params; - vector body; - MethodNode(const string &n="") : name(n) {} - void print(int indent=0) const override { - cout << indentStr(indent) << "Method: " << name << "\n"; - for (auto &b : body) b->print(indent+2); - } -}; - -/* глобальный root AST (используется в main.cpp) */ -std::unique_ptr g_program = nullptr; - -/* Forward: лексер предоставляет этот тип и вектор simpleTokens */ -struct SimpleToken { std::string kind; std::string text; int line; int startCol; int endCol; }; -extern std::vector simpleTokens; /* defined in lexer.l */ - -/* индекс для доступа к simpleTokens из yylex() */ -static size_t parserTokIndex = 0; - - -#line 115 "parser.tab.cc" - - -#include "parser.tab.hh" - - - - -#ifndef YY_ -# if defined YYENABLE_NLS && YYENABLE_NLS -# if ENABLE_NLS -# include // FIXME: INFRINGES ON USER NAME SPACE. -# define YY_(msgid) dgettext ("bison-runtime", msgid) -# endif -# endif -# ifndef YY_ -# define YY_(msgid) msgid -# endif -#endif - - -// Whether we are compiled with exception support. -#ifndef YY_EXCEPTIONS -# if defined __GNUC__ && !defined __EXCEPTIONS -# define YY_EXCEPTIONS 0 -# else -# define YY_EXCEPTIONS 1 -# endif -#endif - - - -// Enable debugging if requested. -#if YYDEBUG - -// A pseudo ostream that takes yydebug_ into account. -# define YYCDEBUG if (yydebug_) (*yycdebug_) - -# define YY_SYMBOL_PRINT(Title, Symbol) \ - do { \ - if (yydebug_) \ - { \ - *yycdebug_ << Title << ' '; \ - yy_print_ (*yycdebug_, Symbol); \ - *yycdebug_ << '\n'; \ - } \ - } while (false) - -# define YY_REDUCE_PRINT(Rule) \ - do { \ - if (yydebug_) \ - yy_reduce_print_ (Rule); \ - } while (false) - -# define YY_STACK_PRINT() \ - do { \ - if (yydebug_) \ - yy_stack_print_ (); \ - } while (false) - -#else // !YYDEBUG - -# define YYCDEBUG if (false) std::cerr -# define YY_SYMBOL_PRINT(Title, Symbol) YY_USE (Symbol) -# define YY_REDUCE_PRINT(Rule) static_cast (0) -# define YY_STACK_PRINT() static_cast (0) - -#endif // !YYDEBUG - -#define yyerrok (yyerrstatus_ = 0) -#define yyclearin (yyla.clear ()) - -#define YYACCEPT goto yyacceptlab -#define YYABORT goto yyabortlab -#define YYERROR goto yyerrorlab -#define YYRECOVERING() (!!yyerrstatus_) - -namespace yy { -#line 193 "parser.tab.cc" - - /// Build a parser object. - parser::parser () -#if YYDEBUG - : yydebug_ (false), - yycdebug_ (&std::cerr) -#else - -#endif - {} - - parser::~parser () - {} - - parser::syntax_error::~syntax_error () YY_NOEXCEPT YY_NOTHROW - {} - - /*---------. - | symbol. | - `---------*/ - - // basic_symbol. - template - parser::basic_symbol::basic_symbol (const basic_symbol& that) - : Base (that) - , value (that.value) - {} - - - /// Constructor for valueless symbols. - template - parser::basic_symbol::basic_symbol (typename Base::kind_type t) - : Base (t) - , value () - {} - - template - parser::basic_symbol::basic_symbol (typename Base::kind_type t, YY_RVREF (value_type) v) - : Base (t) - , value (YY_MOVE (v)) - {} - - - template - parser::symbol_kind_type - parser::basic_symbol::type_get () const YY_NOEXCEPT - { - return this->kind (); - } - - - template - bool - parser::basic_symbol::empty () const YY_NOEXCEPT - { - return this->kind () == symbol_kind::S_YYEMPTY; - } - - template - void - parser::basic_symbol::move (basic_symbol& s) - { - super_type::move (s); - value = YY_MOVE (s.value); - } - - // by_kind. - parser::by_kind::by_kind () YY_NOEXCEPT - : kind_ (symbol_kind::S_YYEMPTY) - {} - -#if 201103L <= YY_CPLUSPLUS - parser::by_kind::by_kind (by_kind&& that) YY_NOEXCEPT - : kind_ (that.kind_) - { - that.clear (); - } -#endif - - parser::by_kind::by_kind (const by_kind& that) YY_NOEXCEPT - : kind_ (that.kind_) - {} - - parser::by_kind::by_kind (token_kind_type t) YY_NOEXCEPT - : kind_ (yytranslate_ (t)) - {} - - - - void - parser::by_kind::clear () YY_NOEXCEPT - { - kind_ = symbol_kind::S_YYEMPTY; - } - - void - parser::by_kind::move (by_kind& that) - { - kind_ = that.kind_; - that.clear (); - } - - parser::symbol_kind_type - parser::by_kind::kind () const YY_NOEXCEPT - { - return kind_; - } - - - parser::symbol_kind_type - parser::by_kind::type_get () const YY_NOEXCEPT - { - return this->kind (); - } - - - - // by_state. - parser::by_state::by_state () YY_NOEXCEPT - : state (empty_state) - {} - - parser::by_state::by_state (const by_state& that) YY_NOEXCEPT - : state (that.state) - {} - - void - parser::by_state::clear () YY_NOEXCEPT - { - state = empty_state; - } - - void - parser::by_state::move (by_state& that) - { - state = that.state; - that.clear (); - } - - parser::by_state::by_state (state_type s) YY_NOEXCEPT - : state (s) - {} - - parser::symbol_kind_type - parser::by_state::kind () const YY_NOEXCEPT - { - if (state == empty_state) - return symbol_kind::S_YYEMPTY; - else - return YY_CAST (symbol_kind_type, yystos_[+state]); - } - - parser::stack_symbol_type::stack_symbol_type () - {} - - parser::stack_symbol_type::stack_symbol_type (YY_RVREF (stack_symbol_type) that) - : super_type (YY_MOVE (that.state), YY_MOVE (that.value)) - { -#if 201103L <= YY_CPLUSPLUS - // that is emptied. - that.state = empty_state; -#endif - } - - parser::stack_symbol_type::stack_symbol_type (state_type s, YY_MOVE_REF (symbol_type) that) - : super_type (s, YY_MOVE (that.value)) - { - // that is emptied. - that.kind_ = symbol_kind::S_YYEMPTY; - } - -#if YY_CPLUSPLUS < 201103L - parser::stack_symbol_type& - parser::stack_symbol_type::operator= (const stack_symbol_type& that) - { - state = that.state; - value = that.value; - return *this; - } - - parser::stack_symbol_type& - parser::stack_symbol_type::operator= (stack_symbol_type& that) - { - state = that.state; - value = that.value; - // that is emptied. - that.state = empty_state; - return *this; - } -#endif - - template - void - parser::yy_destroy_ (const char* yymsg, basic_symbol& yysym) const - { - if (yymsg) - YY_SYMBOL_PRINT (yymsg, yysym); - - // User destructor. - YY_USE (yysym.kind ()); - } - -#if YYDEBUG - template - void - parser::yy_print_ (std::ostream& yyo, const basic_symbol& yysym) const - { - std::ostream& yyoutput = yyo; - YY_USE (yyoutput); - if (yysym.empty ()) - yyo << "empty symbol"; - else - { - symbol_kind_type yykind = yysym.kind (); - yyo << (yykind < YYNTOKENS ? "token" : "nterm") - << ' ' << yysym.name () << " ("; - YY_USE (yykind); - yyo << ')'; - } - } -#endif - - void - parser::yypush_ (const char* m, YY_MOVE_REF (stack_symbol_type) sym) - { - if (m) - YY_SYMBOL_PRINT (m, sym); - yystack_.push (YY_MOVE (sym)); - } - - void - parser::yypush_ (const char* m, state_type s, YY_MOVE_REF (symbol_type) sym) - { -#if 201103L <= YY_CPLUSPLUS - yypush_ (m, stack_symbol_type (s, std::move (sym))); -#else - stack_symbol_type ss (s, sym); - yypush_ (m, ss); -#endif - } - - void - parser::yypop_ (int n) YY_NOEXCEPT - { - yystack_.pop (n); - } - -#if YYDEBUG - std::ostream& - parser::debug_stream () const - { - return *yycdebug_; - } - - void - parser::set_debug_stream (std::ostream& o) - { - yycdebug_ = &o; - } - - - parser::debug_level_type - parser::debug_level () const - { - return yydebug_; - } - - void - parser::set_debug_level (debug_level_type l) - { - yydebug_ = l; - } -#endif // YYDEBUG - - parser::state_type - parser::yy_lr_goto_state_ (state_type yystate, int yysym) - { - int yyr = yypgoto_[yysym - YYNTOKENS] + yystate; - if (0 <= yyr && yyr <= yylast_ && yycheck_[yyr] == yystate) - return yytable_[yyr]; - else - return yydefgoto_[yysym - YYNTOKENS]; - } - - bool - parser::yy_pact_value_is_default_ (int yyvalue) YY_NOEXCEPT - { - return yyvalue == yypact_ninf_; - } - - bool - parser::yy_table_value_is_error_ (int yyvalue) YY_NOEXCEPT - { - return yyvalue == yytable_ninf_; - } - - int - parser::operator() () - { - return parse (); - } - - int - parser::parse () - { - int yyn; - /// Length of the RHS of the rule being reduced. - int yylen = 0; - - // Error handling. - int yynerrs_ = 0; - int yyerrstatus_ = 0; - - /// The lookahead symbol. - symbol_type yyla; - - /// The return value of parse (). - int yyresult; - -#if YY_EXCEPTIONS - try -#endif // YY_EXCEPTIONS - { - YYCDEBUG << "Starting parse\n"; - - - /* Initialize the stack. The initial state will be set in - yynewstate, since the latter expects the semantical and the - location values to have been already stored, initialize these - stacks with a primary value. */ - yystack_.clear (); - yypush_ (YY_NULLPTR, 0, YY_MOVE (yyla)); - - /*-----------------------------------------------. - | yynewstate -- push a new symbol on the stack. | - `-----------------------------------------------*/ - yynewstate: - YYCDEBUG << "Entering state " << int (yystack_[0].state) << '\n'; - YY_STACK_PRINT (); - - // Accept? - if (yystack_[0].state == yyfinal_) - YYACCEPT; - - goto yybackup; - - - /*-----------. - | yybackup. | - `-----------*/ - yybackup: - // Try to take a decision without lookahead. - yyn = yypact_[+yystack_[0].state]; - if (yy_pact_value_is_default_ (yyn)) - goto yydefault; - - // Read a lookahead token. - if (yyla.empty ()) - { - YYCDEBUG << "Reading a token\n"; -#if YY_EXCEPTIONS - try -#endif // YY_EXCEPTIONS - { - yyla.kind_ = yytranslate_ (yylex (&yyla.value)); - } -#if YY_EXCEPTIONS - catch (const syntax_error& yyexc) - { - YYCDEBUG << "Caught exception: " << yyexc.what() << '\n'; - error (yyexc); - goto yyerrlab1; - } -#endif // YY_EXCEPTIONS - } - YY_SYMBOL_PRINT ("Next token is", yyla); - - if (yyla.kind () == symbol_kind::S_YYerror) - { - // The scanner already issued an error message, process directly - // to error recovery. But do not keep the error token as - // lookahead, it is too special and may lead us to an endless - // loop in error recovery. */ - yyla.kind_ = symbol_kind::S_YYUNDEF; - goto yyerrlab1; - } - - /* If the proper action on seeing token YYLA.TYPE is to reduce or - to detect an error, take that action. */ - yyn += yyla.kind (); - if (yyn < 0 || yylast_ < yyn || yycheck_[yyn] != yyla.kind ()) - { - goto yydefault; - } - - // Reduce or error. - yyn = yytable_[yyn]; - if (yyn <= 0) - { - if (yy_table_value_is_error_ (yyn)) - goto yyerrlab; - yyn = -yyn; - goto yyreduce; - } - - // Count tokens shifted since error; after three, turn off error status. - if (yyerrstatus_) - --yyerrstatus_; - - // Shift the lookahead token. - yypush_ ("Shifting", state_type (yyn), YY_MOVE (yyla)); - goto yynewstate; - - - /*-----------------------------------------------------------. - | yydefault -- do the default action for the current state. | - `-----------------------------------------------------------*/ - yydefault: - yyn = yydefact_[+yystack_[0].state]; - if (yyn == 0) - goto yyerrlab; - goto yyreduce; - - - /*-----------------------------. - | yyreduce -- do a reduction. | - `-----------------------------*/ - yyreduce: - yylen = yyr2_[yyn]; - { - stack_symbol_type yylhs; - yylhs.state = yy_lr_goto_state_ (yystack_[yylen].state, yyr1_[yyn]); - /* If YYLEN is nonzero, implement the default value of the - action: '$$ = $1'. Otherwise, use the top of the stack. - - Otherwise, the following line sets YYLHS.VALUE to garbage. - This behavior is undocumented and Bison users should not rely - upon it. */ - if (yylen) - yylhs.value = yystack_[yylen - 1].value; - else - yylhs.value = yystack_[0].value; - - - // Perform the reduction. - YY_REDUCE_PRINT (yyn); -#if YY_EXCEPTIONS - try -#endif // YY_EXCEPTIONS - { - switch (yyn) - { - case 2: // program: top_list -#line 106 "parser.y" - { - /* $1 = std::vector* */ - g_program.reset(new ProgramNode()); - if ((yystack_[0].value.vec)) { - for (ASTNode* p : *(yystack_[0].value.vec)) { - g_program->decls.emplace_back( std::unique_ptr(p) ); - } - delete (yystack_[0].value.vec); - } - } -#line 658 "parser.tab.cc" - break; - - case 3: // top_list: %empty -#line 120 "parser.y" - { (yylhs.value.vec) = new std::vector(); } -#line 664 "parser.tab.cc" - break; - - case 4: // top_list: top_list top_item -#line 122 "parser.y" - { - /* $1 = vector*, $2 = ASTNode* */ - (yylhs.value.vec) = (yystack_[1].value.vec); - if ((yystack_[0].value.node)) (yylhs.value.vec)->push_back((yystack_[0].value.node)); - } -#line 674 "parser.tab.cc" - break; - - case 5: // top_item: class_decl -#line 130 "parser.y" - { (yylhs.value.node) = (yystack_[0].value.node); } -#line 680 "parser.tab.cc" - break; - - case 6: // top_item: var_decl -#line 131 "parser.y" - { (yylhs.value.node) = (yystack_[0].value.node); } -#line 686 "parser.tab.cc" - break; - - case 7: // top_item: method_decl -#line 132 "parser.y" - { (yylhs.value.node) = (yystack_[0].value.node); } -#line 692 "parser.tab.cc" - break; - - case 8: // class_decl: CLASS IDENTIFIER class_opt_ext IS class_body END -#line 137 "parser.y" - { - /* $2 = char* (name), $3 = char* (extends or NULL), $5 = vector* members */ - ClassNode* cn = new ClassNode((yystack_[4].value.str) ? string((yystack_[4].value.str)) : string()); - if ((yystack_[3].value.str)) cn->extendsName = string((yystack_[3].value.str)); - if ((yystack_[1].value.vec)) { - for (ASTNode* m : *(yystack_[1].value.vec)) cn->members.emplace_back( std::unique_ptr(m) ); - delete (yystack_[1].value.vec); - } - if ((yystack_[4].value.str)) free((yystack_[4].value.str)); - if ((yystack_[3].value.str)) free((yystack_[3].value.str)); - (yylhs.value.node) = cn; - } -#line 709 "parser.tab.cc" - break; - - case 9: // class_opt_ext: %empty -#line 152 "parser.y" - { (yylhs.value.str) = nullptr; } -#line 715 "parser.tab.cc" - break; - - case 10: // class_opt_ext: EXTENDS IDENTIFIER -#line 153 "parser.y" - { (yylhs.value.str) = (yystack_[0].value.str); } -#line 721 "parser.tab.cc" - break; - - case 11: // class_body: %empty -#line 157 "parser.y" - { (yylhs.value.vec) = new std::vector(); } -#line 727 "parser.tab.cc" - break; - - case 12: // class_body: class_body class_member -#line 159 "parser.y" - { - (yylhs.value.vec) = (yystack_[1].value.vec); - if ((yystack_[0].value.node)) (yylhs.value.vec)->push_back((yystack_[0].value.node)); - } -#line 736 "parser.tab.cc" - break; - - case 13: // class_member: var_decl -#line 166 "parser.y" - { (yylhs.value.node) = (yystack_[0].value.node); } -#line 742 "parser.tab.cc" - break; - - case 14: // class_member: method_decl -#line 167 "parser.y" - { (yylhs.value.node) = (yystack_[0].value.node); } -#line 748 "parser.tab.cc" - break; - - case 15: // var_decl: VAR IDENTIFIER maybe_type SEMI -#line 172 "parser.y" - { - /* $2 = name (char*), $3 = type (char* or nullptr) */ - VarNode* vn = new VarNode((yystack_[2].value.str) ? string((yystack_[2].value.str)) : string(), (yystack_[1].value.str) ? string((yystack_[1].value.str)) : string()); - if ((yystack_[2].value.str)) free((yystack_[2].value.str)); - if ((yystack_[1].value.str)) free((yystack_[1].value.str)); - (yylhs.value.node) = vn; - } -#line 760 "parser.tab.cc" - break; - - case 16: // maybe_type: %empty -#line 182 "parser.y" - { (yylhs.value.str) = nullptr; } -#line 766 "parser.tab.cc" - break; - - case 17: // maybe_type: COLON IDENTIFIER -#line 183 "parser.y" - { (yylhs.value.str) = (yystack_[0].value.str); } -#line 772 "parser.tab.cc" - break; - - case 18: // method_decl: METHOD IDENTIFIER LPAREN RPAREN IS method_body END -#line 188 "parser.y" - { - /* $2 = name, $6 = vector* body */ - MethodNode* mn = new MethodNode((yystack_[5].value.str) ? string((yystack_[5].value.str)) : string()); - if ((yystack_[1].value.vec)) { - for (ASTNode* b : *(yystack_[1].value.vec)) mn->body.emplace_back( std::unique_ptr(b) ); - delete (yystack_[1].value.vec); - } - if ((yystack_[5].value.str)) free((yystack_[5].value.str)); - (yylhs.value.node) = mn; - } -#line 787 "parser.tab.cc" - break; - - case 19: // method_body: %empty -#line 201 "parser.y" - { (yylhs.value.vec) = new std::vector(); } -#line 793 "parser.tab.cc" - break; - - case 20: // method_body: method_body method_member -#line 203 "parser.y" - { - (yylhs.value.vec) = (yystack_[1].value.vec); - if ((yystack_[0].value.node)) (yylhs.value.vec)->push_back((yystack_[0].value.node)); - } -#line 802 "parser.tab.cc" - break; - - case 21: // method_member: var_decl -#line 210 "parser.y" - { (yylhs.value.node) = (yystack_[0].value.node); } -#line 808 "parser.tab.cc" - break; - - case 22: // method_member: %empty -#line 212 "parser.y" - { (yylhs.value.node) = new VarNode("__dummy__", ""); } -#line 814 "parser.tab.cc" - break; - - -#line 818 "parser.tab.cc" - - default: - break; - } - } -#if YY_EXCEPTIONS - catch (const syntax_error& yyexc) - { - YYCDEBUG << "Caught exception: " << yyexc.what() << '\n'; - error (yyexc); - YYERROR; - } -#endif // YY_EXCEPTIONS - YY_SYMBOL_PRINT ("-> $$ =", yylhs); - yypop_ (yylen); - yylen = 0; - - // Shift the result of the reduction. - yypush_ (YY_NULLPTR, YY_MOVE (yylhs)); - } - goto yynewstate; - - - /*--------------------------------------. - | yyerrlab -- here on detecting error. | - `--------------------------------------*/ - yyerrlab: - // If not already recovering from an error, report this error. - if (!yyerrstatus_) - { - ++yynerrs_; - std::string msg = YY_("syntax error"); - error (YY_MOVE (msg)); - } - - - if (yyerrstatus_ == 3) - { - /* If just tried and failed to reuse lookahead token after an - error, discard it. */ - - // Return failure if at end of input. - if (yyla.kind () == symbol_kind::S_YYEOF) - YYABORT; - else if (!yyla.empty ()) - { - yy_destroy_ ("Error: discarding", yyla); - yyla.clear (); - } - } - - // Else will try to reuse lookahead token after shifting the error token. - goto yyerrlab1; - - - /*---------------------------------------------------. - | yyerrorlab -- error raised explicitly by YYERROR. | - `---------------------------------------------------*/ - yyerrorlab: - /* Pacify compilers when the user code never invokes YYERROR and - the label yyerrorlab therefore never appears in user code. */ - if (false) - YYERROR; - - /* Do not reclaim the symbols of the rule whose action triggered - this YYERROR. */ - yypop_ (yylen); - yylen = 0; - YY_STACK_PRINT (); - goto yyerrlab1; - - - /*-------------------------------------------------------------. - | yyerrlab1 -- common code for both syntax error and YYERROR. | - `-------------------------------------------------------------*/ - yyerrlab1: - yyerrstatus_ = 3; // Each real token shifted decrements this. - // Pop stack until we find a state that shifts the error token. - for (;;) - { - yyn = yypact_[+yystack_[0].state]; - if (!yy_pact_value_is_default_ (yyn)) - { - yyn += symbol_kind::S_YYerror; - if (0 <= yyn && yyn <= yylast_ - && yycheck_[yyn] == symbol_kind::S_YYerror) - { - yyn = yytable_[yyn]; - if (0 < yyn) - break; - } - } - - // Pop the current state because it cannot handle the error token. - if (yystack_.size () == 1) - YYABORT; - - yy_destroy_ ("Error: popping", yystack_[0]); - yypop_ (); - YY_STACK_PRINT (); - } - { - stack_symbol_type error_token; - - - // Shift the error token. - error_token.state = state_type (yyn); - yypush_ ("Shifting", YY_MOVE (error_token)); - } - goto yynewstate; - - - /*-------------------------------------. - | yyacceptlab -- YYACCEPT comes here. | - `-------------------------------------*/ - yyacceptlab: - yyresult = 0; - goto yyreturn; - - - /*-----------------------------------. - | yyabortlab -- YYABORT comes here. | - `-----------------------------------*/ - yyabortlab: - yyresult = 1; - goto yyreturn; - - - /*-----------------------------------------------------. - | yyreturn -- parsing is finished, return the result. | - `-----------------------------------------------------*/ - yyreturn: - if (!yyla.empty ()) - yy_destroy_ ("Cleanup: discarding lookahead", yyla); - - /* Do not reclaim the symbols of the rule whose action triggered - this YYABORT or YYACCEPT. */ - yypop_ (yylen); - YY_STACK_PRINT (); - while (1 < yystack_.size ()) - { - yy_destroy_ ("Cleanup: popping", yystack_[0]); - yypop_ (); - } - - return yyresult; - } -#if YY_EXCEPTIONS - catch (...) - { - YYCDEBUG << "Exception caught: cleaning lookahead and stack\n"; - // Do not try to display the values of the reclaimed symbols, - // as their printers might throw an exception. - if (!yyla.empty ()) - yy_destroy_ (YY_NULLPTR, yyla); - - while (1 < yystack_.size ()) - { - yy_destroy_ (YY_NULLPTR, yystack_[0]); - yypop_ (); - } - throw; - } -#endif // YY_EXCEPTIONS - } - - void - parser::error (const syntax_error& yyexc) - { - error (yyexc.what ()); - } - -#if YYDEBUG || 0 - const char * - parser::symbol_name (symbol_kind_type yysymbol) - { - return yytname_[yysymbol]; - } -#endif // #if YYDEBUG || 0 - - - - - - - - - - const signed char parser::yypact_ninf_ = -25; - - const signed char parser::yytable_ninf_ = -1; - - const signed char - parser::yypact_[] = - { - -25, 4, 0, -25, -13, -6, -4, -25, -25, -25, - -25, 9, -9, -10, -1, 13, 1, -3, -7, -25, - -25, -25, -25, 16, 3, -25, -25, -25, -25, -25, - -5, -25, -25, -25 - }; - - const signed char - parser::yydefact_[] = - { - 3, 0, 2, 1, 0, 0, 0, 4, 5, 6, - 7, 9, 16, 0, 0, 0, 0, 0, 0, 10, - 11, 17, 15, 0, 0, 19, 8, 12, 13, 14, - 0, 18, 21, 20 - }; - - const signed char - parser::yypgoto_[] = - { - -25, -25, -25, -25, -25, -25, -25, -25, -24, -25, - 2, -25, -25 - }; - - const signed char - parser::yydefgoto_[] = - { - 0, 1, 2, 7, 8, 15, 24, 27, 9, 17, - 10, 30, 33 - }; - - const signed char - parser::yytable_[] = - { - 28, 31, 5, 4, 3, 11, 32, 5, 6, 26, - 5, 6, 12, 14, 13, 16, 18, 19, 20, 21, - 23, 25, 22, 0, 0, 0, 29 - }; - - const signed char - parser::yycheck_[] = - { - 24, 6, 7, 3, 0, 18, 30, 7, 8, 6, - 7, 8, 18, 4, 18, 24, 26, 18, 5, 18, - 27, 5, 25, -1, -1, -1, 24 - }; - - const signed char - parser::yystos_[] = - { - 0, 33, 34, 0, 3, 7, 8, 35, 36, 40, - 42, 18, 18, 18, 4, 37, 24, 41, 26, 18, - 5, 18, 25, 27, 38, 5, 6, 39, 40, 42, - 43, 6, 40, 44 - }; - - const signed char - parser::yyr1_[] = - { - 0, 32, 33, 34, 34, 35, 35, 35, 36, 37, - 37, 38, 38, 39, 39, 40, 41, 41, 42, 43, - 43, 44, 44 - }; - - const signed char - parser::yyr2_[] = - { - 0, 2, 1, 0, 2, 1, 1, 1, 6, 0, - 2, 0, 2, 1, 1, 4, 0, 2, 7, 0, - 2, 1, 0 - }; - - -#if YYDEBUG - // YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM. - // First, the terminals, then, starting at \a YYNTOKENS, nonterminals. - const char* - const parser::yytname_[] = - { - "\"end of file\"", "error", "\"invalid token\"", "CLASS", "EXTENDS", - "IS", "END", "VAR", "METHOD", "THIS", "RETURN", "IF", "THEN", "ELSE", - "WHILE", "LOOP", "TRUE", "FALSE", "IDENTIFIER", "INTEGER", "REAL", - "STRING", "SYMBOL", "UNKNOWN", "COLON", "SEMI", "LPAREN", "RPAREN", - "COMMA", "DOT", "ASSIGN", "ARROW", "$accept", "program", "top_list", - "top_item", "class_decl", "class_opt_ext", "class_body", "class_member", - "var_decl", "maybe_type", "method_decl", "method_body", "method_member", YY_NULLPTR - }; -#endif - - -#if YYDEBUG - const unsigned char - parser::yyrline_[] = - { - 0, 105, 105, 120, 121, 130, 131, 132, 136, 152, - 153, 157, 158, 166, 167, 171, 182, 183, 187, 201, - 202, 210, 212 - }; - - void - parser::yy_stack_print_ () const - { - *yycdebug_ << "Stack now"; - for (stack_type::const_iterator - i = yystack_.begin (), - i_end = yystack_.end (); - i != i_end; ++i) - *yycdebug_ << ' ' << int (i->state); - *yycdebug_ << '\n'; - } - - void - parser::yy_reduce_print_ (int yyrule) const - { - int yylno = yyrline_[yyrule]; - int yynrhs = yyr2_[yyrule]; - // Print the symbols being reduced, and their result. - *yycdebug_ << "Reducing stack by rule " << yyrule - 1 - << " (line " << yylno << "):\n"; - // The symbols being reduced. - for (int yyi = 0; yyi < yynrhs; yyi++) - YY_SYMBOL_PRINT (" $" << yyi + 1 << " =", - yystack_[(yynrhs) - (yyi + 1)]); - } -#endif // YYDEBUG - - parser::symbol_kind_type - parser::yytranslate_ (int t) YY_NOEXCEPT - { - // YYTRANSLATE[TOKEN-NUM] -- Symbol number corresponding to - // TOKEN-NUM as returned by yylex. - static - const signed char - translate_table[] = - { - 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 1, 2, 3, 4, - 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, - 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, - 25, 26, 27, 28, 29, 30, 31 - }; - // Last valid token kind. - const int code_max = 286; - - if (t <= 0) - return symbol_kind::S_YYEOF; - else if (t <= code_max) - return static_cast (translate_table[t]); - else - return symbol_kind::S_YYUNDEF; - } - -} // yy -#line 1189 "parser.tab.cc" - -#line 215 "parser.y" - - -/* ---- C/C++ вспомогательный код: yylex() и yyerror() ---- */ -%{ - -/* Реализация yylex() читает из simpleTokens, который создаёт lexer.l (flex) */ -/* simpleTokens: vector (SimpleToken defined in lexer.l) */ -#include - -/* Прототип extern из lexer.l: - extern std::vector simpleTokens; - (SimpleToken = { std::string kind, text; int line,startCol,endCol }) -*/ -extern std::vector simpleTokens; - -int yylex(void) { - if (parserTokIndex >= simpleTokens.size()) return 0; /* EOF */ - - const SimpleToken &t = simpleTokens[parserTokIndex++]; - - /* Ключевые слова и идентификаторы: - Лексер ранее помечал ключевые слова kind == "KEYWORD" и booleans etc. - Здесь сопоставляем текст. При необходимости можно погружать в нижний регистр. - */ - - if (t.kind == "KEYWORD") { - if (t.text == "class") return CLASS; - if (t.text == "extends") return EXTENDS; - if (t.text == "is") return IS; - if (t.text == "end") return END; - if (t.text == "var") return VAR; - if (t.text == "method") return METHOD; - if (t.text == "this") return THIS; - if (t.text == "return") return RETURN; - if (t.text == "if") return IF; - if (t.text == "then") return THEN; - if (t.text == "else") return ELSE; - if (t.text == "while") return WHILE; - if (t.text == "loop") return LOOP; - if (t.text == "true") { - yylval.str = strdup(t.text.c_str()); - return TRUE; - } - if (t.text == "false") { - yylval.str = strdup(t.text.c_str()); - return FALSE; - } - /* если flex пометил как KEYWORD, но текст не совпал, отдаём как IDENTIFIER */ - yylval.str = strdup(t.text.c_str()); - return IDENTIFIER; - } else if (t.kind == "IDENTIFIER") { - yylval.str = strdup(t.text.c_str()); - return IDENTIFIER; - } else if (t.kind == "INTEGER") { - yylval.str = strdup(t.text.c_str()); - return INTEGER; - } else if (t.kind == "REAL") { - yylval.str = strdup(t.text.c_str()); - return REAL; - } else if (t.kind == "STRING") { - yylval.str = strdup(t.text.c_str()); - return STRING; - } else if (t.kind == "SYMBOL") { - if (t.text == ":") { return COLON; } - if (t.text == ";") { return SEMI; } - if (t.text == "(") { return LPAREN; } - if (t.text == ")") { return RPAREN; } - if (t.text == ",") { return COMMA; } - if (t.text == ".") { return DOT; } - if (t.text == ":=") { return ASSIGN; } - if (t.text == "=>") { return ARROW; } - /* иначе отдаём как SYMBOL (несем семантику в yylval.str) */ - yylval.str = strdup(t.text.c_str()); - return SYMBOL; - } else if (t.kind == "UNKNOWN") { - yylval.str = strdup(t.text.c_str()); - return UNKNOWN; - } else { - /* fallback: ignore */ - return 0; - } -} - -/* Простая ошибка парсинга */ -void yyerror(const char *s) { - std::cerr << "Parse error: " << s << "\n"; -} - -%} diff --git a/parser.tab.hh b/parser.tab.hh deleted file mode 100644 index e23d15b..0000000 --- a/parser.tab.hh +++ /dev/null @@ -1,827 +0,0 @@ -// A Bison parser, made by GNU Bison 3.8.2. - -// Skeleton interface for Bison LALR(1) parsers in C++ - -// Copyright (C) 2002-2015, 2018-2021 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 3 of the License, 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, see . - -// 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. - - -/** - ** \file parser.tab.hh - ** Define the yy::parser class. - */ - -// C++ LALR(1) parser skeleton written by Akim Demaille. - -// DO NOT RELY ON FEATURES THAT ARE NOT DOCUMENTED in the manual, -// especially those whose name start with YY_ or yy_. They are -// private implementation details that can be changed or removed. - -#ifndef YY_YY_PARSER_TAB_HH_INCLUDED -# define YY_YY_PARSER_TAB_HH_INCLUDED - - -# include // std::abort -# include -# include -# include -# include - -#if defined __cplusplus -# define YY_CPLUSPLUS __cplusplus -#else -# define YY_CPLUSPLUS 199711L -#endif - -// Support move semantics when possible. -#if 201103L <= YY_CPLUSPLUS -# define YY_MOVE std::move -# define YY_MOVE_OR_COPY move -# define YY_MOVE_REF(Type) Type&& -# define YY_RVREF(Type) Type&& -# define YY_COPY(Type) Type -#else -# define YY_MOVE -# define YY_MOVE_OR_COPY copy -# define YY_MOVE_REF(Type) Type& -# define YY_RVREF(Type) const Type& -# define YY_COPY(Type) const Type& -#endif - -// Support noexcept when possible. -#if 201103L <= YY_CPLUSPLUS -# define YY_NOEXCEPT noexcept -# define YY_NOTHROW -#else -# define YY_NOEXCEPT -# define YY_NOTHROW throw () -#endif - -// Support constexpr when possible. -#if 201703 <= YY_CPLUSPLUS -# define YY_CONSTEXPR constexpr -#else -# define YY_CONSTEXPR -#endif - - - -#ifndef YY_ATTRIBUTE_PURE -# if defined __GNUC__ && 2 < __GNUC__ + (96 <= __GNUC_MINOR__) -# define YY_ATTRIBUTE_PURE __attribute__ ((__pure__)) -# else -# define YY_ATTRIBUTE_PURE -# endif -#endif - -#ifndef YY_ATTRIBUTE_UNUSED -# if defined __GNUC__ && 2 < __GNUC__ + (7 <= __GNUC_MINOR__) -# define YY_ATTRIBUTE_UNUSED __attribute__ ((__unused__)) -# else -# define YY_ATTRIBUTE_UNUSED -# endif -#endif - -/* Suppress unused-variable warnings by "using" E. */ -#if ! defined lint || defined __GNUC__ -# define YY_USE(E) ((void) (E)) -#else -# define YY_USE(E) /* empty */ -#endif - -/* Suppress an incorrect diagnostic about yylval being uninitialized. */ -#if defined __GNUC__ && ! defined __ICC && 406 <= __GNUC__ * 100 + __GNUC_MINOR__ -# if __GNUC__ * 100 + __GNUC_MINOR__ < 407 -# define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN \ - _Pragma ("GCC diagnostic push") \ - _Pragma ("GCC diagnostic ignored \"-Wuninitialized\"") -# else -# define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN \ - _Pragma ("GCC diagnostic push") \ - _Pragma ("GCC diagnostic ignored \"-Wuninitialized\"") \ - _Pragma ("GCC diagnostic ignored \"-Wmaybe-uninitialized\"") -# endif -# define YY_IGNORE_MAYBE_UNINITIALIZED_END \ - _Pragma ("GCC diagnostic pop") -#else -# define YY_INITIAL_VALUE(Value) Value -#endif -#ifndef YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN -# define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN -# define YY_IGNORE_MAYBE_UNINITIALIZED_END -#endif -#ifndef YY_INITIAL_VALUE -# define YY_INITIAL_VALUE(Value) /* Nothing. */ -#endif - -#if defined __cplusplus && defined __GNUC__ && ! defined __ICC && 6 <= __GNUC__ -# define YY_IGNORE_USELESS_CAST_BEGIN \ - _Pragma ("GCC diagnostic push") \ - _Pragma ("GCC diagnostic ignored \"-Wuseless-cast\"") -# define YY_IGNORE_USELESS_CAST_END \ - _Pragma ("GCC diagnostic pop") -#endif -#ifndef YY_IGNORE_USELESS_CAST_BEGIN -# define YY_IGNORE_USELESS_CAST_BEGIN -# define YY_IGNORE_USELESS_CAST_END -#endif - -# ifndef YY_CAST -# ifdef __cplusplus -# define YY_CAST(Type, Val) static_cast (Val) -# define YY_REINTERPRET_CAST(Type, Val) reinterpret_cast (Val) -# else -# define YY_CAST(Type, Val) ((Type) (Val)) -# define YY_REINTERPRET_CAST(Type, Val) ((Type) (Val)) -# endif -# endif -# ifndef YY_NULLPTR -# if defined __cplusplus -# if 201103L <= __cplusplus -# define YY_NULLPTR nullptr -# else -# define YY_NULLPTR 0 -# endif -# else -# define YY_NULLPTR ((void*)0) -# endif -# endif - -/* Debug traces. */ -#ifndef YYDEBUG -# define YYDEBUG 0 -#endif - -namespace yy { -#line 182 "parser.tab.hh" - - - - - /// A Bison parser. - class parser - { - public: -#ifdef YYSTYPE -# ifdef __GNUC__ -# pragma GCC message "bison: do not #define YYSTYPE in C++, use %define api.value.type" -# endif - typedef YYSTYPE value_type; -#else - /// Symbol semantic values. - union value_type - { -#line 82 "parser.y" - - char* str; /* для IDENTIFIER, STRING, INTEGER (как строка) */ - ASTNode* node; /* для отдельных узлов (Var, Class, Method) */ - std::vector* vec; /* для списков узлов (список объявлений) */ - -#line 206 "parser.tab.hh" - - }; -#endif - /// Backward compatibility (Bison 3.8). - typedef value_type semantic_type; - - - /// Syntax errors thrown from user actions. - struct syntax_error : std::runtime_error - { - syntax_error (const std::string& m) - : std::runtime_error (m) - {} - - syntax_error (const syntax_error& s) - : std::runtime_error (s.what ()) - {} - - ~syntax_error () YY_NOEXCEPT YY_NOTHROW; - }; - - /// Token kinds. - struct token - { - enum token_kind_type - { - YYEMPTY = -2, - YYEOF = 0, // "end of file" - YYerror = 256, // error - YYUNDEF = 257, // "invalid token" - CLASS = 258, // CLASS - EXTENDS = 259, // EXTENDS - IS = 260, // IS - END = 261, // END - VAR = 262, // VAR - METHOD = 263, // METHOD - THIS = 264, // THIS - RETURN = 265, // RETURN - IF = 266, // IF - THEN = 267, // THEN - ELSE = 268, // ELSE - WHILE = 269, // WHILE - LOOP = 270, // LOOP - TRUE = 271, // TRUE - FALSE = 272, // FALSE - IDENTIFIER = 273, // IDENTIFIER - INTEGER = 274, // INTEGER - REAL = 275, // REAL - STRING = 276, // STRING - SYMBOL = 277, // SYMBOL - UNKNOWN = 278, // UNKNOWN - COLON = 279, // COLON - SEMI = 280, // SEMI - LPAREN = 281, // LPAREN - RPAREN = 282, // RPAREN - COMMA = 283, // COMMA - DOT = 284, // DOT - ASSIGN = 285, // ASSIGN - ARROW = 286 // ARROW - }; - /// Backward compatibility alias (Bison 3.6). - typedef token_kind_type yytokentype; - }; - - /// Token kind, as returned by yylex. - typedef token::token_kind_type token_kind_type; - - /// Backward compatibility alias (Bison 3.6). - typedef token_kind_type token_type; - - /// Symbol kinds. - struct symbol_kind - { - enum symbol_kind_type - { - YYNTOKENS = 32, ///< Number of tokens. - S_YYEMPTY = -2, - S_YYEOF = 0, // "end of file" - S_YYerror = 1, // error - S_YYUNDEF = 2, // "invalid token" - S_CLASS = 3, // CLASS - S_EXTENDS = 4, // EXTENDS - S_IS = 5, // IS - S_END = 6, // END - S_VAR = 7, // VAR - S_METHOD = 8, // METHOD - S_THIS = 9, // THIS - S_RETURN = 10, // RETURN - S_IF = 11, // IF - S_THEN = 12, // THEN - S_ELSE = 13, // ELSE - S_WHILE = 14, // WHILE - S_LOOP = 15, // LOOP - S_TRUE = 16, // TRUE - S_FALSE = 17, // FALSE - S_IDENTIFIER = 18, // IDENTIFIER - S_INTEGER = 19, // INTEGER - S_REAL = 20, // REAL - S_STRING = 21, // STRING - S_SYMBOL = 22, // SYMBOL - S_UNKNOWN = 23, // UNKNOWN - S_COLON = 24, // COLON - S_SEMI = 25, // SEMI - S_LPAREN = 26, // LPAREN - S_RPAREN = 27, // RPAREN - S_COMMA = 28, // COMMA - S_DOT = 29, // DOT - S_ASSIGN = 30, // ASSIGN - S_ARROW = 31, // ARROW - S_YYACCEPT = 32, // $accept - S_program = 33, // program - S_top_list = 34, // top_list - S_top_item = 35, // top_item - S_class_decl = 36, // class_decl - S_class_opt_ext = 37, // class_opt_ext - S_class_body = 38, // class_body - S_class_member = 39, // class_member - S_var_decl = 40, // var_decl - S_maybe_type = 41, // maybe_type - S_method_decl = 42, // method_decl - S_method_body = 43, // method_body - S_method_member = 44 // method_member - }; - }; - - /// (Internal) symbol kind. - typedef symbol_kind::symbol_kind_type symbol_kind_type; - - /// The number of tokens. - static const symbol_kind_type YYNTOKENS = symbol_kind::YYNTOKENS; - - /// A complete symbol. - /// - /// Expects its Base type to provide access to the symbol kind - /// via kind (). - /// - /// Provide access to semantic value. - template - struct basic_symbol : Base - { - /// Alias to Base. - typedef Base super_type; - - /// Default constructor. - basic_symbol () YY_NOEXCEPT - : value () - {} - -#if 201103L <= YY_CPLUSPLUS - /// Move constructor. - basic_symbol (basic_symbol&& that) - : Base (std::move (that)) - , value (std::move (that.value)) - {} -#endif - - /// Copy constructor. - basic_symbol (const basic_symbol& that); - /// Constructor for valueless symbols. - basic_symbol (typename Base::kind_type t); - - /// Constructor for symbols with semantic value. - basic_symbol (typename Base::kind_type t, - YY_RVREF (value_type) v); - - /// Destroy the symbol. - ~basic_symbol () - { - clear (); - } - - - - /// Destroy contents, and record that is empty. - void clear () YY_NOEXCEPT - { - Base::clear (); - } - -#if YYDEBUG || 0 - /// The user-facing name of this symbol. - const char *name () const YY_NOEXCEPT - { - return parser::symbol_name (this->kind ()); - } -#endif // #if YYDEBUG || 0 - - - /// Backward compatibility (Bison 3.6). - symbol_kind_type type_get () const YY_NOEXCEPT; - - /// Whether empty. - bool empty () const YY_NOEXCEPT; - - /// Destructive move, \a s is emptied into this. - void move (basic_symbol& s); - - /// The semantic value. - value_type value; - - private: -#if YY_CPLUSPLUS < 201103L - /// Assignment operator. - basic_symbol& operator= (const basic_symbol& that); -#endif - }; - - /// Type access provider for token (enum) based symbols. - struct by_kind - { - /// The symbol kind as needed by the constructor. - typedef token_kind_type kind_type; - - /// Default constructor. - by_kind () YY_NOEXCEPT; - -#if 201103L <= YY_CPLUSPLUS - /// Move constructor. - by_kind (by_kind&& that) YY_NOEXCEPT; -#endif - - /// Copy constructor. - by_kind (const by_kind& that) YY_NOEXCEPT; - - /// Constructor from (external) token numbers. - by_kind (kind_type t) YY_NOEXCEPT; - - - - /// Record that this symbol is empty. - void clear () YY_NOEXCEPT; - - /// Steal the symbol kind from \a that. - void move (by_kind& that); - - /// The (internal) type number (corresponding to \a type). - /// \a empty when empty. - symbol_kind_type kind () const YY_NOEXCEPT; - - /// Backward compatibility (Bison 3.6). - symbol_kind_type type_get () const YY_NOEXCEPT; - - /// The symbol kind. - /// \a S_YYEMPTY when empty. - symbol_kind_type kind_; - }; - - /// Backward compatibility for a private implementation detail (Bison 3.6). - typedef by_kind by_type; - - /// "External" symbols: returned by the scanner. - struct symbol_type : basic_symbol - {}; - - /// Build a parser object. - parser (); - virtual ~parser (); - -#if 201103L <= YY_CPLUSPLUS - /// Non copyable. - parser (const parser&) = delete; - /// Non copyable. - parser& operator= (const parser&) = delete; -#endif - - /// Parse. An alias for parse (). - /// \returns 0 iff parsing succeeded. - int operator() (); - - /// Parse. - /// \returns 0 iff parsing succeeded. - virtual int parse (); - -#if YYDEBUG - /// The current debugging stream. - std::ostream& debug_stream () const YY_ATTRIBUTE_PURE; - /// Set the current debugging stream. - void set_debug_stream (std::ostream &); - - /// Type for debugging levels. - typedef int debug_level_type; - /// The current debugging level. - debug_level_type debug_level () const YY_ATTRIBUTE_PURE; - /// Set the current debugging level. - void set_debug_level (debug_level_type l); -#endif - - /// Report a syntax error. - /// \param msg a description of the syntax error. - virtual void error (const std::string& msg); - - /// Report a syntax error. - void error (const syntax_error& err); - -#if YYDEBUG || 0 - /// The user-facing name of the symbol whose (internal) number is - /// YYSYMBOL. No bounds checking. - static const char *symbol_name (symbol_kind_type yysymbol); -#endif // #if YYDEBUG || 0 - - - - - private: -#if YY_CPLUSPLUS < 201103L - /// Non copyable. - parser (const parser&); - /// Non copyable. - parser& operator= (const parser&); -#endif - - - /// Stored state numbers (used for stacks). - typedef signed char state_type; - - /// Compute post-reduction state. - /// \param yystate the current state - /// \param yysym the nonterminal to push on the stack - static state_type yy_lr_goto_state_ (state_type yystate, int yysym); - - /// Whether the given \c yypact_ value indicates a defaulted state. - /// \param yyvalue the value to check - static bool yy_pact_value_is_default_ (int yyvalue) YY_NOEXCEPT; - - /// Whether the given \c yytable_ value indicates a syntax error. - /// \param yyvalue the value to check - static bool yy_table_value_is_error_ (int yyvalue) YY_NOEXCEPT; - - static const signed char yypact_ninf_; - static const signed char yytable_ninf_; - - /// Convert a scanner token kind \a t to a symbol kind. - /// In theory \a t should be a token_kind_type, but character literals - /// are valid, yet not members of the token_kind_type enum. - static symbol_kind_type yytranslate_ (int t) YY_NOEXCEPT; - -#if YYDEBUG || 0 - /// For a symbol, its name in clear. - static const char* const yytname_[]; -#endif // #if YYDEBUG || 0 - - - // Tables. - // YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing - // STATE-NUM. - static const signed char yypact_[]; - - // YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM. - // Performed when YYTABLE does not specify something else to do. Zero - // means the default is an error. - static const signed char yydefact_[]; - - // YYPGOTO[NTERM-NUM]. - static const signed char yypgoto_[]; - - // YYDEFGOTO[NTERM-NUM]. - static const signed char yydefgoto_[]; - - // YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM. If - // positive, shift that token. If negative, reduce the rule whose - // number is the opposite. If YYTABLE_NINF, syntax error. - static const signed char yytable_[]; - - static const signed char yycheck_[]; - - // YYSTOS[STATE-NUM] -- The symbol kind of the accessing symbol of - // state STATE-NUM. - static const signed char yystos_[]; - - // YYR1[RULE-NUM] -- Symbol kind of the left-hand side of rule RULE-NUM. - static const signed char yyr1_[]; - - // YYR2[RULE-NUM] -- Number of symbols on the right-hand side of rule RULE-NUM. - static const signed char yyr2_[]; - - -#if YYDEBUG - // YYRLINE[YYN] -- Source line where rule number YYN was defined. - static const unsigned char yyrline_[]; - /// Report on the debug stream that the rule \a r is going to be reduced. - virtual void yy_reduce_print_ (int r) const; - /// Print the state stack on the debug stream. - virtual void yy_stack_print_ () const; - - /// Debugging level. - int yydebug_; - /// Debug stream. - std::ostream* yycdebug_; - - /// \brief Display a symbol kind, value and location. - /// \param yyo The output stream. - /// \param yysym The symbol. - template - void yy_print_ (std::ostream& yyo, const basic_symbol& yysym) const; -#endif - - /// \brief Reclaim the memory associated to a symbol. - /// \param yymsg Why this token is reclaimed. - /// If null, print nothing. - /// \param yysym The symbol. - template - void yy_destroy_ (const char* yymsg, basic_symbol& yysym) const; - - private: - /// Type access provider for state based symbols. - struct by_state - { - /// Default constructor. - by_state () YY_NOEXCEPT; - - /// The symbol kind as needed by the constructor. - typedef state_type kind_type; - - /// Constructor. - by_state (kind_type s) YY_NOEXCEPT; - - /// Copy constructor. - by_state (const by_state& that) YY_NOEXCEPT; - - /// Record that this symbol is empty. - void clear () YY_NOEXCEPT; - - /// Steal the symbol kind from \a that. - void move (by_state& that); - - /// The symbol kind (corresponding to \a state). - /// \a symbol_kind::S_YYEMPTY when empty. - symbol_kind_type kind () const YY_NOEXCEPT; - - /// The state number used to denote an empty symbol. - /// We use the initial state, as it does not have a value. - enum { empty_state = 0 }; - - /// The state. - /// \a empty when empty. - state_type state; - }; - - /// "Internal" symbol: element of the stack. - struct stack_symbol_type : basic_symbol - { - /// Superclass. - typedef basic_symbol super_type; - /// Construct an empty symbol. - stack_symbol_type (); - /// Move or copy construction. - stack_symbol_type (YY_RVREF (stack_symbol_type) that); - /// Steal the contents from \a sym to build this. - stack_symbol_type (state_type s, YY_MOVE_REF (symbol_type) sym); -#if YY_CPLUSPLUS < 201103L - /// Assignment, needed by push_back by some old implementations. - /// Moves the contents of that. - stack_symbol_type& operator= (stack_symbol_type& that); - - /// Assignment, needed by push_back by other implementations. - /// Needed by some other old implementations. - stack_symbol_type& operator= (const stack_symbol_type& that); -#endif - }; - - /// A stack with random access from its top. - template > - class stack - { - public: - // Hide our reversed order. - typedef typename S::iterator iterator; - typedef typename S::const_iterator const_iterator; - typedef typename S::size_type size_type; - typedef typename std::ptrdiff_t index_type; - - stack (size_type n = 200) YY_NOEXCEPT - : seq_ (n) - {} - -#if 201103L <= YY_CPLUSPLUS - /// Non copyable. - stack (const stack&) = delete; - /// Non copyable. - stack& operator= (const stack&) = delete; -#endif - - /// Random access. - /// - /// Index 0 returns the topmost element. - const T& - operator[] (index_type i) const - { - return seq_[size_type (size () - 1 - i)]; - } - - /// Random access. - /// - /// Index 0 returns the topmost element. - T& - operator[] (index_type i) - { - return seq_[size_type (size () - 1 - i)]; - } - - /// Steal the contents of \a t. - /// - /// Close to move-semantics. - void - push (YY_MOVE_REF (T) t) - { - seq_.push_back (T ()); - operator[] (0).move (t); - } - - /// Pop elements from the stack. - void - pop (std::ptrdiff_t n = 1) YY_NOEXCEPT - { - for (; 0 < n; --n) - seq_.pop_back (); - } - - /// Pop all elements from the stack. - void - clear () YY_NOEXCEPT - { - seq_.clear (); - } - - /// Number of elements on the stack. - index_type - size () const YY_NOEXCEPT - { - return index_type (seq_.size ()); - } - - /// Iterator on top of the stack (going downwards). - const_iterator - begin () const YY_NOEXCEPT - { - return seq_.begin (); - } - - /// Bottom of the stack. - const_iterator - end () const YY_NOEXCEPT - { - return seq_.end (); - } - - /// Present a slice of the top of a stack. - class slice - { - public: - slice (const stack& stack, index_type range) YY_NOEXCEPT - : stack_ (stack) - , range_ (range) - {} - - const T& - operator[] (index_type i) const - { - return stack_[range_ - i]; - } - - private: - const stack& stack_; - index_type range_; - }; - - private: -#if YY_CPLUSPLUS < 201103L - /// Non copyable. - stack (const stack&); - /// Non copyable. - stack& operator= (const stack&); -#endif - /// The wrapped container. - S seq_; - }; - - - /// Stack type. - typedef stack stack_type; - - /// The stack. - stack_type yystack_; - - /// Push a new state on the stack. - /// \param m a debug message to display - /// if null, no trace is output. - /// \param sym the symbol - /// \warning the contents of \a s.value is stolen. - void yypush_ (const char* m, YY_MOVE_REF (stack_symbol_type) sym); - - /// Push a new look ahead token on the state on the stack. - /// \param m a debug message to display - /// if null, no trace is output. - /// \param s the state - /// \param sym the symbol (for its value and location). - /// \warning the contents of \a sym.value is stolen. - void yypush_ (const char* m, state_type s, YY_MOVE_REF (symbol_type) sym); - - /// Pop \a n symbols from the stack. - void yypop_ (int n = 1) YY_NOEXCEPT; - - /// Constants. - enum - { - yylast_ = 26, ///< Last index in yytable_. - yynnts_ = 13, ///< Number of nonterminal symbols. - yyfinal_ = 3 ///< Termination state number. - }; - - - - }; - - -} // yy -#line 823 "parser.tab.hh" - - - - -#endif // !YY_YY_PARSER_TAB_HH_INCLUDED diff --git a/parser.y b/parser.y index 024cccb..06c0439 100644 --- a/parser.y +++ b/parser.y @@ -1,152 +1,229 @@ -%define parse.error verbose - -%{ -#include -#include -#include -#include -#include -#include "ast.h" -#include - -using namespace std; - -unique_ptr g_program = nullptr; -size_t parserTokIndex = 0; -vector simpleTokens; - -void yyerror(const char *s) { - cerr << "Parse error: " << s << "\n"; -} - -void printAST() { - if (g_program) { - g_program->print(0); - } else { - cout << "No AST generated.\n"; - } -} -%} - -%code requires { - #include - class ASTNode; // forward declaration -} - -%union { - char* str; - ASTNode* node; - std::vector* vec; -} - -%token CLASS EXTENDS IS END VAR METHOD THIS RETURN IF THEN ELSE WHILE LOOP TRUE FALSE -%token IDENTIFIER INTEGER REAL STRING -%token SYMBOL UNKNOWN -%token COLON SEMI LPAREN RPAREN COMMA DOT ASSIGN ARROW - -%type top_list class_body method_body -%type top_item class_decl var_decl method_decl class_member method_member -%type maybe_type class_opt_ext -%start program - -%% - -program: - top_list { - g_program = make_unique(); - if ($1) { - for (ASTNode* p : *$1) { - g_program->decls.emplace_back(unique_ptr(p)); - } - delete $1; - } - } -; - -top_list: - /* empty */ { $$ = new vector(); } - | top_list top_item { - $$ = $1; - if ($2) $$->push_back($2); - } -; - -top_item: - class_decl { $$ = $1; } - | var_decl { $$ = $1; } - | method_decl { $$ = $1; } -; - -class_decl: - CLASS IDENTIFIER class_opt_ext IS class_body END { - auto cn = new ClassNode($2 ? string($2) : string()); - if ($3) cn->extendsName = string($3); - if ($5) { - for (ASTNode* m : *$5) cn->members.emplace_back(unique_ptr(m)); - delete $5; - } - if ($2) free($2); - if ($3) free($3); - $$ = cn; - } -; - -class_opt_ext: - /* empty */ { $$ = nullptr; } - | EXTENDS IDENTIFIER { $$ = $2; } -; - -class_body: - /* empty */ { $$ = new vector(); } - | class_body class_member { - $$ = $1; - if ($2) $$->push_back($2); - } -; - -class_member: - var_decl { $$ = $1; } - | method_decl { $$ = $1; } -; - -var_decl: - VAR IDENTIFIER maybe_type SEMI { - auto vn = new VarNode($2 ? string($2) : string(), $3 ? string($3) : string()); - if ($2) free($2); - if ($3) free($3); - $$ = vn; - } -; - -maybe_type: - /* empty */ { $$ = nullptr; } - | COLON IDENTIFIER { $$ = $2; } -; - -method_decl: - METHOD IDENTIFIER LPAREN RPAREN IS method_body END { - auto mn = new MethodNode($2 ? string($2) : string()); - if ($6) { - for (ASTNode* b : *$6) mn->body.emplace_back(unique_ptr(b)); - delete $6; - } - if ($2) free($2); - $$ = mn; - } -; - -method_body: - /* empty */ { $$ = new vector(); } - | method_body method_member { - $$ = $1; - if ($2) $$->push_back($2); - } -; - -method_member: - var_decl { $$ = $1; } -; - -%% - -int yylex(void); +%{ +#include +#include +#include +#include +#include +#include "ast.hpp" +#include "tokens.hpp" + +extern int yylex(void); +extern int yylineno; +void yyerror(const char* s); + +AST::Program* g_program = nullptr; +%} + +%code requires { + #include + namespace AST { + struct Node; + struct Program; + struct ClassDecl; + struct VarDecl; + struct Expr; + struct Stmt; + struct MethodDecl; + struct Param; + } +} + +%defines "parser.hpp" +%define parse.error verbose + +%union { + long long ival; + char* cstr; + AST::Program* program; + AST::ClassDecl* classdecl; + AST::VarDecl* vardecl; + AST::Expr* expr; + AST::Stmt* stmt; + AST::MethodDecl* methoddecl; + AST::Param* param; + AST::Node* node; + std::vector* classlist; + std::vector* memberlist; + std::vector* varlist; + std::vector* paramlist; +} + +%token CLASS VAR IS END +%token METHOD RETURN IF THEN ELSE +%token TRUE FALSE +%token COLON SEMICOLON COMMA +%token LPAREN RPAREN LBRACE RBRACE +%token ASSIGN ARROW PLUS MINUS STAR SLASH +%token IDENTIFIER +%token TYPE_NAME +%token INT_LITERAL + +%type program +%type class_list +%type class_decl +%type class_body member_list +%type member +%type var_decl +%type method_decl +%type opt_params param_list +%type param +%type method_body stmt if_stmt +%type expr additive_expr multiplicative_expr unary_expr primary_expr + +%% + +program + : class_list + { + g_program = new AST::Program(); + for (auto* c : *$1) g_program->classes.push_back(c); + delete $1; + } + ; + +class_list + : class_list class_decl + { $$ = $1; $1->push_back($2); } + | class_decl + { $$ = new std::vector(); $$->push_back($1); } + ; + +class_decl + : CLASS IDENTIFIER IS class_body END + { + $$ = new AST::ClassDecl($2); + for (auto* n : *$4) { + if (auto* v = dynamic_cast(n)) $$->fields.push_back(v); + else if (auto* m = dynamic_cast(n)) $$->methods.push_back(m); + else delete n; + } + free($2); + delete $4; + } + ; + +class_body + : member_list { $$ = $1; } + | { $$ = new std::vector(); } + ; + +member_list + : member_list member + { $$ = $1; $1->push_back($2); } + | member + { $$ = new std::vector(); $$->push_back($1); } + ; + +member + : var_decl { $$ = $1; } + | method_decl { $$ = $1; } + ; + +var_decl + : VAR IDENTIFIER COLON TYPE_NAME SEMICOLON + { + $$ = new AST::VarDecl($2, $4, nullptr); + free($2); free($4); + } + | VAR IDENTIFIER COLON TYPE_NAME ASSIGN expr SEMICOLON + { + $$ = new AST::VarDecl($2, $4, $6); + free($2); free($4); + } + ; + +method_decl + : METHOD IDENTIFIER LPAREN opt_params RPAREN COLON TYPE_NAME ARROW method_body + { + $$ = new AST::MethodDecl($2, $7, $9); + if ($4) { for (auto* p : *$4) $$->params.push_back(p); delete $4; } + free($2); free($7); + } + ; + +opt_params + : param_list { $$ = $1; } + | { $$ = new std::vector(); } + ; + +param_list + : param_list COMMA param + { $$ = $1; $1->push_back($3); } + | param + { $$ = new std::vector(); $$->push_back($1); } + ; + +param + : IDENTIFIER COLON TYPE_NAME + { + $$ = new AST::Param($1, $3); + free($1); free($3); + } + ; + +method_body + : expr + { $$ = new AST::ReturnStmt($1); } + | stmt + { $$ = $1; } + ; + +stmt + : RETURN expr + { $$ = new AST::ReturnStmt($2); } + | if_stmt + { $$ = $1; } + ; + +if_stmt + : IF expr THEN stmt ELSE stmt + { $$ = new AST::IfStmt($2, $4, $6); } + ; + +expr + : additive_expr { $$ = $1; } + ; + +additive_expr + : additive_expr PLUS multiplicative_expr + { $$ = new AST::Binary(AST::BinOp::Add, $1, $3); } + | additive_expr MINUS multiplicative_expr + { $$ = new AST::Binary(AST::BinOp::Sub, $1, $3); } + | multiplicative_expr + { $$ = $1; } + ; + +multiplicative_expr + : multiplicative_expr STAR unary_expr + { $$ = new AST::Binary(AST::BinOp::Mul, $1, $3); } + | multiplicative_expr SLASH unary_expr + { $$ = new AST::Binary(AST::BinOp::Div, $1, $3); } + | unary_expr + { $$ = $1; } + ; + +unary_expr + : MINUS unary_expr + { $$ = new AST::Unary(AST::Unary::Op::Neg, $2); } + | primary_expr + { $$ = $1; } + ; + +primary_expr + : INT_LITERAL + { $$ = new AST::IntLiteral($1); } + | TRUE + { $$ = new AST::BoolLiteral(true); } + | FALSE + { $$ = new AST::BoolLiteral(false); } + | IDENTIFIER + { $$ = new AST::Identifier($1); free($1); } + | LPAREN expr RPAREN + { $$ = $2; } + ; + +%% + +void yyerror(const char* s) { + std::fprintf(stderr, "Parse error at line %d: %s\n", yylineno, s); +} diff --git a/stack.hh b/stack.hh deleted file mode 100644 index 746965d..0000000 --- a/stack.hh +++ /dev/null @@ -1,8 +0,0 @@ -// A Bison parser, made by GNU Bison 3.8.2. - -// Starting with Bison 3.2, this file is useless: the structure it -// used to define is now defined with the parser itself. -// -// To get rid of this file: -// 1. add '%require "3.2"' (or newer) to your grammar file -// 2. remove references to this file from your build system. diff --git a/tests/old/test.o b/tests/old/test.o index 5c111b0..d1a85d7 100644 --- a/tests/old/test.o +++ b/tests/old/test.o @@ -1,24 +1,22 @@ + class C is - var x : 10.1.2.3 - method Foo(a : Float ) : Float => a + var x : Float; + method Foo(a : Float) : Float => a end -class Animal { - void speak() { - output("..."); - } -} +class Animal is + method speak() : Void => + "..." +end -class Dog : Animal { - void bark() { - output("woof"); - } -} +class Dog is + method bark() : Void => + "woof" +end -class Main { - void main() { - Dog d; - d.speak(); - d.bark(); - } -} \ No newline at end of file +class Main is + method main() : Void => + var d : Dog; + d.Foo(0); # для примера, так как вызовы методов надо упрощать + d.bark() +end diff --git a/tests/old/test1.o b/tests/old/test1.o index cd3c59d..8e2a0df 100644 --- a/tests/old/test1.o +++ b/tests/old/test1.o @@ -1,5 +1,4 @@ -class Main { - void main() { - output("Hello, World!"); - } -} +class Main is + method main() : Void => + output("Hello, World!") +end \ No newline at end of file diff --git a/tests/old/test10.o b/tests/old/test10.o index 2b94af2..c04c96c 100644 --- a/tests/old/test10.o +++ b/tests/old/test10.o @@ -1,19 +1,16 @@ -class Animal { - void speak() { - output("..."); - } -} +class Animal is + method speak() : Void => + output("...") +end -class Dog : Animal { - void bark() { - output("woof"); - } -} +class Dog is + method bark() : Void => + output("woof") +end -class Main { - void main() { - Dog d; +class Main is + var d : Dog; + method main() : Void => d.speak(); - d.bark(); - } -} + d.bark() +end \ No newline at end of file diff --git a/tests/old/test11.o b/tests/old/test11.o index 928c7aa..0873c64 100644 --- a/tests/old/test11.o +++ b/tests/old/test11.o @@ -1,20 +1,17 @@ -class Animal { - virtual void speak() { - output("..."); - } -} +class Animal is + method speak() : Void => + output("...") +end -class Dog : Animal { - override void speak() { - output("woof"); - } -} +class Dog is + method speak() : Void => + output("woof") +end -class Main { - void main() { - Animal a; - Dog d; - a = d; // полиморфизм - a.speak(); // должен вызвать Dog.speak() - } -} +class Main is + var a : Animal; + var d : Dog; + method main() : Void => + a := d; # полиморфизм + a.speak() # должен вызвать Dog.speak() +end \ No newline at end of file diff --git a/tests/old/test12.o b/tests/old/test12.o index f49a43b..f0776f4 100644 --- a/tests/old/test12.o +++ b/tests/old/test12.o @@ -1,9 +1,9 @@ -class Main { - void main() { - int[3] arr; - arr[0] = 10; - arr[1] = 20; - arr[2] = 30; - output(arr[1]); - } -} +class Main is + var arr : Array; + method main() : Void => + arr := Array(3); + arr[0] := 10; + arr[1] := 20; + arr[2] := 30; + output(arr[1]) +end \ No newline at end of file diff --git a/tests/old/test13.o b/tests/old/test13.o index e3caf70..b28eedd 100644 --- a/tests/old/test13.o +++ b/tests/old/test13.o @@ -1,10 +1,11 @@ -class Main { - int fact(int n) { - if (n == 0) return 1; - return n * fact(n - 1); - } - - void main() { - output(fact(5)); - } -} +class Main is + method fact(n : Int) : Int => + if n == 0 then + 1 + else + n * fact(n - 1) + end + + method main() : Void => + output(fact(5)) +end \ No newline at end of file diff --git a/tests/old/test14.o b/tests/old/test14.o index a840d7d..74b939d 100644 --- a/tests/old/test14.o +++ b/tests/old/test14.o @@ -1,11 +1,10 @@ -class Main { - void main() { - bool a; - a = true; - if (a) { - output(1); - } else { - output(0); - } - } -} +class Main is + var a : Bool; + method main() : Void => + a := true; + if a then + output(1) + else + output(0) + end +end \ No newline at end of file diff --git a/tests/old/test15.o b/tests/old/test15.o index 1e3e0f4..a7449da 100644 --- a/tests/old/test15.o +++ b/tests/old/test15.o @@ -1,20 +1,15 @@ -class A { - int get() { - return 42; - } -} +class A is + method get() : Int => 42 +end -class B { - A obj; +class B is + var obj : A; + method print() : Void => + output(obj.get()) +end - void print() { - output(obj.get()); - } -} - -class Main { - void main() { - B b; - b.print(); - } -} +class Main is + var b : B; + method main() : Void => + b.print() +end \ No newline at end of file diff --git a/tests/old/test2.o b/tests/old/test2.o index f4f9b63..e3a40a4 100644 --- a/tests/old/test2.o +++ b/tests/old/test2.o @@ -1,7 +1,6 @@ -class Main { - void main() { - int x; - x = 5; - output(x); - } -} +class Main is + var x : Int; + method main() : Void => + x := 5; + output(x) +end \ No newline at end of file diff --git a/tests/old/test3.o b/tests/old/test3.o index 71dc050..5c95525 100644 --- a/tests/old/test3.o +++ b/tests/old/test3.o @@ -1,7 +1,8 @@ -class Main { - void main() { - int a; int b; - a = 2; b = 3; - output(a + b); - } -} +class Main is + var a : Int; + var b : Int; + method main() : Void => + a := 2; + b := 3; + output(a + b) +end \ No newline at end of file diff --git a/tests/old/test4.o b/tests/old/test4.o index 08115ec..a4cbc14 100644 --- a/tests/old/test4.o +++ b/tests/old/test4.o @@ -1,11 +1,10 @@ -class Main { - void main() { - int x; - x = 10; - if (x > 5) { - output(1); - } else { - output(0); - } - } -} +class Main is + var x : Int; + method main() : Void => + x := 10; + if x > 5 then + output(1) + else + output(0) + end +end \ No newline at end of file diff --git a/tests/old/test5.o b/tests/old/test5.o index 14e9b6a..4dd7b80 100644 --- a/tests/old/test5.o +++ b/tests/old/test5.o @@ -1,10 +1,9 @@ -class Main { - void main() { - int i; - i = 0; - while (i < 3) { +class Main is + var i : Int; + method main() : Void => + i := 0; + while i < 3 do output(i); - i = i + 1; - } - } -} + i := i + 1 + end +end \ No newline at end of file diff --git a/tests/old/test6.o b/tests/old/test6.o index 6109f2a..0e7da82 100644 --- a/tests/old/test6.o +++ b/tests/old/test6.o @@ -1,8 +1,6 @@ -class Main { - int square(int x) { - return x * x; - } - void main() { - output(square(4)); - } -} +class Main is + method square(x : Int) : Int => x * x + + method main() : Void => + output(square(4)) +end \ No newline at end of file diff --git a/tests/old/test7.o b/tests/old/test7.o index 22ef258..c9bbaf6 100644 --- a/tests/old/test7.o +++ b/tests/old/test7.o @@ -1,12 +1,8 @@ -class Main { - int add(int a, int b) { - return a + b; - } - int mul(int a, int b) { - return a * b; - } - void main() { +class Main is + method add(a : Int, b : Int) : Int => a + b + method mul(a : Int, b : Int) : Int => a * b + + method main() : Void => output(add(2, 3)); - output(mul(2, 3)); - } -} + output(mul(2, 3)) +end \ No newline at end of file diff --git a/tests/old/test8.o b/tests/old/test8.o index 3c709c4..104f74d 100644 --- a/tests/old/test8.o +++ b/tests/old/test8.o @@ -1,7 +1,6 @@ -class Main { - void main() { - int x; +class Main is + var x : Int; + method main() : Void => input(x); - output(x + 1); - } -} + output(x + 1) +end \ No newline at end of file diff --git a/tests/old/test9.o b/tests/old/test9.o index 73f1407..e8be7b9 100644 --- a/tests/old/test9.o +++ b/tests/old/test9.o @@ -1,21 +1,19 @@ -class Point { - int x; - int y; - - void init(int a, int b) { - x = a; y = b; - } - - void print() { +class Point is + var x : Int; + var y : Int; + + method init(a : Int, b : Int) : Void => + x := a; + y := b + + method print() : Void => output(x); - output(y); - } -} + output(y) +end -class Main { - void main() { - Point p; +class Main is + var p : Point; + method main() : Void => p.init(3, 4); - p.print(); - } -} + p.print() +end \ No newline at end of file diff --git a/tokens.hpp b/tokens.hpp new file mode 100644 index 0000000..5571070 --- /dev/null +++ b/tokens.hpp @@ -0,0 +1,101 @@ +#pragma once +#include +#include +#include +#include +#include + +enum class TokenKind { + CLASS, VAR, IS, END, + METHOD, RETURN, IF, THEN, ELSE, + TRUEKW, FALSEKW, + IDENTIFIER, TYPE_NAME, INT_LITERAL, + COLON, SEMICOLON, COMMA, + LPAREN, RPAREN, LBRACE, RBRACE, + ASSIGN, ARROW, PLUS, MINUS, STAR, SLASH, + END_OF_FILE +}; + +struct Token { + TokenKind kind; + std::string lexeme; + int line; + int column; + Token(TokenKind k, std::string lx, int ln, int col) + : kind(k), lexeme(std::move(lx)), line(ln), column(col) {} + virtual ~Token() = default; +}; + +struct KeywordToken : Token { + KeywordToken(TokenKind k, const std::string& lx, int ln, int col) + : Token(k, lx, ln, col) {} +}; + +struct IdentifierToken : Token { + IdentifierToken(const std::string& lx, int ln, int col) + : Token(TokenKind::IDENTIFIER, lx, ln, col) {} +}; + +struct TypeNameToken : Token { + TypeNameToken(const std::string& lx, int ln, int col) + : Token(TokenKind::TYPE_NAME, lx, ln, col) {} +}; + +struct IntegerToken : Token { + std::int64_t value; + IntegerToken(const std::string& lx, std::int64_t v, int ln, int col) + : Token(TokenKind::INT_LITERAL, lx, ln, col), value(v) {} +}; + +struct SymbolToken : Token { + SymbolToken(TokenKind k, const std::string& lx, int ln, int col) + : Token(k, lx, ln, col) {} +}; + +inline const char* TokenKindToString(TokenKind k) { + switch (k) { + case TokenKind::CLASS: return "CLASS"; + case TokenKind::VAR: return "VAR"; + case TokenKind::IS: return "IS"; + case TokenKind::END: return "END"; + case TokenKind::METHOD: return "METHOD"; + case TokenKind::RETURN: return "RETURN"; + case TokenKind::IF: return "IF"; + case TokenKind::THEN: return "THEN"; + case TokenKind::ELSE: return "ELSE"; + case TokenKind::TRUEKW: return "TRUE"; + case TokenKind::FALSEKW: return "FALSE"; + case TokenKind::IDENTIFIER: return "IDENTIFIER"; + case TokenKind::TYPE_NAME: return "TYPE_NAME"; + case TokenKind::INT_LITERAL: return "INT_LITERAL"; + case TokenKind::COLON: return "COLON"; + case TokenKind::SEMICOLON: return "SEMICOLON"; + case TokenKind::COMMA: return "COMMA"; + case TokenKind::LPAREN: return "LPAREN"; + case TokenKind::RPAREN: return "RPAREN"; + case TokenKind::LBRACE: return "LBRACE"; + case TokenKind::RBRACE: return "RBRACE"; + case TokenKind::ASSIGN: return "ASSIGN"; + case TokenKind::ARROW: return "ARROW"; + case TokenKind::PLUS: return "PLUS"; + case TokenKind::MINUS: return "MINUS"; + case TokenKind::STAR: return "STAR"; + case TokenKind::SLASH: return "SLASH"; + case TokenKind::END_OF_FILE: return "EOF"; + } + return "UNKNOWN"; +} + +inline std::vector> g_tokens; + +inline void EmitToken(std::unique_ptr t) { + std::cout << TokenKindToString(t->kind); + if (t->kind == TokenKind::IDENTIFIER || t->kind == TokenKind::TYPE_NAME) { + std::cout << "(" << t->lexeme << ")"; + } else if (t->kind == TokenKind::INT_LITERAL) { + auto* it = static_cast(t.get()); + std::cout << "(" << it->value << ")"; + } + std::cout << "\n"; + g_tokens.emplace_back(std::move(t)); +}