How to write a bison grammar for WDI?

I need help building a bison grammar.

From my other question: I am trying to create a metalanguage for writing markup code (e.g. xml and html) that can be directly embedded in C / C ++ code. Here is a simple example written in this language, I call it WDI (web development interface):

 /*
  * Simple wdi/html sample source code
  */
 #include <mySite>

 string name = "myName";
 string toCapital(string str);

 html
 {
  head {
   title { mySiteTitle; }
   link(rel="stylesheet", href="style.css");
  }
  body(id="default") {
   // Page content wrapper
   div(id="wrapper", class="some_class") {
    h1 { "Hello, " + toCapital(name) + "!"; }

    // Lists post
    ul(id="post_list") {
     for(post in posts) {
      li { a(href=post.getID()) { post.tilte; } }
     }
    }
   }
  }
 }

, C html. , , , C-, , . , HTML C, . C . wdi print, return ( printf). HTML-.

, , 1 :

h1 { "Hello, " + toCapital(name) + "!"; }
// would become:
printf("<h1>Hello, %s!</h1>", toCapital(name));

- wdi html :

tag(attributes) {content} => <tag attributes>content</tag>

-, HTML-, , C- printfs. , wdi, , printf ( toCapital (name) ).

flex/bison:

id        [a-zA-Z_]([a-zA-Z0-9_])*
number    [0-9]+
string    \".*\"

%%

{id} {
        yylval.string = strdup(yytext);
        return(ID);
    }

{number} {
        yylval.number = atoi(yytext);
        return(NUMBER);
    }

{string} {
        yylval.string = strdup(yytext);
        return(STRING);
    }

"(" { return(LPAREN); }
")" { return(RPAREN); }
"{" { return(LBRACE); }
"}" { return(RBRACE); }
"=" { return(ASSIGN); }
"," { return(COMMA);  }
";" { return(SEMICOLON); }

\n|\r|\f { /* ignore EOL */ }
[ \t]+   { /* ignore whitespace */ }
.        { /* return(CCODE); Find C source */ }

%%

%start wdi
%token LPAREN RPAREN LBRACE RBRACE ASSIGN COMMA SEMICOLON CCODE QUOTE

%union
{
    int number;
    char *string;
}

%token <string> ID STRING
%token <number> NUMBER

%%
wdi
    : /* empty */
    | blocks
    ;

blocks
    : block
    | blocks block
    ;

block
    : head SEMICOLON
    | head body
    ;

head
    : ID
    | ID
    attributes
    ;

attributes
    : LPAREN RPAREN
    | LPAREN attribute_list RPAREN
    ;

attribute_list
    : attribute
    | attribute COMMA attribute_list
    ;

attribute
    : key ASSIGN value
    ;

key
    : ID {$$=$1}
    ;

value
    : STRING {$$=$1}
    /*| NUMBER*/
    /*| CCODE*/
    ;

body
    : LBRACE content RBRACE
    ;

content
    : /* */
    | blocks
    | STRING SEMICOLON
    | NUMBER SEMICOLON
    | CCODE
    ;

%%

, WDI . , . - , ?

+2
1

C WDI, . LALR (1) ( Bison), , C, , , C ( C + WDI).

:

a) , Bison parse C, ( , GNU GCC, , ),

b) , GLR ( Bison ) , ,

c) WDI , , WDI , WDI, ( C). - (ASP, PHP, JSP...). , , WDI , , WDI, . , WDI C // WDI . . SO :

antlr3

, , .

+2

All Articles