Jawoll ja!
Nachdem ich die letzten Tage es geschafft habe, die libphorward und die pggrammar-Erweiterung zumindest skizziert in einen Zustand zu bringen, der letztendlich erreicht werden soll, bin ich heute auch noch dort ziemlich weit gekommen.
Zumindest kann die libphorward jetzt sogar einen abstract syntax tree sowie schon seit vorvorgestern einen syntax tree aus einer geparsten Eingabe generieren!
Demo-Programm:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
#include <phorward.h> int main() { pggrammar* g; pgparser* p; pgparser* p2; pgterminal* i; pgterminal* op_a; pgterminal* op_s; pgterminal* op_d; pgterminal* op_m; pgterminal* br_op; pgterminal* br_cl; pgnonterminal* start; pgnonterminal* expr; pgnonterminal* term; pgnonterminal* factor; pgtoken* tok; g = pg_grammar_create(); i = pg_terminal_create( g, "@INTEGER", "[0-9]+" ); op_a = pg_terminal_create( g, "+", "\\+" ); op_s = pg_terminal_create( g, "-", "-" ); op_d = pg_terminal_create( g, "/", "/" ); op_m = pg_terminal_create( g, "*", "\\*" ); br_op = pg_terminal_create( g, "(", "\\(" ); br_cl = pg_terminal_create( g, ")", "\\)" ); start = pg_nonterminal_create( g, "start" ); expr = pg_nonterminal_create( g, "expr" ); term = pg_nonterminal_create( g, "term" ); factor = pg_nonterminal_create( g, "factor" ); /* start */ pg_production_create( start, expr, (pgsymbol*)NULL ); /* expr */ pg_production_create_as_node( expr, "add", NULL, expr, op_a, term, (pgsymbol*)NULL ); pg_production_create_as_node( expr, "sub", NULL, expr, op_s, term, (pgsymbol*)NULL ); pg_production_create( expr, term, (pgsymbol*)NULL ); /* term */ pg_production_create_as_node( term, "mul", NULL, term, op_m, factor, (pgsymbol*)NULL ); pg_production_create_as_node( term, "div", NULL, term, op_d, factor, (pgsymbol*)NULL ); pg_production_create( term, factor, (pgsymbol*)NULL ); /* factor */ pg_production_create( factor, br_op, expr, br_cl, (pgsymbol*)NULL ); pg_production_create( factor, i, (pgsymbol*)NULL ); pg_grammar_print( g ); p = pg_parser_create( g, PGPARADIGM_LALR1 ); pg_lexer_set_source( p->lexer, PG_LEX_SRCTYPE_STRING, "1*2+3" ); pg_parser_parse( p ); getchar(); fprintf( stderr, "------------------------------\n" ); pg_lexer_set_source( p->lexer, PG_LEX_SRCTYPE_STRING, "(7+3)*2-5" ); pg_parser_parse( p ); return 0; } |
Erzeugt nun das hier:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 |
*** FINAL STATES*** -- State 0 0x1191330 -- Kernel: start : . expr -> Shift on '(' to state 3 <- Shift/Reduce on '@INTEGER' by production 'factor : @@INTEGER' -> Goto state 1 on 'expr' -> Goto state 2 on 'term' <- Goto/Reduce by production 'term : factor' in 'factor' -- State 1 0x11925a0 -- Kernel: start : expr . [ @@eof ] expr : expr . @+ term expr : expr . @- term -> Shift on '+' to state 4 -> Shift on '-' to state 5 <- Reduce on '@eof' by production 'start : expr' -- State 2 0x1192830 -- Kernel: expr : term . [ @- @) @+ @@eof ] term : term . @* factor term : term . @/ factor -> Shift on '*' to state 6 -> Shift on '/' to state 7 <- Reduce on '-' by production 'expr : term' <- Reduce on ')' by production 'expr : term' <- Reduce on '+' by production 'expr : term' <- Reduce on '@eof' by production 'expr : term' -- State 3 0x1192b10 -- Kernel: factor : @( . expr @) <- Shift/Reduce on '@INTEGER' by production 'factor : @@INTEGER' -> Goto state 8 on 'expr' -> Goto state 2 on 'term' <- Goto/Reduce by production 'term : factor' in 'factor' -- State 4 0x1192e90 -- Kernel: expr : expr @+ . term -> Shift on '(' to state 3 <- Shift/Reduce on '@INTEGER' by production 'factor : @@INTEGER' -> Goto state 9 on 'term' <- Goto/Reduce by production 'term : factor' in 'factor' -- State 5 0x1193080 -- Kernel: expr : expr @- . term -> Shift on '(' to state 3 <- Shift/Reduce on '@INTEGER' by production 'factor : @@INTEGER' -> Goto state 10 on 'term' <- Goto/Reduce by production 'term : factor' in 'factor' -- State 6 0x11936d0 -- Kernel: term : term @* . factor -> Shift on '(' to state 3 <- Shift/Reduce on '@INTEGER' by production 'factor : @@INTEGER' <- Goto/Reduce by production 'term : term @* factor' in 'factor' -- State 7 0x11938c0 -- Kernel: term : term @/ . factor -> Shift on '(' to state 3 <- Shift/Reduce on '@INTEGER' by production 'factor : @@INTEGER' <- Goto/Reduce by production 'term : term @/ factor' in 'factor' -- State 8 0x1194640 -- Kernel: factor : @( expr . @) expr : expr . @+ term expr : expr . @- term <- Shift/Reduce on ')' by production 'factor : @( expr @)' -> Shift on '+' to state 4 -> Shift on '-' to state 5 -- State 9 0x11956f0 -- Kernel: expr : expr @+ term . [ @- @) @+ @@eof ] term : term . @* factor term : term . @/ factor -> Shift on '*' to state 6 -> Shift on '/' to state 7 <- Reduce on '-' by production 'expr : expr @+ term' <- Reduce on ')' by production 'expr : expr @+ term' <- Reduce on '+' by production 'expr : expr @+ term' <- Reduce on '@eof' by production 'expr : expr @+ term' -- State 10 0x1196080 -- Kernel: expr : expr @- term . [ @- @) @+ @@eof ] term : term . @* factor term : term . @/ factor -> Shift on '*' to state 6 -> Shift on '/' to state 7 <- Reduce on '-' by production 'expr : expr @- term' <- Reduce on ')' by production 'expr : expr @- term' <- Reduce on '+' by production 'expr : expr @- term' <- Reduce on '@eof' by production 'expr : expr @- term' #00: columns=24 accept=-1 default=-1 trans=47(/);47(/):01 trans=45(-);45(-):02 trans=43(+);43(+):03 trans=41());41()):04 trans=48(0);57(9):05 trans=40(();40(():06 trans=42(*);42(*):07 #01: columns=3 accept=5 default=-1 #02: columns=3 accept=4 default=-1 #03: columns=3 accept=3 default=-1 #04: columns=3 accept=8 default=-1 #05: columns=6 accept=2 default=-1 trans=48(0);57(9):05 #06: columns=3 accept=7 default=-1 #07: columns=3 accept=6 default=-1 >>> 00: sym: '(X)' state: 0 token: (X)((X)) get token Token 2 len 1 lexem >1< got token '@INTEGER' lexem '1' shift/reduce by production 8 >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: '@INTEGER' state: -1 token: @INTEGER(1) reduce by production 8 popping 1 items off the stack, replacing by 'factor' <<< 00: sym: '(X)' state: 0 token: (X)((X)) >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'factor' state: -1 token: (X)((X)) reduce by production 6 popping 1 items off the stack, replacing by 'term' <<< 00: sym: '(X)' state: 0 token: (X)((X)) >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'term' state: 2 token: (X)((X)) get token Token 6 len 1 lexem >*< got token '*' lexem '*' shift to state 6 >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'term' state: 2 token: (X)((X)) 02: sym: '*' state: 6 token: *(*) get token Token 2 len 1 lexem >2< got token '@INTEGER' lexem '2' shift/reduce by production 8 >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'term' state: 2 token: (X)((X)) 02: sym: '*' state: 6 token: *(*) 03: sym: '@INTEGER' state: -1 token: @INTEGER(2) reduce by production 8 popping 1 items off the stack, replacing by 'factor' <<< 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'term' state: 2 token: (X)((X)) 02: sym: '*' state: 6 token: *(*) >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'term' state: 2 token: (X)((X)) 02: sym: '*' state: 6 token: *(*) 03: sym: 'factor' state: -1 token: (X)((X)) reduce by production 4 popping 3 items off the stack, replacing by 'term' <<< <<< <<< 00: sym: '(X)' state: 0 token: (X)((X)) >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'term' state: 2 token: (X)((X)) get token Token 3 len 1 lexem >+< got token '+' lexem '+' reduce by production 3 popping 1 items off the stack, replacing by 'expr' <<< 00: sym: '(X)' state: 0 token: (X)((X)) >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'expr' state: 1 token: (X)((X)) get token got token '+' lexem '+' shift to state 4 >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'expr' state: 1 token: (X)((X)) 02: sym: '+' state: 4 token: +(+) get token Token 2 len 1 lexem >3< got token '@INTEGER' lexem '3' shift/reduce by production 8 >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'expr' state: 1 token: (X)((X)) 02: sym: '+' state: 4 token: +(+) 03: sym: '@INTEGER' state: -1 token: @INTEGER(3) reduce by production 8 popping 1 items off the stack, replacing by 'factor' <<< 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'expr' state: 1 token: (X)((X)) 02: sym: '+' state: 4 token: +(+) >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'expr' state: 1 token: (X)((X)) 02: sym: '+' state: 4 token: +(+) 03: sym: 'factor' state: -1 token: (X)((X)) reduce by production 6 popping 1 items off the stack, replacing by 'term' <<< 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'expr' state: 1 token: (X)((X)) 02: sym: '+' state: 4 token: +(+) >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'expr' state: 1 token: (X)((X)) 02: sym: '+' state: 4 token: +(+) 03: sym: 'term' state: 9 token: (X)((X)) get token EOF read got token '@eof' lexem '' reduce by production 1 popping 3 items off the stack, replacing by 'expr' <<< <<< <<< 00: sym: '(X)' state: 0 token: (X)((X)) >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'expr' state: 1 token: (X)((X)) get token got token '@eof' lexem '' reduce by production 0 popping 1 items off the stack, replacing by 'start' <<< 00: sym: '(X)' state: 0 token: (X)((X)) goal symbol reduced! add mul @INTEGER = >1< @INTEGER = >2< @INTEGER = >3< ------------------------------ >>> 00: sym: '(X)' state: 0 token: (X)((X)) get token Token 7 len 1 lexem >(< got token '(' lexem '(' shift to state 3 >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: '(' state: 3 token: ((() get token Token 2 len 1 lexem >7< got token '@INTEGER' lexem '7' shift/reduce by production 8 >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: '(' state: 3 token: ((() 02: sym: '@INTEGER' state: -1 token: @INTEGER(7) reduce by production 8 popping 1 items off the stack, replacing by 'factor' <<< 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: '(' state: 3 token: ((() >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: '(' state: 3 token: ((() 02: sym: 'factor' state: -1 token: (X)((X)) reduce by production 6 popping 1 items off the stack, replacing by 'term' <<< 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: '(' state: 3 token: ((() >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: '(' state: 3 token: ((() 02: sym: 'term' state: 2 token: (X)((X)) get token Token 3 len 1 lexem >+< got token '+' lexem '+' reduce by production 3 popping 1 items off the stack, replacing by 'expr' <<< 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: '(' state: 3 token: ((() >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: '(' state: 3 token: ((() 02: sym: 'expr' state: 8 token: (X)((X)) get token got token '+' lexem '+' shift to state 4 >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: '(' state: 3 token: ((() 02: sym: 'expr' state: 8 token: (X)((X)) 03: sym: '+' state: 4 token: +(+) get token Token 2 len 1 lexem >3< got token '@INTEGER' lexem '3' shift/reduce by production 8 >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: '(' state: 3 token: ((() 02: sym: 'expr' state: 8 token: (X)((X)) 03: sym: '+' state: 4 token: +(+) 04: sym: '@INTEGER' state: -1 token: @INTEGER(3) reduce by production 8 popping 1 items off the stack, replacing by 'factor' <<< 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: '(' state: 3 token: ((() 02: sym: 'expr' state: 8 token: (X)((X)) 03: sym: '+' state: 4 token: +(+) >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: '(' state: 3 token: ((() 02: sym: 'expr' state: 8 token: (X)((X)) 03: sym: '+' state: 4 token: +(+) 04: sym: 'factor' state: -1 token: (X)((X)) reduce by production 6 popping 1 items off the stack, replacing by 'term' <<< 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: '(' state: 3 token: ((() 02: sym: 'expr' state: 8 token: (X)((X)) 03: sym: '+' state: 4 token: +(+) >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: '(' state: 3 token: ((() 02: sym: 'expr' state: 8 token: (X)((X)) 03: sym: '+' state: 4 token: +(+) 04: sym: 'term' state: 9 token: (X)((X)) get token Token 8 len 1 lexem >)< got token ')' lexem ')' reduce by production 1 popping 3 items off the stack, replacing by 'expr' <<< <<< <<< 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: '(' state: 3 token: ((() >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: '(' state: 3 token: ((() 02: sym: 'expr' state: 8 token: (X)((X)) get token got token ')' lexem ')' shift/reduce by production 7 >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: '(' state: 3 token: ((() 02: sym: 'expr' state: 8 token: (X)((X)) 03: sym: ')' state: -1 token: )()) reduce by production 7 popping 3 items off the stack, replacing by 'factor' <<< <<< <<< 00: sym: '(X)' state: 0 token: (X)((X)) >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'factor' state: -1 token: (X)((X)) reduce by production 6 popping 1 items off the stack, replacing by 'term' <<< 00: sym: '(X)' state: 0 token: (X)((X)) >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'term' state: 2 token: (X)((X)) get token Token 6 len 1 lexem >*< got token '*' lexem '*' shift to state 6 >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'term' state: 2 token: (X)((X)) 02: sym: '*' state: 6 token: *(*) get token Token 2 len 1 lexem >2< got token '@INTEGER' lexem '2' shift/reduce by production 8 >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'term' state: 2 token: (X)((X)) 02: sym: '*' state: 6 token: *(*) 03: sym: '@INTEGER' state: -1 token: @INTEGER(2) reduce by production 8 popping 1 items off the stack, replacing by 'factor' <<< 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'term' state: 2 token: (X)((X)) 02: sym: '*' state: 6 token: *(*) >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'term' state: 2 token: (X)((X)) 02: sym: '*' state: 6 token: *(*) 03: sym: 'factor' state: -1 token: (X)((X)) reduce by production 4 popping 3 items off the stack, replacing by 'term' <<< <<< <<< 00: sym: '(X)' state: 0 token: (X)((X)) >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'term' state: 2 token: (X)((X)) get token Token 4 len 1 lexem >-< got token '-' lexem '-' reduce by production 3 popping 1 items off the stack, replacing by 'expr' <<< 00: sym: '(X)' state: 0 token: (X)((X)) >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'expr' state: 1 token: (X)((X)) get token got token '-' lexem '-' shift to state 5 >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'expr' state: 1 token: (X)((X)) 02: sym: '-' state: 5 token: -(-) get token Token 2 len 1 lexem >5< got token '@INTEGER' lexem '5' shift/reduce by production 8 >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'expr' state: 1 token: (X)((X)) 02: sym: '-' state: 5 token: -(-) 03: sym: '@INTEGER' state: -1 token: @INTEGER(5) reduce by production 8 popping 1 items off the stack, replacing by 'factor' <<< 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'expr' state: 1 token: (X)((X)) 02: sym: '-' state: 5 token: -(-) >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'expr' state: 1 token: (X)((X)) 02: sym: '-' state: 5 token: -(-) 03: sym: 'factor' state: -1 token: (X)((X)) reduce by production 6 popping 1 items off the stack, replacing by 'term' <<< 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'expr' state: 1 token: (X)((X)) 02: sym: '-' state: 5 token: -(-) >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'expr' state: 1 token: (X)((X)) 02: sym: '-' state: 5 token: -(-) 03: sym: 'term' state: 10 token: (X)((X)) get token EOF read got token '@eof' lexem '' reduce by production 2 popping 3 items off the stack, replacing by 'expr' <<< <<< <<< 00: sym: '(X)' state: 0 token: (X)((X)) >>> 00: sym: '(X)' state: 0 token: (X)((X)) 01: sym: 'expr' state: 1 token: (X)((X)) get token got token '@eof' lexem '' reduce by production 0 popping 1 items off the stack, replacing by 'start' <<< 00: sym: '(X)' state: 0 token: (X)((X)) goal symbol reduced! sub mul add @INTEGER = >7< @INTEGER = >3< @INTEGER = >2< @INTEGER = >5< |
Man beachte das, was vor der Linie (Zeile 233) kommt, und das was hier am Ende steht (Zeile 467): Ein abstrakter Syntaxbaum! Hier am Ende: AST der Eingabe (7+3)*2-5 ist sub( mul( add( 7, 3 ), 2 ), 5 ).
Ok klingt vielleicht für die meisten nicht so Spannend, aber es ist für mich nur.. geile Scheiße!!! 😀