source: tools/prettyprinter/parser.yy @ 7d4f6ed

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 7d4f6ed was 7d4f6ed, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

remainder of pretty printer

  • Property mode set to 100644
File size: 11.7 KB
Line 
1// 
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6// 
7// parser.yy --
8// 
9// Author           : Rodolfo G. Esteves
10// Created On       : Sat Dec 15 13:44:21 2001
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Wed Jun 28 23:27:34 2017
13// Update Count     : 1043
14//
15
16%{
17#define YYDEBUG_LEXER_TEXT( yylval )                                    // lexer loads this up each time
18#define YYDEBUG 1                                                                               // get the pretty debugging code to compile
19
20#include <iostream>
21using namespace std;
22#include "parser.hh"
23#include "filter.h"
24
25extern list<string> ws_list;                                                    // lex variable containing accumulated whitespace
26void lexC( void );
27string lexYacc( void );
28
29void yyerror( string s ) {
30        extern int yylineno;
31
32        cerr << "Error in line: " << yylineno << ": " << s << endl;
33        return;
34}
35
36Token *declstart;
37Token *rulestart;
38Token *nameliststart;
39%}
40
41%union {
42        Token *tokenp;
43}
44
45%token<tokenp>  ','
46%token<tokenp>  '<'
47%token<tokenp>  '>'
48%token<tokenp>  '{'
49%token<tokenp>  '}'
50%token<tokenp>  ':'
51%token<tokenp>  ';'
52%token<tokenp>  '|'
53
54%token<tokenp>  MARK                                                                    // %%
55%token<tokenp>  LCURL                                                                   // %{
56%token<tokenp>  RCURL                                                                   // %}
57
58%token<tokenp>  INTEGER                                                                 // integer constant
59%token<tokenp>  CHARACTER                                                               // character constant
60%token<tokenp>  IDENTIFIER                                                              // identifier
61%token<tokenp>  CODE                                                                    // C code
62
63%token<tokenp>  START                                                                   // %start
64%token<tokenp>  UNION                                                                   // %union
65%token<tokenp>  TOKEN                                                                   // %token
66%token<tokenp>  LEFT                                                                    // %left
67%token<tokenp>  RIGHT                                                                   // %right
68%token<tokenp>  NONASSOC                                                                // %nonassoc
69%token<tokenp>  TYPE                                                                    // %type
70%token<tokenp>  PURE_PARSER                                                             // %pure_parser
71%token<tokenp>  SEMANTIC_PARSER                                                 // %semantic_parser
72%token<tokenp>  EXPECT                                                                  // %expect
73%token<tokenp>  THONG                                                                   // %thong
74
75%token<tokenp>  PREC                                                                    // %prec
76
77%token          END_TERMINALS                                                           // ALL TERMINAL TOKEN NAMES MUST APPEAR BEFORE THIS
78
79%type<tokenp>   sections
80%token          _SECTIONS
81%type<tokenp>   mark
82%type<tokenp>   defsection_opt
83%token          _DEFSECTION_OPT
84%type<tokenp>   declarations
85%type<tokenp>   literalblock
86%token          _LITERALBLOCK
87%type<tokenp>   declaration
88%token          _DECLARATION
89%type<tokenp>   union
90%type<tokenp>   rword
91%type<tokenp>   tag_opt
92%token          _TAG_OPT
93%type<tokenp>   namenolist
94%token          _NAMENOLIST
95%type<tokenp>   nameno
96%token          _NAMENO
97%type<tokenp>   namelist
98%token          _NAMELIST
99%type<tokenp>   name
100%type<tokenp>   rulesection
101%token          _RULESECTION
102%type<tokenp>   rules
103%token          _RULE
104%type<tokenp>   lhs
105%token          _LHS
106%type<tokenp>   rhs
107%token          _RHS
108%type<tokenp>   prod
109%type<tokenp>   prec
110%token          _PREC
111%type<tokenp>   action
112%token          _ACTION
113%type<tokenp>   usersection_opt
114%token          _USERSECTION_OPT
115%type<tokenp>   ccode_opt
116%type<tokenp>   blocks
117
118%start grammar
119
120%%
121grammar :
122        sections
123                {
124                        filter( $1 );                                                           // filter parse tree
125                        freeTree( $1 );                                                         // free parse-tree storage (optional: used with purify)
126                }
127        ;
128
129sections :
130        defsection_opt mark rulesection usersection_opt
131                {
132                        $$ = new Token( "sections", _SECTIONS );
133                        $1->left = $2;
134                        $2->left = $3;
135                        $3->left = $4;
136                        $$->down = $1;
137                }
138        ;
139
140mark :
141        MARK
142        | error                                                                                         // missing %%
143                {
144                        cerr << "no input grammar, missing %% mark" << endl;
145                        exit( -1 );
146                }
147        ;
148
149defsection_opt :
150        // empty
151                {
152                        //cerr << "defsection_opt1: " << endl;
153                        $$ = new Token( "declaration_opt", _DEFSECTION_OPT );
154                }
155        | declarations
156                {
157                        //cerr << "defsection_opt2: " << $1->text << "(" << $1 << ")" << endl;
158                        $$ = new Token( "declaration_opt", _DEFSECTION_OPT );
159                        $$->down = declstart;
160                }
161        ;
162
163declarations :
164        literalblock
165                {
166                        //cerr << "declarations1: " << $1->text << "(" << $1 << ")" << endl;
167                        $$ = declstart = $1;
168                }
169        | declaration
170                {
171                        //cerr << "declarations2: " << $1->text << "(" << $1 << ")" << endl;
172                        $$ = declstart = new Token( "declaration", _DECLARATION );
173                        $$->down = $1;
174                }
175        | declarations literalblock
176                {
177                        //cerr << "declarations3: "<< $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ")" << endl;
178                        $1->left = $2;
179                        $$ = $2;
180                }
181        | declarations declaration
182                {
183                        //cerr << "declarations4: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ")" << endl;
184                        $$ = new Token( "declaration", _DECLARATION );
185                        $1->left = $$;
186                        $$->down = $2;
187                }
188        ;
189
190literalblock :
191        LCURL
192                { lexC(); }
193          ccode_opt
194                { $<tokenp>$ = new Token( lexYacc(), CODE ); }
195          RCURL
196                {
197                        //cerr << "literalblock: " << $1->text << "(" << $1 << ") " << $<tokenp>4->text << " " << $5->text << "(" << $5 << ")" << endl;
198                        $1->left = $<tokenp>4;
199                        $<tokenp>4->left = $5;
200                        $$ = new Token( "literalblock", _LITERALBLOCK );
201                        $$->down = $1;
202                }
203        ;
204
205declaration :
206        union
207        | START IDENTIFIER
208                {
209                        $1->left = $2;
210                        $$ = $1;
211                }
212        | rword tag_opt namenolist
213                {
214                    Token *n = new Token( "namenolist", _NAMENOLIST );
215                    n->down = nameliststart;
216                    $1->left = $2;
217                    $2->left = n;
218                    $$ = $1;
219                }
220        | TYPE tag_opt namelist
221                {
222                    Token *n = new Token( "namelist", _NAMELIST );
223                    n->down = nameliststart;
224                    $1->left = $2;
225                    $2->left = n;
226                    $$ = $1;
227                }
228        | PURE_PARSER
229        | SEMANTIC_PARSER
230        | EXPECT INTEGER                                                                        // bison
231                {
232                    $1->left = $2;
233                    $$ = $1;
234                }
235| THONG                                                                                                 // bison
236        ;
237
238union :
239        UNION '{'
240                { lexC(); }
241          ccode_opt
242                {
243                    // Remove the trailing '}' which is added in lex.
244                    string temp( lexYacc() );
245                    $<tokenp>$ = new Token( temp.substr( 0, temp.length() - 1 ), CODE );
246                }
247          '}'
248                {
249                    $1->left = $2;
250                    $2->left = $<tokenp>5;
251                    $<tokenp>5->left = $6;
252                    $$ = $1;
253                }
254        ;
255
256rword :
257        TOKEN
258        | LEFT
259        | RIGHT
260        | NONASSOC
261        ;
262
263tag_opt :
264        // empty
265                {
266                    //cerr << "tag_opt" << endl;
267                    $$ = new Token( "tag_opt", _TAG_OPT );
268                }
269        | '<' IDENTIFIER '>'
270                {
271                    $1->left = $2;
272                    $2->left = $3;
273                    $$ = new Token( "tag_opt", _TAG_OPT );
274                    $$->down = $1;
275                }
276        ;
277
278namenolist      : nameno
279                        {
280                            //cerr << "namenolist1: " << $1->text << "(" << $1 << ")" << endl;
281                            $$ = nameliststart = $1;
282                        }
283                | namenolist nameno
284                        {
285                            //cerr << "namenolist2: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ")" << endl;
286                            $1->left = $2;
287                            $$ = $2;
288                        }
289                | namenolist ',' nameno
290                        {
291                            //cerr << "namenolist3: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ") " << $3->text << "(" << $3 << ")" << endl;
292                            $1->left = $2;
293                            $2->left = $3;
294                            $$ = $3;
295                        }
296                ;
297
298nameno          : name
299                        {
300                            $$ = new Token( "nameno", _NAMENO );
301                            $$->down = $1;
302                        }
303                | name INTEGER
304                        {
305                            $$ = new Token( "nameno", _NAMENO );
306                            $1->left = $2;
307                            $$->down = $1;
308                        }
309                ;
310
311namelist        : name
312                        {
313                            //cerr << "namelist1: " << $1->text << "(" << $1 << ")" << endl;
314                            $$ = nameliststart = $1;
315                        }
316                | namelist name
317                        {
318                            //cerr << "namelist2: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ")" << endl;
319                            $1->left = $2;
320                            $$ = $2;
321                        }
322                | namelist ',' name
323                        {
324                            //cerr << "namelist3: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ") " << $3->text << "(" << $3 << ")" << endl;
325                            $1->left = $2;
326                            $2->left = $3;
327                            $$ = $3;
328                        }
329                ;
330
331name            : IDENTIFIER
332                | CHARACTER
333                ;
334
335rulesection     : rules
336                        {
337                            //cerr << "rulesection1: " << $1->text << "(" << $1 << ")" << endl;
338                            $$ = new Token( "rulesection", _RULESECTION );
339                            $$->down = $1;
340                        }
341                | error                                 /* no rules */
342                        {
343                            cerr << "no rules in the input grammar" << endl;
344                            exit( -1 );
345                        }
346                ;
347
348// These grammar rules are complex because the Yacc language is LR(2) due to the optional ';' at the end of rules. The
349// following rules convert the LR(2) grammar into LR(1) by lengthening the rules to allow sufficient look
350// ahead. Unfortunately, this change makes handling the semantic actions more complex because there are two lists
351// (rules, rhs) being built but only one list tail can be returned through $$ for chaining.
352
353rules           : lhs rhs
354                        {
355                            //cerr << "rules1: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ")" << endl;
356                            $$ = rulestart;
357                        }
358                | lhs rhs ';'
359                        {
360                            //cerr << "rules2: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ") " << $3->text << "(" << $3 << ")" << endl;
361                            $2->addDownLeftTail( $3 );
362                            $$ = rulestart;
363                        }
364                ;
365
366lhs             : IDENTIFIER ':'
367                        {
368                            //cerr << "lhs: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ")" << endl;
369                            $$ = new Token( "lhs", _LHS );
370                            //cerr << " lhs: "  << $$->text << "(" << $$ << ")" << endl;
371                            $1->left = $2;
372                            $$->down = $1;
373                        }
374                ;
375
376rhs     :
377        // empty
378                {
379                    //cerr << "rhs1: " << $<tokenp>0->text << "(" << $<tokenp>0 << ")"  << endl;
380                    rulestart = new Token( "rule", _RULE );
381                    rulestart->down = $<tokenp>0; // initial lhs is already on the stack from "rules"
382                    $$ = new Token( "rhs", _RHS );
383                    //cerr << "  rhs: " << $$->text << "(" << $$ << ")" << endl;
384                    $<tokenp>0->left = $$;
385                }
386        | rhs lhs
387                {
388                    //cerr << "rhs2: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ")" << endl;
389                    Token *temp = new Token( "rule", _RULE );
390                    rulestart->addLeftTail( temp );
391                    temp->down = $2;
392                    $$ = new Token( "rhs", _RHS );
393                    //cerr << "  rhs: "  << $$->text << "(" << $$ << ")" << endl;
394                    $2->left = $$;
395                }
396        | rhs ';' lhs
397                {
398                    //cerr << "rhs3: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ") " << $3->text << "(" << $3 << ")" << endl;
399                    $1->addDownLeftTail( $2 );
400                    Token *temp = new Token( "rule", _RULE );
401                    rulestart->addLeftTail( temp );
402                    temp->down = $3;
403                    $$ = new Token( "rhs", _RHS );
404                    //cerr << "  rhs: "  << $$->text << "(" << $$ << ")" << endl;
405                    $3->left = $$;
406                }
407        | rhs prod
408                {
409                    //cerr << "rhs4: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ")" << endl;
410                    $1->addDownLeftTail( $2 );
411                    $$ = $1;
412                }
413        | rhs '|'
414                {
415                    //cerr << "rhs5: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ")" << endl;
416                    $1->addDownLeftTail( $2 );
417                    $$ = new Token( "rhs", _RHS );
418                    $1->left = $$;
419                    //cerr << "  rhs: "  << $$->text << "(" << $$ << ")" << endl;
420                }
421        ;
422
423prod :
424        action
425        | IDENTIFIER
426        | CHARACTER
427        | prec
428        ;
429
430prec :
431        PREC name
432                {
433                    //cerr << "prec: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ")" << endl;
434                    $1->left = $2;
435                    $$ = new Token( "prec", _PREC );
436                    $$->down = $1;
437                }
438        ;
439
440action :
441        '{'
442                { lexC(); }
443          ccode_opt
444                {
445                    // Remove the trailing '}' added in lex.
446                    string temp( lexYacc() );
447                    $<tokenp>$ = new Token( temp.substr( 0, temp.length() - 1 ), CODE );
448                }
449          '}'
450                {
451                    $1->left = $<tokenp>4;
452                    $<tokenp>4->left = $5;
453                    $$ = new Token( "action", _ACTION );
454                    $$->down = $1;
455                }
456        ;
457
458usersection_opt :
459        // empty
460                {
461                    //cerr << "usersection_opt" << endl;
462                    // attach remaining WS to fictitious code
463                    Token *temp = new Token( "", ws_list, CODE );
464                    $$ = new Token( "usersection_opt", _USERSECTION_OPT );
465                    $$->down = temp;
466                }
467        | MARK
468                { lexC(); }
469          ccode_opt
470                {
471                    Token *temp = new Token( lexYacc(), CODE );
472                    //cerr << "usersection_opt: " << $1->text << " " << temp->text << endl;
473                    $1->left = temp;
474                    $$ = new Token( "usersection_opt", _USERSECTION_OPT );
475                    $$->down = $1;
476                }
477        ;
478
479ccode_opt :
480        // empty
481        | blocks
482        ;
483
484// This rule matches internal braces "{}" in C code to the level of the braces of a union/action.  These internal braces
485// are returned as Tokens from the lexer but are unused because the braces are already concatenated into the code string
486// built by the lexer. Therefore, the tokens for the braces are immediately deleted.
487
488blocks :
489        '{'
490                { delete $1; }
491          ccode_opt '}'
492                { delete $4; }
493        | blocks '{'
494                { delete $2; }
495          ccode_opt '}'
496                { delete $5; }
497        ;
498%%
499
500// Local Variables: //
501// mode: c++ //
502// tab-width: 4 //
503// compile-command: "make install" //
504// End: //
Note: See TracBrowser for help on using the repository browser.