diff options
Diffstat (limited to 'src/tpc.y')
-rw-r--r-- | src/tpc.y | 238 |
1 files changed, 127 insertions, 111 deletions
@@ -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 | ||
134 | int main(int argc, char **argv) { | 151 | int main(int argc, char **argv) { |
135 | yyparse(); | 152 | return yyparse(); |
136 | return 0; | ||
137 | } | 153 | } |
138 | 154 | ||