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):
#include <mySite>
string name = "myName";
string toCapital(string str);
html
{
head {
title { mySiteTitle; }
link(rel="stylesheet", href="style.css");
}
body(id="default") {
div(id="wrapper", class="some_class") {
h1 { "Hello, " + toCapital(name) + "!"; }
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) + "!"; }
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
:
| 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}
;
body
: LBRACE content RBRACE
;
content
:
| blocks
| STRING SEMICOLON
| NUMBER SEMICOLON
| CCODE
;
%%
, WDI . , . - , ?