util/qlalr/examples/glsl/glsl.g
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 ----------------------------------------------------------------------------
       
     2 --
       
     3 -- Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 -- All rights reserved.
       
     5 -- Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 --
       
     7 -- This file is part of the QtCore module of the Qt Toolkit.
       
     8 --
       
     9 -- $QT_BEGIN_LICENSE:LGPL$
       
    10 -- No Commercial Usage
       
    11 -- This file contains pre-release code and may not be distributed.
       
    12 -- You may use this file in accordance with the terms and conditions
       
    13 -- contained in the Technology Preview License Agreement accompanying
       
    14 -- this package.
       
    15 --
       
    16 -- GNU Lesser General Public License Usage
       
    17 -- Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 -- General Public License version 2.1 as published by the Free Software
       
    19 -- Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 -- packaging of this file.  Please review the following information to
       
    21 -- ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 -- will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 --
       
    24 -- In addition, as a special exception, Nokia gives you certain additional
       
    25 -- rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 -- version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 --
       
    28 -- If you have questions regarding the use of this file, please contact
       
    29 -- Nokia at qt-info@nokia.com.
       
    30 --
       
    31 --
       
    32 --
       
    33 --
       
    34 --
       
    35 --
       
    36 --
       
    37 --
       
    38 -- $QT_END_LICENSE$
       
    39 --
       
    40 ----------------------------------------------------------------------------
       
    41 
       
    42 %parser GLSLParserTable
       
    43 %merged_output glsl.cpp
       
    44 
       
    45 %token ADD_ASSIGN
       
    46 %token AMPERSAND
       
    47 %token AND_ASSIGN
       
    48 %token AND_OP
       
    49 %token ATTRIBUTE
       
    50 %token BANG
       
    51 %token BOOL
       
    52 %token BOOLCONSTANT
       
    53 %token BREAK
       
    54 %token BVEC2
       
    55 %token BVEC3
       
    56 %token BVEC4
       
    57 %token CARET
       
    58 %token COLON
       
    59 %token COMMA
       
    60 %token CONST
       
    61 %token CONTINUE
       
    62 %token DASH
       
    63 %token DEC_OP
       
    64 %token DISCARD
       
    65 %token DIV_ASSIGN
       
    66 %token DO
       
    67 %token DOT
       
    68 %token ELSE
       
    69 %token EQUAL
       
    70 %token EQ_OP
       
    71 %token FLOAT
       
    72 %token FLOATCONSTANT
       
    73 %token FOR
       
    74 %token GE_OP
       
    75 %token IDENTIFIER
       
    76 %token IF
       
    77 %token IN
       
    78 %token INC_OP
       
    79 %token INOUT
       
    80 %token INT
       
    81 %token INTCONSTANT
       
    82 %token IVEC2
       
    83 %token IVEC3
       
    84 %token IVEC4
       
    85 %token LEFT_ANGLE
       
    86 %token LEFT_ASSIGN
       
    87 %token LEFT_BRACE
       
    88 %token LEFT_BRACKET
       
    89 %token LEFT_OP
       
    90 %token LEFT_PAREN
       
    91 %token LE_OP
       
    92 %token MAT2
       
    93 %token MAT3
       
    94 %token MAT4
       
    95 %token MOD_ASSIGN
       
    96 %token MUL_ASSIGN
       
    97 %token NE_OP
       
    98 %token OR_ASSIGN
       
    99 %token OR_OP
       
   100 %token OUT
       
   101 %token PERCENT
       
   102 %token PLUS
       
   103 %token QUESTION
       
   104 %token RETURN
       
   105 %token RIGHT_ANGLE
       
   106 %token RIGHT_ASSIGN
       
   107 %token RIGHT_BRACE
       
   108 %token RIGHT_BRACKET
       
   109 %token RIGHT_OP
       
   110 %token RIGHT_PAREN
       
   111 %token SAMPLER1D
       
   112 %token SAMPLER1DSHADOW
       
   113 %token SAMPLER2D
       
   114 %token SAMPLER2DSHADOW
       
   115 %token SAMPLER3D
       
   116 %token SAMPLERCUBE
       
   117 %token SEMICOLON
       
   118 %token SLASH
       
   119 %token STAR
       
   120 %token STRUCT
       
   121 %token SUB_ASSIGN
       
   122 %token TILDE
       
   123 %token TYPE_NAME
       
   124 %token UNIFORM
       
   125 %token VARYING
       
   126 %token VEC2
       
   127 %token VEC3
       
   128 %token VEC4
       
   129 %token VERTICAL_BAR
       
   130 %token VOID
       
   131 %token WHILE
       
   132 %token XOR_ASSIGN
       
   133 %token XOR_OP
       
   134 %token ERROR
       
   135 %token HIGH_PRECISION
       
   136 %token MEDIUM_PRECISION
       
   137 %token LOW_PRECISION
       
   138 %start translation_unit
       
   139 
       
   140 
       
   141 /:
       
   142 
       
   143 #include <QtCore>
       
   144 
       
   145 class GLSLParser: protected $table
       
   146 {
       
   147 public:
       
   148     union Value {
       
   149       int i;
       
   150       unsigned u;
       
   151       unsigned long ul;
       
   152       unsigned long long ull;
       
   153       long l;
       
   154       double d;
       
   155       float f;
       
   156       const QString *s;
       
   157       // ### more...
       
   158     };
       
   159 
       
   160 public:
       
   161     GLSLParser();
       
   162     ~GLSLParser();
       
   163 
       
   164     bool parse();
       
   165 
       
   166 protected:
       
   167     inline void reallocateStack();
       
   168 
       
   169     inline Value &sym(int index)
       
   170     { return sym_stack [tos + index - 1]; }
       
   171 
       
   172     int nextToken();
       
   173 
       
   174     bool isTypename(const QString *s) const
       
   175     {
       
   176       return types.contains(s);
       
   177     }
       
   178 
       
   179     inline const QString *intern(const QString &s)
       
   180     { return &*string_repository.insert(s); }
       
   181 
       
   182 protected:
       
   183     int tos;
       
   184     int stack_size;
       
   185     Value *sym_stack;
       
   186     int *state_stack;
       
   187     Value yylval;
       
   188     QSet<QString> string_repository;
       
   189     QSet<const QString*> types;
       
   190 
       
   191     struct /*Context*/ {
       
   192       int line;
       
   193       const QString *function_name;
       
   194       QString fileName;
       
   195 
       
   196       void init()
       
   197       {
       
   198         line = 1;
       
   199         function_name = 0;
       
   200         fileName.clear();
       
   201       }
       
   202     } context;
       
   203 };
       
   204 
       
   205 inline void GLSLParser::reallocateStack()
       
   206 {
       
   207     if (! stack_size)
       
   208         stack_size = 128;
       
   209     else
       
   210         stack_size <<= 1;
       
   211 
       
   212     sym_stack = reinterpret_cast<Value*> (qRealloc(sym_stack, stack_size * sizeof(Value)));
       
   213     state_stack = reinterpret_cast<int*> (qRealloc(state_stack, stack_size * sizeof(int)));
       
   214 }
       
   215 
       
   216 :/
       
   217 
       
   218 
       
   219 /.
       
   220 
       
   221 GLSLParser::GLSLParser():
       
   222     tos(0),
       
   223     stack_size(0),
       
   224     sym_stack(0),
       
   225     state_stack(0)
       
   226 {
       
   227 }
       
   228 
       
   229 GLSLParser::~GLSLParser()
       
   230 {
       
   231     if (stack_size) {
       
   232         qFree(sym_stack);
       
   233         qFree(state_stack);
       
   234     }
       
   235 }
       
   236 
       
   237 bool GLSLParser::parse()
       
   238 {
       
   239   const int INITIAL_STATE = 0;
       
   240 
       
   241   int yytoken = -1;
       
   242 
       
   243   reallocateStack();
       
   244 
       
   245   context.init();
       
   246   tos = 0;
       
   247   state_stack[++tos] = INITIAL_STATE;
       
   248 
       
   249   while (true)
       
   250     {
       
   251       if (yytoken == -1 && - TERMINAL_COUNT != action_index [state_stack [tos]])
       
   252         yytoken = nextToken();
       
   253 
       
   254       int act = t_action (state_stack [tos], yytoken);
       
   255 
       
   256       if (act == ACCEPT_STATE) {
       
   257         return true;
       
   258       }
       
   259 
       
   260       else if (act > 0)
       
   261         {
       
   262           if (++tos == stack_size)
       
   263             reallocateStack();
       
   264 
       
   265           sym_stack [tos] = yylval;
       
   266           state_stack [tos] = act;
       
   267           yytoken = -1;
       
   268         }
       
   269 
       
   270       else if (act < 0)
       
   271         {
       
   272           int r = - act - 1;
       
   273 
       
   274           int ridx = rule_index [r];
       
   275           printf ("*** reduce using rule %d %s ::=", r + 1, spell[rule_info [ridx]]);
       
   276           ++ridx;
       
   277           for (int i = ridx; i < ridx + rhs [r]; ++i)
       
   278             {
       
   279               int symbol = rule_info [i];
       
   280               if (const char *name = spell [symbol])
       
   281                 printf (" %s", name);
       
   282               else
       
   283                 printf (" #%d", symbol);
       
   284             }
       
   285           printf ("\n");
       
   286 
       
   287           tos -= rhs [r];
       
   288           act = state_stack [tos++];
       
   289 
       
   290           switch (r) {
       
   291 ./
       
   292 
       
   293 
       
   294 translation_unit ::= external_declaration ;
       
   295 translation_unit ::= translation_unit external_declaration ;
       
   296 
       
   297 variable_identifier ::= IDENTIFIER ;
       
   298 
       
   299 primary_expression ::= variable_identifier ;
       
   300 primary_expression ::= INTCONSTANT ;
       
   301 primary_expression ::= FLOATCONSTANT ;
       
   302 primary_expression ::= BOOLCONSTANT ;
       
   303 primary_expression ::= LEFT_PAREN expression RIGHT_PAREN ;
       
   304 
       
   305 
       
   306 postfix_expression ::= primary_expression ;
       
   307 postfix_expression ::= postfix_expression LEFT_BRACKET integer_expression RIGHT_BRACKET ;
       
   308 postfix_expression ::= function_call ;
       
   309 postfix_expression ::= postfix_expression DOT IDENTIFIER ;
       
   310 postfix_expression ::= postfix_expression DOT TYPE_NAME ;
       
   311 postfix_expression ::= postfix_expression INC_OP ;
       
   312 postfix_expression ::= postfix_expression DEC_OP ;
       
   313 
       
   314 
       
   315 integer_expression ::= expression ;
       
   316 
       
   317 function_call ::= function_call_generic ;
       
   318 
       
   319 function_call_generic ::= function_call_header_with_parameters RIGHT_PAREN ;
       
   320 function_call_generic ::= function_call_header_no_parameters RIGHT_PAREN ;
       
   321 
       
   322 function_call_header_no_parameters ::= function_call_header VOID ;
       
   323 function_call_header_no_parameters ::= function_call_header ;
       
   324 
       
   325 
       
   326 function_call_header_with_parameters ::= function_call_header assignment_expression ;
       
   327 function_call_header_with_parameters ::= function_call_header_with_parameters COMMA assignment_expression ;
       
   328 
       
   329 function_call_header ::=  function_identifier LEFT_PAREN ;
       
   330 
       
   331 function_identifier ::= constructor_identifier ;
       
   332 function_identifier ::= IDENTIFIER ;
       
   333 
       
   334 
       
   335 constructor_identifier ::= FLOAT ;
       
   336 constructor_identifier ::= INT ;
       
   337 constructor_identifier ::= BOOL ;
       
   338 constructor_identifier ::= VEC2 ;
       
   339 constructor_identifier ::= VEC3 ;
       
   340 constructor_identifier ::= VEC4 ;
       
   341 constructor_identifier ::= BVEC2 ;
       
   342 constructor_identifier ::= BVEC3 ;
       
   343 constructor_identifier ::= BVEC4 ;
       
   344 constructor_identifier ::= IVEC2 ;
       
   345 constructor_identifier ::= IVEC3 ;
       
   346 constructor_identifier ::= IVEC4 ;
       
   347 constructor_identifier ::= MAT2 ;
       
   348 constructor_identifier ::= MAT3 ;
       
   349 constructor_identifier ::= MAT4 ;
       
   350 constructor_identifier ::= TYPE_NAME ;
       
   351 
       
   352 unary_expression ::= postfix_expression ;
       
   353 unary_expression ::= INC_OP unary_expression ;
       
   354 unary_expression ::= DEC_OP unary_expression ;
       
   355 unary_expression ::= unary_operator unary_expression ;
       
   356 
       
   357 -- Grammar Note:  No traditional style type casts.
       
   358 
       
   359 unary_operator ::= PLUS ;
       
   360 unary_operator ::= DASH ;
       
   361 unary_operator ::= BANG ;
       
   362 unary_operator ::= TILDE  ;    -- reserved
       
   363 
       
   364 -- Grammar Note:  No '*' or '&' unary ops. Pointers are not supported.
       
   365 
       
   366 multiplicative_expression ::= unary_expression ;
       
   367 multiplicative_expression ::= multiplicative_expression STAR unary_expression ;
       
   368 multiplicative_expression ::= multiplicative_expression SLASH unary_expression ;
       
   369 multiplicative_expression ::= multiplicative_expression PERCENT unary_expression ;          -- reserved
       
   370 
       
   371 
       
   372 additive_expression ::= multiplicative_expression ;
       
   373 additive_expression ::= additive_expression PLUS multiplicative_expression ;
       
   374 additive_expression ::= additive_expression DASH multiplicative_expression ;
       
   375 
       
   376 shift_expression ::= additive_expression ;
       
   377 shift_expression ::= shift_expression LEFT_OP additive_expression ;  -- reserved
       
   378 shift_expression ::= shift_expression RIGHT_OP additive_expression ;  -- reserved
       
   379 
       
   380 relational_expression ::= shift_expression ;
       
   381 relational_expression ::= relational_expression LEFT_ANGLE shift_expression ;
       
   382 relational_expression ::= relational_expression RIGHT_ANGLE shift_expression ;
       
   383 relational_expression ::= relational_expression LE_OP shift_expression ;
       
   384 relational_expression ::= relational_expression GE_OP shift_expression ;
       
   385 
       
   386 equality_expression ::= relational_expression ;
       
   387 equality_expression ::= equality_expression EQ_OP relational_expression ;
       
   388 equality_expression ::= equality_expression NE_OP relational_expression ;
       
   389 
       
   390 and_expression ::= equality_expression ;
       
   391 and_expression ::= and_expression AMPERSAND equality_expression ; -- reserved
       
   392 
       
   393 exclusive_or_expression ::= and_expression ;
       
   394 exclusive_or_expression ::= exclusive_or_expression CARET and_expression ; -- reserved
       
   395 
       
   396 inclusive_or_expression ::= exclusive_or_expression ;
       
   397 inclusive_or_expression ::= inclusive_or_expression VERTICAL_BAR exclusive_or_expression ; -- reserved
       
   398 
       
   399 logical_and_expression ::= inclusive_or_expression ;
       
   400 logical_and_expression ::= logical_and_expression AND_OP inclusive_or_expression ;
       
   401 
       
   402 logical_xor_expression ::= logical_and_expression ;
       
   403 logical_xor_expression ::= logical_xor_expression XOR_OP logical_and_expression ;
       
   404 
       
   405 logical_or_expression ::= logical_xor_expression ;
       
   406 logical_or_expression ::= logical_or_expression OR_OP logical_xor_expression ;
       
   407 
       
   408 conditional_expression ::= logical_or_expression ;
       
   409 conditional_expression ::= logical_or_expression QUESTION expression COLON conditional_expression ;
       
   410 
       
   411 assignment_expression ::= conditional_expression ;
       
   412 assignment_expression ::= unary_expression assignment_operator assignment_expression ;
       
   413 
       
   414 assignment_operator ::= EQUAL ;
       
   415 assignment_operator ::= MUL_ASSIGN ;
       
   416 assignment_operator ::= DIV_ASSIGN ;
       
   417 assignment_operator ::= MOD_ASSIGN ; -- reserved
       
   418 assignment_operator ::= ADD_ASSIGN ;
       
   419 assignment_operator ::= SUB_ASSIGN ;
       
   420 assignment_operator ::= LEFT_ASSIGN ; -- reserved
       
   421 assignment_operator ::= RIGHT_ASSIGN ; -- reserved
       
   422 assignment_operator ::= AND_ASSIGN ; -- reserved
       
   423 assignment_operator ::= XOR_ASSIGN ; -- reserved
       
   424 assignment_operator ::= OR_ASSIGN ; -- reserved
       
   425 
       
   426 expression ::= assignment_expression ;
       
   427 expression ::= expression COMMA assignment_expression ;
       
   428 
       
   429 constant_expression ::= conditional_expression ;
       
   430 
       
   431 declaration ::= function_prototype SEMICOLON ;
       
   432 declaration ::= init_declarator_list SEMICOLON ;
       
   433 
       
   434 function_prototype ::= function_declarator RIGHT_PAREN ;
       
   435 
       
   436 function_declarator ::= function_header ;
       
   437 function_declarator ::= function_header_with_parameters ;
       
   438 
       
   439 function_header_with_parameters ::= function_header parameter_declaration ;
       
   440 function_header_with_parameters ::= function_header_with_parameters COMMA parameter_declaration ;
       
   441 
       
   442 function_header ::= fully_specified_type IDENTIFIER LEFT_PAREN ;
       
   443 /.
       
   444 case $rule_number: {
       
   445   context.function_name = sym(2).s;
       
   446 } break;
       
   447 ./
       
   448 
       
   449 parameter_declarator ::= type_specifier IDENTIFIER ;
       
   450 parameter_declarator ::= type_specifier IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET ;
       
   451 
       
   452 parameter_declaration ::= type_qualifier parameter_qualifier parameter_declarator ;
       
   453 parameter_declaration ::= parameter_qualifier parameter_declarator ;
       
   454 parameter_declaration ::= type_qualifier parameter_qualifier parameter_type_specifier ;
       
   455 parameter_declaration ::= parameter_qualifier parameter_type_specifier ;
       
   456 
       
   457 parameter_qualifier ::=  ;
       
   458 parameter_qualifier ::= IN ;
       
   459 parameter_qualifier ::= OUT ;
       
   460 parameter_qualifier ::= INOUT ;
       
   461 
       
   462 parameter_type_specifier ::= type_specifier ;
       
   463 parameter_type_specifier ::= type_specifier LEFT_BRACKET constant_expression RIGHT_BRACKET ;
       
   464 
       
   465 init_declarator_list ::= single_declaration ;
       
   466 init_declarator_list ::= init_declarator_list COMMA IDENTIFIER ;
       
   467 init_declarator_list ::= init_declarator_list COMMA IDENTIFIER LEFT_BRACKET RIGHT_BRACKET ;
       
   468 init_declarator_list ::= init_declarator_list COMMA IDENTIFIER LEFT_BRACKET constant_expression ;
       
   469 init_declarator_list ::= RIGHT_BRACKET ;
       
   470 init_declarator_list ::= init_declarator_list COMMA IDENTIFIER EQUAL initializer ;
       
   471 
       
   472 single_declaration ::= fully_specified_type ;
       
   473 single_declaration ::= fully_specified_type IDENTIFIER ;
       
   474 single_declaration ::= fully_specified_type IDENTIFIER LEFT_BRACKET RIGHT_BRACKET ;
       
   475 single_declaration ::= fully_specified_type IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET ;
       
   476 single_declaration ::= fully_specified_type IDENTIFIER EQUAL initializer ;
       
   477 
       
   478 -- Grammar Note:  No 'enum', or 'typedef'.
       
   479 
       
   480 --fully_specified_type ::= type_specifier ;
       
   481 --fully_specified_type ::= type_qualifier type_specifier ;
       
   482 
       
   483 fully_specified_type ::= type_specifier ;
       
   484 fully_specified_type ::= type_qualifier ;
       
   485 fully_specified_type ::= fully_specified_type type_specifier ;
       
   486 fully_specified_type ::= fully_specified_type type_qualifier ;
       
   487 
       
   488 type_qualifier ::= CONST ;
       
   489 type_qualifier ::= ATTRIBUTE ;           -- Vertex only.
       
   490 type_qualifier ::= VARYING ;
       
   491 type_qualifier ::= UNIFORM ;
       
   492 
       
   493 type_specifier ::= type_specifier_no_prec ;
       
   494 type_specifier ::= precision_qualifier type_specifier_no_prec ;
       
   495 
       
   496 type_specifier_no_prec ::= VOID ;
       
   497 type_specifier_no_prec ::= FLOAT ;
       
   498 type_specifier_no_prec ::= INT ;
       
   499 type_specifier_no_prec ::= BOOL ;
       
   500 type_specifier_no_prec ::= VEC2 ;
       
   501 type_specifier_no_prec ::= VEC3 ;
       
   502 type_specifier_no_prec ::= VEC4 ;
       
   503 type_specifier_no_prec ::= BVEC2 ;
       
   504 type_specifier_no_prec ::= BVEC3 ;
       
   505 type_specifier_no_prec ::= BVEC4 ;
       
   506 type_specifier_no_prec ::= IVEC2 ;
       
   507 type_specifier_no_prec ::= IVEC3 ;
       
   508 type_specifier_no_prec ::= IVEC4 ;
       
   509 type_specifier_no_prec ::= MAT2 ;
       
   510 type_specifier_no_prec ::= MAT3 ;
       
   511 type_specifier_no_prec ::= MAT4 ;
       
   512 type_specifier_no_prec ::= SAMPLER1D ;
       
   513 type_specifier_no_prec ::= SAMPLER2D ;
       
   514 type_specifier_no_prec ::= SAMPLER3D ;
       
   515 type_specifier_no_prec ::= SAMPLERCUBE ;
       
   516 type_specifier_no_prec ::= SAMPLER1DSHADOW ;
       
   517 type_specifier_no_prec ::= SAMPLER2DSHADOW ;
       
   518 type_specifier_no_prec ::= struct_specifier ;
       
   519 type_specifier_no_prec ::= TYPE_NAME ;
       
   520 
       
   521 precision_qualifier ::= HIGH_PRECISION ;
       
   522 precision_qualifier ::= MEDIUM_PRECISION ;
       
   523 precision_qualifier ::= LOW_PRECISION ;
       
   524 
       
   525 struct_specifier ::= STRUCT IDENTIFIER LEFT_BRACE struct_declaration_list RIGHT_BRACE ;
       
   526 /.
       
   527 case $rule_number: {
       
   528   types.insert(sym(2).s);
       
   529 } break;
       
   530 ./
       
   531 
       
   532 struct_specifier ::= STRUCT LEFT_BRACE struct_declaration_list RIGHT_BRACE ;
       
   533 
       
   534 struct_declaration_list ::= struct_declaration ;
       
   535 struct_declaration_list ::= struct_declaration_list struct_declaration ;
       
   536 
       
   537 struct_declaration ::= type_specifier struct_declarator_list SEMICOLON ;
       
   538 
       
   539 struct_declarator_list ::= struct_declarator ;
       
   540 struct_declarator_list ::= struct_declarator_list COMMA struct_declarator ;
       
   541 
       
   542 struct_declarator ::= IDENTIFIER ;
       
   543 struct_declarator ::= IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET ;
       
   544 
       
   545 initializer ::= assignment_expression ;
       
   546 
       
   547 declaration_statement ::= declaration ;
       
   548 
       
   549 statement ::= compound_statement ;
       
   550 statement ::= simple_statement ;
       
   551 
       
   552 -- Grammar Note:  No labeled statements; 'goto' is not supported.
       
   553 
       
   554 simple_statement ::= declaration_statement ;
       
   555 simple_statement ::= expression_statement ;
       
   556 simple_statement ::= selection_statement ;
       
   557 simple_statement ::= iteration_statement ;
       
   558 simple_statement ::= jump_statement ;
       
   559 
       
   560 compound_statement ::= LEFT_BRACE RIGHT_BRACE ;
       
   561 compound_statement ::= LEFT_BRACE statement_list RIGHT_BRACE ;
       
   562 
       
   563 statement_no_new_scope ::= compound_statement_no_new_scope ;
       
   564 statement_no_new_scope ::= simple_statement ;
       
   565 
       
   566 compound_statement_no_new_scope ::= LEFT_BRACE RIGHT_BRACE ;
       
   567 compound_statement_no_new_scope ::= LEFT_BRACE statement_list RIGHT_BRACE ;
       
   568 
       
   569 statement_list ::= statement ;
       
   570 statement_list ::= statement_list statement ;
       
   571 
       
   572 expression_statement ::= SEMICOLON ;
       
   573 expression_statement ::= expression SEMICOLON ;
       
   574 
       
   575 selection_statement ::= IF LEFT_PAREN expression RIGHT_PAREN statement ELSE statement ;
       
   576 selection_statement ::= IF LEFT_PAREN expression RIGHT_PAREN statement ;
       
   577 
       
   578 -- Grammar Note:  No 'switch'. Switch statements not supported.
       
   579 
       
   580 condition ::= expression ;
       
   581 condition ::= fully_specified_type IDENTIFIER EQUAL initializer ;
       
   582 
       
   583 iteration_statement ::= WHILE LEFT_PAREN condition RIGHT_PAREN statement_no_new_scope ;
       
   584 iteration_statement ::= DO statement WHILE LEFT_PAREN expression RIGHT_PAREN SEMICOLON ;
       
   585 iteration_statement ::= FOR LEFT_PAREN for_init_statement for_rest_statement RIGHT_PAREN statement_no_new_scope ;
       
   586 
       
   587 for_init_statement ::= expression_statement ;
       
   588 for_init_statement ::= declaration_statement ;
       
   589 
       
   590 conditionopt ::=  ;
       
   591 conditionopt ::=  condition ;
       
   592 
       
   593 for_rest_statement ::= conditionopt SEMICOLON ;
       
   594 for_rest_statement ::= conditionopt SEMICOLON expression ;
       
   595 
       
   596 jump_statement ::= CONTINUE SEMICOLON ;
       
   597 jump_statement ::= BREAK SEMICOLON ;
       
   598 jump_statement ::= RETURN SEMICOLON ;
       
   599 jump_statement ::= RETURN expression SEMICOLON ;
       
   600 jump_statement ::= DISCARD SEMICOLON ;                -- Fragment shader only.
       
   601 
       
   602 -- Grammar Note:  No 'goto'. Gotos are not supported.
       
   603 
       
   604 external_declaration ::= function_definition ;
       
   605 external_declaration ::= declaration ;
       
   606 
       
   607 function_definition ::= function_prototype compound_statement_no_new_scope ;
       
   608 /.
       
   609     case $rule_number: { // $rule_name
       
   610        qDebug() << "--> function" << *context.function_name;
       
   611     }  break;
       
   612 ./
       
   613 
       
   614 
       
   615 
       
   616 
       
   617 
       
   618 /.
       
   619           } // switch
       
   620 
       
   621           state_stack [tos] = nt_action (act, lhs [r] - TERMINAL_COUNT);
       
   622         }
       
   623 
       
   624       else
       
   625         {
       
   626           // ### ERROR RECOVERY HERE
       
   627           break;
       
   628         }
       
   629     }
       
   630 
       
   631     fprintf (stderr, "%s:%d: Syntax Error\n", qPrintable(context.fileName), context.line);
       
   632 
       
   633     return false;
       
   634 }
       
   635 
       
   636 #include "glsl-lex.incl"
       
   637 
       
   638 
       
   639 /////////////////////////////
       
   640 // entry point
       
   641 /////////////////////////////
       
   642 int main()
       
   643 {
       
   644 #if 0 // dump the GLSL grammar
       
   645     for (int r = 0; r < GLSLParserTable::RULE_COUNT; ++r)
       
   646       {
       
   647         int ridx = GLSLParserTable::rule_index [r];
       
   648         int rhs = GLSLParserTable::rhs [r];
       
   649         printf ("%3d) %s ::=", r + 1, GLSLParserTable::spell[GLSLParserTable::rule_info [ridx]]);
       
   650         ++ridx;
       
   651         for (int i = ridx; i < ridx + rhs; ++i)
       
   652           {
       
   653             int symbol = GLSLParserTable::rule_info [i];
       
   654             if (const char *name = GLSLParserTable::spell [symbol])
       
   655               printf (" %s", name);
       
   656             else
       
   657               printf (" #%d", symbol);
       
   658           }
       
   659         printf ("\n");
       
   660       }
       
   661 #endif
       
   662 
       
   663     GLSLParser parser;
       
   664 
       
   665     if (parser.parse())
       
   666         qDebug() << "OK";
       
   667     else
       
   668         qDebug() << "KO";
       
   669 }
       
   670 
       
   671 ./