LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-23-2011, 08:38 AM   #1
neioo
LQ Newbie
 
Registered: Jan 2008
Location: Sant Quinti de Mediona
Distribution: Gentoo
Posts: 25

Rep: Reputation: 3
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.
 
Old 03-23-2011, 04:48 PM   #2
neioo
LQ Newbie
 
Registered: Jan 2008
Location: Sant Quinti de Mediona
Distribution: Gentoo
Posts: 25

Original Poster
Rep: Reputation: 3
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).
 
  


Reply

Tags
bison, flex, html



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there any support for bison-bridge and bison-locations in flex on windows systems? rami alkhateeb Linux - Software 0 12-29-2010 09:10 AM
flex and bison saurav.nith Linux - General 1 04-06-2010 06:38 AM
bison / flex zaman Programming 1 08-16-2005 10:19 AM
Problem with binutils first pass / bison & flex installation peristaltic Linux From Scratch 3 03-20-2005 10:17 AM
[flex & bison] Why does my rule fail from line 2? chuanyung Programming 0 07-30-2004 04:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration