Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions 06-c-syntax-analyzer/err2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
int a = 3;
char g_c = (char) 3;

void main() {

switch (a) {
case 1:
g_c = 'z';
case 2:
g_c = 's';
case 3:
a = 2;
default:
a++;
}

int x , y = 2;
}
20 changes: 20 additions & 0 deletions 06-c-syntax-analyzer/err3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
int func1(int pr);
void func2();
char func3();

void main(){
int i = 16;
char c;
char str[i+1];

char *s = "string";

int a = func1();
c = func2(); // return type unmatched
func3();
}
int func1() {return 1;}
void func2() {}
char func3() {
return 's';
}
24 changes: 24 additions & 0 deletions 06-c-syntax-analyzer/err4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
int global;
char global_c;

int func();

int main () {

int a1 = 1;
enum ss { s, f , sk} ff;
struct df {
int a;
int c;
char i;
} *st;

a2 = (int) func(); // undefined var
st->b = 1; // undefined field
gobal_c = (char)1; // typo
return 0;
}

int func(){
return 3;
}
11 changes: 11 additions & 0 deletions 06-c-syntax-analyzer/err5.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
int main () {
struct ss {
int a;
int b;
} a;

struct ss {
int a;
char c;
} t;
}
10 changes: 10 additions & 0 deletions 06-c-syntax-analyzer/err6.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
int func1();
char func2(int a);

int main(void){
return 1;
}

char func1() {
return '2';
}
6 changes: 6 additions & 0 deletions 06-c-syntax-analyzer/err7.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
int func(int, char);
int main(void)
{
int a;

}
2 changes: 1 addition & 1 deletion 06-c-syntax-analyzer/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ void main(int argc, char *argv[]){
}
print_ast(root);
exit(0);
}
}
2 changes: 1 addition & 1 deletion 06-c-syntax-analyzer/test2.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ int a = 3;
char g_c = (char) 3;

void main() {
int x , y = 2;

switch (a) {
case 1:
Expand All @@ -14,5 +15,4 @@ void main() {
a++;
}

int x , y = 2;
}
4 changes: 2 additions & 2 deletions 06-c-syntax-analyzer/test4.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ int main () {
int a;
int c;
char i;
} st;
} *st;

a1 = (int) func();
st.a = 1;
st->a = 1;
global_c = (char)1;
return 0;
}
Expand Down
9 changes: 9 additions & 0 deletions 07-c-semantic-analyzer/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
a.out: yacc lex
gcc -g y.tab.c lex.yy.c sem_print.c func.c sem_func.c main.c -w
yacc: yacc.y
yacc -d yacc.y
lex: lex.l
lex lex.l

make clean:
rm a.out
4 changes: 2 additions & 2 deletions 07-c-semantic-analyzer/lex.l
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ while { return(WHILE_SYM); }
"\=" { return(ASSIGN); }

{digit}+ { yylval = atoi(yytext); return(INTEGER_CONSTANT); }
{digit}+\.{digit}+ { yylval = (YYSTYPE)makeString(yytext); return(FLOAT_CONSTANT); }
{digit}+\.{digit}+ { yylval = makeString(yytext); return(FLOAT_CONSTANT); }
{letter}({letter}|{digit})* { return(checkIdentifier(yytext)); }
\"([^"\n]|\\["\n])*\" { yylval = (YYSTYPE)makeString(yytext); return(STRING_LITERAL); }
\"([^"\n]|\\["\n])*\" { yylval = makeString(yytext); return(STRING_LITERAL); }
\'([^'\n]|\'\')\' { yylval = *(yytext+1); return(CHARACTER_CONSTANT); }
"//"[^\n]* { }

Expand Down
81 changes: 14 additions & 67 deletions 07-c-semantic-analyzer/lex.yy.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,27 +162,8 @@ extern FILE *yyin, *yyout;
#define EOB_ACT_END_OF_FILE 1
#define EOB_ACT_LAST_MATCH 2

/* Note: We specifically omit the test for yy_rule_can_match_eol because it requires
* access to the local variable yy_act. Since yyless() is a macro, it would break
* existing scanners that call yyless() from OUTSIDE yylex.
* One obvious solution it to make yy_act a global. I tried that, and saw
* a 5% performance hit in a non-yylineno scanner, because yy_act is
* normally declared as a register variable-- so it is not worth it.
*/
#define YY_LESS_LINENO(n) \
do { \
int yyl;\
for ( yyl = n; yyl < yyleng; ++yyl )\
if ( yytext[yyl] == '\n' )\
--yylineno;\
}while(0)
#define YY_LINENO_REWIND_TO(dst) \
do {\
const char *p;\
for ( p = yy_cp-1; p >= (dst); --p)\
if ( *p == '\n' )\
--yylineno;\
}while(0)
#define YY_LESS_LINENO(n)
#define YY_LINENO_REWIND_TO(ptr)

/* Return all but the first "n" matched characters back to the input stream. */
#define yyless(n) \
Expand Down Expand Up @@ -347,11 +328,14 @@ FILE *yyin = NULL, *yyout = NULL;

typedef int yy_state_type;

#define YY_FLEX_LEX_COMPAT
extern int yylineno;
int yylineno = 1;

extern char yytext[];
extern char *yytext;
#ifdef yytext_ptr
#undef yytext_ptr
#endif
#define yytext_ptr yytext

