diff options
author | pacien | 2018-02-23 01:11:34 +0100 |
---|---|---|
committer | pacien | 2018-02-23 01:11:34 +0100 |
commit | 806dd4ff649a1fb9a28dc469cc17d7dd5dbc1da5 (patch) | |
tree | da414ebbb1807a2dea82235faa429f6e6613db69 /src | |
parent | 11acfa2e75350d31a665b8ee99925916c2c8b5f1 (diff) | |
download | tpc-compiler-806dd4ff649a1fb9a28dc469cc17d7dd5dbc1da5.tar.gz |
Adapt for new grammar
Diffstat (limited to 'src')
-rw-r--r-- | src/tpc.lex | 9 | ||||
-rw-r--r-- | src/tpc.y | 215 |
2 files changed, 129 insertions, 95 deletions
diff --git a/src/tpc.lex b/src/tpc.lex index 63d4391..8b824c0 100644 --- a/src/tpc.lex +++ b/src/tpc.lex | |||
@@ -16,15 +16,13 @@ int lineno = 1; | |||
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 | "*"|"/"|% { return DIVSTAR; } | 21 | "*"|"/"|% { return DIVSTAR; } |
22 | "+"|- { return ADDSUB; } | 22 | "+"|- { return ADDSUB; } |
23 | "<"|"<="|">"|>= { return ORDER; } | 23 | "<"|"<="|">"|">=" { return ORDER; } |
24 | ==|!= { return EQ; } | 24 | ==|!= { return EQ; } |
25 | int { return TYPE; } | 25 | int { return TYPE; } |
26 | float { return TYPE; } | ||
27 | double { return TYPE; } | ||
28 | char { return TYPE; } | 26 | char { return TYPE; } |
29 | void { return VOID; } | 27 | void { return VOID; } |
30 | const { return CONST; } | 28 | const { return CONST; } |
@@ -32,6 +30,9 @@ 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; } |
33 | print { return PRINT; } | ||
34 | readc { return READC; } | ||
35 | reade { return READE; } | ||
35 | [a-zA-Z_][a-zA-Z0-9_]* { return IDENT; } | 36 | [a-zA-Z_][a-zA-Z0-9_]* { return IDENT; } |
36 | [0-9]+ { return NUM; } | 37 | [0-9]+ { return NUM; } |
37 | '\\?.' { return CARACTERE; } | 38 | '\\?.' { return CARACTERE; } |
@@ -12,103 +12,136 @@ void yyerror(char *); | |||
12 | 12 | ||
13 | %} | 13 | %} |
14 | 14 | ||
15 | %token CARACTERE NUM IDENT ORDER EQ ADDSUB DIVSTAR | 15 | %token CARACTERE NUM IDENT |
16 | %token OR AND CONST IF WHILE RETURN VOID TYPE | 16 | %token ADDSUB DIVSTAR |
17 | %token ORDER EQ OR AND | ||
18 | %token IF WHILE RETURN | ||
19 | %token CONST VOID TYPE | ||
20 | %token PRINT READC READE | ||
17 | 21 | ||
18 | %left ',' | 22 | %left ',' |
19 | %precedence ')' | 23 | %precedence ')' |
20 | %precedence ELSE | 24 | %precedence ELSE |
21 | 25 | ||
22 | %% | 26 | %% |
23 | Prog : DeclConsts DeclVars DeclFoncts | 27 | Prog: |
24 | ; | 28 | DeclConsts DeclVars DeclFoncts |
25 | DeclConsts : DeclConsts CONST ListConst ';' | 29 | ; |
26 | | | 30 | DeclConsts: |
27 | ; | 31 | DeclConsts CONST ListConst ';' |
28 | ListConst : ListConst ',' IDENT '=' Litteral | 32 | | |
29 | | IDENT '=' Litteral | 33 | ; |
30 | ; | 34 | ListConst: |
31 | Litteral : NombreSigne | 35 | ListConst ',' IDENT '=' Litteral |
32 | | CARACTERE | 36 | | IDENT '=' Litteral |
33 | ; | 37 | ; |
34 | NombreSigne : NUM | 38 | Litteral: |
35 | | ADDSUB NUM | 39 | NombreSigne |
36 | ; | 40 | | CARACTERE |
37 | DeclVars : DeclVars TYPE Declarateurs ';' | 41 | ; |
38 | | | 42 | NombreSigne: |
39 | ; | 43 | NUM |
40 | Declarateurs : Declarateurs ',' Declarateur | 44 | | ADDSUB NUM |
41 | | Declarateur | 45 | ; |
42 | ; | 46 | DeclVars: |
43 | Declarateur : IDENT | 47 | DeclVars TYPE Declarateurs ';' |
44 | | IDENT '[' NUM ']' | 48 | | |
45 | ; | 49 | ; |
46 | DeclFoncts : DeclFoncts DeclFonct | 50 | Declarateurs: |
47 | | DeclFonct | 51 | Declarateurs ',' Declarateur |
48 | ; | 52 | | Declarateur |
49 | DeclFonct : EnTeteFonct Corps | 53 | ; |
50 | ; | 54 | Declarateur: |
51 | EnTeteFonct : TYPE IDENT '(' Parametres ')' | 55 | IDENT |
52 | | VOID IDENT '(' Parametres ')' | 56 | | IDENT '[' NUM ']' |
53 | ; | 57 | ; |
54 | Parametres : VOID | 58 | DeclFoncts: |
55 | | ListTypVar | 59 | DeclFoncts DeclFonct |
56 | ; | 60 | | DeclFonct |
57 | ListTypVar : ListTypVar ',' TYPE IDENT | 61 | ; |
58 | | TYPE IDENT | 62 | DeclFonct: |
59 | ; | 63 | EnTeteFonct Corps |
60 | Corps : '{' DeclConsts DeclVars SuiteInstr '}' | 64 | ; |
61 | ; | 65 | EnTeteFonct: |
62 | SuiteInstr : SuiteInstr Instr | 66 | TYPE IDENT '(' Parametres ')' |
63 | | | 67 | | VOID IDENT '(' Parametres ')' |
64 | ; | 68 | ; |
65 | Instr : Exp ';' | 69 | Parametres: |
66 | | RETURN Exp ';' | 70 | VOID |
67 | | RETURN ';' | 71 | | ListTypVar |
68 | | ';' | 72 | ; |
69 | | IF '(' Exp ')' Instr | 73 | ListTypVar: |
70 | | IF '(' Exp ')' Instr ELSE Instr | 74 | ListTypVar ',' TYPE IDENT |
71 | | WHILE '(' Exp ')' Instr | 75 | | TYPE IDENT |
72 | | '{' SuiteInstr '}' | 76 | ; |
73 | ; | 77 | Corps: |
74 | Exp : LValue '=' Exp | 78 | '{' DeclConsts DeclVars SuiteInstr '}' |
75 | | EB | 79 | ; |
76 | ; | 80 | SuiteInstr: |
77 | EB : EB OR TB | 81 | SuiteInstr Instr |
78 | | TB | 82 | | ; |
79 | ; | 83 | Instr: |
80 | TB : TB AND FB | 84 | Exp ';' |
81 | | FB | 85 | | ';' |
82 | ; | 86 | | RETURN Exp ';' |
83 | FB : FB EQ M | 87 | | RETURN ';' |
84 | | M | 88 | | READE '(' IDENT ')' ';' |
85 | ; | 89 | | READC '(' IDENT ')' ';' |
86 | M : M ORDER E | 90 | | PRINT '(' Exp ')' ';' |
87 | | E | 91 | | IF '(' Exp ')' Instr |
88 | ; | 92 | | IF '(' Exp ')' Instr ELSE Instr |
89 | E : E ADDSUB T | 93 | | WHILE '(' Exp ')' Instr |
90 | | T | 94 | | '{' SuiteInstr '}' |
91 | ; | 95 | ; |
92 | T : T DIVSTAR F | 96 | Exp: |
93 | | F | 97 | LValue '=' Exp |
94 | ; | 98 | | EB |
95 | F : ADDSUB F | 99 | ; |
96 | | '!' F | 100 | EB: |
97 | | '(' Exp ')' | 101 | EB OR TB |
98 | | LValue | 102 | | TB |
99 | | NUM | 103 | ; |
100 | | CARACTERE | 104 | TB: |
101 | | IDENT '(' Arguments ')' | 105 | TB AND FB |
102 | ; | 106 | | FB |
103 | LValue : IDENT | 107 | ; |
104 | | IDENT '[' Exp ']' | 108 | FB: |
105 | ; | 109 | FB EQ M |
106 | Arguments : ListExp | 110 | | M |
107 | | | 111 | ; |
108 | ; | 112 | M: |
109 | ListExp : ListExp ',' ListExp | 113 | M ORDER E |
110 | | Exp | 114 | | E |
111 | ; | 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 | ; | ||
112 | %% | 145 | %% |
113 | 146 | ||
114 | void yyerror(char *msg){ | 147 | void yyerror(char *msg){ |