1 | |
---|
2 | /** |
---|
3 | * Package: UAM Text Tools |
---|
4 | * Component: dgp (dg parser) |
---|
5 | * Version: 1.0 |
---|
6 | * Author: Tomasz Obrebski |
---|
7 | */ |
---|
8 | |
---|
9 | #include "global.hh" |
---|
10 | #include "mgraph.hh" |
---|
11 | #include "sgraph.hh" |
---|
12 | #include "grammar.hh" |
---|
13 | #include "dgp0.hh" |
---|
14 | #include "../common/common.h" |
---|
15 | #include "cmdline.h" |
---|
16 | |
---|
17 | #define MAXSEGMENTS 500 |
---|
18 | |
---|
19 | char segment[MAXSEGMENTS][MAXLINE]; |
---|
20 | int segcount=0; |
---|
21 | char seg_mnode[MAXSEGMENTS]; |
---|
22 | |
---|
23 | Grammar grammar; |
---|
24 | MGraph mgraph; |
---|
25 | SGraph sgraph; |
---|
26 | |
---|
27 | FILE* grammarf; |
---|
28 | FILE* debugf=stdout; |
---|
29 | unsigned int info=0U; |
---|
30 | |
---|
31 | void output(); |
---|
32 | |
---|
33 | main(int argc, char* argv[]) |
---|
34 | { |
---|
35 | gengetopt_args_info args; |
---|
36 | |
---|
37 | if(cmdline_parser(argc,argv,&args) != 0) exit(1); |
---|
38 | |
---|
39 | if(args.help_given) cmdline_parser_print_help (); |
---|
40 | |
---|
41 | if(args.input_given) |
---|
42 | if(!(inputf=fopen(args.input_arg,"r"))) |
---|
43 | fprintf(stderr,"dgp: input file not found: %s.\n", args.input_arg), exit(1); |
---|
44 | |
---|
45 | if(args.output_given) |
---|
46 | if(!(outputf=fopen(args.output_arg,"w"))) |
---|
47 | fprintf(stderr,"dgp: cannot open output file: %s.\n", args.output_arg), exit(1); |
---|
48 | |
---|
49 | if(!(grammarf=fopen(args.grammar_arg,"r"))) |
---|
50 | fprintf(stderr,"dgp: grammar file not found: %s.\n", args.grammar_arg), exit(1); |
---|
51 | |
---|
52 | if(args.debug_given) debug=true; |
---|
53 | |
---|
54 | for(char* c=args.info_arg; *c!='\0' ; ++c) |
---|
55 | switch(*c) |
---|
56 | { |
---|
57 | case 'h': info|=SGraph::HEADS; break; |
---|
58 | case 'd': info|=SGraph::DEPS; break; |
---|
59 | case 's': info|=SGraph::SETS; break; |
---|
60 | case 'c': info|=SGraph::CONSTRAINTS; break; |
---|
61 | } |
---|
62 | |
---|
63 | grammar.read(grammarf); |
---|
64 | fclose(grammarf); |
---|
65 | |
---|
66 | mgraph.clear(); |
---|
67 | sgraph.clear(); |
---|
68 | |
---|
69 | char line[1000]; |
---|
70 | while (fgets(line, MAXLINE+1, inputf)) |
---|
71 | { |
---|
72 | line[strlen(line)-1] = '\0'; |
---|
73 | strcpy(segment[segcount],line); |
---|
74 | |
---|
75 | char segtype[80]; |
---|
76 | |
---|
77 | seg_mnode[segcount] = process_seg(line, args) ? mgraph.add_node(line) : -1; |
---|
78 | |
---|
79 | segcount++; |
---|
80 | |
---|
81 | getfield(line,"3",segtype); |
---|
82 | if(strcmp(segtype,"EOS")==0) |
---|
83 | { |
---|
84 | dgp0(); // parametry!!! MGraph, SGraph, Grammar |
---|
85 | output(); |
---|
86 | |
---|
87 | mgraph.clear(); |
---|
88 | sgraph.clear(); |
---|
89 | segcount=0; |
---|
90 | } |
---|
91 | // if(args.interactive_flag) { fflush(outputf); fflush(failedf); } |
---|
92 | } |
---|
93 | |
---|
94 | fclose(inputf); |
---|
95 | fclose(outputf); |
---|
96 | cmdline_parser_free(&args); |
---|
97 | exit(0); |
---|
98 | } |
---|
99 | |
---|
100 | void output() |
---|
101 | { |
---|
102 | for(int si=0; si<segcount; ++si) |
---|
103 | { |
---|
104 | if(seg_mnode[si]>=0) |
---|
105 | { |
---|
106 | MNode& m=mgraph.nodes[seg_mnode[si]]; |
---|
107 | for(vector<int>::iterator s=m.snodes.begin(); s!=m.snodes.end(); ++s) |
---|
108 | { |
---|
109 | fputs(segment[si],outputf); |
---|
110 | sgraph.print_node(outputf, *s, info); |
---|
111 | fputc('\n',outputf); |
---|
112 | } |
---|
113 | } |
---|
114 | else |
---|
115 | { |
---|
116 | fputs(segment[si],outputf); |
---|
117 | fputc('\n',outputf); |
---|
118 | } |
---|
119 | } |
---|
120 | } |
---|