LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Flex & Bison: html templates (https://www.linuxquestions.org/questions/programming-9/flex-and-bison-html-templates-870467/)

neioo 03-23-2011 08:38 AM

Flex & Bison: html templates
 
Hello folks,

I'm trying to develop an html template parser employing Flex & Bison, but now I found a problem and I don't know if surrender, so let's see if you can guide me to the solution (if any).

My idea is, having an html template like:

Quote:

<b><!--MARKUP print (username)--></b>
I want to generate the following code:

Quote:

print ("<b>")
print (username)
print ("</b>")
In that way modifying the template I can design the web without care about the code (and even the language).

I have a flex file, which I show its core, mytemplate.l:


Quote:

START_COMMENT <!--
END_COMMENT -->

%%

{START_COMMENT}"MARKUP".*{END_COMMENT} {printf ("code: %s\n", yytext);}
{START_COMMENT}.*{END_COMMENT} {printf ("HTML Comment: %s\n", yytext);}
. {printf ("Unknow: %s\n", yytext);}
(Here I simplified the code to make only print what it gets, but it returns a token and the text to the bison program).

Now my problem is that I would like to get the unknown characters as an unique string. [using the flex file above, returns char by char all HTML code that is not a comment!].

So, Could it be possible to make a flex rule that gets the HTML code?. It is not necessary to check if HTML code is correct I do it (before parse) using expat. Also there is no need to check the resulted code it will be done by the parser or compiler of the used language.

I know that are a lot of tools that uses this kind of templates in all languages and systems... but I'm just exercising my pour flex&bison skills.

Also, using my current flex file, I have to remove the string "<!--MARKUP" from the beginning and "-->" from the end of returned string to bison... But in reality, if I could manage the HTML code as a flex rule, I could solve that sednding to bison an start Token, the code and and end token.

Any hint or link will be appreciated.

Thanks for reading.

neioo 03-23-2011 04:48 PM

It's me again,

I have solved my problem, I don't know if it is an optimal solution but it seems to work (at least the flex's part; tomorrow I will work on bison's part).

I present the full flex file (if someone wants to test and so), template.l:

Quote:

%{

#include <stdio.h>
#include <string.h>

#define BUFF_LEN 100
char buffer[BUFF_LEN];
int counter = 0;
%}

%option noyywrap

START <!--
END -->

%%

{START}"MARKUP" {printf ("\tSTART CODE:\n"); /*return CODE*/}
{START} {printf ("\tSTART HTML COMMENT:\n); /*return HTML_COMMENT*/}
{END} {
if (counter != 0)
{
printf ("Content: %s\n", buffer);
//yylval.sword = strdup (buffer);
buffer[0] = '\0';
counter = 0;
//return CONTENT;
}
printf ("\tEND\n");
}
. {
strcpy (buffer+counter, yytext);
counter ++;
if (counter == BUFF_LEN)
{
printf ("Content: %s\n", buffer);
//yylval.sword = strdup (buffer);
buffer[0] = '\0';
counter = 0;
//return CONTENT;
}
}

%%
int
main()
{
yylex();
return 0;
}
Commented lines are for (future) bison use: process the token and get the result.

It's important to note that using that code, all templates are forced to finish with an (empty) HTML coment, if not content after the last "-->" will be not processed.

I close this thread as solved :)

Thanks for your attention and sorry for the inconvenience (if any).


All times are GMT -5. The time now is 08:50 AM.