Skip to content

Commit b21f135

Browse files
author
hcj
committed
first commit
0 parents  commit b21f135

File tree

111 files changed

+6370
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+6370
-0
lines changed

.Trash-0/files/src.lex

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
TOKEN-TYPE TOKEN-VALUE
2+
-------------------------------------------------
3+
T_Int int
4+
T_Identifier main
5+
( (
6+
) )
7+
{ {
8+
T_Int int
9+
T_Identifier n
10+
; ;
11+
T_Identifier n
12+
= =
13+
T_IntConstant 1
14+
; ;
15+
T_Print print
16+
( (
17+
T_StringConstant "The first 10 number of the fibonacci sequence:"
18+
) )
19+
; ;
20+
T_While while
21+
( (
22+
T_Identifier n
23+
T_Le <=
24+
T_IntConstant 10
25+
) )
26+
{ {
27+
T_Print print
28+
( (
29+
T_StringConstant "fib(%d)=%d"
30+
, ,
31+
T_Identifier n
32+
, ,
33+
T_Identifier fib
34+
( (
35+
T_Identifier n
36+
) )
37+
) )
38+
; ;
39+
T_Identifier n
40+
= =
41+
T_Identifier n
42+
+ +
43+
T_IntConstant 1
44+
; ;
45+
} }
46+
T_Return return
47+
T_IntConstant 0
48+
; ;
49+
} }
50+
T_Int int
51+
T_Identifier fib
52+
( (
53+
T_Int int
54+
T_Identifier n
55+
) )
56+
{ {
57+
T_If if
58+
( (
59+
T_Identifier n
60+
T_Le <=
61+
T_IntConstant 2
62+
) )
63+
{ {
64+
T_Return return
65+
T_IntConstant 1
66+
; ;
67+
} }
68+
T_Return return
69+
T_Identifier fib
70+
( (
71+
T_Identifier n
72+
- -
73+
T_IntConstant 1
74+
) )
75+
+ +
76+
T_Identifier fib
77+
( (
78+
T_Identifier n
79+
- -
80+
T_IntConstant 2
81+
) )
82+
; ;
83+
} }

.Trash-0/info/src.lex.trashinfo

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[Trash Info]
2+
Path=ch8/tinyc_scanner/src.lex
3+
DeletionDate=2015-05-31T09:42:08

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
http://pandolia.net/tinyc

ch13/bison_1/calc.l

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
%{
2+
#include "y.tab.h"
3+
%}
4+
5+
%%
6+
[0-9]+ { yylval = atoi(yytext); return T_NUM; }
7+
[-/+*()\n] { return yytext[0]; }
8+
. { return 0; /* end when meet everything else */ }
9+
%%
10+
11+
int yywrap(void) {
12+
return 1;
13+
}

ch13/bison_1/calc.y

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
%{
2+
#include <stdio.h>
3+
void yyerror(const char* msg) {}
4+
%}
5+
6+
%token T_NUM
7+
8+
%left '+' '-'
9+
%left '*' '/'
10+
11+
%%
12+
13+
S : S E '\n' { printf("ans = %d\n", $2); }
14+
| /* empty */ { /* empty */ }
15+
;
16+
17+
E : E '+' E { $$ = $1 + $3; }
18+
| E '-' E { $$ = $1 - $3; }
19+
| E '*' E { $$ = $1 * $3; }
20+
| E '/' E { $$ = $1 / $3; }
21+
| T_NUM { $$ = $1; }
22+
| '(' E ')' { $$ = $2; }
23+
;
24+
25+
%%
26+
27+
int main() {
28+
return yyparse();
29+
}

ch13/bison_1/makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CC = gcc
2+
OBJ = lex.yy.o y.tab.o
3+
4+
calc: $(OBJ)
5+
$(CC) -o calc $(OBJ)
6+
7+
lex.yy.c: calc.l y.tab.c
8+
flex $<
9+
10+
y.tab.c: calc.y
11+
bison -vdty $<
12+
13+
clean:
14+
rm -f *.o lex.yy.c y.tab.c y.tab.h y.output calc

ch13/bison_2/makefile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
CC = gcc
2+
OUT = tcc
3+
OBJ = lex.yy.o y.tab.o
4+
SCANNER = scanner.l
5+
PARSER = parser.y
6+
7+
build: $(OUT)
8+
9+
run: $(OUT)
10+
./$(OUT) < test.c > test.asm
11+
12+
clean:
13+
rm -f *.o lex.yy.c y.tab.c y.tab.h y.output $(OUT)
14+
15+
$(OUT): $(OBJ)
16+
$(CC) -o $(OUT) $(OBJ)
17+
18+
lex.yy.c: $(SCANNER) y.tab.c
19+
flex $<
20+
21+
y.tab.c: $(PARSER)
22+
bison -vdty $<

ch13/bison_2/parser.y

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
%{
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
void yyerror(const char*);
5+
#define YYSTYPE char *
6+
%}
7+
8+
%token T_IntConstant T_Identifier
9+
10+
%left '+' '-'
11+
%left '*' '/'
12+
%right U_neg
13+
14+
%%
15+
16+
S : Stmt
17+
| S Stmt
18+
;
19+
20+
Stmt: T_Identifier '=' E ';' { printf("pop %s\n\n", $1); }
21+
;
22+
23+
E : E '+' E { printf("add\n"); }
24+
| E '-' E { printf("sub\n"); }
25+
| E '*' E { printf("mul\n"); }
26+
| E '/' E { printf("div\n"); }
27+
| '-' E %prec U_neg { printf("neg\n"); }
28+
| T_IntConstant { printf("push %s\n", $1); }
29+
| T_Identifier { printf("push %s\n", $1); }
30+
| '(' E ')' { /* empty */ }
31+
;
32+
33+
%%
34+
35+
int main() {
36+
return yyparse();
37+
}

ch13/bison_2/scanner.l

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
%{
2+
#define YYSTYPE char *
3+
#include "y.tab.h"
4+
int cur_line = 1;
5+
void yyerror(const char *msg);
6+
void unrecognized_char(char c);
7+
%}
8+
9+
OPERATOR [-/+*()=;]
10+
INTEGER [0-9]+
11+
IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
12+
WHITESPACE [ \t]*
13+
14+
%%
15+
{OPERATOR} { return yytext[0]; }
16+
{INTEGER} { yylval = strdup(yytext); return T_IntConstant; }
17+
{IDENTIFIER} { yylval = strdup(yytext); return T_Identifier; }
18+
{WHITESPACE} { /* ignore every whitespcace */ }
19+
\n { cur_line++; }
20+
. { unrecognized_char(yytext[0]); }
21+
%%
22+
23+
int yywrap(void) {
24+
return 1;
25+
}
26+
27+
void unrecognized_char(char c) {
28+
char buf[32] = "Unrecognized character: ?";
29+
buf[24] = c;
30+
yyerror(buf);
31+
}
32+
33+
void yyerror(const char *msg) {
34+
printf("Error at line %d:\n\t%s\n", cur_line, msg);
35+
exit(1);
36+
}

ch13/bison_2/test.asm

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
push 1
2+
push 2
3+
push 2
4+
push 2
5+
add
6+
mul
7+
add
8+
pop a
9+
10+
push c
11+
push d
12+
add
13+
pop b
14+
15+
push f
16+
push 7
17+
push 8
18+
mul
19+
push 9
20+
div
21+
add
22+
pop e
23+

0 commit comments

Comments
 (0)