diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/tpc.lex | 46 | ||||
-rw-r--r-- | src/tpc.y | 238 |
2 files changed, 147 insertions, 137 deletions
diff --git a/src/tpc.lex b/src/tpc.lex index eaeea89..8b824c0 100644 --- a/src/tpc.lex +++ b/src/tpc.lex | |||
@@ -14,37 +14,31 @@ int lineno = 1; | |||
14 | 14 | ||
15 | %% | 15 | %% |
16 | [ \t\r]+ ; | 16 | [ \t\r]+ ; |
17 | \n { lineno++;} | 17 | \n { lineno++; } |
18 | "/*" { BEGIN COMMENT;} | 18 | "/*" { BEGIN COMMENT; } |
19 | && { return AND; } | 19 | "&&" { return AND; } |
20 | "||" { return OR; } | 20 | "||" { return OR; } |
21 | "*"|"/"|% { yylval.divstar=yytext[0]; return DIVSTAR; } | 21 | "*"|"/"|% { return DIVSTAR; } |
22 | "+"|- { yylval.addsub=yytext[0]; return ADDSUB; } | 22 | "+"|- { return ADDSUB; } |
23 | "<"|"<="|">"|>= { strcpy(yylval.comp, yytext); return ORDER; } | 23 | "<"|"<="|">"|">=" { return ORDER; } |
24 | ==|!= { strcpy(yylval.comp, yytext); return EQ; } | 24 | ==|!= { return EQ; } |
25 | int { strcpy(yylval.type, yytext); return TYPE; } | 25 | int { return TYPE; } |
26 | float { strcpy(yylval.type, yytext); return TYPE; } | 26 | char { return TYPE; } |
27 | double { strcpy(yylval.type, yytext); return TYPE; } | ||
28 | char { strcpy(yylval.type, yytext); return TYPE; } | ||
29 | void { return VOID; } | 27 | void { return VOID; } |
30 | const { return CONST; } | 28 | const { return CONST; } |
31 | if { return IF;} | 29 | if { return IF; } |
32 | else { return ELSE;} | 30 | else { return ELSE; } |
33 | while { return WHILE; } | 31 | while { return WHILE; } |
34 | return { return RETURN; } | 32 | return { return RETURN; } |
35 | [a-zA-Z_][a-zA-Z0-9_]* { strcpy(yylval.ident, yytext); return IDENT; } | 33 | print { return PRINT; } |
36 | [0-9]+ { sscanf(yytext, "%d", &(yylval.num)); return NUM; } | 34 | readc { return READC; } |
37 | '\\?.' { if (strlen(yytext)==3) | 35 | reade { return READE; } |
38 | yylval.caractere=yytext[1]; | 36 | [a-zA-Z_][a-zA-Z0-9_]* { return IDENT; } |
39 | else switch(yytext[2]) { | 37 | [0-9]+ { return NUM; } |
40 | case 'n': yylval.caractere='\n'; break; | 38 | '\\?.' { return CARACTERE; } |
41 | case 't': yylval.caractere='\t'; break; | 39 | . { return yytext[0]; } |
42 | case '\'': yylval.caractere='\''; break; | 40 | <COMMENT>"*/" { BEGIN INITIAL; } |
43 | } | 41 | <COMMENT>\n { lineno++; } |
44 | return CARACTERE; } | ||
45 | . { return yytext[0];} | ||
46 | <COMMENT>"*/" { BEGIN INITIAL;} | ||
47 | <COMMENT>\n { lineno++;} | ||
48 | <COMMENT>. ; | 42 | <COMMENT>. ; |
49 | %% | 43 | %% |
50 | 44 | ||
@@ -12,127 +12,143 @@ void yyerror(char *); | |||
12 | 12 | ||
13 | %} | 13 | %} |
14 | 14 | ||
15 | %union { | 15 | %token CARACTERE NUM IDENT |
16 | char caractere; | 16 | %token ADDSUB DIVSTAR |
17 | int num; | 17 | %token ORDER EQ OR AND |
18 | char ident[64]; | 18 | %token IF WHILE RETURN |
19 | char type[16]; | 19 | %token CONST VOID TYPE |
20 | char comp[3]; | 20 | %token PRINT READC READE |
21 | char addsub; | ||
22 | char divstar; | ||
23 | } | ||
24 | |||
25 | %token <caractere> CARACTERE | ||
26 | %token <num> NUM | ||
27 | %token <ident> IDENT | ||
28 | %token <comp> ORDER EQ | ||
29 | %token <addsub> ADDSUB | ||
30 | %token <divstar> DIVSTAR | ||
31 | %token OR AND CONST IF WHILE RETURN VOID | ||
32 | %token <type> TYPE | ||
33 | 21 | ||
34 | %left ',' | 22 | %left ',' |
35 | %precedence ')' | 23 | %precedence ')' |
36 | %precedence ELSE | 24 | %precedence ELSE |
37 | 25 | ||
38 | %% | 26 | %% |
39 | Prog : DeclConsts DeclVars DeclFoncts | 27 | Prog: |
40 | ; | 28 | DeclConsts DeclVars DeclFoncts |
41 | DeclConsts : DeclConsts CONST ListConst ';' | 29 | ; |
42 | | | 30 | DeclConsts: |
43 | ; | 31 | DeclConsts CONST ListConst ';' |
44 | ListConst : ListConst ',' IDENT '=' Litteral | 32 | | |
45 | | IDENT '=' Litteral | 33 | ; |
46 | ; | 34 | ListConst: |
47 | Litteral : NombreSigne | 35 | ListConst ',' IDENT '=' Litteral |
48 | | CARACTERE | 36 | | IDENT '=' Litteral |
49 | ; | 37 | ; |
50 | NombreSigne : NUM | 38 | Litteral: |
51 | | ADDSUB NUM | 39 | NombreSigne |
52 | ; | 40 | | CARACTERE |
53 | DeclVars : DeclVars TYPE Declarateurs ';' | 41 | ; |
54 | | | 42 | NombreSigne: |
55 | ; | 43 | NUM |
56 | Declarateurs : Declarateurs ',' Declarateur | 44 | | ADDSUB NUM |
57 | | Declarateur | 45 | ; |
58 | ; | 46 | DeclVars: |
59 | Declarateur : IDENT | 47 | DeclVars TYPE Declarateurs ';' |
60 | | IDENT '[' NUM ']' | 48 | | |
61 | ; | 49 | ; |
62 | DeclFoncts : DeclFoncts DeclFonct | 50 | Declarateurs: |
63 | | DeclFonct | 51 | Declarateurs ',' Declarateur |
64 | ; | 52 | | Declarateur |
65 | DeclFonct : EnTeteFonct Corps | 53 | ; |
66 | ; | 54 | Declarateur: |
67 | EnTeteFonct : TYPE IDENT '(' Parametres ')' | 55 | IDENT |
68 | | VOID IDENT '(' Parametres ')' | 56 | | IDENT '[' NUM ']' |
69 | ; | 57 | ; |
70 | Parametres : VOID | 58 | DeclFoncts: |
71 | | ListTypVar | 59 | DeclFoncts DeclFonct |
72 | ; | 60 | | DeclFonct |
73 | ListTypVar : ListTypVar ',' TYPE IDENT | 61 | ; |
74 | | TYPE IDENT | 62 | DeclFonct: |
75 | ; | 63 | EnTeteFonct Corps |
76 | Corps : '{' DeclConsts DeclVars SuiteInstr '}' | 64 | ; |
77 | ; | 65 | EnTeteFonct: |
78 | SuiteInstr : SuiteInstr Instr | 66 | TYPE IDENT '(' Parametres ')' |
79 | | | 67 | | VOID IDENT '(' Parametres ')' |
80 | ; | 68 | ; |
81 | Instr : Exp ';' | 69 | Parametres: |
82 | | RETURN Exp ';' | 70 | VOID |
83 | | RETURN ';' | 71 | | ListTypVar |
84 | | ';' | 72 | ; |
85 | | IF '(' Exp ')' Instr | 73 | ListTypVar: |
86 | | IF '(' Exp ')' Instr ELSE Instr | 74 | ListTypVar ',' TYPE IDENT |
87 | | WHILE '(' Exp ')' Instr | 75 | | TYPE IDENT |
88 | | '{' SuiteInstr '}' | 76 | ; |
89 | ; | 77 | Corps: |
90 | Exp : LValue '=' Exp | 78 | '{' DeclConsts DeclVars SuiteInstr '}' |
91 | | EB | 79 | ; |
92 | ; | 80 | SuiteInstr: |
93 | EB : EB OR TB | 81 | SuiteInstr Instr |
94 | | TB | 82 | | ; |
95 | ; | 83 | Instr: |
96 | TB : TB AND FB | 84 | Exp ';' |
97 | | FB | 85 | | ';' |
98 | ; | 86 | | RETURN Exp ';' |
99 | FB : FB EQ M | 87 | | RETURN ';' |
100 | | M | 88 | | READE '(' IDENT ')' ';' |
101 | ; | 89 | | READC '(' IDENT ')' ';' |
102 | M : M ORDER E | 90 | | PRINT '(' Exp ')' ';' |
103 | | E | 91 | | IF '(' Exp ')' Instr |
104 | ; | 92 | | IF '(' Exp ')' Instr ELSE Instr |
105 | E : E ADDSUB T | 93 | | WHILE '(' Exp ')' Instr |
106 | | T | 94 | | '{' SuiteInstr '}' |
107 | ; | 95 | ; |
108 | T : T DIVSTAR F | 96 | Exp: |
109 | | F | 97 | LValue '=' Exp |
110 | ; | 98 | | EB |
111 | F : ADDSUB F | 99 | ; |
112 | | '!' F | 100 | EB: |
113 | | '(' Exp ')' | 101 | EB OR TB |
114 | | LValue | 102 | | TB |
115 | | NUM | 103 | ; |
116 | | CARACTERE | 104 | TB: |
117 | | IDENT '(' Arguments ')' | 105 | TB AND FB |
118 | ; | 106 | | FB |
119 | LValue : IDENT | 107 | ; |
120 | | IDENT '[' Exp ']' | 108 | FB: |
121 | ; | 109 | FB EQ M |
122 | Arguments : ListExp | 110 | | M |
123 | | | 111 | ; |
124 | ; | 112 | M: |
125 | ListExp : ListExp ',' ListExp | 113 | M ORDER E |
126 | | Exp | 114 | | E |
127 | ; | 115 | ; |
116 | E: | ||
117 | E ADDSUB T | ||
118 | | T | ||
119 | ; | ||
120 | T: | ||
121 | T DIVSTAR F | ||
122 | | F | ||
123 | ; | ||
124 | F: | ||
125 | ADDSUB F | ||
126 | | '!' F | ||
127 | | '(' Exp ')' | ||
128 | | LValue | ||
129 | | NUM | ||
130 | | CARACTERE | ||
131 | | IDENT '(' Arguments ')' | ||
132 | ; | ||
133 | LValue: | ||
134 | IDENT | ||
135 | | IDENT '[' Exp ']' | ||
136 | ; | ||
137 | Arguments: | ||
138 | ListExp | ||
139 | | | ||
140 | ; | ||
141 | ListExp: | ||
142 | ListExp ',' Exp | ||
143 | | Exp | ||
144 | ; | ||
128 | %% | 145 | %% |
129 | 146 | ||
130 | void yyerror(char *s){ | 147 | void yyerror(char *msg){ |
131 | fprintf(stderr, "Line no%d: %s\n", lineno, s); | 148 | fprintf(stderr, "%s at line %d\n", msg, lineno); |
132 | } | 149 | } |
133 | 150 | ||