[5f4d9c3] | 1 | |
---|
| 2 | /** |
---|
| 3 | * Package: UAM Text Tools |
---|
| 4 | * Component: dgp (dg parser) |
---|
| 5 | * Version: 1.0 |
---|
| 6 | * Author: Tomasz Obrebski |
---|
| 7 | */ |
---|
| 8 | |
---|
[a15e59b] | 9 | #include <ctime> |
---|
| 10 | #include <sys/time.h> |
---|
| 11 | |
---|
[5f4d9c3] | 12 | #include "global.hh" |
---|
| 13 | #include "mgraph.hh" |
---|
| 14 | #include "sgraph.hh" |
---|
| 15 | #include "grammar.hh" |
---|
[e7de6cc] | 16 | #include "dgp1.hh" |
---|
[5f4d9c3] | 17 | #include "../common/common.h" |
---|
| 18 | #include "cmdline.h" |
---|
| 19 | |
---|
| 20 | #define MAXSEGMENTS 500 |
---|
| 21 | |
---|
[a15e59b] | 22 | #define MICROSECONDSELAPSED(A,B) ((B.tv_sec - A.tv_sec)*1000000 + (B.tv_usec - A.tv_usec)) |
---|
| 23 | |
---|
[5f4d9c3] | 24 | char segment[MAXSEGMENTS][MAXLINE]; |
---|
| 25 | int segcount=0; |
---|
| 26 | char seg_mnode[MAXSEGMENTS]; |
---|
| 27 | char grammarfile[255]; |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | Grammar grammar; |
---|
| 31 | MGraph mgraph; |
---|
[e7de6cc] | 32 | SGraph sgraph(mgraph); |
---|
[5f4d9c3] | 33 | |
---|
| 34 | FILE* grammarf; |
---|
| 35 | FILE* debugf=stdout; |
---|
| 36 | unsigned int info=0U; |
---|
| 37 | |
---|
[a15e59b] | 38 | bool printtimeinfo=false; |
---|
| 39 | |
---|
[5f4d9c3] | 40 | void output(); |
---|
| 41 | |
---|
| 42 | main(int argc, char* argv[]) |
---|
| 43 | { |
---|
| 44 | gengetopt_args_info args; |
---|
| 45 | |
---|
| 46 | if(cmdline_parser(argc,argv,&args) != 0) |
---|
| 47 | exit(1); |
---|
| 48 | |
---|
| 49 | process_config_files(&args,argv[0]); |
---|
| 50 | process_common_options(&args,argv[0]); |
---|
| 51 | |
---|
| 52 | if(!args.grammar_given) |
---|
| 53 | fprintf(stderr,"dgp: no grammar given\n"); |
---|
| 54 | |
---|
| 55 | expand_path(args.grammar_arg,grammarfile); |
---|
| 56 | |
---|
| 57 | if(!(grammarf=fopen(grammarfile,"r"))) |
---|
| 58 | fprintf(stderr,"dgp: grammar file not found: %s.\n", grammarfile), exit(1); |
---|
| 59 | |
---|
| 60 | if(args.debug_given) debug=true; |
---|
| 61 | |
---|
[a15e59b] | 62 | if(args.time_given) printtimeinfo=true; |
---|
| 63 | |
---|
[5f4d9c3] | 64 | for(char* c=args.info_arg; *c!='\0' ; ++c) |
---|
| 65 | switch(*c) |
---|
| 66 | { |
---|
| 67 | case 'h': info|=SGraph::HEADS; break; |
---|
| 68 | case 'd': info|=SGraph::DEPS; break; |
---|
| 69 | case 's': info|=SGraph::SETS; break; |
---|
| 70 | case 'c': info|=SGraph::CONSTRAINTS; break; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | grammar.read(grammarf); |
---|
| 74 | fclose(grammarf); |
---|
| 75 | |
---|
[e7de6cc] | 76 | |
---|
| 77 | |
---|
| 78 | // grammar.write(cout); |
---|
| 79 | // exit(0); |
---|
| 80 | |
---|
[a15e59b] | 81 | struct timeval starttime,afterinput,afterparse,endtime; |
---|
[e7de6cc] | 82 | |
---|
[5f4d9c3] | 83 | mgraph.clear(); |
---|
| 84 | sgraph.clear(); |
---|
| 85 | |
---|
| 86 | char line[1000]; |
---|
| 87 | while (fgets(line, MAXLINE+1, inputf)) |
---|
| 88 | { |
---|
[a15e59b] | 89 | gettimeofday(&starttime,NULL); |
---|
[5f4d9c3] | 90 | line[strlen(line)-1] = '\0'; |
---|
| 91 | strcpy(segment[segcount],line); |
---|
| 92 | |
---|
| 93 | char segtype[80]; |
---|
| 94 | |
---|
| 95 | seg_mnode[segcount] = process_seg(line, args) ? mgraph.add_node(line) : -1; |
---|
| 96 | |
---|
| 97 | segcount++; |
---|
| 98 | |
---|
| 99 | getfield(line,"3",segtype); |
---|
| 100 | if(strcmp(segtype,"EOS")==0) |
---|
| 101 | { |
---|
[a15e59b] | 102 | gettimeofday(&afterinput,NULL); |
---|
[e7de6cc] | 103 | dgp1(); // parametry!!! MGraph, SGraph, Grammar |
---|
[a15e59b] | 104 | gettimeofday(&afterparse,NULL); |
---|
[5f4d9c3] | 105 | output(); |
---|
[a15e59b] | 106 | gettimeofday(&endtime,NULL); |
---|
| 107 | |
---|
| 108 | if(printtimeinfo) |
---|
| 109 | { |
---|
| 110 | fprintf(stderr,"### INPUT TIME: %10.2fms\n", (float)MICROSECONDSELAPSED(starttime,afterinput)/1000 ); |
---|
| 111 | fprintf(stderr,"### PARSE TIME: %10.2fms\n", (float)MICROSECONDSELAPSED(afterinput,afterparse)/1000 ); |
---|
| 112 | fprintf(stderr,"### OUTPUT TIME: %10.2fms\n", (float)MICROSECONDSELAPSED(afterparse,endtime)/1000 ); |
---|
| 113 | fprintf(stderr,"### TOTAL TIME: %10.2fms\n", (float)MICROSECONDSELAPSED(starttime,endtime)/1000 ); |
---|
| 114 | } |
---|
[5f4d9c3] | 115 | |
---|
| 116 | mgraph.clear(); |
---|
| 117 | sgraph.clear(); |
---|
| 118 | segcount=0; |
---|
| 119 | } |
---|
| 120 | // if(args.interactive_flag) { fflush(outputf); fflush(failedf); } |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | fclose(inputf); |
---|
| 124 | fclose(outputf); |
---|
| 125 | cmdline_parser_free(&args); |
---|
| 126 | exit(0); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | void output() |
---|
| 130 | { |
---|
| 131 | for(int si=0; si<segcount; ++si) |
---|
| 132 | { |
---|
| 133 | if(seg_mnode[si]>=0) |
---|
| 134 | { |
---|
[e7de6cc] | 135 | MNode& m=mgraph[seg_mnode[si]]; |
---|
[5f4d9c3] | 136 | for(vector<int>::iterator s=m.snodes.begin(); s!=m.snodes.end(); ++s) |
---|
| 137 | { |
---|
| 138 | fputs(segment[si],outputf); |
---|
| 139 | sgraph.print_node(outputf, *s, info); |
---|
| 140 | fputc('\n',outputf); |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | else |
---|
| 144 | { |
---|
| 145 | fputs(segment[si],outputf); |
---|
| 146 | fputc('\n',outputf); |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | } |
---|