static yy_state_type yy_get_previous_state ( void );
static yy_state_type yy_try_NUL_trans ( yy_state_type current_state );
Expand All @@ -366,9 +350,6 @@ static void yynoreturn yy_fatal_error ( const char* msg );
yyleng = (int) (yy_cp - yy_bp); \
(yy_hold_char) = *yy_cp; \
*yy_cp = '\0'; \
if ( yyleng >= YYLMAX ) \
YY_FATAL_ERROR( "token too large, exceeds YYLMAX" ); \
yy_flex_strncpy( yytext, (yytext_ptr), yyleng + 1 ); \
(yy_c_buf_p) = yy_cp;
#define YY_NUM_RULES 57
#define YY_END_OF_BUFFER 58
Expand Down Expand Up @@ -535,13 +516,6 @@ static const flex_int16_t yy_chk[213] =
140, 140
} ;

/* Table of booleans, true if rule could match eol. */
static const flex_int32_t yy_rule_can_match_eol[58] =
{ 0,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, };

static yy_state_type yy_last_accepting_state;
static char *yy_last_accepting_cpos;

Expand All @@ -555,12 +529,7 @@ int yy_flex_debug = 0;
#define yymore() yymore_used_but_not_detected
#define YY_MORE_ADJ 0
#define YY_RESTORE_YY_MORE_OFFSET
#ifndef YYLMAX
#define YYLMAX 8192
#endif

char yytext[YYLMAX];
char *yytext_ptr;
char *yytext;
#line 1 "lex.l"
#line 8 "lex.l"
#define YYSTYPE_IS_DECLARED 1
Expand All @@ -575,8 +544,8 @@ extern A_ID *current_id;

char *makeString();
int checkIdentifier();
#line 579 "lex.yy.c"
#line 580 "lex.yy.c"
#line 548 "lex.yy.c"
#line 549 "lex.yy.c"

#define INITIAL 0

Expand Down Expand Up @@ -795,7 +764,7 @@ YY_DECL
{
#line 22 "lex.l"

#line 799 "lex.yy.c"
#line 768 "lex.yy.c"

while ( /*CONSTCOND*/1 ) /* loops until end-of-file is reached */
{
Expand Down Expand Up @@ -841,16 +810,6 @@ YY_DECL

YY_DO_BEFORE_ACTION;

if ( yy_act != YY_END_OF_BUFFER && yy_rule_can_match_eol[yy_act] )
{
int yyl;
for ( yyl = 0; yyl < yyleng; ++yyl )
if ( yytext[yyl] == '\n' )

yylineno++;
;
}

do_action: /* This label is used only to access EOF actions. */

switch ( yy_act )
Expand Down Expand Up @@ -1121,7 +1080,7 @@ YY_RULE_SETUP
case 52:
YY_RULE_SETUP
#line 76 "lex.l"
{ yylval = (YYSTYPE)makeString(yytext); return(FLOAT_CONSTANT); }
{ yylval = makeString(yytext); return(FLOAT_CONSTANT); }
YY_BREAK
case 53:
YY_RULE_SETUP
Expand All @@ -1132,7 +1091,7 @@ case 54:
/* rule 54 can match eol */
YY_RULE_SETUP
#line 78 "lex.l"
{ yylval = (YYSTYPE)makeString(yytext); return(STRING_LITERAL); }
{ yylval = makeString(yytext); return(STRING_LITERAL); }
YY_BREAK
case 55:
YY_RULE_SETUP
Expand All @@ -1149,7 +1108,7 @@ YY_RULE_SETUP
#line 82 "lex.l"
ECHO;
YY_BREAK
#line 1153 "lex.yy.c"
#line 1112 "lex.yy.c"
case YY_STATE_EOF(INITIAL):
yyterminate();

Expand Down Expand Up @@ -1517,10 +1476,6 @@ static int yy_get_next_buffer (void)

*--yy_cp = (char) c;

if ( c == '\n' ){
--yylineno;
}

(yytext_ptr) = yy_bp;
(yy_hold_char) = *yy_cp;
(yy_c_buf_p) = yy_cp;
Expand Down Expand Up @@ -1598,11 +1553,6 @@ static int yy_get_next_buffer (void)
*(yy_c_buf_p) = '\0'; /* preserve yytext */
(yy_hold_char) = *++(yy_c_buf_p);

if ( c == '\n' )

yylineno++;
;

return c;
}
#endif /* ifndef YY_NO_INPUT */
Expand Down Expand Up @@ -2069,9 +2019,6 @@ static int yy_init_globals (void)
* This function is called from yylex_destroy(), so don't allocate here.
*/

/* We do not touch yylineno unless the option is enabled. */
yylineno = 1;

(yy_buffer_stack) = NULL;
(yy_buffer_stack_top) = 0;
(yy_buffer_stack_max) = 0;
Expand Down
5 changes: 3 additions & 2 deletions 07-c-semantic-analyzer/main.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#include <stdio.h>
#include <stdlib.h>
#include "type.h"
#include "sem_func.h"

extern int syntax_err;
extern int semantic_err;
extern A_NODE *root;

void initialize();
void print_ast(); // semantic
void print_sem_ast(); // semantic

int main(){
initialize();
yyparse();
if (syntax_err) exit(1);
semantic_analysis(root);
if (semantic_err) exit(1);
print_ast(root);
print_sem_ast(root);
}
Loading