diff options
author | Teddy <[email protected]> | 2014-03-25 06:57:44 +0800 |
---|---|---|
committer | Teddy <[email protected]> | 2014-03-25 06:57:44 +0800 |
commit | 79b8219a7b8a786740a6c57b2d819953bcf261de (patch) | |
tree | a0e81987d32f5748ff99de2c67dd543abd42417f | |
parent | 04ee9c8bef572f4351090b768413e5b6a3ac4a77 (diff) |
verbose error reporting
-rw-r--r-- | ast.c | 16 | ||||
-rw-r--r-- | ast.h | 2 | ||||
-rw-r--r-- | cibic.l | 19 | ||||
-rw-r--r-- | cibic.y | 7 | ||||
-rw-r--r-- | main.c | 12 |
5 files changed, 34 insertions, 22 deletions
@@ -81,12 +81,12 @@ CNode *cnode_create_int_const(int val) { return exp; } -CNode *cnode_create_char_const(int val) { +CNode *cnode_create_char_const(char *val) { /* TODO: overflow checking */ CNode *exp = NEW_CNODE; exp->type = CHAR; exp->chd = exp->next = NULL; - exp->rec.intval = val; + exp->rec.strval = val; return exp; } @@ -375,13 +375,13 @@ char *cnode_debug_type_repr(CNode *ast) { } void cnode_debug_print_plain(CNode *ast) { - printf("(%s", cnode_debug_type_repr(ast)); + fprintf(stderr, "(%s", cnode_debug_type_repr(ast)); for (ast = ast->chd; ast; ast = ast->next) { - printf(" "); + fprintf(stderr, " "); cnode_debug_print_plain(ast); } - printf(")"); + fprintf(stderr, ")"); } void cnode_debug_print_fancy(CNode *ast, int lvl) { @@ -389,10 +389,10 @@ void cnode_debug_print_fancy(CNode *ast, int lvl) { int i; show[lvl] = 1; for (i = 0; i < lvl - 1; i++) - printf("%c ", show[i] ? '|' : ' '); + fprintf(stderr, "%c ", show[i] ? '|' : ' '); if (lvl) - printf("|____"); - printf("[%s]\n", cnode_debug_type_repr(ast)); + fprintf(stderr, "|____"); + fprintf(stderr, "[%s]\n", cnode_debug_type_repr(ast)); for (ast = ast->chd; ast; ast = ast->next) { if (!ast->next) show[lvl] = 0; @@ -89,7 +89,7 @@ CNode *cnode_create_plain_decl(CNode *type_spec, CNode *declr); CNode *cnode_create_identifier(char *val); CNode *cnode_create_int_const(int val); -CNode *cnode_create_char_const(int val); +CNode *cnode_create_char_const(char *val); CNode *cnode_create_str_const(char *val); void cnode_debug_print(CNode *ast, int fancy); @@ -1,12 +1,20 @@ %{ #include "cibic.tab.h" +int yycolumn = 1; +#define YY_USER_ACTION \ + yylloc.first_line = yylloc.last_line = yylineno; \ + yylloc.first_column = yycolumn; yylloc.last_column = yycolumn + yyleng - 1; \ + yycolumn += yyleng; + %} letter [a-zA-Z_$] digit [0-9] %s IN_BLOCK_COMMENT IN_INLINE_COMMENT IN_DIRECTIVE +%option yylineno %% + <INITIAL>{ "/*" BEGIN(IN_BLOCK_COMMENT); } @@ -14,22 +22,22 @@ digit [0-9] "*/" BEGIN(INITIAL); [^*\n]+ // eat comment in chunks "*" // eat the lone star -\n +\n { yycolumn = 1; } } <INITIAL>{ "//" BEGIN(IN_INLINE_COMMENT); } <IN_INLINE_COMMENT>{ -\n BEGIN(INITIAL); +\n { yycolumn = 1; BEGIN(INITIAL); } [^\n]+ } <INITIAL>{ -"#" BEGIN(IN_INLINE_COMMENT); +"#" BEGIN(IN_DIRECTIVE); } <IN_DIRECTIVE>{ -\n BEGIN(INITIAL); +\n { yycolumn = 1; BEGIN(INITIAL); } [^\n]+ } @@ -99,6 +107,7 @@ digit [0-9] [();,={}\[\]*|\^&<>+\-*//%~!.] { return *yytext; } -[ \t\n\r] /* skip whitespaces */ +[ \t\r] /* skip whitespaces */ +\n { yycolumn = 1; } . { return UNKNOWN; } %% @@ -7,7 +7,7 @@ char *strval; struct CNode *cnode; } - +%error-verbose %token IDENTIFIER INT_CONST CHAR_CONST STR_CONST %token KW_VOID KW_CHAR KW_INT KW_STRUCT KW_UNION KW_IF KW_ELSE KW_WHILE %token KW_FOR KW_CONT KW_BREAK KW_RET KW_SIZEOF @@ -161,7 +161,10 @@ jump_statement expression : assignment_expression - | expression ',' assignment_expression { $$ = cnode_create_exp(',', 2, $1, $3); } + | expression ',' assignment_expression { + printf("%d, %d\n", @$.first_line, @$.first_column); + $$ = cnode_create_exp(',', 2, $1, $3); + } assignment_expression : logical_or_expression @@ -1,5 +1,6 @@ #include <stdio.h> #include <getopt.h> +#include <stdlib.h> #include "cibic.tab.h" #include "ast.h" @@ -12,21 +13,20 @@ int yywrap() { } int yyerror(char *s) { + fprintf(stderr, "%s\n", s); } void print_ast() { - yyparse(); if (fname) - printf("AST for file: \"%s\"\n", fname); + fprintf(stderr, "AST for file: \"%s\"\n", fname); else - printf("AST for stdin\n"); - + fprintf(stderr, "AST for stdin\n"); + yyparse(); if (ast_root) { cnode_debug_print(ast_root, 1); } - else - fprintf(stdout, "Syntax Error\n"); + else exit(1); } void print_help() { |