diff options
164 files changed, 4851 insertions, 13 deletions
diff --git a/.clang_complete b/.clang_complete new file mode 100644 index 0000000..825bf46 --- /dev/null +++ b/.clang_complete @@ -0,0 +1 @@ +-Ic_lexer/include diff --git a/.dir-locals.el b/.dir-locals.el new file mode 100644 index 0000000..b170598 --- /dev/null +++ b/.dir-locals.el @@ -0,0 +1,3 @@ +((nil . ((company-clang-arguments . ("-I../include/")) + (company-c-headers-path-user . ("../include/")) + (flycheck-clang-include-path . ("../include"))))) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..44b4cf2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +bin/ +c_lexer/build/ +c_parser/build/ +c_compiler/build/ +*.yy.cpp +*.tab.cpp +*.tab.hpp +*.o +*.output +#* +.#* +*.log +.vagrant +test/ diff --git a/Notes.org b/Notes.org new file mode 100644 index 0000000..8b2d525 --- /dev/null +++ b/Notes.org @@ -0,0 +1,132 @@ +#+TITLE: Notes +#+DATE: <2017-02-20 Mon> +#+AUTHOR: Yann Herklotz +#+EMAIL: ymherklotz@gmail.com +#+DESCRIPTION: These are notes about the Compiler project. +#+DATE: <2017-03-09 Thu> + +* Lexer + +** DONE Lab (calculate statistics over text) + DEADLINE: <2017-01-31 Tue> + +*** Task + + We had to calculate statistics over text. This was to learn about + flex and how to use it. + + +** DONE Coursework (c lexer) + DEADLINE: <2017-02-07 Tue> + +*** Task + + Design a C lexer that could lex C and output the important information + about the C code. This made us read the C spec for the first time. + + +* Parser + +** DONE Lab (parse maths) + DEADLINE: <2017-02-14 Tue> + +*** Task + + We had to design a maths parser and then analyse the tree that was + created by parsing it. The additional parts where to differentiate + equations using the different rules for differentiating. We also had to + evaluate the expressions and do other operations on the tree. This taught + us how to use bison. + + +** TODO Coursework (c parser) + DEADLINE: <2017-03-07 Tue> + +*** Task + +*** AST (Abstract Syntax Tree) + + Finsihed the AST for now, using polymorphism to achieve this. Right now i + t achieves the goal of this coursework as it does print out the AST in xml + format and supports all the features that the parser should support. The + expressions branch of the AST is completely bare because they are not needed + for this parser. I will continue working on this AST in the compiler section + so that it supports all the other constructs that I need for the compiler. + +*** Grammar + + The grammar is not completely done yet as I dont support all of the declartions + that are possible, and as I add more constructs in the AST I will also have to add + declarations in the bison file. This I will only continue in the Compiler section. + + +* Compiler + +** DONE Lab (codegen) + DEADLINE: <2017-02-28 Tue> + +*** Task + + In this lab we used the setup provided to us to make a compiler for the language + specified in the lab. The AST was already built for us and the language was already + parsed using C++ instead of bison. + + +** TODO Coursework (c compiler) + DEADLINE: <2017-03-28 Tue> + +*** Task + +*** Mips Assembly + + - %hi(id) :: loads the upper address of the global variable id + - %lo(id) :: loads the lower address of the global variable id + - addiu :: Add Immediate Unsigned + - jr :: Jump Register + - li :: Pseudo instruction interpreted as ( lui $rd, LabelAddr[31:16] + ori $rd,$rd, LabelAddr[15:0] ) + - lui :: loads uppper immediate. + - lw :: Load Word + - move :: Pseudo instruction interpreted as (add $rt, $rs, $zero) + - nop :: No Operation + - sw :: Store Word +*** TODOs + +**** Expression + +***** void evaluate(VariableStackBindings binding) + + Statements evaluate the expression by writing it out and printing the resulting asm + onto the command line. I will also have to pass the bindings to the evaluate function + + examples: + + z = a + b + c, will be calculated as $2 = b + c, and then $2 = a + $2 + + This should happen automatically because of recursion. + Have to be careful because of the way the expressions are parsed. + Constant expressions will just output li $2, 6 for example + +**** Function class + +**** Statement class + + - Need function that prints declaration bits and expression bits, as well as connected + statements + +**** Definition class + + - need function that will print the code for declaration. Basic code should be: + + li $2, 6 + sw $2, 4($fp) + + - Before that is executed it should evaluate the expression. + + - The expression should be the thing doing: li $2, 6 + That is if we want a 6 stored in the variable. + + - The declaration class should only be in charge of storing it in the right location in + the stack. + + diff --git a/c_compiler/GPATH b/c_compiler/GPATH Binary files differnew file mode 100644 index 0000000..1af534b --- /dev/null +++ b/c_compiler/GPATH diff --git a/c_compiler/GRTAGS b/c_compiler/GRTAGS Binary files differnew file mode 100644 index 0000000..c1933dc --- /dev/null +++ b/c_compiler/GRTAGS diff --git a/c_compiler/GTAGS b/c_compiler/GTAGS Binary files differnew file mode 100644 index 0000000..4128c61 --- /dev/null +++ b/c_compiler/GTAGS diff --git a/c_compiler/TAGS b/c_compiler/TAGS new file mode 100644 index 0000000..77480fc --- /dev/null +++ b/c_compiler/TAGS @@ -0,0 +1,694 @@ + +include/node.hpp,96 +#define AST_BASE_HPPAST_BASE_HPP2,21 +class Node {Node7,64 + virtual ~Node() {}~Node9,85 + +include/statement.hpp,786 +#define AST_STATEMENT_HPPAST_STATEMENT_HPP2,26 +class Statement : public Node {Statement7,74 + Statement* next_statement;next_statement9,117 +class CompoundStatement : public Statement {CompoundStatement26,524 + Declaration* m_decl;m_decl28,580 + Statement* m_statement;m_statement29,605 +class SelectionStatement : public Statement {SelectionStatement45,1029 + Statement* m_if;m_if47,1086 + Statement* m_else;m_else48,1107 +class ExpressionStatement : public Statement {ExpressionStatement62,1470 + Expression* m_expr;m_expr64,1528 +class JumpStatement : public Statement {JumpStatement78,1867 + Expression* m_expr;m_expr80,1919 +class IterationStatement : public Statement {IterationStatement94,2252 + Statement* m_statement;m_statement96,2309 + +include/function.hpp,363 +#define AST_FUNCTION_HPPAST_FUNCTION_HPP2,25 +struct VarLocation {VarLocation7,72 + Type* type;type8,93 + int32_t stack_position;stack_position9,109 +class Function : public Node {Function13,142 + Type* type;type15,184 + std::string id;id16,200 + Declaration* parameter_list;parameter_list17,220 + Statement* statement;statement18,253 + +include/expression.hpp,286 +#define AST_EXPRESSION_HPPAST_EXPRESSION_HPP2,27 +class Expression : public Node {Expression6,75 +class Identifier : public Expression {Identifier16,267 + std::string m_id;m_id18,315 +class Constant : public Expression {Constant26,425 + int32_t m_constant;m_constant28,471 + +include/translation_unit.hpp,163 +#define EXTERNAL_HPPEXTERNAL_HPP2,21 +class TranslationUnit : public Node {TranslationUnit7,64 + std::vector<Node* > translation_unit;translation_unit9,113 + +include/type.hpp,447 +#define TYPE_HPPTYPE_HPP2,17 +class Type : public Node {Type7,56 +class Specifier : public Type {Specifier17,244 +class Pointer : public Type {Pointer23,334 + Type* pointer_type;pointer_type25,375 +class Array : public Type {Array34,493 + int32_t size;size36,532 + Type* array_type;array_type37,550 +class Void : public Specifier {Void46,681 +class Int : public Specifier {Int54,784 +class Char : public Specifier {Char62,885 + +include/ast.hpp,108 +#define AST_HPPAST_HPP2,16 +typedef std::map<std::string, VarLocation> VariableStack;VariableStack12,145 + +include/declaration.hpp,297 +#define AST_DECLARATION_HPPAST_DECLARATION_HPP2,28 +class Declaration : public Node {Declaration8,127 + Type* type;type10,170 + std::string id;id11,186 + Expression* init;init12,206 + Declaration* next_decl;next_decl13,228 + Declaration* list_next_decl;list_next_decl14,256 + +src/c_parser.tab.cpp,9030 +#define YYBISON YYBISON44,2037 +#define YYBISON_VERSION YYBISON_VERSION47,2078 +#define YYSKELETON_NAME YYSKELETON_NAME50,2133 +#define YYPURE YYPURE53,2188 +#define YYPUSH YYPUSH56,2227 +#define YYPULL YYPULL59,2266 +# define YY_NULLPTR YY_NULLPTR70,2471 +# define YY_NULLPTR YY_NULLPTR72,2509 +# undef YYERROR_VERBOSEYYERROR_VERBOSE78,2614 +# define YYERROR_VERBOSE YYERROR_VERBOSE79,2638 +# define YYERROR_VERBOSE YYERROR_VERBOSE81,2671 +# define YY_YY_C_COMPILER_SRC_C_PARSER_TAB_HPP_INCLUDEDYY_YY_C_COMPILER_SRC_C_PARSER_TAB_HPP_INCLUDED87,2863 +# define YYDEBUG YYDEBUG90,2956 +# define YYTOKENTYPEYYTOKENTYPE114,3494 + enum yytokentypeyytokentype115,3515 + T_IDENTIFIER = 258,T_IDENTIFIER117,3538 + T_SC = 259,T_SC118,3562 + T_CMA = 260,T_CMA119,3578 + T_LRB = 261,T_LRB120,3595 + T_LCB = 262,T_LCB121,3612 + T_RCB = 263,T_RCB122,3629 + T_LSB = 264,T_LSB123,3646 + T_RSB = 265,T_RSB124,3663 + T_QU = 266,T_QU125,3680 + T_COL = 267,T_COL126,3696 + T_LOG_OR = 268,T_LOG_OR127,3713 + T_LOG_AND = 269,T_LOG_AND128,3733 + T_OR = 270,T_OR129,3754 + T_XOR = 271,T_XOR130,3770 + T_AND = 272,T_AND131,3787 + T_EQUALITY_OP = 273,T_EQUALITY_OP132,3804 + T_REL_OP = 274,T_REL_OP133,3829 + T_SHIFT_OP = 275,T_SHIFT_OP134,3849 + T_MULT = 276,T_MULT135,3871 + T_DIV = 277,T_DIV136,3889 + T_REM = 278,T_REM137,3906 + T_TILDE = 279,T_TILDE138,3923 + T_NOT = 280,T_NOT139,3942 + T_DOT = 281,T_DOT140,3959 + T_ARROW = 282,T_ARROW141,3976 + T_INCDEC = 283,T_INCDEC142,3995 + T_ADDSUB_OP = 284,T_ADDSUB_OP143,4015 + T_ASSIGN_OPER = 285,T_ASSIGN_OPER144,4038 + T_EQ = 286,T_EQ145,4063 + T_SIZEOF = 287,T_SIZEOF146,4079 + T_INT_CONST = 288,T_INT_CONST147,4099 + T_IF = 289,T_IF148,4122 + T_WHILE = 290,T_WHILE149,4138 + T_DO = 291,T_DO150,4157 + T_FOR = 292,T_FOR151,4173 + T_RETURN = 293,T_RETURN152,4190 + T_VOID = 294,T_VOID153,4210 + T_CHAR = 295,T_CHAR154,4228 + T_SCHAR = 296,T_SCHAR155,4246 + T_UCHAR = 297,T_UCHAR156,4265 + T_SSINT = 298,T_SSINT157,4284 + T_USINT = 299,T_USINT158,4303 + T_LINT = 300,T_LINT159,4322 + T_ULINT = 301,T_ULINT160,4340 + T_UINT = 302,T_UINT161,4359 + T_SINT = 303,T_SINT162,4377 + T_RRB = 304,T_RRB163,4395 + T_ELSE = 305T_ELSE164,4412 +union YYSTYPEYYSTYPE171,4517 + Node* node;node175,4589 + TranslationUnit* trans_unit;trans_unit176,4605 + Function* function;function177,4638 + Statement* statement;statement178,4662 + Declaration* declaration;declaration179,4688 + Expression* expression;expression180,4718 + Type* type;type181,4746 + double number;number182,4762 + std::string* string;string183,4781 +typedef union YYSTYPE YYSTYPE;YYSTYPE188,4873 +# define YYSTYPE_IS_TRIVIAL YYSTYPE_IS_TRIVIAL189,4904 +# define YYSTYPE_IS_DECLARED YYSTYPE_IS_DECLARED190,4934 +# undef shortshort205,5209 +typedef YYTYPE_UINT8 yytype_uint8;yytype_uint8209,5251 +typedef unsigned char yytype_uint8;yytype_uint8211,5292 +typedef YYTYPE_INT8 yytype_int8;yytype_int8215,5355 +typedef signed char yytype_int8;yytype_int8217,5394 +typedef YYTYPE_UINT16 yytype_uint16;yytype_uint16221,5456 +typedef unsigned short int yytype_uint16;yytype_uint16223,5499 +typedef YYTYPE_INT16 yytype_int16;yytype_int16227,5569 +typedef short int yytype_int16;yytype_int16229,5610 +# define YYSIZE_T YYSIZE_T234,5689 +# define YYSIZE_T YYSIZE_T236,5744 +# define YYSIZE_T YYSIZE_T239,5853 +# define YYSIZE_T YYSIZE_T241,5886 +#define YYSIZE_MAXIMUM YYSIZE_MAXIMUM245,5934 +# define YY_(YY_251,6104 +# define YY_(YY_255,6190 +# define YY_ATTRIBUTE(YY_ATTRIBUTE263,6447 +# define YY_ATTRIBUTE(YY_ATTRIBUTE265,6503 +# define YY_ATTRIBUTE_PURE YY_ATTRIBUTE_PURE270,6586 +# define YY_ATTRIBUTE_UNUSED YY_ATTRIBUTE_UNUSED274,6677 +# define _Noreturn _Noreturn280,6874 +# define _Noreturn _Noreturn282,6923 +# define YYUSE(YYUSE288,7083 +# define YYUSE(YYUSE290,7120 +# define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN295,7295 +# define YY_IGNORE_MAYBE_UNINITIALIZED_END YY_IGNORE_MAYBE_UNINITIALIZED_END299,7505 +# define YY_INITIAL_VALUE(YY_INITIAL_VALUE302,7591 +# define YY_IGNORE_MAYBE_UNINITIALIZED_BEGINYY_IGNORE_MAYBE_UNINITIALIZED_BEGIN305,7681 +# define YY_IGNORE_MAYBE_UNINITIALIZED_ENDYY_IGNORE_MAYBE_UNINITIALIZED_END306,7726 +# define YY_INITIAL_VALUE(YY_INITIAL_VALUE309,7801 +# define YYSTACK_ALLOC YYSTACK_ALLOC320,8049 +# define YYSTACK_ALLOC YYSTACK_ALLOC324,8212 +# define alloca alloca327,8332 +# define YYSTACK_ALLOC YYSTACK_ALLOC329,8368 +# define EXIT_SUCCESS EXIT_SUCCESS334,8598 +# define YYSTACK_FREE(YYSTACK_FREE343,8748 +# define YYSTACK_ALLOC_MAXIMUM YYSTACK_ALLOC_MAXIMUM349,9138 +# define YYSTACK_ALLOC YYSTACK_ALLOC352,9220 +# define YYSTACK_FREE YYSTACK_FREE353,9253 +# define YYSTACK_ALLOC_MAXIMUM YYSTACK_ALLOC_MAXIMUM355,9315 +# define EXIT_SUCCESS EXIT_SUCCESS362,9612 +# define YYMALLOC YYMALLOC366,9677 +# define YYFREE YYFREE372,9850 +union yyallocyyalloc386,10246 + yytype_int16 yyss_alloc;yyss_alloc388,10262 + YYSTYPE yyvs_alloc;yyvs_alloc389,10289 +# define YYSTACK_GAP_MAXIMUM YYSTACK_GAP_MAXIMUM393,10390 +# define YYSTACK_BYTES(YYSTACK_BYTES397,10538 +# define YYCOPY_NEEDED YYCOPY_NEEDED401,10653 +# define YYSTACK_RELOCATE(YYSTACK_RELOCATE408,10962 +# define YYCOPY(YYCOPY426,11841 +# define YYCOPY(YYCOPY429,11947 +#define YYFINAL YYFINAL442,12416 +#define YYLAST YYLAST444,12476 +#define YYNTOKENS YYNTOKENS447,12539 +#define YYNNTS YYNNTS449,12602 +#define YYNRULES YYNRULES451,12657 +#define YYNSTATES YYNSTATES453,12717 +#define YYUNDEFTOK YYUNDEFTOK457,12857 +#define YYMAXUTOK YYMAXUTOK458,12879 +#define YYTRANSLATE(YYTRANSLATE460,12904 +static const yytype_uint8 yytranslate[] =yytranslate465,13180 +static const yytype_uint16 yyrline[] =yyrline502,15546 +static const char *const yytname[] =yytname522,16610 +static const yytype_uint16 yytoknum[] =yytoknum552,18299 +#define YYPACT_NINF YYPACT_NINF563,18727 +#define yypact_value_is_default(yypact_value_is_default565,18753 +#define YYTABLE_NINF YYTABLE_NINF568,18825 +#define yytable_value_is_error(yytable_value_is_error570,18850 +static const yytype_int16 yypact[] =yypact575,18992 +static const yytype_uint8 yydefact[] =yydefact602,20659 +static const yytype_int16 yypgoto[] =yypgoto627,22171 +static const yytype_int16 yydefgoto[] =yydefgoto637,22582 +static const yytype_uint8 yytable[] =yytable649,23168 +static const yytype_int16 yycheck[] =yycheck698,26482 +static const yytype_uint8 yystos[] =yystos749,29897 +static const yytype_uint8 yyr1[] =yyr1774,31447 +static const yytype_uint8 yyr2[] =yyr2791,32404 +#define yyerrok yyerrok808,33286 +#define yyclearin yyclearin809,33328 +#define YYEMPTY YYEMPTY810,33371 +#define YYEOF YYEOF811,33400 +#define YYACCEPT YYACCEPT813,33427 +#define YYABORT YYABORT814,33468 +#define YYERROR YYERROR815,33508 +#define YYRECOVERING(YYRECOVERING818,33550 +#define YYBACKUP(YYBACKUP820,33591 +#define YYTERROR YYTERROR838,34606 +#define YYERRCODE YYERRCODE839,34632 +# define YYFPRINTF YYFPRINTF848,34789 +# define YYDPRINTF(YYDPRINTF851,34826 +# define YY_LOCATION_PRINT(YY_LOCATION_PRINT859,35122 +# define YY_SYMBOL_PRINT(YY_SYMBOL_PRINT863,35180 +yy_symbol_value_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep)yy_symbol_value_print880,35981 +yy_symbol_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep)yy_symbol_print899,36380 +yy_stack_print (yytype_int16 *yybottom, yytype_int16 *yytop)yy_stack_print914,36944 +# define YY_STACK_PRINT(YY_STACK_PRINT925,37194 +yy_reduce_print (yytype_int16 *yyssp, YYSTYPE *yyvsp, int yyrule)yy_reduce_print937,37641 +# define YY_REDUCE_PRINT(YY_REDUCE_PRINT956,38249 +int yydebug;yydebug964,38539 +# define YYDPRINTF(YYDPRINTF966,38573 +# define YY_SYMBOL_PRINT(YY_SYMBOL_PRINT967,38598 +# define YY_STACK_PRINT(YY_STACK_PRINT968,38653 +# define YY_REDUCE_PRINT(YY_REDUCE_PRINT969,38690 +# define YYINITDEPTH YYINITDEPTH975,38824 +# define YYMAXDEPTH YYMAXDEPTH986,39180 +# define yystrlen yystrlen994,39299 +yystrlen (const char *yystr)yystrlen998,39385 +# define yystpcpy yystpcpy1010,39611 +yystpcpy (char *yydest, const char *yysrc)yystpcpy1015,39749 +yytnamerr (char *yyres, const char *yystr)yytnamerr1037,40397 +yysyntax_error (YYSIZE_T *yymsg_alloc, char **yymsg,yysyntax_error1085,41559 +# define YYCASE_(YYCASE_1161,44945 +# undef YYCASE_YYCASE_1171,45496 +yydestruct (const char *yymsg, int yytype, YYSTYPE *yyvaluep)yydestruct1217,46620 +int yychar;yychar1233,46928 +YYSTYPE yylval;yylval1236,46992 +int yynerrs;yynerrs1238,47047 +yyparse (void)yyparse1246,47109 +#define YYPOPSTACK(YYPOPSTACK1286,48119 +TranslationUnit* g_root; // Definition of variable (to match declaration earlier)g_root2398,82507 +TranslationUnit* parseAST() {parseAST2400,82590 + +src/function.cpp,249 +Function::Function(const std::string& _id, Declaration* _parameter_list, Statement* _statement)Function6,45 +void Function::print() constprint10,215 +void Function::printxml() constprintxml21,403 +void Function::printasm() constprintasm46,1054 + +src/c_lexer.yy.cpp,9407 +#define YY_INT_ALIGNED YY_INT_ALIGNED5,82 +#define FLEX_SCANNERFLEX_SCANNER9,160 +#define YY_FLEX_MAJOR_VERSION YY_FLEX_MAJOR_VERSION10,181 +#define YY_FLEX_MINOR_VERSION YY_FLEX_MINOR_VERSION11,213 +#define YY_FLEX_SUBMINOR_VERSION YY_FLEX_SUBMINOR_VERSION12,245 +#define FLEX_BETAFLEX_BETA14,313 +#define FLEXINT_HFLEXINT_H30,611 +#define __STDC_LIMIT_MACROS __STDC_LIMIT_MACROS40,922 +typedef int8_t flex_int8_t;flex_int8_t44,982 +typedef uint8_t flex_uint8_t;flex_uint8_t45,1010 +typedef int16_t flex_int16_t;flex_int16_t46,1040 +typedef uint16_t flex_uint16_t;flex_uint16_t47,1070 +typedef int32_t flex_int32_t;flex_int32_t48,1102 +typedef uint32_t flex_uint32_t;flex_uint32_t49,1132 +typedef signed char flex_int8_t;flex_int8_t51,1170 +typedef short int flex_int16_t;flex_int16_t52,1203 +typedef int flex_int32_t;flex_int32_t53,1235 +typedef unsigned char flex_uint8_t; flex_uint8_t54,1261 +typedef unsigned short int flex_uint16_t;flex_uint16_t55,1298 +typedef unsigned int flex_uint32_t;flex_uint32_t56,1340 +#define INT8_MIN INT8_MIN60,1426 +#define INT16_MIN INT16_MIN63,1489 +#define INT32_MIN INT32_MIN66,1556 +#define INT8_MAX INT8_MAX69,1627 +#define INT16_MAX INT16_MAX72,1689 +#define INT32_MAX INT32_MAX75,1753 +#define UINT8_MAX UINT8_MAX78,1822 +#define UINT16_MAX UINT16_MAX81,1886 +#define UINT32_MAX UINT32_MAX84,1952 +#define YY_USE_CONSTYY_USE_CONST94,2122 +#define YY_USE_CONSTYY_USE_CONST101,2243 +#define yyconst yyconst107,2345 +#define yyconstyyconst109,2373 +#define YY_NULL YY_NULL113,2430 +#define YY_SC_TO_UI(YY_SC_TO_UI120,2677 +#define BEGIN BEGIN126,2908 +#define YY_START YY_START132,3107 +#define YYSTATE YYSTATE133,3147 +#define YY_STATE_EOF(YY_STATE_EOF136,3230 +#define YY_NEW_FILE YY_NEW_FILE139,3350 +#define YY_END_OF_BUFFER_CHAR YY_END_OF_BUFFER_CHAR141,3389 +#define YY_BUF_SIZE YY_BUF_SIZE150,3655 +#define YY_BUF_SIZE YY_BUF_SIZE152,3687 +#define YY_STATE_BUF_SIZE YY_STATE_BUF_SIZE158,3837 +#define YY_TYPEDEF_YY_BUFFER_STATEYY_TYPEDEF_YY_BUFFER_STATE161,3945 +typedef struct yy_buffer_state *YY_BUFFER_STATE;YY_BUFFER_STATE162,3980 +#define YY_TYPEDEF_YY_SIZE_TYY_TYPEDEF_YY_SIZE_T166,4066 +typedef size_t yy_size_t;yy_size_t167,4095 +#define EOB_ACT_CONTINUE_SCAN EOB_ACT_CONTINUE_SCAN174,4183 +#define EOB_ACT_END_OF_FILE EOB_ACT_END_OF_FILE175,4215 +#define EOB_ACT_LAST_MATCH EOB_ACT_LAST_MATCH176,4245 + #define YY_LESS_LINENO(YY_LESS_LINENO178,4275 + #define YY_LINENO_REWIND_TO(YY_LINENO_REWIND_TO179,4305 +#define yyless(yyless182,4427 +#define unput(unput195,4782 +#define YY_STRUCT_YY_BUFFER_STATEYY_STRUCT_YY_BUFFER_STATE198,4862 +struct yy_buffer_stateyy_buffer_state199,4896 + FILE *yy_input_file;yy_input_file201,4922 + char *yy_ch_buf; /* input buffer */yy_ch_buf203,4945 + char *yy_buf_pos; /* current position in input buffer */yy_buf_pos204,4983 + yy_size_t yy_buf_size;yy_buf_size209,5126 + int yy_n_chars;yy_n_chars214,5236 + int yy_is_our_buffer;yy_is_our_buffer220,5398 + int yy_is_interactive;yy_is_interactive227,5636 + int yy_at_bol;yy_at_bol233,5803 + int yy_bs_lineno; /**< The line count. */yy_bs_lineno235,5820 + int yy_bs_column; /**< The column count. */yy_bs_column236,5866 + int yy_fill_buffer;yy_fill_buffer241,6001 + int yy_buffer_status;yy_buffer_status243,6023 +#define YY_BUFFER_NEW YY_BUFFER_NEW245,6047 +#define YY_BUFFER_NORMAL YY_BUFFER_NORMAL246,6071 +#define YY_BUFFER_EOF_PENDING YY_BUFFER_EOF_PENDING257,6569 +static size_t yy_buffer_stack_top = 0; /**< index of top of stack. */yy_buffer_stack_top263,6677 +static size_t yy_buffer_stack_max = 0; /**< capacity of stack. */yy_buffer_stack_max264,6747 +static YY_BUFFER_STATE * yy_buffer_stack = 0; /**< Stack as an array. */yy_buffer_stack265,6813 +#define YY_CURRENT_BUFFER YY_CURRENT_BUFFER273,7081 +#define YY_CURRENT_BUFFER_LVALUE YY_CURRENT_BUFFER_LVALUE280,7377 +static char yy_hold_char;yy_hold_char283,7519 +static int yy_n_chars; /* number of characters read into yy_ch_buf */yy_n_chars284,7545 +yy_size_t yyleng;yyleng285,7616 +static char *yy_c_buf_p = (char *) 0;yy_c_buf_p288,7680 +static int yy_init = 0; /* whether we need to initialize */yy_init289,7718 +static int yy_start = 0; /* start state number */yy_start290,7779 +static int yy_did_buffer_switch_on_eof;yy_did_buffer_switch_on_eof295,7958 +#define YY_FLUSH_BUFFER YY_FLUSH_BUFFER309,8478 +#define yy_new_buffer yy_new_buffer319,8820 +#define yy_set_interactive(yy_set_interactive321,8860 +#define yy_set_bol(yy_set_bol331,9136 +#define YY_AT_BOL(YY_AT_BOL341,9379 +#define yywrap(yywrap345,9462 +#define YY_SKIP_YYWRAPYY_SKIP_YYWRAP346,9496 +typedef unsigned char YY_CHAR;YY_CHAR348,9520 +FILE *yyin = (FILE *) 0, *yyout = (FILE *) 0;yyin350,9552 +FILE *yyin = (FILE *) 0, *yyout = (FILE *) 0;yyout350,9552 +typedef int yy_state_type;yy_state_type352,9599 +int yylineno = 1;yylineno356,9649 +#undef yytext_ptryytext_ptr360,9707 +#define yytext_ptr yytext_ptr362,9732 +#define YY_DO_BEFORE_ACTION YY_DO_BEFORE_ACTION375,10161 +#define YY_NUM_RULES YY_NUM_RULES382,10324 +#define YY_END_OF_BUFFER YY_END_OF_BUFFER383,10348 +struct yy_trans_infoyy_trans_info386,10457 + flex_int32_t yy_verify;yy_verify388,10481 + flex_int32_t yy_nxt;yy_nxt389,10506 +static yyconst flex_int16_t yy_accept[166] =yy_accept391,10532 +static yyconst YY_CHAR yy_ec[256] =yy_ec413,11672 +static yyconst YY_CHAR yy_meta[55] =yy_meta445,13389 +static yyconst flex_uint16_t yy_base[168] =yy_base455,13799 +static yyconst flex_int16_t yy_def[168] =yy_def477,14950 +static yyconst flex_uint16_t yy_nxt[284] =yy_nxt499,16099 +static yyconst flex_int16_t yy_chk[284] =yy_chk534,18006 +static yy_state_type yy_last_accepting_state;yy_last_accepting_state569,19912 +static char *yy_last_accepting_cpos;yy_last_accepting_cpos570,19958 +int yy_flex_debug = 0;yy_flex_debug573,20022 +#define REJECT REJECT578,20148 +#define yymore(yymore579,20192 +#define YY_MORE_ADJ YY_MORE_ADJ580,20238 +#define YY_RESTORE_YY_MORE_OFFSETYY_RESTORE_YY_MORE_OFFSET581,20260 +char *yytext;yytext582,20294 +#define INITIAL INITIAL590,20457 +#define YY_EXTRA_TYPE YY_EXTRA_TYPE601,20759 +#define YY_READ_BUF_SIZE YY_READ_BUF_SIZE675,22080 +#define YY_READ_BUF_SIZE YY_READ_BUF_SIZE677,22117 +#define ECHO ECHO686,22357 +#define YY_INPUT(YY_INPUT693,22567 +#define yyterminate(yyterminate730,23481 +#define YY_START_STACK_INCR YY_START_STACK_INCR735,23616 +#define YY_FATAL_ERROR(YY_FATAL_ERROR740,23706 +#define YY_DECL_IS_OURS YY_DECL_IS_OURS749,23939 +#define YY_DECL YY_DECL753,23992 +#define YY_USER_ACTIONYY_USER_ACTION760,24168 +#define YY_BREAK YY_BREAK765,24261 +#define YY_RULE_SETUP YY_RULE_SETUP768,24303 +static int yy_get_next_buffer (void)yy_get_next_buffer1268,35271 + static yy_state_type yy_get_previous_state (void)yy_get_previous_state1402,38843 + static yy_state_type yy_try_NUL_trans (yy_state_type yy_current_state )yy_try_NUL_trans1434,39718 + static void yyunput (int c, char * yy_bp )yyunput1459,40385 + static int yyinput (void)yyinput1500,41414 + void yyrestart (FILE * input_file )yyrestart1577,43181 + void yy_switch_to_buffer (YY_BUFFER_STATE new_buffer )yy_switch_to_buffer1594,43542 +static void yy_load_buffer_state (void)yy_load_buffer_state1625,44385 + YY_BUFFER_STATE yy_create_buffer (FILE * file, int size )yy_create_buffer1639,44858 + void yy_delete_buffer (YY_BUFFER_STATE b )yy_delete_buffer1667,45560 + static void yy_init_buffer (YY_BUFFER_STATE b, FILE * file )yy_init_buffer1686,46009 + void yy_flush_buffer (YY_BUFFER_STATE b )yy_flush_buffer1714,46742 +void yypush_buffer_state (YY_BUFFER_STATE new_buffer )yypush_buffer_state1743,47428 +void yypop_buffer_state (void)yypop_buffer_state1773,48193 +static void yyensure_buffer_stack (void)yyensure_buffer_stack1792,48588 +YY_BUFFER_STATE yy_scan_buffer (char * base, yy_size_t size )yy_scan_buffer1841,50272 +YY_BUFFER_STATE yy_scan_string (yyconst char * yystr )yy_scan_string1878,51345 +YY_BUFFER_STATE yy_scan_bytes (yyconst char * yybytes, yy_size_t _yybytes_len )yy_scan_bytes1891,51762 +#define YY_EXIT_FAILURE YY_EXIT_FAILURE1922,52497 +static void yy_fatal_error (yyconst char* msg )yy_fatal_error1925,52531 +#undef yylessyyless1933,52708 +#define yyless(yyless1934,52722 +int yyget_lineno (void)yyget_lineno1953,53181 +FILE *yyget_in (void)yyget_in1962,53275 +FILE *yyget_out (void)yyget_out1970,53359 +yy_size_t yyget_leng (void)yyget_leng1978,53459 +char *yyget_text (void)yyget_text1987,53552 +void yyset_lineno (int _line_number )yyset_lineno1996,53681 +void yyset_in (FILE * _in_str )yyset_in2008,53909 +void yyset_out (FILE * _out_str )yyset_out2013,53972 +int yyget_debug (void)yyget_debug2018,54039 +void yyset_debug (int _bdebug )yyset_debug2023,54098 +static int yy_init_globals (void)yy_init_globals2028,54170 +int yylex_destroy (void)yylex_destroy2057,54852 +static void yy_flex_strncpy (char* s1, yyconst char * s2, int n )yy_flex_strncpy2083,55406 +static int yy_flex_strlen (yyconst char * s )yy_flex_strlen2093,55561 +void *yyalloc (yy_size_t size )yyalloc2103,55669 +void *yyrealloc (void * ptr, yy_size_t size )yyrealloc2108,55742 +void yyfree (void * ptr )yyfree2121,56201 +#define YYTABLES_NAME YYTABLES_NAME2126,56297 +void yyerror(char const *s) {yyerror2132,56373 + +src/expression.cpp,312 +void Expression::print() constprint6,47 +void Expression::printxml() constprintxml9,82 +Identifier::Identifier(const std::string& id)Identifier15,147 +void Identifier::printasm() constprintasm19,212 +Constant::Constant(const int32_t& constant)Constant25,275 +void Constant::printasm() constprintasm29,350 + +src/statement.cpp,2074 +Statement::Statement(Statement* statement)Statement6,59 +void Statement::addStatement(Statement* _next)addStatement10,138 +CompoundStatement::CompoundStatement(Declaration* decl, Statement* statement)CompoundStatement18,253 +CompoundStatement::CompoundStatement(Statement* statement)CompoundStatement22,391 +void CompoundStatement::print() constprint26,483 +void CompoundStatement::printxml() constprintxml35,629 +void CompoundStatement::printasm() constprintasm51,945 +void CompoundStatement::count_variables(int32_t& var_count) constcount_variables62,1154 +void CompoundStatement::make_variables(VariableStack var_stack, int32_t& var_count) constmake_variables87,1715 +SelectionStatement::SelectionStatement(Statement* _if, Statement* _else)SelectionStatement95,1851 +void SelectionStatement::print() constprint98,1972 +void SelectionStatement::printxml() constprintxml104,2056 +void SelectionStatement::printasm() constprintasm114,2256 +void SelectionStatement::count_variables(int32_t& var_count) constcount_variables117,2302 +ExpressionStatement::ExpressionStatement(Expression* expr)ExpressionStatement132,2614 +void ExpressionStatement::print() constprint136,2709 +void ExpressionStatement::printxml() constprintxml139,2753 +void ExpressionStatement::printasm() constprintasm142,2800 +void ExpressionStatement::count_variables(int32_t& var_count) constcount_variables145,2847 +JumpStatement::JumpStatement(Expression* expr)JumpStatement154,3030 +void JumpStatement::print() constprint158,3100 +void JumpStatement::printxml() constprintxml161,3138 +void JumpStatement::printasm() constprintasm167,3243 +void JumpStatement::count_variables(int32_t& var_count) constcount_variables172,3309 +IterationStatement::IterationStatement(Statement* statement)IterationStatement181,3491 +void IterationStatement::print() constprint185,3585 +void IterationStatement::printxml() constprintxml188,3628 +void IterationStatement::printasm() constprintasm196,3795 +void IterationStatement::count_variables(int32_t& var_count) constcount_variables199,3841 + +src/c_parser.tab.hpp,2446 +# define YY_YY_C_COMPILER_SRC_C_PARSER_TAB_HPP_INCLUDEDYY_YY_C_COMPILER_SRC_C_PARSER_TAB_HPP_INCLUDED34,1569 +# define YYDEBUG YYDEBUG37,1662 +# define YYTOKENTYPEYYTOKENTYPE61,2201 + enum yytokentypeyytokentype62,2222 + T_IDENTIFIER = 258,T_IDENTIFIER64,2245 + T_SC = 259,T_SC65,2269 + T_CMA = 260,T_CMA66,2285 + T_LRB = 261,T_LRB67,2302 + T_LCB = 262,T_LCB68,2319 + T_RCB = 263,T_RCB69,2336 + T_LSB = 264,T_LSB70,2353 + T_RSB = 265,T_RSB71,2370 + T_QU = 266,T_QU72,2387 + T_COL = 267,T_COL73,2403 + T_LOG_OR = 268,T_LOG_OR74,2420 + T_LOG_AND = 269,T_LOG_AND75,2440 + T_OR = 270,T_OR76,2461 + T_XOR = 271,T_XOR77,2477 + T_AND = 272,T_AND78,2494 + T_EQUALITY_OP = 273,T_EQUALITY_OP79,2511 + T_REL_OP = 274,T_REL_OP80,2536 + T_SHIFT_OP = 275,T_SHIFT_OP81,2556 + T_MULT = 276,T_MULT82,2578 + T_DIV = 277,T_DIV83,2596 + T_REM = 278,T_REM84,2613 + T_TILDE = 279,T_TILDE85,2630 + T_NOT = 280,T_NOT86,2649 + T_DOT = 281,T_DOT87,2666 + T_ARROW = 282,T_ARROW88,2683 + T_INCDEC = 283,T_INCDEC89,2702 + T_ADDSUB_OP = 284,T_ADDSUB_OP90,2722 + T_ASSIGN_OPER = 285,T_ASSIGN_OPER91,2745 + T_EQ = 286,T_EQ92,2770 + T_SIZEOF = 287,T_SIZEOF93,2786 + T_INT_CONST = 288,T_INT_CONST94,2806 + T_IF = 289,T_IF95,2829 + T_WHILE = 290,T_WHILE96,2845 + T_DO = 291,T_DO97,2864 + T_FOR = 292,T_FOR98,2880 + T_RETURN = 293,T_RETURN99,2897 + T_VOID = 294,T_VOID100,2917 + T_CHAR = 295,T_CHAR101,2935 + T_SCHAR = 296,T_SCHAR102,2953 + T_UCHAR = 297,T_UCHAR103,2972 + T_SSINT = 298,T_SSINT104,2991 + T_USINT = 299,T_USINT105,3010 + T_LINT = 300,T_LINT106,3029 + T_ULINT = 301,T_ULINT107,3047 + T_UINT = 302,T_UINT108,3066 + T_SINT = 303,T_SINT109,3084 + T_RRB = 304,T_RRB110,3102 + T_ELSE = 305T_ELSE111,3119 +union YYSTYPEYYSTYPE118,3224 + Node* node;node122,3297 + TranslationUnit* trans_unit;trans_unit123,3313 + Function* function;function124,3346 + Statement* statement;statement125,3370 + Declaration* declaration;declaration126,3396 + Expression* expression;expression127,3426 + Type* type;type128,3454 + double number;number129,3470 + std::string* string;string130,3489 +typedef union YYSTYPE YYSTYPE;YYSTYPE135,3582 +# define YYSTYPE_IS_TRIVIAL YYSTYPE_IS_TRIVIAL136,3613 +# define YYSTYPE_IS_DECLARED YYSTYPE_IS_DECLARED137,3643 + +src/compiler_main.cpp,43 +int main(int argc, char *argv[])main5,41 + +src/declaration.cpp,654 +Declaration::Declaration(const std::string& _id)Declaration6,48 +void Declaration::print() constprint10,115 +void Declaration::printxml() constprintxml19,255 +void Declaration::printasm() constprintasm32,504 +void Declaration::addDeclaration(Declaration* _next_decl)addDeclaration42,746 +void Declaration::addList(Declaration* _next_decl)addList47,837 +void Declaration::setType(Type* _type)setType52,926 +Declaration* Declaration::getNext() constgetNext57,988 +Declaration* Declaration::getNextListItem() constgetNextListItem62,1057 +std::string Declaration::getId() constgetId67,1139 +std::string Declaration::getType() constgetType72,1198 + +src/translation_unit.cpp,276 +TranslationUnit::TranslationUnit(Node* decl)TranslationUnit6,53 +void TranslationUnit::print() constprint11,119 +void TranslationUnit::printxml() constprintxml18,223 +void TranslationUnit::printasm() constprintasm27,445 +void TranslationUnit::push(Node* decl)push34,555 + +src/type.cpp,626 +void Type::print() constprint6,41 +void Type::printxml() constprintxml11,119 +void Type::printasm() constprintasm14,151 +Pointer::Pointer(Type* _pointer_type) : pointer_type(_pointer_type)Pointer20,207 +std::string Pointer::getType() constgetType23,279 +Array::Array(Type* _array_type, int32_t _size) : size(_size), array_type(_array_type)Array31,392 +std::string Array::getType() constgetType34,482 +Void::Void()Void42,588 +std::string Void::getType() constgetType45,605 +Int::Int()Int53,682 +std::string Int::getType() constgetType56,697 +Char::Char()Char64,774 +std::string Char::getType() constgetType67,791 + +src/c_parser.y,1943 +ROOT:ROOT74,2156 +TranslationUnit:TranslationUnit80,2226 +ExternalDeclaration:ExternalDeclaration85,2367 +FunctionDefinition:FunctionDefinition92,2492 +ParameterList:ParameterList96,2628 +Parameter:Parameter102,2787 +DeclarationList:DeclarationList108,2881 +Declaration:Declaration113,2998 +DeclarationSpec:DeclarationSpec125,3233 +InitDeclaratorList:InitDeclaratorList138,3543 +InitDeclarator:InitDeclarator143,3695 +Declarator:Declarator148,3831 +DirectDeclarator:DirectDeclarator153,3920 +IdentifierList:IdentifierList162,4241 +StatementList:StatementList168,4380 +Statement:Statement173,4487 +CompoundStatement:CompoundStatement181,4678 +CompoundStatement_2:CompoundStatement_2185,4743 +SelectionStatement:SelectionStatement192,5008 +ExpressionStatement:ExpressionStatement197,5209 +JumpStatement:JumpStatement202,5332 +IterationStatement:IterationStatement206,5413 +Expression:Expression214,5664 +AssignmentExpression:AssignmentExpression218,5717 +ASSIGN_OPER:ASSIGN_OPER223,5846 +ConditionalExpression:ConditionalExpression228,5899 +LogicalOrExpression:LogicalOrExpression233,6042 +LogicalAndExpression:LogicalAndExpression238,6170 +InclusiveOrExpression:InclusiveOrExpression243,6303 +ExclusiveOrExpression:ExclusiveOrExpression248,6433 +AndExpression:AndExpression253,6548 +EqualityExpression:EqualityExpression258,6657 +RelationalExpression:RelationalExpression263,6795 +ShiftExpression:ShiftExpression268,6921 +AdditiveExpression:AdditiveExpression273,7039 +MultiplicativeExpression:MultiplicativeExpression278,7176 +MultDivRemOP:MultDivRemOP283,7306 +CastExpression:CastExpression289,7391 +UnaryExpression:UnaryExpression294,7502 +UnaryOperator:UnaryOperator302,7750 +PostfixExpression:PostfixExpression310,7888 +PostfixExpression2:PostfixExpression2319,8212 +ArgumentExpressionList:ArgumentExpressionList324,8329 +PrimaryExpression:PrimaryExpression329,8460 +Constant:Constant335,8599 + +test/out/03.s,16 +main:main3,20 + +test/out/01.s,16 +main:main3,20 + +test/out/04.s,28 +main:main3,20 +f:f14,153 + +test/out/05.s,0 + +test/out/02.s,16 +main:main3,20 + +test/in/02.c,22 +int main() {main1,0 + +test/in/05.c,44 +char *glob;glob1,0 +int main() {main3,13 + +test/in/01.c,22 +int main() {main1,0 + +test/in/03.c,22 +int main() {main1,0 + +test/in/04.c,51 +int main() {main1,0 +int f(int a, int v) {f7,55 + +test/ref/03.s,18 +main:main15,207 + +test/ref/01.s,18 +main:main15,207 + +test/ref/04.s,30 +main:main15,207 +f:f47,695 + +test/ref/05.s,36 +$LC0:$LC012,145 +main:main21,267 + +test/ref/02.s,18 +main:main15,207 diff --git a/c_compiler/include/ast.hpp b/c_compiler/include/ast.hpp new file mode 100644 index 0000000..88ef36b --- /dev/null +++ b/c_compiler/include/ast.hpp @@ -0,0 +1,24 @@ +#ifndef AST_HPP +#define AST_HPP + +#include <vector> +#include <string> +#include <iostream> +#include <map> +#include <cstdint> + +struct VarLocation; + +typedef std::map<std::string, VarLocation> VariableStackBindings; + +#include "node.hpp" +#include "type.hpp" +#include "expression.hpp" +#include "declaration.hpp" +#include "statement.hpp" +#include "function.hpp" +#include "translation_unit.hpp" + +Node* parseAST(); + +#endif diff --git a/c_compiler/include/declaration.hpp b/c_compiler/include/declaration.hpp new file mode 100644 index 0000000..8226090 --- /dev/null +++ b/c_compiler/include/declaration.hpp @@ -0,0 +1,35 @@ +#ifndef AST_DECLARATION_HPP +#define AST_DECLARATION_HPP + +#include "node.hpp" + +class Expression; + + +class Declaration : public Node { +private: + Type* type; + std::string id; + Expression* init; + Declaration* next_decl; + Declaration* list_next_decl; + +public: + Declaration(const std::string& _id = ""); + + virtual void print() const; + virtual void printxml() const; + virtual VariableStackBindings printasm(VariableStackBindings bindings) const; + + void addDeclaration(Declaration* _next_decl); + void addList(Declaration* _next_decl); + + void setType(Type* _type); + + Declaration* getNext() const; + Declaration* getNextListItem() const; + std::string getId() const; + std::string getType() const; +}; + +#endif diff --git a/c_compiler/include/expression.hpp b/c_compiler/include/expression.hpp new file mode 100644 index 0000000..d446972 --- /dev/null +++ b/c_compiler/include/expression.hpp @@ -0,0 +1,35 @@ +#ifndef AST_EXPRESSION_HPP +#define AST_EXPRESSION_HPP + +#include "node.hpp" + + +class Expression : public Node { +public: + virtual void print() const; + virtual void printxml() const; + virtual VariableStackBindings printasm(VariableStackBindings bindings) const = 0; +}; + + +class Identifier : public Expression { +private: + std::string m_id; +public: + Identifier(const std::string& id); + + virtual VariableStackBindings printasm(VariableStackBindings bindings) const; +}; + + +class Constant : public Expression { +private: + int32_t m_constant; +public: + Constant(const int32_t& constant); + + virtual VariableStackBindings printasm(VariableStackBindings bindings) const; +}; + + +#endif diff --git a/c_compiler/include/function.hpp b/c_compiler/include/function.hpp new file mode 100644 index 0000000..77d1372 --- /dev/null +++ b/c_compiler/include/function.hpp @@ -0,0 +1,26 @@ +#ifndef AST_FUNCTION_HPP +#define AST_FUNCTION_HPP + +#include "node.hpp" + +class Declaration; +class Statement; + + +class Function : public Node { +protected: + Type* type; + std::string id; + Declaration* parameter_list; + Statement* statement; + +public: + Function(const std::string& _id, Declaration* _parameter_list, Statement* _statement); + + virtual void print() const; + virtual void printxml() const; + virtual VariableStackBindings printasm(VariableStackBindings bindings) const; +}; + + +#endif diff --git a/c_compiler/include/node.hpp b/c_compiler/include/node.hpp new file mode 100644 index 0000000..584ed32 --- /dev/null +++ b/c_compiler/include/node.hpp @@ -0,0 +1,30 @@ +#ifndef NODE_HPP +#define NODE_HPP + +#include <cstdint> +#include <map> +#include <string> + +struct VarLocation; +class Type; + +typedef std::map<std::string, VarLocation> VariableStackBindings; + + +struct VarLocation { + Type* type; + int32_t stack_position; +}; + + +class Node { +public: + virtual ~Node() {} + + virtual void print() const = 0; + virtual void printxml() const = 0; + virtual VariableStackBindings printasm(VariableStackBindings bindings) const = 0; +}; + + +#endif diff --git a/c_compiler/include/statement.hpp b/c_compiler/include/statement.hpp new file mode 100644 index 0000000..b5f7e9c --- /dev/null +++ b/c_compiler/include/statement.hpp @@ -0,0 +1,101 @@ +#ifndef AST_STATEMENT_HPP +#define AST_STATEMENT_HPP + +#include "node.hpp" + +class Declaration; +class Expression; + + +class Statement : public Node { +protected: + Statement* next_statement; + +public: + Statement(Statement* statement = nullptr); + + virtual void print() const = 0; + virtual void printxml() const = 0; + virtual VariableStackBindings printasm(VariableStackBindings bindings) const = 0; + + virtual void count_variables(int32_t& var_count) const = 0; + + void addStatement(Statement* _next); +}; + + +class CompoundStatement : public Statement { +protected: + Declaration* m_decl; + Statement* m_statement; + +public: + CompoundStatement(Declaration* decl = nullptr, Statement* statement = nullptr); + CompoundStatement(Statement* statement); + + virtual void print() const; + virtual void printxml() const; + virtual VariableStackBindings printasm(VariableStackBindings bindings) const; + + virtual void count_variables(int32_t& var_count) const; +}; + + +class SelectionStatement : public Statement { +protected: + Statement* m_if; + Statement* m_else; +public: + SelectionStatement(Statement* _if = nullptr, Statement* _else = nullptr); + + virtual void print() const; + virtual void printxml() const; + virtual VariableStackBindings printasm(VariableStackBindings bindings) const; + + virtual void count_variables(int32_t& var_count) const; +}; + + +class ExpressionStatement : public Statement { +protected: + Expression* m_expr; +public: + ExpressionStatement(Expression* expr = nullptr); + + virtual void print() const; + virtual void printxml() const; + virtual VariableStackBindings printasm(VariableStackBindings bindings) const; + + virtual void count_variables(int32_t& var_count) const; +}; + + +class JumpStatement : public Statement { +protected: + Expression* m_expr; +public: + JumpStatement(Expression* expr = nullptr); + + virtual void print() const; + virtual void printxml() const; + virtual VariableStackBindings printasm(VariableStackBindings bindings) const; + + virtual void count_variables(int32_t& var_count) const; +}; + + +class IterationStatement : public Statement { +protected: + Statement* m_statement; +public: + IterationStatement(Statement* statement); + + virtual void print() const; + virtual void printxml() const; + virtual VariableStackBindings printasm(VariableStackBindings bindings) const; + + virtual void count_variables(int32_t& var_count) const; +}; + + +#endif diff --git a/c_compiler/include/translation_unit.hpp b/c_compiler/include/translation_unit.hpp new file mode 100644 index 0000000..c290163 --- /dev/null +++ b/c_compiler/include/translation_unit.hpp @@ -0,0 +1,24 @@ +#ifndef TRANSLATION_UNIT_HPP +#define TRANSLATION_UNIT_HPP + +#include "node.hpp" + +#include <vector> + + +class TranslationUnit : public Node { +protected: + std::vector<Node* > translation_unit; + +public: + TranslationUnit(Node* decl); + + virtual void print() const; + virtual void printxml() const; + virtual VariableStackBindings printasm(VariableStackBindings bindings) const; + + void push(Node* decl); +}; + + +#endif diff --git a/c_compiler/include/type.hpp b/c_compiler/include/type.hpp new file mode 100644 index 0000000..3deca58 --- /dev/null +++ b/c_compiler/include/type.hpp @@ -0,0 +1,70 @@ +#ifndef TYPE_HPP +#define TYPE_HPP + +#include "node.hpp" + + +class Type : public Node { +public: + virtual void print() const; + virtual void printxml() const; + virtual VariableStackBindings printasm(VariableStackBindings bindings) const; + + virtual std::string getType() const = 0; +}; + + +class Specifier : public Type { +public: + virtual std::string getType() const = 0; +}; + + +class Pointer : public Type { +protected: + Type* pointer_type; + +public: + Pointer(Type* _pointer_type); + + virtual std::string getType() const; +}; + + +class Array : public Type { +protected: + int32_t size; + Type* array_type; + +public: + Array(Type* _array_type, int32_t _size = 0); + + virtual std::string getType() const; +}; + + +class Void : public Specifier { +public: + Void(); + + virtual std::string getType() const; +}; + + +class Int : public Specifier { +public: + Int(); + + virtual std::string getType() const; +}; + + +class Char : public Specifier { +public: + Char(); + + virtual std::string getType() const; +}; + + +#endif diff --git a/c_compiler/src/c_lexer.flex b/c_compiler/src/c_lexer.flex new file mode 100644 index 0000000..b225522 --- /dev/null +++ b/c_compiler/src/c_lexer.flex @@ -0,0 +1,96 @@ +%option noyywrap + +%{ + +#include "c_parser.tab.hpp" + +%} + +IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]* + +ASSIGNMENT_OPERATOR (([<>][<>]|[*\/%+\-&^|])[=]|[=]) + +INTEGERSUFFIX ([uU][lL]|[lL][uU]|[uUlL]) + +DECIMALCONSTANT ([1-9][0-9]*) +OCTALCONSTANT ([0][0-7]*) +HEXCONSTANT ([0][xX][0-9A-Fa-f]+) + +CHARCONSTANT ('(([\\]['])|([^']))+') + +STRINGLITERAL ["](([\\]["])|([^"]))*["] + +WHITESPACE [ \t\r\n]+ + +PREPROC [#][ ][0-9]+[ ]{STRINGLITERAL}[ 0-9]* + +ALL . + +%% + +(void) { return T_VOID; } +(char) { return T_CHAR; } +(signed[ ]char) { return T_SCHAR; } +(unsigned[ ]char) { return T_UCHAR; } +((short[ ]int)|(signed[ ]short[ ]int)|short|(signed[ ]short)) { return T_SSINT; } +((unsigned[ ]short[ ]int)|(unsigned[ ]short)) { return T_USINT; } +((signed[ ]long[ ]int)|(signed[ ]long)|(long[ ]int)|long) { return T_LINT; } +((unsigned[ ]long[ ]int)|(unsigned[ ]long)) { return T_ULINT; } +((unsigned[ ]int)|unsigned) { return T_UINT; } +((signed[ ]int)|int|signed) { return T_SINT; } + + +[;] { return T_SC; } +[,] { return T_CMA; } +[(] { return T_LRB; } +[)] { return T_RRB; } +[{] { return T_LCB; } +[}] { return T_RCB; } +[[] { return T_LSB; } +[]] { return T_RSB; } +[?] { return T_QU; } +[:] { return T_COL; } +[|][|] { return T_LOG_OR; } +[&][&] { return T_LOG_AND; } +[|] { return T_OR; } +[\^] { return T_XOR; } +[&] { return T_AND; } +[=][=] { return T_EQUALITY_OP; } +[!][=] { return T_EQUALITY_OP; } +([<>][=])|[<>] { return T_REL_OP; } +[<>][<>] { return T_SHIFT_OP; } +[*] { return T_MULT; } +[\/] { return T_DIV; } +[%] { return T_REM; } +[~] { return T_TILDE; } +[!] { return T_NOT; } +[.] { return T_DOT; } +[-][>] { return T_ARROW; } +[+-][+-] { return T_INCDEC; } +[+-] { return T_ADDSUB_OP; } +[=] { yylval.string = new std::string(yytext); return T_EQ; } + +{ASSIGNMENT_OPERATOR} { yylval.string = new std::string(yytext); return T_ASSIGN_OPER; } + +if { return T_IF; } +else { return T_ELSE; } +return { return T_RETURN; } +while { return T_WHILE; } +do { return T_DO; } +for { return T_FOR; } +sizeof { return T_SIZEOF; } + +{IDENTIFIER} { yylval.string = new std::string(yytext); return T_IDENTIFIER; } + +({HEXCONSTANT}|{OCTALCONSTANT}|{DECIMALCONSTANT}){INTEGERSUFFIX}? { yylval.number = strtol(yytext, NULL, 0); return T_INT_CONST; } + +{WHITESPACE} { ; } + +. { fprintf(stderr, "Invalid token\n"); exit(1); } + +%% + +void yyerror(char const *s) { + fprintf (stderr, "Parse error : %s\n", s); + exit(1); +} diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y new file mode 100644 index 0000000..a42463c --- /dev/null +++ b/c_compiler/src/c_parser.y @@ -0,0 +1,354 @@ +%code requires{ + +#include "node.hpp" +#include "translation_unit.hpp" +#include "function.hpp" +#include "declaration.hpp" +#include "statement.hpp" +#include "expression.hpp" +#include "type.hpp" + + +extern Node* g_root; // A way of getting the AST out + +//! This is to fix problems when generating C++ +// We are declaring the functions provided by Flex, so +// that Bison generated code can call them. +int yylex(void); +void yyerror(const char *); + +} + +// Represents the value associated with any kind of +// AST node. +%union{ + Node* node; + TranslationUnit* trans_unit; + Function* function; + Statement* statement; + Declaration* declaration; + Expression* expression; + Type* type; + double number; + std::string* string; +} + +%token T_IDENTIFIER T_SC T_CMA T_LRB T_LCB T_RCB T_LSB T_RSB T_QU T_COL T_LOG_OR + T_LOG_AND T_OR T_XOR T_AND T_EQUALITY_OP T_REL_OP T_SHIFT_OP T_MULT T_DIV + T_REM T_TILDE T_NOT T_DOT T_ARROW T_INCDEC T_ADDSUB_OP T_ASSIGN_OPER T_EQ + T_SIZEOF T_INT_CONST T_IF T_WHILE T_DO T_FOR T_RETURN + + T_VOID T_CHAR T_SCHAR T_UCHAR T_SSINT T_USINT T_LINT T_ULINT T_UINT T_SINT + +%nonassoc T_RRB +%nonassoc T_ELSE + + +%type <node> ExternalDeclaration + +%type <trans_unit> TranslationUnit + +%type <function> FunctionDefinition + +%type <statement> StatementList Statement CompoundStatement CompoundStatement_2 + SelectionStatement + ExpressionStatement JumpStatement IterationStatement + +%type <declaration> ParameterList Parameter DeclarationList Declaration InitDeclaratorList + InitDeclarator + IdentifierList + +%type <expression> Expression AssignmentExpression ConditionalExpression LogicalOrExpression + LogicalAndExpression InclusiveOrExpression ExclusiveOrExpression + AndExpression EqualityExpression RelationalExpression ShiftExpression + AdditiveExpression MultiplicativeExpression CastExpression UnaryExpression + PostfixExpression PostfixExpression2 ArgumentExpressionList PrimaryExpression + Constant + +%type <type> DeclarationSpec + +%type <string> Declarator DirectDeclarator + +%type <number> T_INT_CONST + +%type <string> T_IDENTIFIER ASSIGN_OPER T_ASSIGN_OPER T_EQ T_AND T_ADDSUB_OP T_TILDE T_NOT + T_MULT T_DIV T_REM MultDivRemOP UnaryOperator + +%start ROOT + +%% + +ROOT: + TranslationUnit { g_root = $1; } + ; + +// EXTERNAL DEFINITION + +TranslationUnit: + ExternalDeclaration { $$ = new TranslationUnit($1); } + | TranslationUnit ExternalDeclaration { $$->push($2); } + ; + +ExternalDeclaration: + Declaration { $$ = $1; } + | FunctionDefinition { $$ = $1; } + ; + +// FUNCTION DEFINITION + +FunctionDefinition: + DeclarationSpec T_IDENTIFIER T_LRB ParameterList T_RRB CompoundStatement { $$ = new Function(*$2, $4, $6); } + ; + +ParameterList: + %empty { $$ = new Declaration(); } + | Parameter { $$ = $1; } + | ParameterList T_CMA Parameter { $3->addDeclaration($$); $$ = $3;} + ; + +Parameter: + DeclarationSpec T_IDENTIFIER { $$ = new Declaration(*$2); } + ; + +// Declaration + +DeclarationList: + Declaration { $$ = $1; } + | DeclarationList Declaration { $2->addDeclaration($$); $$ = $2; } + ; + +Declaration: + DeclarationSpec InitDeclaratorList T_SC { + $$ = $2; + Declaration* tmp_decl = $2; + + while(tmp_decl != nullptr) { + tmp_decl->setType($1); + tmp_decl = tmp_decl->getNextListItem(); + } + } + ; + +DeclarationSpec: + T_VOID { $$ = new Void; } + | T_CHAR { $$ = new Char; } + | T_SCHAR { $$ = new Char; } + | T_UCHAR { $$ = new Char; } + | T_SSINT { $$ = new Int; } + | T_USINT { $$ = new Int; } + | T_LINT { $$ = new Int; } + | T_ULINT { $$ = new Int; } + | T_UINT { $$ = new Int; } + | T_SINT { $$ = new Int; } + ; + +InitDeclaratorList: + InitDeclarator { $$ = new Declaration(*$1); } + | InitDeclaratorList T_CMA InitDeclarator { $3->addList($$); $$ = $3; } + ; + +InitDeclarator: + Declarator { $$ = new Declaration(*$1); } + | Declarator T_EQ AssignmentExpression { $$ = new Declaration(*$1); } + ; + +Declarator: + DirectDeclarator { $$ = $1; } + | T_MULT DirectDeclarator { $$ = $2; } + ; + +DirectDeclarator: + T_IDENTIFIER { $$ = $1; } + | T_LRB Declarator T_RRB { $$ = $2; } + | DirectDeclarator T_LSB ConditionalExpression T_RSB { $$ = $1; } + | DirectDeclarator T_LSB T_RSB { $$ = $1; } + | DirectDeclarator T_LRB ParameterList T_RRB { $$ = $1; } + | DirectDeclarator T_LRB IdentifierList T_RRB { $$ = $1; } + ; + +IdentifierList: + T_IDENTIFIER { $$ = new Declaration(); } + | IdentifierList T_CMA T_IDENTIFIER { $$ = new Declaration(); } + +// Statement + +StatementList: + Statement { $$ = $1; } + | StatementList Statement { $2->addStatement($$); $$ = $2; } + ; + +Statement: + CompoundStatement { $$ = $1; } + | SelectionStatement { $$ = $1; } + | ExpressionStatement { $$ = $1; } + | JumpStatement { $$ = $1; } + | IterationStatement { $$ = $1; } + ; + +CompoundStatement: + T_LCB CompoundStatement_2 { $$ = $2; } + ; + +CompoundStatement_2: + T_RCB { $$ = new CompoundStatement; } + | DeclarationList T_RCB { $$ = new CompoundStatement($1); } + | DeclarationList StatementList T_RCB { $$ = new CompoundStatement($1, $2); } + | StatementList T_RCB { $$ = new CompoundStatement($1); } + ; + +SelectionStatement: + T_IF T_LRB Expression T_RRB Statement { $$ = new SelectionStatement($5); } + | T_IF T_LRB Expression T_RRB Statement T_ELSE Statement { $$ = new SelectionStatement($5, $7); } + ; + +ExpressionStatement: + T_SC { $$ = new ExpressionStatement(); } +| Expression T_SC { $$ = new ExpressionStatement(); } + ; + +JumpStatement: + T_RETURN Expression T_SC { $$ = new JumpStatement($2); } + ; + +IterationStatement: + T_WHILE T_LRB Expression T_RRB Statement { $$ = $5; } + | T_DO Statement T_WHILE T_LRB Expression T_RRB T_SC { $$ = $2; } + | T_FOR T_LRB Expression T_SC Expression T_SC Expression T_RRB Statement { $$ = $9; } + ; + +// Expressions + +Expression: + AssignmentExpression { $$ = $1; } + ; + +AssignmentExpression: + ConditionalExpression { $$ = $1; } + | UnaryExpression ASSIGN_OPER AssignmentExpression { $$ = $1; } + ; + +ASSIGN_OPER: + T_ASSIGN_OPER { ; } + | T_EQ { ; } + ; + +ConditionalExpression: + LogicalOrExpression { $$ = $1; } + | LogicalOrExpression T_QU Expression T_COL ConditionalExpression { $$ = $1; } + ; + +LogicalOrExpression: + LogicalAndExpression { $$ = $1; } + | LogicalOrExpression T_LOG_OR LogicalAndExpression { $$ = $3; } + ; + +LogicalAndExpression: + InclusiveOrExpression { $$ = $1; } + | LogicalAndExpression T_LOG_AND InclusiveOrExpression { $$ = $3; } + ; + +InclusiveOrExpression: + ExclusiveOrExpression { $$ = $1; } + | InclusiveOrExpression T_OR ExclusiveOrExpression { $$ = $3; } + ; + +ExclusiveOrExpression: + AndExpression { $$ = $1; } + | ExclusiveOrExpression T_XOR AndExpression { $$ = $3; } + ; + +AndExpression: + EqualityExpression { $$ = $1; } + | AndExpression T_AND EqualityExpression { $$ = $3; } + ; + +EqualityExpression: + RelationalExpression { $$ = $1; } + | EqualityExpression T_EQUALITY_OP RelationalExpression { $$ = $3; } + ; + +RelationalExpression: + ShiftExpression { $$ = $1; } + | RelationalExpression T_REL_OP ShiftExpression { $$ = $3; } + ; + +ShiftExpression: + AdditiveExpression { $$ = $1; } + | ShiftExpression T_SHIFT_OP AdditiveExpression { $$ = $3; } + ; + +AdditiveExpression: + MultiplicativeExpression { $$ = $1; } + | AdditiveExpression T_ADDSUB_OP MultiplicativeExpression { $$ = $3; } + ; + +MultiplicativeExpression: + CastExpression { $$ = $1; } + | MultiplicativeExpression MultDivRemOP CastExpression { $$ = $3; } + ; + +MultDivRemOP: + T_MULT { $$ = $1; } + | T_DIV { $$ = $1; } + | T_REM { $$ = $1; } + ; + +CastExpression: + UnaryExpression { $$ = $1; } + | T_LRB DeclarationSpec T_RRB CastExpression { $$ = $4; } + ; + +UnaryExpression: + PostfixExpression { $$ = $1; } + | T_INCDEC UnaryExpression { $$ = $2; } + | UnaryOperator CastExpression { $$ = $2; } + | T_SIZEOF UnaryExpression { $$ = $2; } + | T_SIZEOF T_LRB DeclarationSpec T_RRB { $$ = new Constant(0); } + ; + +UnaryOperator: + T_AND { $$ = $1; } + | T_ADDSUB_OP { $$ = $1; } + | T_MULT { $$ = $1; } + | T_TILDE { $$ = $1; } + | T_NOT { $$ = $1; } + ; + +PostfixExpression: + PrimaryExpression { $$ = $1; } + | PostfixExpression T_LSB Expression T_RSB { $$ = $3; } + | PostfixExpression T_LRB PostfixExpression2 { $$ = $3; } + | PostfixExpression T_DOT T_IDENTIFIER { $$ = $1; } + | PostfixExpression T_ARROW T_IDENTIFIER { $$ = $1; } + | PostfixExpression T_INCDEC { $$ = $1; } + ; + +PostfixExpression2: + T_RRB { $$ = new Constant(0); } + | ArgumentExpressionList T_RRB { $$ = new Constant(0); } + ; + +ArgumentExpressionList: + AssignmentExpression { $$ = $1; } + | ArgumentExpressionList T_CMA AssignmentExpression { $$ = $1; } + ; + +PrimaryExpression: + T_IDENTIFIER { $$ = new Identifier(*$1); } + | Constant { $$ = $1; } + | T_LRB Expression T_RRB { $$ = $2; } + ; + +Constant: + T_INT_CONST { $$ = new Constant($1); } + ; + +%% + +Node* g_root; // Definition of variable (to match declaration earlier) + +Node* parseAST() { + g_root = 0; + yyparse(); + return g_root; +} diff --git a/c_compiler/src/compiler_main.cpp b/c_compiler/src/compiler_main.cpp new file mode 100644 index 0000000..a7d7eb3 --- /dev/null +++ b/c_compiler/src/compiler_main.cpp @@ -0,0 +1,15 @@ +#include "ast.hpp" + +#include <iostream> + +int main(int argc, char *argv[]) +{ + Node* ast = parseAST(); + + VariableStackBindings bindings; + int32_t var_count; + + ast->printasm(bindings, var_count); + + return 0; +} diff --git a/c_compiler/src/declaration.cpp b/c_compiler/src/declaration.cpp new file mode 100644 index 0000000..0f125b0 --- /dev/null +++ b/c_compiler/src/declaration.cpp @@ -0,0 +1,75 @@ +#include "ast.hpp" + + +// Declaration definition + +Declaration::Declaration(const std::string& _id) + : id(_id) +{} + +void Declaration::print() const +{ + if(next_decl != nullptr) + next_decl->print(); + + if(id != "") + std::cout << id << std::endl; +} + +void Declaration::printxml() const +{ + if(next_decl != nullptr) + next_decl->printxml(); + + if(list_next_decl != nullptr) { + list_next_decl->printxml(); + } + + if(id != "") + std::cout << "<Variable id=\""<< id << "\" />" << std::endl; +} + +void Declaration::printasm(VariableStackBindings bindings, int32_t& var_count) const +{ + if(init == nullptr) + std::cout << "\t.comm\t" << id << ",4,4" << std::endl; + else { + std::cout << "\t.data\n\t.globl\t" << id << std::endl; + std::cout << id << ":\n\t.word\t" << std::endl; + } +} + +void Declaration::addDeclaration(Declaration* _next_decl) +{ + next_decl = _next_decl; +} + +void Declaration::addList(Declaration* _next_decl) +{ + list_next_decl = _next_decl; +} + +void Declaration::setType(Type* _type) +{ + type = _type; +} + +Declaration* Declaration::getNext() const +{ + return next_decl; +} + +Declaration* Declaration::getNextListItem() const +{ + return list_next_decl; +} + +std::string Declaration::getId() const +{ + return id; +} + +std::string Declaration::getType() const +{ + return type->getType(); +} diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp new file mode 100644 index 0000000..11fa66e --- /dev/null +++ b/c_compiler/src/expression.cpp @@ -0,0 +1,34 @@ +#include "expression.hpp" + +#include <iostream> + +// Expression definition + +void Expression::print() const +{} + +void Expression::printxml() const +{} + + +// Identifier definition + +Identifier::Identifier(const std::string& id) + : m_id(id) +{} + +void Identifier::printasm(VariableStackBindings bindings) const +{} + + +// Constant definition + +Constant::Constant(const int32_t& constant) + : m_constant(constant) +{} + +void Constant::printasm(VariableStackBindings bindings) const +{ + std::cout << "\tli\t$2," << m_constant << std::endl; +} + diff --git a/c_compiler/src/function.cpp b/c_compiler/src/function.cpp new file mode 100644 index 0000000..090e070 --- /dev/null +++ b/c_compiler/src/function.cpp @@ -0,0 +1,72 @@ +#include "function.hpp" +#include "statement.hpp" +#include "declaration.hpp" + +#include <iostream> +#include <vector> + + +// Function definition + +Function::Function(const std::string& _id, Declaration* _parameter_list, Statement* _statement) + : id(_id), parameter_list(_parameter_list), statement(_statement) +{} + +void Function::print() const +{ + std::cout << id << std::endl; + + if(parameter_list != nullptr) + parameter_list->print(); + + if(statement != nullptr) + statement->print(); +} + +void Function::printxml() const +{ + std::cout << "<Function id=\"" << id << "\">" << std::endl; + + Declaration* parameter = parameter_list; + std::vector<std::string> parameter_vec; + + while(parameter != nullptr) { + if(parameter->getId() != "") + parameter_vec.push_back(parameter->getId()); + parameter = parameter->getNext(); + } + + for(std::vector<std::string>::reverse_iterator itr = parameter_vec.rbegin(); + itr != parameter_vec.rend(); ++itr) { + + std::cout << "<Parameter id=\"" << *itr << "\" />" << std::endl; + } + + if(statement != nullptr) + statement->printxml(); + + std::cout << "</Function>" << std::endl; +} + +VariableStackBindings Function::printasm(VariableStackBindings bindings) const +{ + // Counting all the variables being declared in the function + int32_t count = 0; + if(statement != nullptr) + statement->count_variables(count); + + // This includes the space for the old frame counter and (return address)? + int32_t memory_needed = 4*count+8; + + std::cout << "\t.text\n\t.globl\t" << id << std::endl << id << ":\n\taddiu\t$sp,$sp,-" + << memory_needed << "\n\tsw\t$fp," << memory_needed-4 << "($sp)\n\tmove\t$fp,$sp" + << std::endl; + + // TODO print asm for parameters + + // Prints the asm for the compound statement in the function + statement->printasm(bindings); + + std::cout << "\tmove\t$sp,$fp\n\tlw\t$fp," << memory_needed-4 << "($sp)\n\taddiu\t$sp,$sp," + << memory_needed << "\n\tjr\t$31\n\tnop" << std::endl; +} diff --git a/c_compiler/src/statement.cpp b/c_compiler/src/statement.cpp new file mode 100644 index 0000000..7b98631 --- /dev/null +++ b/c_compiler/src/statement.cpp @@ -0,0 +1,205 @@ +#include "statement.hpp" +#include "declaration.hpp" +#include "expression.hpp" + +#include <iostream> + + +// General base Statement definition + +Statement::Statement(Statement* statement) + : next_statement(statement) +{} + +void Statement::addStatement(Statement* _next) +{ + next_statement = _next; +} + + +// Compound Statement definition + +CompoundStatement::CompoundStatement(Declaration* decl, Statement* statement) + : Statement(), m_decl(decl), m_statement(statement) +{} + +CompoundStatement::CompoundStatement(Statement* statement) + : m_statement(statement) +{} + +void CompoundStatement::print() const +{ + if(m_decl != nullptr) + m_decl->print(); + + if(m_statement != nullptr) + m_statement->print(); +} + +void CompoundStatement::printxml() const +{ + if(next_statement != nullptr) + next_statement->printxml(); + + std::cout << "<Scope>" << std::endl; + + if(m_decl != nullptr) + m_decl->printxml(); + + if(m_statement != nullptr) + m_statement->printxml(); + + std::cout << "</Scope>" << std::endl; +} + +VariableStackBindings CompoundStatement::printasm(VariableStackBindings bindings, int32_t& var_count) const +{ + if(next_statement != nullptr) + next_statement->printasm(bindings, var_count); + + // TODO printasm for the declaration + + if(m_statement != nullptr) + m_statement->printasm(bindings, var_count); +} + +void CompoundStatement::count_variables(int32_t& var_count) const +{ + Declaration* declaration = m_decl; + + if(next_statement != nullptr) + next_statement->count_variables(var_count); + + if(m_statement != nullptr) + m_statement->count_variables(var_count); + + while(declaration != nullptr) { + Declaration* declaration_list = declaration->getNextListItem(); + + while(declaration_list != nullptr) { + var_count++; + + declaration_list = declaration_list->getNextListItem(); + } + + var_count++; + + declaration = declaration->getNext(); + } +} + + +// Selection Statement definition + +SelectionStatement::SelectionStatement(Statement* _if, Statement* _else) + : Statement(), m_if(_if), m_else(_else) {} + +void SelectionStatement::print() const +{ + m_if->print(); + m_else->print(); +} + +void SelectionStatement::printxml() const +{ + if(next_statement != nullptr) + next_statement->printxml(); + if(m_if != nullptr) + m_if->printxml(); + if(m_else != nullptr) + m_else->printxml(); +} + +VariableStackBindings SelectionStatement::printasm(VariableStackBindings bindings, int32_t& var_count) const +{} + +void SelectionStatement::count_variables(int32_t& var_count) const +{ + if(next_statement != nullptr) + next_statement->count_variables(var_count); + + if(m_if != nullptr) + m_if->count_variables(var_count); + + if(m_else != nullptr) + m_else->count_variables(var_count); +} + + +// Expression Statement definition + +ExpressionStatement::ExpressionStatement(Expression* expr) + : Statement(), m_expr(expr) +{} + +void ExpressionStatement::print() const +{} + +void ExpressionStatement::printxml() const +{} + +VariableStackBindings ExpressionStatement::printasm(VariableStackBindings bindings, int32_t& var_count) const +{} + +void ExpressionStatement::count_variables(int32_t& var_count) const +{ + if(next_statement != nullptr) + next_statement->count_variables(var_count); +} + + +// Jump Statement definition + +JumpStatement::JumpStatement(Expression* expr) + : m_expr(expr) +{} + +void JumpStatement::print() const +{} + +void JumpStatement::printxml() const +{ + if(next_statement != nullptr) + next_statement->printxml(); +} + +VariableStackBindings JumpStatement::printasm(VariableStackBindings bindings, int32_t& var_count) const +{ + m_expr->printasm(bindings, var_count); +} + +void JumpStatement::count_variables(int32_t& var_count) const +{ + if(next_statement != nullptr) + next_statement->count_variables(var_count); +} + + +// Iteration Statement definition + +IterationStatement::IterationStatement(Statement* statement) + : m_statement(statement) +{} + +void IterationStatement::print() const +{} + +void IterationStatement::printxml() const +{ + if(next_statement != nullptr) + next_statement->printxml(); + if(m_statement != nullptr) + m_statement->printxml(); +} + +VariableStackBindings IterationStatement::printasm(VariableStackBindings bindings, int32_t& var_count) const +{} + +void IterationStatement::count_variables(int32_t& var_count) const +{ + if(next_statement != nullptr) + next_statement->count_variables(var_count); + + if(m_statement != nullptr) + m_statement->count_variables(var_count); +} diff --git a/c_compiler/src/translation_unit.cpp b/c_compiler/src/translation_unit.cpp new file mode 100644 index 0000000..e85237b --- /dev/null +++ b/c_compiler/src/translation_unit.cpp @@ -0,0 +1,37 @@ +#include "ast.hpp" + + +// Translation Unit definition + +TranslationUnit::TranslationUnit(Node* decl) +{ + push(decl); +} + +void TranslationUnit::print() const +{ + for(auto& node : translation_unit) { + node->print(); + } +} + +void TranslationUnit::printxml() const +{ + std::cout << "<?xml version=\"1.0\"?>\n<Program>" << std::endl; + for(auto& node : translation_unit) { + node->printxml(); + } + std::cout << "</Program>" << std::endl; +} + +void TranslationUnit::printasm(VariableStackBindings bindings, int32_t& var_count) const +{ + for(auto& node : translation_unit) { + node->printasm(bindings, var_count); + } +} + +void TranslationUnit::push(Node* decl) +{ + translation_unit.push_back(decl); +} diff --git a/c_compiler/src/type.cpp b/c_compiler/src/type.cpp new file mode 100644 index 0000000..13496e9 --- /dev/null +++ b/c_compiler/src/type.cpp @@ -0,0 +1,70 @@ +#include "ast.hpp" + + +// Type definition + +void Type::print() const +{ + std::cout << getType() << " " << std::endl; +} + +void Type::printxml() const +{} + +void Type::printasm(VariableStackBindings bindings, int32_t& var_count) const +{} + + +// Pointer definition + +Pointer::Pointer(Type* _pointer_type) : pointer_type(_pointer_type) +{} + +std::string Pointer::getType() const +{ + return "pointer " + pointer_type->getType(); +} + + +// Array definition + +Array::Array(Type* _array_type, int32_t _size) : size(_size), array_type(_array_type) +{} + +std::string Array::getType() const +{ + return "array " + array_type->getType(); +} + + +// Void definition + +Void::Void() +{} + +std::string Void::getType() const +{ + return "void"; +} + + +// Int defintion + +Int::Int() +{} + +std::string Int::getType() const +{ + return "int"; +} + + +// Char definition + +Char::Char() +{} + +std::string Char::getType() const +{ + return "char"; +} diff --git a/c_compiler/test/.gitignore b/c_compiler/test/.gitignore new file mode 100644 index 0000000..9ff5da3 --- /dev/null +++ b/c_compiler/test/.gitignore @@ -0,0 +1,2 @@ +out/ +ref/ diff --git a/c_compiler/test/in/01.c b/c_compiler/test/in/01.c new file mode 100644 index 0000000..c362948 --- /dev/null +++ b/c_compiler/test/in/01.c @@ -0,0 +1,3 @@ +int main() { + return 13; +} diff --git a/c_compiler/test/in/02.c b/c_compiler/test/in/02.c new file mode 100644 index 0000000..95c954c --- /dev/null +++ b/c_compiler/test/in/02.c @@ -0,0 +1,4 @@ +int main() { + int x = 26; + return x; +} diff --git a/c_compiler/test/in/03.c b/c_compiler/test/in/03.c new file mode 100644 index 0000000..911f7e2 --- /dev/null +++ b/c_compiler/test/in/03.c @@ -0,0 +1,5 @@ +int main() { + int x; + x = 15; + return x; +} diff --git a/c_compiler/test/in/04.c b/c_compiler/test/in/04.c new file mode 100644 index 0000000..57cf697 --- /dev/null +++ b/c_compiler/test/in/04.c @@ -0,0 +1,9 @@ +int main() { + int a = 3; + int b = 5; + return a + b; +} + +int f(int a, int v) { + return a + v; +} diff --git a/c_compiler/test/in/05.c b/c_compiler/test/in/05.c new file mode 100644 index 0000000..546012c --- /dev/null +++ b/c_compiler/test/in/05.c @@ -0,0 +1,6 @@ +char *glob; + +int main() { + glob = "Yann"; + return 0; +} diff --git a/c_compiler/test/out/01 b/c_compiler/test/out/01 Binary files differnew file mode 100755 index 0000000..068ecc4 --- /dev/null +++ b/c_compiler/test/out/01 diff --git a/c_compiler/test/out/01.s b/c_compiler/test/out/01.s new file mode 100644 index 0000000..2fdc6fa --- /dev/null +++ b/c_compiler/test/out/01.s @@ -0,0 +1,12 @@ + .text + .globl main +main: + addiu $sp,$sp,-8 + sw $fp,4($sp) + move $fp,$sp + li $2,13 + move $sp,$fp + lw $fp,4($sp) + addiu $sp,$sp,8 + jr $31 + nop diff --git a/c_compiler/test/out/02 b/c_compiler/test/out/02 Binary files differnew file mode 100755 index 0000000..a020bfc --- /dev/null +++ b/c_compiler/test/out/02 diff --git a/c_compiler/test/out/02.s b/c_compiler/test/out/02.s new file mode 100644 index 0000000..eae78d0 --- /dev/null +++ b/c_compiler/test/out/02.s @@ -0,0 +1,11 @@ + .text + .globl main +main: + addiu $sp,$sp,-12 + sw $fp,8($sp) + move $fp,$sp + move $sp,$fp + lw $fp,8($sp) + addiu $sp,$sp,12 + jr $31 + nop diff --git a/bin/.gitignore b/c_compiler/test/out/02.stderr.txt index e69de29..e69de29 100644 --- a/bin/.gitignore +++ b/c_compiler/test/out/02.stderr.txt diff --git a/c_compiler/test/out/03 b/c_compiler/test/out/03 Binary files differnew file mode 100755 index 0000000..a020bfc --- /dev/null +++ b/c_compiler/test/out/03 diff --git a/c_compiler/test/out/03.s b/c_compiler/test/out/03.s new file mode 100644 index 0000000..eae78d0 --- /dev/null +++ b/c_compiler/test/out/03.s @@ -0,0 +1,11 @@ + .text + .globl main +main: + addiu $sp,$sp,-12 + sw $fp,8($sp) + move $fp,$sp + move $sp,$fp + lw $fp,8($sp) + addiu $sp,$sp,12 + jr $31 + nop diff --git a/src/.gitignore b/c_compiler/test/out/03.stderr.txt index e69de29..e69de29 100644 --- a/src/.gitignore +++ b/c_compiler/test/out/03.stderr.txt diff --git a/c_compiler/test/out/04 b/c_compiler/test/out/04 Binary files differnew file mode 100755 index 0000000..a23dad4 --- /dev/null +++ b/c_compiler/test/out/04 diff --git a/c_compiler/test/out/04.s b/c_compiler/test/out/04.s new file mode 100644 index 0000000..992f12a --- /dev/null +++ b/c_compiler/test/out/04.s @@ -0,0 +1,22 @@ + .text + .globl main +main: + addiu $sp,$sp,-16 + sw $fp,12($sp) + move $fp,$sp + move $sp,$fp + lw $fp,12($sp) + addiu $sp,$sp,16 + jr $31 + nop + .text + .globl f +f: + addiu $sp,$sp,-8 + sw $fp,4($sp) + move $fp,$sp + move $sp,$fp + lw $fp,4($sp) + addiu $sp,$sp,8 + jr $31 + nop diff --git a/c_compiler/test/out/04.stderr.txt b/c_compiler/test/out/04.stderr.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_compiler/test/out/04.stderr.txt diff --git a/c_compiler/test/ref/02 b/c_compiler/test/ref/02 Binary files differnew file mode 100755 index 0000000..6efa3df --- /dev/null +++ b/c_compiler/test/ref/02 diff --git a/c_compiler/test/ref/02.s b/c_compiler/test/ref/02.s new file mode 100644 index 0000000..5298142 --- /dev/null +++ b/c_compiler/test/ref/02.s @@ -0,0 +1,37 @@ + .file 1 "02.c" + .section .mdebug.abi32 + .previous + .nan legacy + .module fp=xx + .module nooddspreg + .abicalls + .text + .align 2 + .globl main + .set nomips16 + .set nomicromips + .ent main + .type main, @function +main: + .frame $fp,16,$31 # vars= 8, regs= 1/0, args= 0, gp= 0 + .mask 0x40000000,-4 + .fmask 0x00000000,0 + .set noreorder + .set nomacro + addiu $sp,$sp,-16 + sw $fp,12($sp) + move $fp,$sp + li $2,26 # 0x1a + sw $2,4($fp) + lw $2,4($fp) + move $sp,$fp + lw $fp,12($sp) + addiu $sp,$sp,16 + j $31 + nop + + .set macro + .set reorder + .end main + .size main, .-main + .ident "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.1) 5.4.0 20160609" diff --git a/c_compiler/test/ref/03 b/c_compiler/test/ref/03 Binary files differnew file mode 100755 index 0000000..404308a --- /dev/null +++ b/c_compiler/test/ref/03 diff --git a/c_compiler/test/ref/03.s b/c_compiler/test/ref/03.s new file mode 100644 index 0000000..2232b11 --- /dev/null +++ b/c_compiler/test/ref/03.s @@ -0,0 +1,37 @@ + .file 1 "03.c" + .section .mdebug.abi32 + .previous + .nan legacy + .module fp=xx + .module nooddspreg + .abicalls + .text + .align 2 + .globl main + .set nomips16 + .set nomicromips + .ent main + .type main, @function +main: + .frame $fp,16,$31 # vars= 8, regs= 1/0, args= 0, gp= 0 + .mask 0x40000000,-4 + .fmask 0x00000000,0 + .set noreorder + .set nomacro + addiu $sp,$sp,-16 + sw $fp,12($sp) + move $fp,$sp + li $2,15 # 0xf + sw $2,4($fp) + lw $2,4($fp) + move $sp,$fp + lw $fp,12($sp) + addiu $sp,$sp,16 + j $31 + nop + + .set macro + .set reorder + .end main + .size main, .-main + .ident "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.1) 5.4.0 20160609" diff --git a/c_compiler/test/ref/04 b/c_compiler/test/ref/04 Binary files differnew file mode 100755 index 0000000..1ae97bf --- /dev/null +++ b/c_compiler/test/ref/04 diff --git a/c_compiler/test/ref/04.s b/c_compiler/test/ref/04.s new file mode 100644 index 0000000..c104d42 --- /dev/null +++ b/c_compiler/test/ref/04.s @@ -0,0 +1,71 @@ + .file 1 "04.c" + .section .mdebug.abi32 + .previous + .nan legacy + .module fp=xx + .module nooddspreg + .abicalls + .text + .align 2 + .globl main + .set nomips16 + .set nomicromips + .ent main + .type main, @function +main: + .frame $fp,16,$31 # vars= 8, regs= 1/0, args= 0, gp= 0 + .mask 0x40000000,-4 + .fmask 0x00000000,0 + .set noreorder + .set nomacro + addiu $sp,$sp,-16 + sw $fp,12($sp) + move $fp,$sp + li $2,3 # 0x3 + sw $2,0($fp) + li $2,5 # 0x5 + sw $2,4($fp) + lw $3,0($fp) + lw $2,4($fp) + addu $2,$3,$2 + move $sp,$fp + lw $fp,12($sp) + addiu $sp,$sp,16 + j $31 + nop + + .set macro + .set reorder + .end main + .size main, .-main + .align 2 + .globl f + .set nomips16 + .set nomicromips + .ent f + .type f, @function +f: + .frame $fp,8,$31 # vars= 0, regs= 1/0, args= 0, gp= 0 + .mask 0x40000000,-4 + .fmask 0x00000000,0 + .set noreorder + .set nomacro + addiu $sp,$sp,-8 + sw $fp,4($sp) + move $fp,$sp + sw $4,8($fp) + sw $5,12($fp) + lw $3,8($fp) + lw $2,12($fp) + addu $2,$3,$2 + move $sp,$fp + lw $fp,4($sp) + addiu $sp,$sp,8 + j $31 + nop + + .set macro + .set reorder + .end f + .size f, .-f + .ident "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.1) 5.4.0 20160609" diff --git a/c_lexer/include/c_lexer.hpp b/c_lexer/include/c_lexer.hpp new file mode 100644 index 0000000..90c4d53 --- /dev/null +++ b/c_lexer/include/c_lexer.hpp @@ -0,0 +1,33 @@ +#ifndef C_LEXER_HPP +#define C_LEXER_HPP + +#include <string> + +enum TokenType { + None, + Keyword, + Identifier, + Operator, + Constant, + StringLiteral, + Invalid +}; + +// Global variable that will be looked for by byson +extern std::string *yylval; + +// flex function to run on input +extern int yylex(); + +extern int lineCount; +extern int spaceCount; +extern int sourceLineCount; + +extern int charLength; + +extern std::string fileName; + +// get the correct output +std::string toJson(const std::string& classType, const std::string& text, const std::string& strLine, const std::string& srcCol, const std::string& srcLine, const std::string& fName); + +#endif diff --git a/c_lexer/src/c_lexer.cpp b/c_lexer/src/c_lexer.cpp new file mode 100644 index 0000000..0e9f021 --- /dev/null +++ b/c_lexer/src/c_lexer.cpp @@ -0,0 +1,11 @@ +#include "c_lexer.hpp" + +std::string toJson(const std::string& classType, const std::string& text, + const std::string& strLine, const std::string& srcCol, + const std::string& srcLine, const std::string& fName) { + std::string tmp = "{\"Class\":\"" + classType + "\", \"Text\":\"" + text + + "\", \"StreamLine\":" + strLine + ", \"SourceFile\":\"" + fName + + "\", \"SourceLine\":" + srcLine + ", \"SourceCol\":" + srcCol + "}"; + + return tmp; +} diff --git a/c_lexer/src/c_lexer.flex b/c_lexer/src/c_lexer.flex new file mode 100644 index 0000000..6c7a424 --- /dev/null +++ b/c_lexer/src/c_lexer.flex @@ -0,0 +1,144 @@ +%option noyywrap + +%{ + +#include "c_lexer.hpp" + +#include <sstream> +#include <stdlib.h> + +int lineCount = 1; +int spaceCount = 1; +int sourceLineCount = 1; + +int charLength = 0; + +std::string fileName; + +%} + +KEYWORD auto|double|int|struct|break|else|long|switch|case|enum|register|typedef|char|extern|return|union|const|float|short|unsigned|continue|for|signed|void|default|goto|sizeof|volatile|do|if|static|while + +IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]* + +OPERATOR [.][.][.]|[<>][<>][=]|[-][-]|[+][+]|[|][|]|[#][#]|[&][&]|[+\-*\/<>=!%^|&][=]|[<][<]|[->][>]|[<>&=+\/\-*(){}\[\]\.,%~!?:|^;] + +FRACTIONALCONSTANT (([0-9]*\.[0-9]+)|([0-9]+\.)) +EXPONENTPART ([eE][+-]?[0-9]+) + +FLOATINGSUFFIX ([flFL]) +INTEGERSUFFIX ([uU][lL]|[lL][uU]|[uUlL]) + +DECIMALCONSTANT ([1-9][0-9]*) +OCTALCONSTANT ([0][0-7]*) +HEXCONSTANT ([0][xX][0-9A-Fa-f]+) + +CHARCONSTANT ('(([\\]['])|([^']))+') + +STRINGLITERAL ["](([\\]["])|([^"]))*["] + +NEWLINE (\r\n?|\n) + +WHITESPACE [ ] + +TAB \t + +PREPROC [#][ ][0-9]+[ ]{STRINGLITERAL}[ 0-9]* + +ALL . + +%% + +{KEYWORD} { + yylval = new std::string(yytext); + charLength = (int)yyleng; + return Keyword; +} + +{IDENTIFIER} { + yylval = new std::string(yytext); + charLength = (int)yyleng; + return Identifier; +} + +{OPERATOR} { + yylval = new std::string(yytext); + charLength = (int)yyleng; + return Operator; +} + +{FRACTIONALCONSTANT}{EXPONENTPART}?{FLOATINGSUFFIX}? { + yylval = new std::string(yytext); + charLength = (int)yyleng; + return Constant; +} + +([0-9]+){EXPONENTPART}{FLOATINGSUFFIX}? { + yylval = new std::string(yytext); + charLength = (int)yyleng; + return Constant; +} + +{HEXCONSTANT}{INTEGERSUFFIX}? { + yylval = new std::string(yytext); + charLength = (int)yyleng; + return Constant; +} + +{DECIMALCONSTANT}{INTEGERSUFFIX}? { + yylval = new std::string(yytext); + charLength = (int)yyleng; + return Constant; +} + +{OCTALCONSTANT}{INTEGERSUFFIX}? { + yylval = new std::string(yytext); + charLength = (int)yyleng; + return Constant; +} + +{CHARCONSTANT} { + std::string tmp(yytext); + yylval = new std::string(tmp.substr(1, tmp.length()-2)); + charLength = (int)yyleng; + return Constant; +} + +{STRINGLITERAL} { + std::string tmp(yytext); + yylval = new std::string(tmp.substr(1, tmp.length()-2)); + charLength = (int)yyleng; + return StringLiteral; +} + +{NEWLINE} { + spaceCount = 1; + lineCount++; + sourceLineCount++; +} + +{WHITESPACE} { + spaceCount++; +} + +{PREPROC} { + int srcLineInt; + + yylval = new std::string(yytext); + std::stringstream preProcLine((*yylval).substr(1, (*yylval).length())); + preProcLine >> srcLineInt >> fileName; + sourceLineCount = srcLineInt - 1; + fileName = fileName.substr(1, fileName.length() - 2); +} + +{TAB} { + spaceCount += 8; +} + +{ALL} { + yylval = new std::string(yytext); + charLength = (int)yyleng; + return Invalid; +} + +%% diff --git a/c_lexer/src/main.cpp b/c_lexer/src/main.cpp new file mode 100644 index 0000000..cf674ff --- /dev/null +++ b/c_lexer/src/main.cpp @@ -0,0 +1,73 @@ +#include "c_lexer.hpp" + +#include <cassert> +#include <sstream> +#include <vector> + +std::string *yylval; + +int main() { + printf("[\n"); + std::string sourceLine, streamLine, sourceCol, classType, text; + while(1) { + // get the token type and run the lexer + std::stringstream str_line, src_col, src_line; + + TokenType type = (TokenType)yylex(); + str_line << lineCount; + streamLine = str_line.str(); + + src_col << spaceCount; + sourceCol = src_col.str(); + + src_line << sourceLineCount; + sourceLine = src_line.str(); + + if(type == None) { + // returns None when the file ends and we want to break then + break; + } else if(type == Invalid) { + // type is Invalid + text = *yylval; + classType = "Invalid"; + delete yylval; + } else if(type == Keyword) { + // found a keyword + text = *yylval; + classType = "Keyword"; + delete yylval; + } else if(type == Identifier) { + // found an identifier + text = *yylval; + classType = "Identifier"; + delete yylval; + } else if(type == Operator) { + // found an operator + text = *yylval; + classType = "Operator"; + delete yylval; + } else if(type == Constant) { + // found a constant + text = *yylval; + classType = "Constant"; + delete yylval; + } else if(type == StringLiteral) { + // found a string literal + text = *yylval; + classType = "StringLiteral"; + delete yylval; + } else { + // if any other type comes we assert to 0 + assert(0); + return 1; + } + + printf("%s,\n", toJson(classType, text, streamLine, sourceCol, sourceLine, fileName).c_str()); + + spaceCount += charLength; + } + + printf("{}\n]\n"); + + return 0; +} diff --git a/c_lexer/test/.gitignore b/c_lexer/test/.gitignore new file mode 100644 index 0000000..94ca778 --- /dev/null +++ b/c_lexer/test/.gitignore @@ -0,0 +1,2 @@ +output.json +pre_processed_test_lex.c diff --git a/c_lexer/test/test_lex.c b/c_lexer/test/test_lex.c new file mode 100644 index 0000000..e962c60 --- /dev/null +++ b/c_lexer/test/test_lex.c @@ -0,0 +1,8 @@ +int main(int argc, char* argv[]) { + char* h = "Hello World\""; + int j = POTATO; + int u = 2398uL; + float rt = 23.238e-283; + char x = 'a'; + return 0; +} diff --git a/c_parser/include/ast.hpp b/c_parser/include/ast.hpp new file mode 100644 index 0000000..b73506f --- /dev/null +++ b/c_parser/include/ast.hpp @@ -0,0 +1,14 @@ +#ifndef AST_HPP +#define AST_HPP + +#include "ast_base.hpp" +#include "ast_statement.hpp" +#include "ast_function.hpp" +#include "ast_declaration.hpp" +#include "ast_expression.hpp" +#include "ast_primitives.hpp" +#include "ast_top.hpp" + +ast_Top *parseAST(); + +#endif diff --git a/c_parser/include/ast_base.hpp b/c_parser/include/ast_base.hpp new file mode 100644 index 0000000..9451609 --- /dev/null +++ b/c_parser/include/ast_base.hpp @@ -0,0 +1,16 @@ +#ifndef AST_BASE_HPP +#define AST_BASE_HPP + +#include <string> +#include <iostream> +#include <vector> + +class Base { +public: + virtual ~Base() {} + + virtual void print() const = 0; + virtual void push(const Base* _var) const = 0; +}; + +#endif diff --git a/c_parser/include/ast_declaration.hpp b/c_parser/include/ast_declaration.hpp new file mode 100644 index 0000000..bfd3070 --- /dev/null +++ b/c_parser/include/ast_declaration.hpp @@ -0,0 +1,50 @@ +#ifndef AST_DECLARATION_HPP +#define AST_DECLARATION_HPP + +#include "ast.hpp" + +#include <vector> + +// Declaration that holds a list of declarations + +class DeclarationList : public Base { +private: + mutable std::vector<const Base*> dec_list; + +public: + DeclarationList(const Base* _dec) { + dec_list.push_back(_dec); + } + + virtual void print() const { + for(size_t i = 0; i < dec_list.size(); ++i) { + dec_list[i]->print(); + } + } + + virtual void push(const Base* _dec) const { + dec_list.push_back(_dec); + } +}; + +class VariableDeclaration : public Base { +private: + mutable std::vector<const Base*> var_list; + +public: + VariableDeclaration(const Base* _var) { + var_list.push_back(_var); + } + + virtual void print() const { + for(size_t i = 0; i < var_list.size(); ++i) { + var_list[i]->print(); + } + } + + virtual void push(const Base* _var) const { + var_list.push_back(_var); + } +}; + +#endif diff --git a/c_parser/include/ast_expression.hpp b/c_parser/include/ast_expression.hpp new file mode 100644 index 0000000..f67d2d7 --- /dev/null +++ b/c_parser/include/ast_expression.hpp @@ -0,0 +1,24 @@ +#ifndef AST_EXPRESSION_HPP +#define AST_EXPRESSION_HPP + +#include "ast.hpp" + +#include <string> +#include <iostream> + +class Expression : public Base { +private: +public: + Expression() {} + + virtual void print() const { + + } + + virtual void push(const Base* _base) const { + std::cerr << "Can't call this function for this type" << std::endl; + (void)_base; + } +}; + +#endif diff --git a/c_parser/include/ast_function.hpp b/c_parser/include/ast_function.hpp new file mode 100644 index 0000000..6fbcdee --- /dev/null +++ b/c_parser/include/ast_function.hpp @@ -0,0 +1,53 @@ +#ifndef AST_FUNCTION_HPP +#define AST_FUNCTION_HPP + +#include "ast.hpp" + +#include <string> +#include <iostream> + +class Function : public Base { +private: + std::string id; + const Base* param; + const Base* comp_statement; +public: + Function(const std::string& _id, const Base* _param, const Base* _comp_statement) : + id(_id), param(_param), comp_statement(_comp_statement) {} + + virtual void print() const { + std::cout << "<Function id=\"" << id << "\">" << std::endl; + param->print(); + comp_statement->print(); + std::cout << "</Function>" << std::endl; + } + + virtual void push(const Base* var) const { + std::cerr << "Error: Can't call this function on this class" << std::endl; + (void)var; + } +}; + +class ParamList : public Base { +private: + mutable std::vector<const Base*> param_list; + +public: + ParamList() {} + + ParamList(const Base* param) { + param_list.push_back(param); + } + + virtual void print() const { + for(size_t i = 0; i < param_list.size(); ++i) { + param_list[i]->print(); + } + } + + virtual void push(const Base* _var) const { + param_list.push_back(_var); + } +}; + +#endif diff --git a/c_parser/include/ast_primitives.hpp b/c_parser/include/ast_primitives.hpp new file mode 100644 index 0000000..2eeaa19 --- /dev/null +++ b/c_parser/include/ast_primitives.hpp @@ -0,0 +1,40 @@ +#ifndef AST_PRIMITIVES_HPP +#define AST_PRIMITIVES_HPP + +#include "ast.hpp" + +#include <string> + +class Variable : public Base { +private: + std::string id; +public: + Variable(const std::string& _id) : id(_id) {} + + virtual void print() const { + std::cout << "<Variable id=\"" << id << "\" />" << std::endl; + } + + virtual void push(const Base* var) const { + std::cerr << "Error: Can't call this function on this class" << std::endl; + (void)var; + } +}; + +class Parameter : public Base { +private: + std::string id; +public: + Parameter(const std::string& _id) : id(_id) {} + + virtual void print() const { + std::cout << "<Parameter id=\"" << id << "\" />" << std::endl; + } + + virtual void push(const Base* var) const { + std::cerr << "Error: Can't call this function on this class" << std::endl; + (void)var; + } +}; + +#endif diff --git a/c_parser/include/ast_statement.hpp b/c_parser/include/ast_statement.hpp new file mode 100644 index 0000000..4761efb --- /dev/null +++ b/c_parser/include/ast_statement.hpp @@ -0,0 +1,81 @@ +#ifndef AST_STATEMENT_HPP +#define AST_STATEMENT_HPP + +#include "ast.hpp" + +class Statement : public Base { +protected: + mutable std::vector<const Base*> list; + +public: + Statement() {} + + Statement(const Base* _el) { + list.push_back(_el); + } + + Statement(const Base* _dec, const Base* _statement) { + list.push_back(_dec); + list.push_back(_statement); + } + virtual void print() const { + for(size_t i = 0; i < list.size(); ++i) { + list[i]->print(); + } + } + + virtual void push(const Base* _var) const { + list.push_back(_var); + } +}; + +class StatementList : public Statement { +public: + StatementList(const Base* _statement) : Statement(_statement) {} +}; + +class CompoundStatement : public Statement { +public: + CompoundStatement() : Statement() {} + CompoundStatement(const Base* _el) : Statement(_el) {} + CompoundStatement(const Base* _dec, const Base* _statement) : + Statement(_dec, _statement) {} + + virtual void print() const override { + std::cout << "<Scope>" << std::endl; + for(size_t i = 0; i < list.size(); ++i) { + list[i]->print(); + } + std::cout << "</Scope>" << std::endl; + } +}; + +class SelectionStatement : public Statement { +public: + SelectionStatement() : Statement() {} + SelectionStatement(const Base* _el) : Statement(_el) {} + SelectionStatement(const Base* _if, const Base* _else) : + Statement(_if, _else) {} +}; + +class ExpressionStatement : public Statement { +public: + ExpressionStatement() : Statement() {} + ExpressionStatement(const Base* _el) : Statement(_el) {} +}; + +class JumpStatement : public Statement { +public: + JumpStatement() : Statement() {} + JumpStatement(const Base* _el) : Statement(_el) {} +}; + +class IterationStatement : public Statement { +public: + IterationStatement() : Statement() {} + IterationStatement(const Base* _el) : Statement(_el) {} + IterationStatement(const Base* _if, const Base* _else) : + Statement(_if, _else) {} +}; + +#endif diff --git a/c_parser/include/ast_top.hpp b/c_parser/include/ast_top.hpp new file mode 100644 index 0000000..737ff58 --- /dev/null +++ b/c_parser/include/ast_top.hpp @@ -0,0 +1,24 @@ +#ifndef TOP_AST_HPP +#define TOP_AST_HPP + +#include "ast.hpp" + +#include <vector> + +class ast_Top { +public: + void print() { + for(size_t i = 0; i < vec.size(); ++i) { + vec[i]->print(); + } + } + + void push(const Base *stmnt) { + vec.push_back(stmnt); + } + +private: + std::vector<const Base *> vec; +}; + +#endif diff --git a/c_parser/src/c_lexer.flex b/c_parser/src/c_lexer.flex new file mode 100644 index 0000000..91b35a9 --- /dev/null +++ b/c_parser/src/c_lexer.flex @@ -0,0 +1,96 @@ +%option noyywrap + +%{ + +#include "c_parser.tab.hpp" + +%} + +KEYWORD auto|double|int|struct|break|else|long|switch|case|enum|register|typedef|char|extern|return|union|const|float|short|unsigned|continue|for|signed|void|default|goto|sizeof|volatile|do|if|static|while + +IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]* + +OPERATOR [.][.][.]|[<>][<>][=]|[-][-]|[+][+]|[|][|]|[#][#]|[&][&]|[+\-*\/<>=!%^|&][=]|[<][<]|[->][>]|[<>&=+\/\-*(){}\[\]\.,%~!?:|^;] + +ASSIGNMENT_OPERATOR (([<>][<>]|[*\/%+\-&^|])[=]|[=]) + +FRACTIONALCONSTANT (([0-9]*\.[0-9]+)|([0-9]+\.)) +EXPONENTPART ([eE][+-]?[0-9]+) + +FLOATINGSUFFI X ([flFL]) +INTEGERSUFFIX ([uU][lL]|[lL][uU]|[uUlL]) + +DECIMALCONSTANT ([1-9][0-9]*) +OCTALCONSTANT ([0][0-7]*) +HEXCONSTANT ([0][xX][0-9A-Fa-f]+) + +CHARCONSTANT ('(([\\]['])|([^']))+') + +STRINGLITERAL ["](([\\]["])|([^"]))*["] + +WHITESPACE [ \t\r\n]+ + +PREPROC [#][ ][0-9]+[ ]{STRINGLITERAL}[ 0-9]* + +ALL . + +%% + +typedef|extern|static|auto|register { return T_STRG_SPEC; } +void|char|short|int|long|float|double|signed|unsigned { return T_TYPE_SPEC; } +const|volatile { return T_TYPE_QUAL; } + +[;] { return T_SC; } +[,] { return T_CMA; } +[(] { return T_LRB; } +[)] { return T_RRB; } +[{] { return T_LCB; } +[}] { return T_RCB; } +[[] { return T_LSB; } +[]] { return T_RSB; } +[?] { return T_QU; } +[:] { return T_COL; } +[|][|] { return T_LOG_OR; } +[&][&] { return T_LOG_AND; } +[|] { return T_OR; } +[\^] { return T_XOR; } +[&] { return T_AND; } +[=][=] { return T_EQUALITY_OP; } +[!][=] { return T_EQUALITY_OP; } +([<>][=])|[<>] { return T_REL_OP; } +[<>][<>] { return T_SHIFT_OP; } +[*] { return T_MULT; } +[\/] { return T_DIV; } +[%] { return T_REM; } +[~] { return T_TILDE; } +[!] { return T_NOT; } +[.] { return T_DOT; } +[-][>] { return T_ARROW; } +[+-][+-] { return T_INCDEC; } +[+-] { return T_ADDSUB_OP; } +[=] { yylval.string = new std::string(yytext); return T_EQ; } + +{ASSIGNMENT_OPERATOR} { yylval.string = new std::string(yytext); return T_ASSIGN_OPER; } + +if { return T_IF; } +else { return T_ELSE; } +return { return T_RETURN; } +while { return T_WHILE; } +do { return T_DO; } +for { return T_FOR; } +sizeof { return T_SIZEOF; } + +{IDENTIFIER} { yylval.string = new std::string(yytext); return T_IDENTIFIER; } + +({HEXCONSTANT}|{OCTALCONSTANT}|{DECIMALCONSTANT}){INTEGERSUFFIX}? { yylval.number = strtol(yytext, NULL, 0); return T_INT_CONST; } + +{WHITESPACE} { ; } + +. { fprintf(stderr, "Invalid token\n"); exit(1); } + +%% + +void yyerror(char const *s) { + fprintf (stderr, "Parse error : %s\n", s); + exit(1); +} diff --git a/c_parser/src/c_parser.y b/c_parser/src/c_parser.y new file mode 100644 index 0000000..1fc4d4a --- /dev/null +++ b/c_parser/src/c_parser.y @@ -0,0 +1,303 @@ +%code requires{ + +#include "ast.hpp" +extern ast_Top *g_root; // A way of getting the AST out + +//! This is to fix problems when generating C++ +// We are declaring the functions provided by Flex, so +// that Bison generated code can call them. +int yylex(void); +void yyerror(const char *); + +} + +// Represents the value associated with any kind of +// AST node. +%union{ + const Base *stmnt; + double number; + std::string *string; +} + +%token T_TYPE_SPEC T_TYPE_QUAL T_STRG_SPEC T_IDENTIFIER +%token T_SC T_CMA T_LRB T_LCB T_RCB T_LSB T_RSB T_QU T_COL T_LOG_OR T_LOG_AND T_OR T_XOR T_AND T_EQUALITY_OP T_REL_OP T_SHIFT_OP T_MULT T_DIV T_REM T_TILDE T_NOT T_DOT T_ARROW T_INCDEC T_ADDSUB_OP T_ASSIGN_OPER T_EQ T_SIZEOF +%token T_INT_CONST +%token T_IF T_WHILE T_DO T_FOR T_RETURN +%nonassoc T_RRB +%nonassoc T_ELSE + + +%type <stmnt> ExtDef ExtDeclaration + +%type <stmnt> FuncDef ParameterList Parameter ParamDeclarator + +%type <stmnt> DeclarationList Declaration DeclarationSpec DeclarationSpec_T InitDeclarator InitDeclaratorList Declarator + +%type <stmnt> StatementList Statement CompoundStatement CompoundStatement_2 SelectionStatement ExpressionStatement JumpStatement IterationStatement + +%type <stmnt> Expression AssignmentExpression ConditionalExpression LogicalOrExpression LogicalAndExpression InclusiveOrExpression ExclusiveOrExpression AndExpression EqualityExpression RelationalExpression ShiftExpression AdditiveExpression MultiplicativeExpression CastExpression UnaryExpression PostfixExpression PostfixExpression2 ArgumentExpressionList PrimaryExpression + + +%type <number> Constant T_INT_CONST + + +%type <string> T_IDENTIFIER MultDivRemOP UnaryOperator ASSIGN_OPER T_ASSIGN_OPER T_EQ T_AND T_ADDSUB_OP T_TILDE T_NOT T_MULT T_DIV T_REM //T_Operator + +%start ROOT + +%% + +ROOT: + ExtDef { ; } + ; + +// EXTERNAL DEFINITION + +ExtDef: + ExtDeclaration { g_root->push($1); } + | ExtDef ExtDeclaration { g_root->push($2); } + ; + +ExtDeclaration: + Declaration { $$ = $1; } + | FuncDef { $$ = $1; } + ; + +// FUNCTION DEFINITION + +FuncDef: + DeclarationSpec T_IDENTIFIER T_LRB ParameterList T_RRB CompoundStatement { $$ = new Function(*$2, $4, $6); } + ; + +ParameterList: + %empty { $$ = new ParamList(); } + | Parameter { $$ = new ParamList($1); } + | ParameterList T_CMA Parameter { $$->push($3); } + ; + +Parameter: + DeclarationSpec ParamDeclarator { $$ = $2; } + ; + +ParamDeclarator: + T_IDENTIFIER { $$ = new Parameter(*$1);} + ; + +// Declaration + +DeclarationList: + Declaration { $$ = new DeclarationList($1); } + | DeclarationList Declaration { $$->push($2); } + ; + +Declaration: + DeclarationSpec InitDeclaratorList T_SC { $$ = $2; } + ; + +DeclarationSpec: + DeclarationSpec_T { ; } + | DeclarationSpec_T DeclarationSpec { ; } + ; + +DeclarationSpec_T: + T_TYPE_SPEC { ; } + | T_TYPE_QUAL { ; } + | T_STRG_SPEC { ; } + ; + +InitDeclaratorList: + InitDeclarator { $$ = new VariableDeclaration($1); } + | InitDeclaratorList T_CMA InitDeclarator { $$->push($3); } + ; + +InitDeclarator: + Declarator { ; } + | Declarator T_EQ AssignmentExpression { ; } + ; + +Declarator: + T_IDENTIFIER {$$ = new Variable(*$1); } + ; + +// Statement + +StatementList: + Statement { $$ = new StatementList($1); } + | StatementList Statement { $$->push($2); } + ; + +Statement: + CompoundStatement { $$ = $1; } + | SelectionStatement { $$ = $1; } + | ExpressionStatement { $$ = $1; } + | JumpStatement { $$ = $1; } + | IterationStatement { $$ = $1; } + ; + +CompoundStatement: + T_LCB CompoundStatement_2 { $$ = $2; } + ; + +CompoundStatement_2: + T_RCB { $$ = new CompoundStatement; } + | DeclarationList T_RCB { $$ = new CompoundStatement($1); } + | DeclarationList StatementList T_RCB { $$ = new CompoundStatement($1, $2); } + | StatementList T_RCB { $$ = new CompoundStatement($1); } + ; + +SelectionStatement: + T_IF T_LRB Expression T_RRB Statement { $$ = new SelectionStatement($5); } +| T_IF T_LRB Expression T_RRB Statement T_ELSE Statement { $$ = new SelectionStatement($5, $7); } + ; + +ExpressionStatement: + T_SC { $$ = new ExpressionStatement(); } + | Expression T_SC { $$ = $1; } + ; + +JumpStatement: + T_RETURN ExpressionStatement { $$ = $2; } + ; + +IterationStatement: + T_WHILE T_LRB Expression T_RRB Statement { $$ = $5; } + | T_DO Statement T_WHILE T_LRB Expression T_RRB T_SC { $$ = $2; } + | T_FOR T_LRB Expression T_SC Expression T_SC Expression T_RRB Statement { $$ = $9; } + ; + +// Expressions + +Expression: + AssignmentExpression { $$ = $1; } + ; + +AssignmentExpression: + ConditionalExpression { $$ = $1; } + | UnaryExpression ASSIGN_OPER AssignmentExpression { $$ = $1; } + ; + +ASSIGN_OPER: + T_ASSIGN_OPER { ; } + | T_EQ { ; } + ; + +ConditionalExpression: + LogicalOrExpression { $$ = $1; } + | LogicalOrExpression T_QU Expression T_COL ConditionalExpression { $$ = $1; } + ; + +LogicalOrExpression: + LogicalAndExpression { $$ = $1; } + | LogicalOrExpression T_LOG_OR LogicalAndExpression { $$ = $3; } + ; + +LogicalAndExpression: + InclusiveOrExpression { $$ = $1; } + | LogicalAndExpression T_LOG_AND InclusiveOrExpression { $$ = $3; } + ; + +InclusiveOrExpression: + ExclusiveOrExpression { $$ = $1; } + | InclusiveOrExpression T_OR ExclusiveOrExpression { $$ = $3; } + ; + +ExclusiveOrExpression: + AndExpression { $$ = $1; } + | ExclusiveOrExpression T_XOR AndExpression { $$ = $3; } + ; + +AndExpression: + EqualityExpression { $$ = $1; } + | AndExpression T_AND EqualityExpression { $$ = $3; } + ; + +EqualityExpression: + RelationalExpression { $$ = $1; } + | EqualityExpression T_EQUALITY_OP RelationalExpression { $$ = $3; } + ; + +RelationalExpression: + ShiftExpression { $$ = $1; } + | RelationalExpression T_REL_OP ShiftExpression { $$ = $3; } + ; + +ShiftExpression: + AdditiveExpression { $$ = $1; } + | ShiftExpression T_SHIFT_OP AdditiveExpression { $$ = $3; } + ; + +AdditiveExpression: + MultiplicativeExpression { $$ = $1; } + | AdditiveExpression T_ADDSUB_OP MultiplicativeExpression { $$ = $3; } + ; + +MultiplicativeExpression: + CastExpression { $$ = $1; } + | MultiplicativeExpression MultDivRemOP CastExpression { $$ = $3; } + ; + +MultDivRemOP: + T_MULT { $$ = $1; } + | T_DIV { $$ = $1; } + | T_REM { $$ = $1; } + ; + +CastExpression: + UnaryExpression { $$ = $1; } + | T_LRB T_TYPE_SPEC T_RRB CastExpression { $$ = $4; } + ; + +UnaryExpression: + PostfixExpression { $$ = $1; } + | T_INCDEC UnaryExpression { $$ = $2; } + | UnaryOperator CastExpression { $$ = $2; } + | T_SIZEOF UnaryExpression { $$ = $2; } + | T_SIZEOF T_LRB T_TYPE_SPEC T_RRB { $$ = new Expression(); } + ; + +UnaryOperator: + T_AND { $$ = $1; } + | T_ADDSUB_OP { $$ = $1; } + | T_MULT { $$ = $1; } + | T_TILDE { $$ = $1; } + | T_NOT { $$ = $1; } + ; + +PostfixExpression: + PrimaryExpression { $$ = $1; } + | PostfixExpression T_LSB Expression T_RSB { $$ = $3; } + | PostfixExpression T_LRB PostfixExpression2 { $$ = $3; } + | PostfixExpression T_DOT T_IDENTIFIER { $$ = new Expression(); } + | PostfixExpression T_ARROW T_IDENTIFIER { $$ = new Expression(); } + | PostfixExpression T_INCDEC { $$ = new Expression(); } + ; + +PostfixExpression2: + T_RRB { $$ = new Expression(); } + | ArgumentExpressionList T_RRB { $$ = $1; } + ; + +ArgumentExpressionList: + AssignmentExpression { $$ = $1; } + | ArgumentExpressionList T_CMA AssignmentExpression { $$ = $3; } + ; + +PrimaryExpression: + T_IDENTIFIER { $$ = new Expression(); } + | Constant { $$ = new Expression(); } + | T_LRB Expression T_RRB { $$ = $2; } + ; + +Constant: + T_INT_CONST { $$ = $1; } + ; + +%% + +ast_Top *g_root; // Definition of variable (to match declaration earlier) + +ast_Top *parseAST() { + g_root = new ast_Top; + yyparse(); + return g_root; +} diff --git a/c_parser/src/parser_main.cpp b/c_parser/src/parser_main.cpp new file mode 100644 index 0000000..9626334 --- /dev/null +++ b/c_parser/src/parser_main.cpp @@ -0,0 +1,15 @@ +#include "ast.hpp" + +#include <iostream> + +int main(int argc, char *argv[]) { + ast_Top *ast = parseAST(); + + std::cout << "<?xml version=\"1.0\"?>" << std::endl << "<Program>" << std::endl; + + ast->print(); + + std::cout << "</Program>" << std::endl; + + return 0; +} diff --git a/c_parser/test/in/01.c b/c_parser/test/in/01.c new file mode 100644 index 0000000..76425b5 --- /dev/null +++ b/c_parser/test/in/01.c @@ -0,0 +1,6 @@ +int a; +int b = 0; +int c, d; + +int e, + f; diff --git a/c_parser/test/in/02.c b/c_parser/test/in/02.c new file mode 100644 index 0000000..45b1467 --- /dev/null +++ b/c_parser/test/in/02.c @@ -0,0 +1 @@ +int f() {} diff --git a/c_parser/test/in/03.c b/c_parser/test/in/03.c new file mode 100644 index 0000000..e57aaa9 --- /dev/null +++ b/c_parser/test/in/03.c @@ -0,0 +1 @@ +int foo(int bar) {} diff --git a/c_parser/test/in/04.c b/c_parser/test/in/04.c new file mode 100644 index 0000000..2e3aa5d --- /dev/null +++ b/c_parser/test/in/04.c @@ -0,0 +1,4 @@ +int foo(int bar1, int bar2) { + int x; + int y, z; +} diff --git a/c_parser/test/in/05.c b/c_parser/test/in/05.c new file mode 100644 index 0000000..2317163 --- /dev/null +++ b/c_parser/test/in/05.c @@ -0,0 +1,3 @@ +int foo(int x, int y) { + {} +} diff --git a/c_parser/test/in/06.c b/c_parser/test/in/06.c new file mode 100644 index 0000000..2dcdc2b --- /dev/null +++ b/c_parser/test/in/06.c @@ -0,0 +1,6 @@ +int f(int a, int b, int c) { + int d; + { + int e; + } +} diff --git a/c_parser/test/in/07.c b/c_parser/test/in/07.c new file mode 100644 index 0000000..80d8051 --- /dev/null +++ b/c_parser/test/in/07.c @@ -0,0 +1,5 @@ +int foo(int a) { + if(x == y) { + int z; + } +} diff --git a/c_parser/test/in/08.c b/c_parser/test/in/08.c new file mode 100644 index 0000000..c57a6ab --- /dev/null +++ b/c_parser/test/in/08.c @@ -0,0 +1,10 @@ +int func(int a, int b) { + int c = 0; + if(a == b) { + int d; + } else if(a == d) { + int e; + return d; + } + return c; +} diff --git a/c_parser/test/in/09.c b/c_parser/test/in/09.c new file mode 100644 index 0000000..ae86bb1 --- /dev/null +++ b/c_parser/test/in/09.c @@ -0,0 +1,15 @@ +int x() +{} + +int g; + +int zz(int a, int b, int c) +{ + if(a==b){ + int a; + return a; + }else{ + int fsdfsdfs; + return c; + } +} diff --git a/c_parser/test/in/10.c b/c_parser/test/in/10.c new file mode 100644 index 0000000..37a6ecc --- /dev/null +++ b/c_parser/test/in/10.c @@ -0,0 +1,18 @@ +int f() +{} + +int g = 2; + +int x(int y) { + int z = 3; + + if(y < z || g < z) { + int r; + ++y; + } else if(y == z) { + int f; + --y; + } else return y; + + return g; +} diff --git a/c_parser/test/in/11.c b/c_parser/test/in/11.c new file mode 100644 index 0000000..2d8cac6 --- /dev/null +++ b/c_parser/test/in/11.c @@ -0,0 +1,10 @@ +int f() { + int x = 0; + + while(x < 5) { + int y = 0; + x++; + } + + return x; +} diff --git a/c_parser/test/in/12.c b/c_parser/test/in/12.c new file mode 100644 index 0000000..37b8b7e --- /dev/null +++ b/c_parser/test/in/12.c @@ -0,0 +1,4 @@ +int f(int b, int c) { + int a = b+c; + return a; +} diff --git a/c_parser/test/in/13.c b/c_parser/test/in/13.c new file mode 100644 index 0000000..d912d70 --- /dev/null +++ b/c_parser/test/in/13.c @@ -0,0 +1,8 @@ +int func(int x, int y) { + int i; + for(i = 0; i < 5; ++i) { + int z = 0; + z = x + y + i; + return x + y; + } +} diff --git a/c_parser/test/in/14.c b/c_parser/test/in/14.c new file mode 100644 index 0000000..94506d7 --- /dev/null +++ b/c_parser/test/in/14.c @@ -0,0 +1,12 @@ +int function_1(int a, int b) { + int x = 0; + + do { + int c = a + b; + x += a; + x = x * b; + x -= c; + } while(x < 500); + + return x; +} diff --git a/c_parser/test/in/15.c b/c_parser/test/in/15.c new file mode 100644 index 0000000..3189b56 --- /dev/null +++ b/c_parser/test/in/15.c @@ -0,0 +1,28 @@ +int add(int a, int b) +{ + int c = a + b; + return c; +} + +int sub(int a, int b) +{ + int c = a - b; + return c; +} + +int mult_by_5(int a) +{ + int b = 5; + return a * b; +} + +int add_5(int a) +{ + int i; + for(i = 0; i < 5; ++i) + { + int b = ++a; + } + + return a; +} diff --git a/c_parser/test/out/.diff.txt b/c_parser/test/out/.diff.txt new file mode 100644 index 0000000..332a940 --- /dev/null +++ b/c_parser/test/out/.diff.txt @@ -0,0 +1,164 @@ +1c1,161 +< +--- +> <?xml version="1.0"?> +> <Program> +> <Variable id="a" /> +> <Variable id="b" /> +> <Variable id="c" /> +> <Variable id="d" /> +> <Variable id="e" /> +> <Variable id="f" /> +> <Function id="f"> +> <Scope></Scope> +> </Function> +> <Function id="foo"> +> <Parameter id="bar" /> +> <Scope></Scope> +> </Function> +> <Function id="foo"> +> <Parameter id="bar1" /> +> <Parameter id="bar2" /> +> <Scope> +> <Variable id="x" /> +> <Variable id="y" /> +> <Variable id="z" /> +> </Scope> +> </Function> +> <Function id="foo"> +> <Parameter id="x" /> +> <Parameter id="y" /> +> <Scope> +> <Scope></Scope> +> </Scope> +> </Function> +> <Function id="f"> +> <Parameter id="a" /> +> <Parameter id="b" /> +> <Parameter id="c" /> +> <Scope> +> <Variable id="d" /> +> <Scope> +> <Variable id="e" /> +> </Scope> +> </Scope> +> </Function> +> <Function id="foo"> +> <Parameter id="a" /> +> <Scope> +> <Scope> +> <Variable id="z" /> +> </Scope> +> </Scope> +> </Function> +> <Function id="func"> +> <Parameter id="a" /> +> <Parameter id="b" /> +> <Scope> +> <Variable id="c" /> +> <Scope> +> <Variable id="d" /> +> </Scope> +> <Scope> +> <Variable id="e" /> +> </Scope> +> </Scope> +> </Function> +> <Function id="x"> +> <Scope></Scope> +> </Function> +> <Variable id="g" /> +> <Function id="zz"> +> <Parameter id="a" /> +> <Parameter id="b" /> +> <Parameter id="c" /> +> <Scope> +> <Scope> +> <Variable id="a" /> +> </Scope> +> <Scope> +> <Variable id="fsdfsdfs" /> +> </Scope> +> </Scope> +> </Function> +> <Function id="f"> +> <Scope></Scope> +> </Function> +> <Variable id="g" /> +> <Function id="x"> +> <Parameter id="y" /> +> <Scope> +> <Variable id="z" /> +> <Scope> +> <Variable id="r" /> +> </Scope> +> <Scope> +> <Variable id="f" /> +> </Scope> +> </Scope> +> </Function> +> <Function id="f"> +> <Scope> +> <Variable id="x" /> +> <Scope> +> <Variable id="y" /> +> </Scope> +> </Scope> +> </Function> +> <Function id="f"> +> <Parameter id="b" /> +> <Parameter id="c" /> +> <Scope> +> <Variable id="a" /> +> </Scope> +> </Function> +> <Function id="func"> +> <Parameter id="x" /> +> <Parameter id="y" /> +> <Scope> +> <Variable id="i" /> +> <Scope> +> <Variable id="z" /> +> </Scope> +> </Scope> +> </Function> +> <Function id="function_1"> +> <Parameter id="a" /> +> <Parameter id="b" /> +> <Scope> +> <Variable id="x" /> +> <Scope> +> <Variable id="c" /> +> </Scope> +> </Scope> +> </Function> +> <Function id="add"> +> <Parameter id="a" /> +> <Parameter id="b" /> +> <Scope> +> <Variable id="c" /> +> </Scope> +> </Function> +> <Function id="sub"> +> <Parameter id="a" /> +> <Parameter id="b" /> +> <Scope> +> <Variable id="c" /> +> </Scope> +> </Function> +> <Function id="mult_by_5"> +> <Parameter id="a" /> +> <Scope> +> <Variable id="b" /> +> </Scope> +> </Function> +> <Function id="add_5"> +> <Parameter id="a" /> +> <Scope> +> <Variable id="i" /> +> <Scope> +> <Variable id="b" /> +> </Scope> +> </Scope> +> </Function> +> </Program> diff --git a/c_parser/test/out/.pretty.xml b/c_parser/test/out/.pretty.xml new file mode 100644 index 0000000..81983a7 --- /dev/null +++ b/c_parser/test/out/.pretty.xml @@ -0,0 +1,161 @@ +<?xml version="1.0"?> +<Program> + <Variable id="a" /> + <Variable id="b" /> + <Variable id="c" /> + <Variable id="d" /> + <Variable id="e" /> + <Variable id="f" /> + <Function id="f"> + <Scope></Scope> + </Function> + <Function id="foo"> + <Parameter id="bar" /> + <Scope></Scope> + </Function> + <Function id="foo"> + <Parameter id="bar1" /> + <Parameter id="bar2" /> + <Scope> + <Variable id="x" /> + <Variable id="y" /> + <Variable id="z" /> + </Scope> + </Function> + <Function id="foo"> + <Parameter id="x" /> + <Parameter id="y" /> + <Scope> + <Scope></Scope> + </Scope> + </Function> + <Function id="f"> + <Parameter id="a" /> + <Parameter id="b" /> + <Parameter id="c" /> + <Scope> + <Variable id="d" /> + <Scope> + <Variable id="e" /> + </Scope> + </Scope> + </Function> + <Function id="foo"> + <Parameter id="a" /> + <Scope> + <Scope> + <Variable id="z" /> + </Scope> + </Scope> + </Function> + <Function id="func"> + <Parameter id="a" /> + <Parameter id="b" /> + <Scope> + <Variable id="c" /> + <Scope> + <Variable id="d" /> + </Scope> + <Scope> + <Variable id="e" /> + </Scope> + </Scope> + </Function> + <Function id="x"> + <Scope></Scope> + </Function> + <Variable id="g" /> + <Function id="zz"> + <Parameter id="a" /> + <Parameter id="b" /> + <Parameter id="c" /> + <Scope> + <Scope> + <Variable id="a" /> + </Scope> + <Scope> + <Variable id="fsdfsdfs" /> + </Scope> + </Scope> + </Function> + <Function id="f"> + <Scope></Scope> + </Function> + <Variable id="g" /> + <Function id="x"> + <Parameter id="y" /> + <Scope> + <Variable id="z" /> + <Scope> + <Variable id="r" /> + </Scope> + <Scope> + <Variable id="f" /> + </Scope> + </Scope> + </Function> + <Function id="f"> + <Scope> + <Variable id="x" /> + <Scope> + <Variable id="y" /> + </Scope> + </Scope> + </Function> + <Function id="f"> + <Parameter id="b" /> + <Parameter id="c" /> + <Scope> + <Variable id="a" /> + </Scope> + </Function> + <Function id="func"> + <Parameter id="x" /> + <Parameter id="y" /> + <Scope> + <Variable id="i" /> + <Scope> + <Variable id="z" /> + </Scope> + </Scope> + </Function> + <Function id="function_1"> + <Parameter id="a" /> + <Parameter id="b" /> + <Scope> + <Variable id="x" /> + <Scope> + <Variable id="c" /> + </Scope> + </Scope> + </Function> + <Function id="add"> + <Parameter id="a" /> + <Parameter id="b" /> + <Scope> + <Variable id="c" /> + </Scope> + </Function> + <Function id="sub"> + <Parameter id="a" /> + <Parameter id="b" /> + <Scope> + <Variable id="c" /> + </Scope> + </Function> + <Function id="mult_by_5"> + <Parameter id="a" /> + <Scope> + <Variable id="b" /> + </Scope> + </Function> + <Function id="add_5"> + <Parameter id="a" /> + <Scope> + <Variable id="i" /> + <Scope> + <Variable id="b" /> + </Scope> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/out/.stderr.txt b/c_parser/test/out/.stderr.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/.stderr.txt diff --git a/c_parser/test/out/.stdout.xml b/c_parser/test/out/.stdout.xml new file mode 100644 index 0000000..efabeac --- /dev/null +++ b/c_parser/test/out/.stdout.xml @@ -0,0 +1,166 @@ +<?xml version="1.0"?> +<Program> +<Variable id="a" /> +<Variable id="b" /> +<Variable id="c" /> +<Variable id="d" /> +<Variable id="e" /> +<Variable id="f" /> +<Function id="f"> +<Scope> +</Scope> +</Function> +<Function id="foo"> +<Parameter id="bar" /> +<Scope> +</Scope> +</Function> +<Function id="foo"> +<Parameter id="bar1" /> +<Parameter id="bar2" /> +<Scope> +<Variable id="x" /> +<Variable id="y" /> +<Variable id="z" /> +</Scope> +</Function> +<Function id="foo"> +<Parameter id="x" /> +<Parameter id="y" /> +<Scope> +<Scope> +</Scope> +</Scope> +</Function> +<Function id="f"> +<Parameter id="a" /> +<Parameter id="b" /> +<Parameter id="c" /> +<Scope> +<Variable id="d" /> +<Scope> +<Variable id="e" /> +</Scope> +</Scope> +</Function> +<Function id="foo"> +<Parameter id="a" /> +<Scope> +<Scope> +<Variable id="z" /> +</Scope> +</Scope> +</Function> +<Function id="func"> +<Parameter id="a" /> +<Parameter id="b" /> +<Scope> +<Variable id="c" /> +<Scope> +<Variable id="d" /> +</Scope> +<Scope> +<Variable id="e" /> +</Scope> +</Scope> +</Function> +<Function id="x"> +<Scope> +</Scope> +</Function> +<Variable id="g" /> +<Function id="zz"> +<Parameter id="a" /> +<Parameter id="b" /> +<Parameter id="c" /> +<Scope> +<Scope> +<Variable id="a" /> +</Scope> +<Scope> +<Variable id="fsdfsdfs" /> +</Scope> +</Scope> +</Function> +<Function id="f"> +<Scope> +</Scope> +</Function> +<Variable id="g" /> +<Function id="x"> +<Parameter id="y" /> +<Scope> +<Variable id="z" /> +<Scope> +<Variable id="r" /> +</Scope> +<Scope> +<Variable id="f" /> +</Scope> +</Scope> +</Function> +<Function id="f"> +<Scope> +<Variable id="x" /> +<Scope> +<Variable id="y" /> +</Scope> +</Scope> +</Function> +<Function id="f"> +<Parameter id="b" /> +<Parameter id="c" /> +<Scope> +<Variable id="a" /> +</Scope> +</Function> +<Function id="func"> +<Parameter id="x" /> +<Parameter id="y" /> +<Scope> +<Variable id="i" /> +<Scope> +<Variable id="z" /> +</Scope> +</Scope> +</Function> +<Function id="function_1"> +<Parameter id="a" /> +<Parameter id="b" /> +<Scope> +<Variable id="x" /> +<Scope> +<Variable id="c" /> +</Scope> +</Scope> +</Function> +<Function id="add"> +<Parameter id="a" /> +<Parameter id="b" /> +<Scope> +<Variable id="c" /> +</Scope> +</Function> +<Function id="sub"> +<Parameter id="a" /> +<Parameter id="b" /> +<Scope> +<Variable id="c" /> +</Scope> +</Function> +<Function id="mult_by_5"> +<Parameter id="a" /> +<Scope> +<Variable id="b" /> +</Scope> +</Function> +<Function id="add_5"> +<Parameter id="a" /> +<Scope> +<Variable id="i" /> +<Scope> +<Variable id="b" /> +</Scope> +</Scope> +</Function> +</Program> diff --git a/c_parser/test/out/01.diff.txt b/c_parser/test/out/01.diff.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/01.diff.txt diff --git a/c_parser/test/out/01.pretty.xml b/c_parser/test/out/01.pretty.xml new file mode 100644 index 0000000..e0545e4 --- /dev/null +++ b/c_parser/test/out/01.pretty.xml @@ -0,0 +1,9 @@ +<?xml version="1.0"?> +<Program> + <Variable id="a" /> + <Variable id="b" /> + <Variable id="c" /> + <Variable id="d" /> + <Variable id="e" /> + <Variable id="f" /> +</Program> diff --git a/c_parser/test/out/01.stderr.txt b/c_parser/test/out/01.stderr.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/01.stderr.txt diff --git a/c_parser/test/out/01.stdout.txt b/c_parser/test/out/01.stdout.txt new file mode 100644 index 0000000..bd7cd1f --- /dev/null +++ b/c_parser/test/out/01.stdout.txt @@ -0,0 +1,9 @@ +<?xml version="1.0"?> +<Program> +<Variable id="a" /> +<Variable id="b" /> +<Variable id="c" /> +<Variable id="d" /> +<Variable id="e" /> +<Variable id="f" /> +</Program> diff --git a/c_parser/test/out/01.stdout.xml b/c_parser/test/out/01.stdout.xml new file mode 100644 index 0000000..bd7cd1f --- /dev/null +++ b/c_parser/test/out/01.stdout.xml @@ -0,0 +1,9 @@ +<?xml version="1.0"?> +<Program> +<Variable id="a" /> +<Variable id="b" /> +<Variable id="c" /> +<Variable id="d" /> +<Variable id="e" /> +<Variable id="f" /> +</Program> diff --git a/c_parser/test/out/02.diff.txt b/c_parser/test/out/02.diff.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/02.diff.txt diff --git a/c_parser/test/out/02.pretty.xml b/c_parser/test/out/02.pretty.xml new file mode 100644 index 0000000..1f73bca --- /dev/null +++ b/c_parser/test/out/02.pretty.xml @@ -0,0 +1,6 @@ +<?xml version="1.0"?> +<Program> + <Function id="f"> + <Scope></Scope> + </Function> +</Program> diff --git a/c_parser/test/out/02.stderr.txt b/c_parser/test/out/02.stderr.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/02.stderr.txt diff --git a/c_parser/test/out/02.stdout.xml b/c_parser/test/out/02.stdout.xml new file mode 100644 index 0000000..bc37d7a --- /dev/null +++ b/c_parser/test/out/02.stdout.xml @@ -0,0 +1,7 @@ +<?xml version="1.0"?> +<Program> +<Function id="f"> +<Scope> +</Scope> +</Function> +</Program> diff --git a/c_parser/test/out/03.diff.txt b/c_parser/test/out/03.diff.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/03.diff.txt diff --git a/c_parser/test/out/03.pretty.xml b/c_parser/test/out/03.pretty.xml new file mode 100644 index 0000000..6ab6ab3 --- /dev/null +++ b/c_parser/test/out/03.pretty.xml @@ -0,0 +1,7 @@ +<?xml version="1.0"?> +<Program> + <Function id="foo"> + <Parameter id="bar" /> + <Scope></Scope> + </Function> +</Program> diff --git a/c_parser/test/out/03.stderr.txt b/c_parser/test/out/03.stderr.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/03.stderr.txt diff --git a/c_parser/test/out/03.stdout.xml b/c_parser/test/out/03.stdout.xml new file mode 100644 index 0000000..6ca0ab2 --- /dev/null +++ b/c_parser/test/out/03.stdout.xml @@ -0,0 +1,8 @@ +<?xml version="1.0"?> +<Program> +<Function id="foo"> +<Parameter id="bar" /> +<Scope> +</Scope> +</Function> +</Program> diff --git a/c_parser/test/out/04.diff.txt b/c_parser/test/out/04.diff.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/04.diff.txt diff --git a/c_parser/test/out/04.pretty.xml b/c_parser/test/out/04.pretty.xml new file mode 100644 index 0000000..caae53b --- /dev/null +++ b/c_parser/test/out/04.pretty.xml @@ -0,0 +1,12 @@ +<?xml version="1.0"?> +<Program> + <Function id="foo"> + <Parameter id="bar1" /> + <Parameter id="bar2" /> + <Scope> + <Variable id="x" /> + <Variable id="y" /> + <Variable id="z" /> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/out/04.stderr.txt b/c_parser/test/out/04.stderr.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/04.stderr.txt diff --git a/c_parser/test/out/04.stdout.xml b/c_parser/test/out/04.stdout.xml new file mode 100644 index 0000000..9257eac --- /dev/null +++ b/c_parser/test/out/04.stdout.xml @@ -0,0 +1,12 @@ +<?xml version="1.0"?> +<Program> +<Function id="foo"> +<Parameter id="bar1" /> +<Parameter id="bar2" /> +<Scope> +<Variable id="x" /> +<Variable id="y" /> +<Variable id="z" /> +</Scope> +</Function> +</Program> diff --git a/c_parser/test/out/05.diff.txt b/c_parser/test/out/05.diff.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/05.diff.txt diff --git a/c_parser/test/out/05.pretty.xml b/c_parser/test/out/05.pretty.xml new file mode 100644 index 0000000..944d950 --- /dev/null +++ b/c_parser/test/out/05.pretty.xml @@ -0,0 +1,10 @@ +<?xml version="1.0"?> +<Program> + <Function id="foo"> + <Parameter id="x" /> + <Parameter id="y" /> + <Scope> + <Scope></Scope> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/out/05.stderr.txt b/c_parser/test/out/05.stderr.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/05.stderr.txt diff --git a/c_parser/test/out/05.stdout.xml b/c_parser/test/out/05.stdout.xml new file mode 100644 index 0000000..7381e51 --- /dev/null +++ b/c_parser/test/out/05.stdout.xml @@ -0,0 +1,11 @@ +<?xml version="1.0"?> +<Program> +<Function id="foo"> +<Parameter id="x" /> +<Parameter id="y" /> +<Scope> +<Scope> +</Scope> +</Scope> +</Function> +</Program> diff --git a/c_parser/test/out/06.diff.txt b/c_parser/test/out/06.diff.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/06.diff.txt diff --git a/c_parser/test/out/06.pretty.xml b/c_parser/test/out/06.pretty.xml new file mode 100644 index 0000000..6422001 --- /dev/null +++ b/c_parser/test/out/06.pretty.xml @@ -0,0 +1,14 @@ +<?xml version="1.0"?> +<Program> + <Function id="f"> + <Parameter id="a" /> + <Parameter id="b" /> + <Parameter id="c" /> + <Scope> + <Variable id="d" /> + <Scope> + <Variable id="e" /> + </Scope> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/out/06.stderr.txt b/c_parser/test/out/06.stderr.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/06.stderr.txt diff --git a/c_parser/test/out/06.stdout.xml b/c_parser/test/out/06.stdout.xml new file mode 100644 index 0000000..fbcbebd --- /dev/null +++ b/c_parser/test/out/06.stdout.xml @@ -0,0 +1,14 @@ +<?xml version="1.0"?> +<Program> +<Function id="f"> +<Parameter id="a" /> +<Parameter id="b" /> +<Parameter id="c" /> +<Scope> +<Variable id="d" /> +<Scope> +<Variable id="e" /> +</Scope> +</Scope> +</Function> +</Program> diff --git a/c_parser/test/out/07.diff.txt b/c_parser/test/out/07.diff.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/07.diff.txt diff --git a/c_parser/test/out/07.pretty.xml b/c_parser/test/out/07.pretty.xml new file mode 100644 index 0000000..11c3b77 --- /dev/null +++ b/c_parser/test/out/07.pretty.xml @@ -0,0 +1,11 @@ +<?xml version="1.0"?> +<Program> + <Function id="foo"> + <Parameter id="a" /> + <Scope> + <Scope> + <Variable id="z" /> + </Scope> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/out/07.stderr.txt b/c_parser/test/out/07.stderr.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/07.stderr.txt diff --git a/c_parser/test/out/07.stdout.xml b/c_parser/test/out/07.stdout.xml new file mode 100644 index 0000000..ff0efb2 --- /dev/null +++ b/c_parser/test/out/07.stdout.xml @@ -0,0 +1,11 @@ +<?xml version="1.0"?> +<Program> +<Function id="foo"> +<Parameter id="a" /> +<Scope> +<Scope> +<Variable id="z" /> +</Scope> +</Scope> +</Function> +</Program> diff --git a/c_parser/test/out/08.diff.txt b/c_parser/test/out/08.diff.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/08.diff.txt diff --git a/c_parser/test/out/08.pretty.xml b/c_parser/test/out/08.pretty.xml new file mode 100644 index 0000000..6cf0f8e --- /dev/null +++ b/c_parser/test/out/08.pretty.xml @@ -0,0 +1,16 @@ +<?xml version="1.0"?> +<Program> + <Function id="func"> + <Parameter id="a" /> + <Parameter id="b" /> + <Scope> + <Variable id="c" /> + <Scope> + <Variable id="d" /> + </Scope> + <Scope> + <Variable id="e" /> + </Scope> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/out/08.stderr.txt b/c_parser/test/out/08.stderr.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/08.stderr.txt diff --git a/c_parser/test/out/08.stdout.xml b/c_parser/test/out/08.stdout.xml new file mode 100644 index 0000000..7929e75 --- /dev/null +++ b/c_parser/test/out/08.stdout.xml @@ -0,0 +1,16 @@ +<?xml version="1.0"?> +<Program> +<Function id="func"> +<Parameter id="a" /> +<Parameter id="b" /> +<Scope> +<Variable id="c" /> +<Scope> +<Variable id="d" /> +</Scope> +<Scope> +<Variable id="e" /> +</Scope> +</Scope> +</Function> +</Program> diff --git a/c_parser/test/out/09.diff.txt b/c_parser/test/out/09.diff.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/09.diff.txt diff --git a/c_parser/test/out/09.pretty.xml b/c_parser/test/out/09.pretty.xml new file mode 100644 index 0000000..6c137df --- /dev/null +++ b/c_parser/test/out/09.pretty.xml @@ -0,0 +1,20 @@ +<?xml version="1.0"?> +<Program> + <Function id="x"> + <Scope></Scope> + </Function> + <Variable id="g" /> + <Function id="zz"> + <Parameter id="a" /> + <Parameter id="b" /> + <Parameter id="c" /> + <Scope> + <Scope> + <Variable id="a" /> + </Scope> + <Scope> + <Variable id="fsdfsdfs" /> + </Scope> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/out/09.stderr.txt b/c_parser/test/out/09.stderr.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/09.stderr.txt diff --git a/c_parser/test/out/09.stdout.xml b/c_parser/test/out/09.stdout.xml new file mode 100644 index 0000000..7a7a770 --- /dev/null +++ b/c_parser/test/out/09.stdout.xml @@ -0,0 +1,21 @@ +<?xml version="1.0"?> +<Program> +<Function id="x"> +<Scope> +</Scope> +</Function> +<Variable id="g" /> +<Function id="zz"> +<Parameter id="a" /> +<Parameter id="b" /> +<Parameter id="c" /> +<Scope> +<Scope> +<Variable id="a" /> +</Scope> +<Scope> +<Variable id="fsdfsdfs" /> +</Scope> +</Scope> +</Function> +</Program> diff --git a/c_parser/test/out/10.diff.txt b/c_parser/test/out/10.diff.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/10.diff.txt diff --git a/c_parser/test/out/10.pretty.xml b/c_parser/test/out/10.pretty.xml new file mode 100644 index 0000000..89a6292 --- /dev/null +++ b/c_parser/test/out/10.pretty.xml @@ -0,0 +1,19 @@ +<?xml version="1.0"?> +<Program> + <Function id="f"> + <Scope></Scope> + </Function> + <Variable id="g" /> + <Function id="x"> + <Parameter id="y" /> + <Scope> + <Variable id="z" /> + <Scope> + <Variable id="r" /> + </Scope> + <Scope> + <Variable id="f" /> + </Scope> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/out/10.stderr.txt b/c_parser/test/out/10.stderr.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/10.stderr.txt diff --git a/c_parser/test/out/10.stdout.xml b/c_parser/test/out/10.stdout.xml new file mode 100644 index 0000000..b07997e --- /dev/null +++ b/c_parser/test/out/10.stdout.xml @@ -0,0 +1,20 @@ +<?xml version="1.0"?> +<Program> +<Function id="f"> +<Scope> +</Scope> +</Function> +<Variable id="g" /> +<Function id="x"> +<Parameter id="y" /> +<Scope> +<Variable id="z" /> +<Scope> +<Variable id="r" /> +</Scope> +<Scope> +<Variable id="f" /> +</Scope> +</Scope> +</Function> +</Program> diff --git a/c_parser/test/out/11.diff.txt b/c_parser/test/out/11.diff.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/11.diff.txt diff --git a/c_parser/test/out/11.pretty.xml b/c_parser/test/out/11.pretty.xml new file mode 100644 index 0000000..b9779d7 --- /dev/null +++ b/c_parser/test/out/11.pretty.xml @@ -0,0 +1,11 @@ +<?xml version="1.0"?> +<Program> + <Function id="f"> + <Scope> + <Variable id="x" /> + <Scope> + <Variable id="y" /> + </Scope> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/out/11.stderr.txt b/c_parser/test/out/11.stderr.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/11.stderr.txt diff --git a/c_parser/test/out/11.stdout.xml b/c_parser/test/out/11.stdout.xml new file mode 100644 index 0000000..0822742 --- /dev/null +++ b/c_parser/test/out/11.stdout.xml @@ -0,0 +1,11 @@ +<?xml version="1.0"?> +<Program> +<Function id="f"> +<Scope> +<Variable id="x" /> +<Scope> +<Variable id="y" /> +</Scope> +</Scope> +</Function> +</Program> diff --git a/c_parser/test/out/12.diff.txt b/c_parser/test/out/12.diff.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/12.diff.txt diff --git a/c_parser/test/out/12.pretty.xml b/c_parser/test/out/12.pretty.xml new file mode 100644 index 0000000..9f49265 --- /dev/null +++ b/c_parser/test/out/12.pretty.xml @@ -0,0 +1,10 @@ +<?xml version="1.0"?> +<Program> + <Function id="f"> + <Parameter id="b" /> + <Parameter id="c" /> + <Scope> + <Variable id="a" /> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/out/12.stderr.txt b/c_parser/test/out/12.stderr.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/12.stderr.txt diff --git a/c_parser/test/out/12.stdout.xml b/c_parser/test/out/12.stdout.xml new file mode 100644 index 0000000..471f225 --- /dev/null +++ b/c_parser/test/out/12.stdout.xml @@ -0,0 +1,10 @@ +<?xml version="1.0"?> +<Program> +<Function id="f"> +<Parameter id="b" /> +<Parameter id="c" /> +<Scope> +<Variable id="a" /> +</Scope> +</Function> +</Program> diff --git a/c_parser/test/out/13.diff.txt b/c_parser/test/out/13.diff.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/13.diff.txt diff --git a/c_parser/test/out/13.pretty.xml b/c_parser/test/out/13.pretty.xml new file mode 100644 index 0000000..a604ecd --- /dev/null +++ b/c_parser/test/out/13.pretty.xml @@ -0,0 +1,13 @@ +<?xml version="1.0"?> +<Program> + <Function id="func"> + <Parameter id="x" /> + <Parameter id="y" /> + <Scope> + <Variable id="i" /> + <Scope> + <Variable id="z" /> + </Scope> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/out/13.stderr.txt b/c_parser/test/out/13.stderr.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/13.stderr.txt diff --git a/c_parser/test/out/13.stdout.xml b/c_parser/test/out/13.stdout.xml new file mode 100644 index 0000000..511d93b --- /dev/null +++ b/c_parser/test/out/13.stdout.xml @@ -0,0 +1,13 @@ +<?xml version="1.0"?> +<Program> +<Function id="func"> +<Parameter id="x" /> +<Parameter id="y" /> +<Scope> +<Variable id="i" /> +<Scope> +<Variable id="z" /> +</Scope> +</Scope> +</Function> +</Program> diff --git a/c_parser/test/out/14.diff.txt b/c_parser/test/out/14.diff.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/14.diff.txt diff --git a/c_parser/test/out/14.pretty.xml b/c_parser/test/out/14.pretty.xml new file mode 100644 index 0000000..866d44e --- /dev/null +++ b/c_parser/test/out/14.pretty.xml @@ -0,0 +1,13 @@ +<?xml version="1.0"?> +<Program> + <Function id="function_1"> + <Parameter id="a" /> + <Parameter id="b" /> + <Scope> + <Variable id="x" /> + <Scope> + <Variable id="c" /> + </Scope> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/out/14.stderr.txt b/c_parser/test/out/14.stderr.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/14.stderr.txt diff --git a/c_parser/test/out/14.stdout.xml b/c_parser/test/out/14.stdout.xml new file mode 100644 index 0000000..1b412d3 --- /dev/null +++ b/c_parser/test/out/14.stdout.xml @@ -0,0 +1,13 @@ +<?xml version="1.0"?> +<Program> +<Function id="function_1"> +<Parameter id="a" /> +<Parameter id="b" /> +<Scope> +<Variable id="x" /> +<Scope> +<Variable id="c" /> +</Scope> +</Scope> +</Function> +</Program> diff --git a/c_parser/test/out/15.diff.txt b/c_parser/test/out/15.diff.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/15.diff.txt diff --git a/c_parser/test/out/15.pretty.xml b/c_parser/test/out/15.pretty.xml new file mode 100644 index 0000000..b06dde0 --- /dev/null +++ b/c_parser/test/out/15.pretty.xml @@ -0,0 +1,32 @@ +<?xml version="1.0"?> +<Program> + <Function id="add"> + <Parameter id="a" /> + <Parameter id="b" /> + <Scope> + <Variable id="c" /> + </Scope> + </Function> + <Function id="sub"> + <Parameter id="a" /> + <Parameter id="b" /> + <Scope> + <Variable id="c" /> + </Scope> + </Function> + <Function id="mult_by_5"> + <Parameter id="a" /> + <Scope> + <Variable id="b" /> + </Scope> + </Function> + <Function id="add_5"> + <Parameter id="a" /> + <Scope> + <Variable id="i" /> + <Scope> + <Variable id="b" /> + </Scope> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/out/15.stderr.txt b/c_parser/test/out/15.stderr.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/15.stderr.txt diff --git a/c_parser/test/out/15.stdout.xml b/c_parser/test/out/15.stdout.xml new file mode 100644 index 0000000..d4c29b2 --- /dev/null +++ b/c_parser/test/out/15.stdout.xml @@ -0,0 +1,32 @@ +<?xml version="1.0"?> +<Program> +<Function id="add"> +<Parameter id="a" /> +<Parameter id="b" /> +<Scope> +<Variable id="c" /> +</Scope> +</Function> +<Function id="sub"> +<Parameter id="a" /> +<Parameter id="b" /> +<Scope> +<Variable id="c" /> +</Scope> +</Function> +<Function id="mult_by_5"> +<Parameter id="a" /> +<Scope> +<Variable id="b" /> +</Scope> +</Function> +<Function id="add_5"> +<Parameter id="a" /> +<Scope> +<Variable id="i" /> +<Scope> +<Variable id="b" /> +</Scope> +</Scope> +</Function> +</Program> diff --git a/c_parser/test/ref/01.stdout.xml b/c_parser/test/ref/01.stdout.xml new file mode 100644 index 0000000..e0545e4 --- /dev/null +++ b/c_parser/test/ref/01.stdout.xml @@ -0,0 +1,9 @@ +<?xml version="1.0"?> +<Program> + <Variable id="a" /> + <Variable id="b" /> + <Variable id="c" /> + <Variable id="d" /> + <Variable id="e" /> + <Variable id="f" /> +</Program> diff --git a/c_parser/test/ref/02.stdout.xml b/c_parser/test/ref/02.stdout.xml new file mode 100644 index 0000000..6caf2fc --- /dev/null +++ b/c_parser/test/ref/02.stdout.xml @@ -0,0 +1,7 @@ +<?xml version="1.0"?> +<Program> + <Function id="f"> + <Scope> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/ref/03.stdout.xml b/c_parser/test/ref/03.stdout.xml new file mode 100644 index 0000000..2c29a50 --- /dev/null +++ b/c_parser/test/ref/03.stdout.xml @@ -0,0 +1,8 @@ +<?xml version="1.0"?> +<Program> + <Function id="foo"> + <Parameter id="bar" /> + <Scope> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/ref/04.stdout.xml b/c_parser/test/ref/04.stdout.xml new file mode 100644 index 0000000..caae53b --- /dev/null +++ b/c_parser/test/ref/04.stdout.xml @@ -0,0 +1,12 @@ +<?xml version="1.0"?> +<Program> + <Function id="foo"> + <Parameter id="bar1" /> + <Parameter id="bar2" /> + <Scope> + <Variable id="x" /> + <Variable id="y" /> + <Variable id="z" /> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/ref/05.stdout.xml b/c_parser/test/ref/05.stdout.xml new file mode 100644 index 0000000..f4b306f --- /dev/null +++ b/c_parser/test/ref/05.stdout.xml @@ -0,0 +1,11 @@ +<?xml version="1.0"?> +<Program> + <Function id="foo"> + <Parameter id="x" /> + <Parameter id="y" /> + <Scope> + <Scope> + </Scope> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/ref/06.stdout.xml b/c_parser/test/ref/06.stdout.xml new file mode 100644 index 0000000..3526139 --- /dev/null +++ b/c_parser/test/ref/06.stdout.xml @@ -0,0 +1,14 @@ +<?xml version="1.0"?> +<Program> + <Function id="f"> + <Parameter id="a" /> + <Parameter id="b" /> + <Parameter id="c" /> + <Scope> + <Variable id="d" /> + <Scope> + <Variable id="e" /> + </Scope> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/ref/07.stdout.xml b/c_parser/test/ref/07.stdout.xml new file mode 100644 index 0000000..17c7c1b --- /dev/null +++ b/c_parser/test/ref/07.stdout.xml @@ -0,0 +1,11 @@ +<?xml version="1.0"?> +<Program> + <Function id="foo"> + <Parameter id="a" /> + <Scope> + <Scope> + <Variable id="z" /> + </Scope> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/ref/08.stdout.xml b/c_parser/test/ref/08.stdout.xml new file mode 100644 index 0000000..0838fe5 --- /dev/null +++ b/c_parser/test/ref/08.stdout.xml @@ -0,0 +1,16 @@ +<?xml version="1.0"?> +<Program> + <Function id="func"> + <Parameter id="a" /> + <Parameter id="b" /> + <Scope> + <Variable id="c" /> + <Scope> + <Variable id="d" /> + </Scope> + <Scope> + <Variable id="e" /> + </Scope> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/ref/09.stdout.xml b/c_parser/test/ref/09.stdout.xml new file mode 100644 index 0000000..646cd98 --- /dev/null +++ b/c_parser/test/ref/09.stdout.xml @@ -0,0 +1,21 @@ +<?xml version="1.0"?> +<Program> + <Function id="x"> + <Scope> + </Scope> + </Function> + <Variable id="g" /> + <Function id="zz"> + <Parameter id="a" /> + <Parameter id="b" /> + <Parameter id="c" /> + <Scope> + <Scope> + <Variable id="a" /> + </Scope> + <Scope> + <Variable id="fsdfsdfs" /> + </Scope> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/ref/10.stdout.xml b/c_parser/test/ref/10.stdout.xml new file mode 100644 index 0000000..21cadeb --- /dev/null +++ b/c_parser/test/ref/10.stdout.xml @@ -0,0 +1,20 @@ +<?xml version="1.0"?> +<Program> + <Function id="f"> + <Scope> + </Scope> + </Function> + <Variable id="g" /> + <Function id="x"> + <Parameter id="y" /> + <Scope> + <Variable id="z" /> + <Scope> + <Variable id="r" /> + </Scope> + <Scope> + <Variable id="f" /> + </Scope> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/ref/11.stdout.xml b/c_parser/test/ref/11.stdout.xml new file mode 100644 index 0000000..ad96d6c --- /dev/null +++ b/c_parser/test/ref/11.stdout.xml @@ -0,0 +1,11 @@ +<?xml version="1.0"?> +<Program> + <Function id="f"> + <Scope> + <Variable id="x" /> + <Scope> + <Variable id="y" /> + </Scope> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/ref/12.stdout.xml b/c_parser/test/ref/12.stdout.xml new file mode 100644 index 0000000..9f49265 --- /dev/null +++ b/c_parser/test/ref/12.stdout.xml @@ -0,0 +1,10 @@ +<?xml version="1.0"?> +<Program> + <Function id="f"> + <Parameter id="b" /> + <Parameter id="c" /> + <Scope> + <Variable id="a" /> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/ref/13.stdout.xml b/c_parser/test/ref/13.stdout.xml new file mode 100644 index 0000000..f0385cb --- /dev/null +++ b/c_parser/test/ref/13.stdout.xml @@ -0,0 +1,13 @@ +<?xml version="1.0"?> +<Program> + <Function id="func"> + <Parameter id="x" /> + <Parameter id="y" /> + <Scope> + <Variable id="i" /> + <Scope> + <Variable id="z" /> + </Scope> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/ref/14.stdout.xml b/c_parser/test/ref/14.stdout.xml new file mode 100644 index 0000000..63aec85 --- /dev/null +++ b/c_parser/test/ref/14.stdout.xml @@ -0,0 +1,13 @@ +<?xml version="1.0"?> +<Program> + <Function id="function_1"> + <Parameter id="a" /> + <Parameter id="b" /> + <Scope> + <Variable id="x" /> + <Scope> + <Variable id="c" /> + </Scope> + </Scope> + </Function> +</Program> diff --git a/c_parser/test/ref/15.stdout.xml b/c_parser/test/ref/15.stdout.xml new file mode 100644 index 0000000..029ccb8 --- /dev/null +++ b/c_parser/test/ref/15.stdout.xml @@ -0,0 +1,32 @@ +<?xml version="1.0"?> +<Program> + <Function id="add"> + <Parameter id="a" /> + <Parameter id="b" /> + <Scope> + <Variable id="c" /> + </Scope> + </Function> + <Function id="sub"> + <Parameter id="a" /> + <Parameter id="b" /> + <Scope> + <Variable id="c" /> + </Scope> + </Function> + <Function id="mult_by_5"> + <Parameter id="a" /> + <Scope> + <Variable id="b" /> + </Scope> + </Function> + <Function id="add_5"> + <Parameter id="a" /> + <Scope> + <Variable id="i" /> + <Scope> + <Variable id="b" /> + </Scope> + </Scope> + </Function> +</Program> @@ -1,13 +1,118 @@ -
-
-bin/c_lexer :
- echo "No current build commands for C lexer."
- exit 1
-
-bin/c_parser :
- echo "No current build commands for C parser."
- exit 1
-
-bin/c_compiler :
- echo "No current build commands for C compile."
- exit 1
+CC := g++ # this is the main compiler +# CC := clang --analyze # and comment out the linker last line + +CFLAGS := -g -Wall -Wextra -Wpedantic -std=c++11 + +LEXINC := -Ic_lexer/include +LEXBUILDDIR := c_lexer/build +LEXSRCDIR := c_lexer/src + +PARINC := -Ic_parser/include +PARBUILDDIR := c_parser/build +PARSRCDIR := c_parser/src + +COMPINC := -Ic_compiler/include +COMPBUILDDIR := c_compiler/build +COMPSRCDIR := c_compiler/src + +all : bin/c_lexer bin/c_parser + +# Make the c_lexer +bin/c_lexer : $(LEXBUILDDIR)/main.o $(LEXBUILDDIR)/c_lexer.o $(LEXBUILDDIR)/c_lexer.yy.o + @echo "Linking..." + @echo " mkdir -p bin"; mkdir -p bin + @echo " $(CC) $^ -o $@"; $(CC) $^ -o $@ + +$(LEXBUILDDIR)/%.o : $(LEXSRCDIR)/%.cpp + @echo "Building sources..." + @echo " mkdir -p $(LEXBUILDDIR)"; mkdir -p $(LEXBUILDDIR) + @echo " $(CC) $(CFLAGS) $(LEXINC) -c -o $@ $<"; $(CC) $(CFLAGS) $(LEXINC) -c -o $@ $< + +$(LEXBUILDDIR)/%.yy.o : $(LEXSRCDIR)/%.yy.cpp + @echo "Building lex..." + @echo " mkdir -p $(LEXBUILDDIR)"; mkdir -p $(LEXBUILDDIR) + @echo " $(CC) $(CFLAGS) $(LEXINC) -c -o $@ $<"; $(CC) $(CFLAGS) $(LEXINC) -c -o $@ $< + +$(LEXSRCDIR)/c_lexer.yy.cpp : $(LEXSRCDIR)/c_lexer.flex + @echo "Creating flex" + @echo " flex -o $@ $<"; flex -o $@ $< + +# Make the c_parser +bin/c_parser : $(PARBUILDDIR)/parser_main.o $(PARBUILDDIR)/c_parser.tab.o $(PARBUILDDIR)/c_lexer.yy.o + @echo "Linking..." + @echo " mkdir -p bin"; mkdir -p bin + @echo " $(CC) $^ -o $@"; $(CC) $^ -o $@ + +$(PARBUILDDIR)/%.o : $(PARSRCDIR)/%.cpp + @echo "Building sources..." + @echo " mkdir -p $(PARBUILDDIR)"; mkdir -p $(PARBUILDDIR) + @echo " $(CC) $(CFLAGS) $(PARINC) -c -o $@ $<"; $(CC) $(CFLAGS) $(PARINC) -c -o $@ $< + +$(PARBUILDDIR)/%.yy.o : $(PARSRCDIR)/%.yy.cpp + @echo "Building lex..." + @echo " mkdir -p $(PARBUILDDIR)"; mkdir -p $(PARBUILDDIR) + @echo " $(CC) $(CFLAGS) $(PARINC) -c -o $@ $<"; $(CC) $(CFLAGS) $(PARINC) -c -o $@ $< + +$(PARBUILDDIR)/c_parser.tab.o : $(PARSRCDIR)/c_parser.tab.cpp + @echo "Building yacc..." + @echo " mkdir -p $(PARBUILDDIR)"; mkdir -p $(PARBUILDDIR) + @echo " $(CC) $(CFLAGS) $(PARINC) -c -o $@ $<"; $(CC) $(CFLAGS) $(PARINC) -c -o $@ $< + +$(PARSRCDIR)/c_lexer.yy.cpp : $(PARSRCDIR)/c_lexer.flex + @echo "Creating lex..." + @echo " flex -o $@ $<"; flex -o $@ $< + +$(PARSRCDIR)/c_parser.tab.cpp $(PARSRCDIR)/c_parser.tab.hpp : $(PARSRCDIR)/c_parser.y + @echo "Creating yacc..." + @echo " bison -v -d $< -o $(PARSRCDIR)/c_parser.tab.cpp"; bison -v -d $< -o $(PARSRCDIR)/c_parser.tab.cpp + +# Make the c_parser +bin/c_compiler: $(COMPBUILDDIR)/compiler_main.o $(COMPBUILDDIR)/statement.o \ + $(COMPBUILDDIR)/function.o $(COMPBUILDDIR)/expression.o \ + $(COMPBUILDDIR)/translation_unit.o \ + $(COMPBUILDDIR)/declaration.o $(COMPBUILDDIR)/type.o \ + $(COMPBUILDDIR)/c_parser.tab.o $(COMPBUILDDIR)/c_lexer.yy.o + @echo "Linking..." + @echo " mkdir -p bin"; mkdir -p bin + @echo " $(CC) $^ -o $@"; $(CC) $^ -o $@ + +$(COMPBUILDDIR)/%.o : $(COMPSRCDIR)/%.cpp + @echo "Building sources..." + @echo " mkdir -p $(COMPBUILDDIR)"; mkdir -p $(COMPBUILDDIR) + @echo " $(CC) $(CFLAGS) $(COMPINC) -c -o $@ $<"; $(CC) $(CFLAGS) $(COMPINC) -c -o $@ $< + +$(COMPBUILDDIR)/%.yy.o : $(COMPSRCDIR)/%.yy.cpp + @echo "Building lex..." + @echo " mkdir -p $(COMPBUILDDIR)"; mkdir -p $(COMPBUILDDIR) + @echo " $(CC) $(CFLAGS) $(COMPINC) -c -o $@ $<"; $(CC) $(CFLAGS) $(COMPINC) -c -o $@ $< + +$(COMPBUILDDIR)/c_parser.tab.o : $(COMPSRCDIR)/c_parser.tab.cpp + @echo "Building yacc..." + @echo " mkdir -p $(COMPBUILDDIR)"; mkdir -p $(COMPBUILDDIR) + @echo " $(CC) $(CFLAGS) $(COMPINC) -c -o $@ $<"; $(CC) $(CFLAGS) $(COMPINC) -c -o $@ $< + +$(COMPSRCDIR)/c_lexer.yy.cpp : $(COMPSRCDIR)/c_lexer.flex + @echo "Creating lex..." + @echo " flex -o $@ $<"; flex -o $@ $< + +$(COMPSRCDIR)/c_parser.tab.cpp $(COMPSRCDIR)/c_parser.tab.hpp : $(COMPSRCDIR)/c_parser.y + @echo "Creating yacc..." + @echo " bison -v -d $< -o $(COMPSRCDIR)/c_parser.tab.cpp"; bison -v -d $< -o $(COMPSRCDIR)/c_parser.tab.cpp + +clean : + @echo "Cleaning..." + rm -rf bin + rm -rf $(LEXBUILDDIR) + rm -f $(LEXSRCDIR)/c_lexer.yy.cpp + rm -rf $(PARBUILDDIR) + rm -f $(PARSRCDIR)/c_lexer.yy.cpp + rm -f $(PARSRCDIR)/c_parser.tab.cpp + rm -f $(PARSRCDIR)/c_parser.tab.hpp + rm -f $(PARSRCDIR)/c_parser.output + rm -rf $(COMPBUILDDIR) + rm -f $(COMPSRCDIR)/c_lexer.yy.cpp + rm -f $(COMPSRCDIR)/c_parser.tab.cpp + rm -f $(COMPSRCDIR)/c_parser.tab.hpp + rm -f $(COMPSRCDIR)/c_parser.output + +.PHONY : clean diff --git a/test_compiler.sh b/test_compiler.sh new file mode 100755 index 0000000..700afe5 --- /dev/null +++ b/test_compiler.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +# script to test the lexer + +echo "========================================" +echo " Cleaning the temporaries and outputs" +make clean +echo " Force building lexer" +make -B bin/c_compiler + +if [[ "$?" -ne 0 ]]; then + echo "Build failed."; +fi + +echo "" +echo "" +echo "=========================================" +echo " Testing compiler" + +PASSED=0 +CHECKED=0 + +mkdir -p c_compiler/test/out +mkdir -p c_compiler/test/ref + +for i in c_compiler/test/in/*.c; do + echo "===========================" + echo "" + echo "Input file : ${i}" + BASENAME=$(basename $i .c); + cat $i | ./bin/c_compiler > c_compiler/test/out/$BASENAME.s 2> c_compiler/test/out/$BASENAME.stderr.txt + + mips-linux-gnu-gcc -S -c c_compiler/test/in/$BASENAME.c -o c_compiler/test/ref/$BASENAME.s + mips-linux-gnu-gcc -static c_compiler/test/ref/$BASENAME.s -o c_compiler/test/ref/$BASENAME + + mips-linux-gnu-gcc -static c_compiler/test/out/$BASENAME.s -o c_compiler/test/out/$BASENAME + + qemu-mips c_compiler/test/ref/$BASENAME + REFOUTPUT=$? + + qemu-mips c_compiler/test/out/$BASENAME + TESTOUTPUT=$? + + if [ "$TESTOUTPUT" = "$REFOUTPUT" ]; then + PASSED=$(( PASSED+1 )) + else + echo -e "\nERROR" + fi + + echo -e "output: $TESTOUTPUT\n" + + CHECKED=$(( CHECKED+1 )) +done + +echo "########################################" +echo "Passed ${PASSED} out of ${CHECKED}". +echo "" diff --git a/test_lexer.sh b/test_lexer.sh new file mode 100755 index 0000000..c9904ee --- /dev/null +++ b/test_lexer.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +# script to test the lexer + +echo "========================================" +echo " Cleaning the temporaries and outputs" +make clean +echo " Force building lexer" +make -B bin/c_lexer + +if [[ "$?" -ne 0 ]]; then + echo "Build failed."; +fi + +echo "=========================================" +echo " Testing lexer" + +cpp c_lexer/test/test_lex.c c_lexer/test/pre_processed_test_lex.c +cat c_lexer/test/pre_processed_test_lex.c | bin/c_lexer | tee c_lexer/test/output.json +#cat test/test_lex.c | ./bin/c_lexer | tee test/output.json diff --git a/test_parser.sh b/test_parser.sh new file mode 100755 index 0000000..cddc491 --- /dev/null +++ b/test_parser.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +# script to test the lexer + +echo "========================================" +echo " Cleaning the temporaries and outputs" +make clean +echo " Force building lexer" +make -B bin/c_parser + +if [[ "$?" -ne 0 ]]; then + echo "Build failed."; +fi + +echo "" +echo "" +echo "=========================================" +echo " Testing parser" + +PASSED=0 +CHECKED=0 + +for i in c_parser/test/in/*.c; do + echo "===========================" + echo "" + echo "Input file : ${i}" + BASENAME=$(basename $i .c); + cat $i | ./bin/c_parser > c_parser/test/out/$BASENAME.stdout.xml 2> c_parser/test/out/$BASENAME.stderr.txt + tidy -xml -i -q -o c_parser/test/out/$BASENAME.pretty.xml c_parser/test/out/$BASENAME.stdout.xml + + diff <(cat c_parser/test/ref/$BASENAME.stdout.xml | tidy -xml -i -q) <(cat c_parser/test/out/$BASENAME.stdout.xml | tidy -xml -i -q) > c_parser/test/out/$BASENAME.diff.txt + if [[ "$?" -ne "0" ]]; then + echo -e "\nERROR" + else + PASSED=$(( ${PASSED}+1 )); + fi + CHECKED=$(( ${CHECKED}+1 )); +done + +echo "########################################" +echo "Passed ${PASSED} out of ${CHECKED}". +echo "" |