source: trunk/test/sandbox/lex/lolfx.y @ 1409

Last change on this file since 1409 was 1409, checked in by sam, 9 years ago

test: start working on a lexer/parser for LolFx using flex and bison.

File size: 1.9 KB
Line 
1%{
2#include <cstdio>
3#include <iostream>
4
5extern "C" int yylex();
6extern "C" int yyparse();
7extern "C" FILE *yyin;
8extern "C" int yylineno;
9
10void yyerror(const char *s);
11%}
12
13/* The classic Bison union trick */
14%union
15{
16    int ival;
17    float fval;
18    char *sval;
19}
20
21 /*
22  * All the LolFx tokens
23  */
24
25%token TECHNIQUE PASS
26%token <ival> INT
27%token <fval> FLOAT
28%token <sval> STRING NAME
29
30%%
31
32fx:
33    section_list
34    ;
35
36section_list:
37    section
38    | section_list section
39    ;
40
41section:
42    technique
43    | shader
44    ;
45
46 /*
47  * Grammar for techniques
48  */
49
50technique:
51    TECHNIQUE NAME '{' pass_list '}' { std::cout << "TECHNIQUE" << std::endl; }
52    ;
53
54 /*
55  * Grammar for passes
56  */
57
58pass_list:
59    pass
60    | pass_list pass
61    ;
62
63pass:
64    PASS NAME '{' pass_stmt_list '}' { std::cout << "PASS" << std::endl; }
65    ;
66
67pass_stmt_list:
68    pass_stmt
69    | pass_stmt_list pass_stmt
70    ;
71
72pass_stmt:
73    ';'
74    | NAME '=' NAME ';'
75    | NAME '=' INT ';'
76    | NAME '[' INT ']' '=' NAME ';'
77    | NAME '[' INT ']' '=' INT ';'
78    ;
79
80 /*
81  * Grammar for shaders
82  */
83
84shader:
85    shader_region shader_code
86    | shader_region
87    ;
88
89shader_region:
90    '#' NAME shader_name { std::cout << "new shader " << $2 << std::endl; }
91    ;
92
93shader_name:
94    NAME
95    | shader_name '.' NAME
96    ;
97
98shader_code:
99    INT shader_code      { std::cout << "int: " << $1 << std::endl; }
100    | FLOAT shader_code  { std::cout << "float: " << $1 << std::endl; }
101    | STRING shader_code { std::cout << "string: " << $1 << std::endl; }
102    | INT            { std::cout << "int: " << $1 << std::endl; }
103    | FLOAT          { std::cout << "float: " << $1 << std::endl; }
104    | STRING         { std::cout << "string: " << $1 << std::endl; }
105    ;
106
107%%
108
109main()
110{
111    yyin = fopen("test.lolfx", "r");
112    do
113    {
114        yyparse();
115    }
116    while (!feof(yyin));
117
118    fclose(yyin);
119}
120
121void yyerror(const char *s)
122{
123    std::cout << "Parse error line " << yylineno << ": " << s << std::endl;
124    exit(-1);
125}
126
Note: See TracBrowser for help on using the repository browser.