aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTeddy <[email protected]>2014-03-25 04:11:54 +0800
committerTeddy <[email protected]>2014-03-25 04:11:54 +0800
commit4f90efb9a08b697b7ef270a4d2ee067dc853f251 (patch)
tree229eb348ca132a3969b1906dec90e29edd8cd06a
parentda3bf74ba8946f88b892907361f86f567301be83 (diff)
move out the main function to a seperate file
-rw-r--r--Makefile10
-rw-r--r--ast.c4
-rw-r--r--ast.h2
-rw-r--r--cibic.y28
-rw-r--r--main.c74
5 files changed, 82 insertions, 36 deletions
diff --git a/Makefile b/Makefile
index 04edd68..cfb1849 100644
--- a/Makefile
+++ b/Makefile
@@ -1,17 +1,19 @@
all: cibic
run:
- ./cibic
+ ./cibic --ast
-debug:
+db:
gdb cibic
-cibic: lex.yy.o cibic.tab.o ast.o
- gcc -o cibic lex.yy.o cibic.tab.o ast.o
+cibic: lex.yy.o cibic.tab.o ast.o main.o
+ gcc -o cibic lex.yy.o cibic.tab.o ast.o main.o
lex.yy.o: lex.yy.c
gcc -c lex.yy.c
cibic.tab.o: cibic.tab.c
gcc -c cibic.tab.c
+main.o: main.c
+ gcc -c main.c -g -Wall -Wextra
ast.o: ast.c
gcc -c ast.c -g -Wall -Wextra -DCNODE_DEBUG
lex.yy.c: cibic.l
diff --git a/ast.c b/ast.c
index f3ad3ba..c1feff9 100644
--- a/ast.c
+++ b/ast.c
@@ -1,6 +1,5 @@
#include <stdlib.h>
#include <assert.h>
-#include <stdarg.h>
#include <stdio.h>
#include "ast.h"
#include "cibic.tab.h"
@@ -8,9 +7,6 @@
CNode *ast_root;
-void cnode_init() {
-}
-
void cnode_reverse_chd(CNode *node) {
static CNode *chdn[MAX_CHDN];
CNode *p;
diff --git a/ast.h b/ast.h
index 17b75d6..3399cb7 100644
--- a/ast.h
+++ b/ast.h
@@ -1,5 +1,6 @@
#ifndef AST_H
#define AST_H
+#include <stdarg.h>
#define EXP_POSTFIX 1024
#define POSTFIX_ARR 1025
@@ -68,7 +69,6 @@ typedef struct CNode {
} loc;
} CNode;
-void cnode_init();
CNode *cnode_create_ast(CNode *wrapped);
CNode *cnode_create_nop();
CNode *cnode_create_general(int type, int subtype, int pnum, va_list ap);
diff --git a/cibic.y b/cibic.y
index 0a98097..f85d28c 100644
--- a/cibic.y
+++ b/cibic.y
@@ -17,10 +17,7 @@
%type<intval> INT_CONST CHAR_CONST
%type<strval> IDENTIFIER STR_CONST
%type<intval> assignment_operator equality_operator relational_operator shift_operator additive_operator multiplicative_operator unary_operator struct_or_union
-%type<cnode> expression assignment_expression constant_expression
-%type<cnode> logical_or_expression logical_and_expression inclusive_or_expression exclusive_or_expression and_expression equality_expression relational_expression shift_expression additive_expression multiplicative_expression cast_expression type_name
-%type<cnode> unary_expression postfix_expression identifier primary_expression arguments postfix type_specifier program declaration function_definition parameters declarators init_declarators init_declarator initializer array_initializer struct_fields struct_field plain_declaration declarator_array plain_declarator expression_statement compound_statement statement
-%type<cnode> comp_decls comp_stmts selection_statement iteration_statement jump_statement optional_exp declarator prog_list
+%type<cnode> expression assignment_expression constant_expression logical_or_expression logical_and_expression inclusive_or_expression exclusive_or_expression and_expression equality_expression relational_expression shift_expression additive_expression multiplicative_expression cast_expression type_name unary_expression postfix_expression identifier primary_expression arguments postfix type_specifier program declaration function_definition parameters declarators init_declarators init_declarator initializer array_initializer struct_fields struct_field plain_declaration declarator_array plain_declarator expression_statement compound_statement statement comp_decls comp_stmts selection_statement iteration_statement jump_statement optional_exp declarator prog_list
%start program
%%
program
@@ -324,26 +321,3 @@ primary_expression
identifier
: IDENTIFIER { $$ = cnode_create_identifier($1); }
%%
-int yywrap() {
- return 1;
-}
-
-int yyerror(char *s) {
-}
-
-extern FILE *yyin;
-void test_ast() {
- yyparse();
- cnode_init();
- if (ast_root)
- cnode_debug_print(ast_root, 1);
- else
- fprintf(stderr, "Syntax Error\n");
-}
-
-int main() {
- int ret;
- yyin = fopen("in.c", "r");
- test_ast();
- return 0;
-}
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..181dde2
--- /dev/null
+++ b/main.c
@@ -0,0 +1,74 @@
+#include <stdio.h>
+#include <getopt.h>
+#include "cibic.tab.h"
+#include "ast.h"
+
+extern int yyparse();
+extern FILE *yyin;
+
+int yywrap() {
+ return 1;
+}
+
+int yyerror(char *s) {
+}
+
+void print_ast() {
+ yyparse();
+ if (ast_root)
+ cnode_debug_print(ast_root, 1);
+ else
+ fprintf(stderr, "Syntax Error\n");
+}
+
+void print_help() {
+}
+
+static struct option lopts[] = {
+ { "ast", no_argument, 0, 'a'},
+ { "help", no_argument, 0, 'h'},
+ {0, 0, 0, 0}
+};
+
+enum {
+ PRINT_AST,
+ PRINT_HELP
+} mode = PRINT_HELP;
+
+int main(int argc, char **argv) {
+ int option_index = 0, i;
+ while (1)
+ {
+ int c = getopt_long(argc, argv, "ah", lopts, &option_index);
+ if (c == -1)
+ break;
+ switch (c)
+ {
+ case 0: break;
+ case 'a': mode = PRINT_AST; break;
+ case 'h': mode = PRINT_HELP; break;
+ }
+ }
+ if (optind == argc - 1)
+ {
+ yyin = fopen(argv[argc - 1], "r");
+ if (!yyin)
+ {
+ fprintf(stderr, "Error while opening file.");
+ return 1;
+ }
+ }
+ else if (optind == argc)
+ yyin = stdin;
+ else
+ {
+ fprintf(stderr, "Only one source file is exepected.\n");
+ return 1;
+ }
+ switch (mode)
+ {
+ case PRINT_AST: print_ast(); break;
+ case PRINT_HELP: print_help(); break;
+ }
+ return 0;
+}