diff options
Diffstat (limited to 'src/tpc.y')
-rw-r--r-- | src/tpc.y | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/src/tpc.y b/src/tpc.y new file mode 100644 index 0000000..88e31fb --- /dev/null +++ b/src/tpc.y | |||
@@ -0,0 +1,139 @@ | |||
1 | %{ | ||
2 | /* | ||
3 | * UPEM / Compilation / Projet | ||
4 | * Pacien TRAN-GIRARD, Adam NAILI | ||
5 | */ | ||
6 | |||
7 | #include <stdio.h> | ||
8 | |||
9 | extern int lineno; | ||
10 | int yylex(); | ||
11 | void yyerror(char *); | ||
12 | |||
13 | %} | ||
14 | |||
15 | %union { | ||
16 | char caractere; | ||
17 | int num; | ||
18 | char ident[64]; | ||
19 | char type[16]; | ||
20 | char comp[3]; | ||
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 | |||
34 | %left ',' | ||
35 | %precedence ')' | ||
36 | %precedence ELSE | ||
37 | |||
38 | %% | ||
39 | Prog : DeclConsts DeclVars DeclFoncts | ||
40 | ; | ||
41 | DeclConsts : DeclConsts CONST ListConst ';' | ||
42 | | | ||
43 | ; | ||
44 | ListConst : ListConst ',' IDENT '=' Litteral | ||
45 | | IDENT '=' Litteral | ||
46 | ; | ||
47 | Litteral : NombreSigne | ||
48 | | CARACTERE | ||
49 | ; | ||
50 | NombreSigne : NUM | ||
51 | | ADDSUB NUM | ||
52 | ; | ||
53 | DeclVars : | ||
54 | DeclVars TYPE Declarateurs ';' | ||
55 | | | ||
56 | ; | ||
57 | Declarateurs : Declarateurs ',' Declarateur | ||
58 | | Declarateur | ||
59 | ; | ||
60 | Declarateur : IDENT | ||
61 | | IDENT '[' NUM ']' | ||
62 | ; | ||
63 | DeclFoncts : DeclFoncts DeclFonct | ||
64 | | DeclFonct | ||
65 | ; | ||
66 | DeclFonct : EnTeteFonct Corps | ||
67 | ; | ||
68 | EnTeteFonct : TYPE IDENT '(' Parametres ')' | ||
69 | | VOID IDENT '(' Parametres ')' | ||
70 | ; | ||
71 | Parametres : VOID | ||
72 | | ListTypVar | ||
73 | ; | ||
74 | ListTypVar : ListTypVar ',' TYPE IDENT | ||
75 | | TYPE IDENT | ||
76 | ; | ||
77 | Corps : '{' DeclConsts DeclVars SuiteInstr '}' | ||
78 | ; | ||
79 | SuiteInstr : SuiteInstr Instr | ||
80 | | | ||
81 | ; | ||
82 | Instr : Exp ';' | ||
83 | | RETURN Exp ';' | ||
84 | | RETURN ';' | ||
85 | | ';' | ||
86 | | IF '(' Exp ')' Instr | ||
87 | | IF '(' Exp ')' Instr ELSE Instr | ||
88 | | WHILE '(' Exp ')' Instr | ||
89 | | '{' SuiteInstr '}' | ||
90 | ; | ||
91 | Exp : LValue '=' Exp | ||
92 | | EB | ||
93 | ; | ||
94 | EB : EB OR TB | ||
95 | | TB | ||
96 | ; | ||
97 | TB : TB AND FB | ||
98 | | FB | ||
99 | ; | ||
100 | FB : FB EQ M | ||
101 | | M | ||
102 | ; | ||
103 | M : M ORDER E | ||
104 | | E | ||
105 | ; | ||
106 | E : E ADDSUB T | ||
107 | | T | ||
108 | ; | ||
109 | T : T DIVSTAR F | ||
110 | | F | ||
111 | ; | ||
112 | F : ADDSUB F | ||
113 | | '!' F | ||
114 | | '(' Exp ')' | ||
115 | | LValue | ||
116 | | NUM | ||
117 | | CARACTERE | ||
118 | | IDENT '(' Arguments ')' | ||
119 | ; | ||
120 | LValue : IDENT | ||
121 | | IDENT '[' Exp ']' | ||
122 | ; | ||
123 | Arguments : ListExp | ||
124 | | | ||
125 | ; | ||
126 | ListExp : ListExp ',' ListExp | ||
127 | | Exp | ||
128 | ; | ||
129 | %% | ||
130 | |||
131 | void yyerror(char *s){ | ||
132 | fprintf(stderr, "Line no%d: %s\n", lineno, s); | ||
133 | } | ||
134 | |||
135 | int main(int argc, char **argv) { | ||
136 | yyparse(); | ||
137 | return 0; | ||
138 | } | ||
139 | |||