Code Comments
Programming Forum and web based access to our favorite programming groups.Folks: I'm trying to write a server side page templating thingy. Here's what I would like to do, syntax wise: [[....]] are code sections. Code sections are copied to the resulting document verbatim. If code sections appear within quoted strings, they are ignored. [.....] are expression sections. They are converted into: out.println(.....) everywhere they occur in the source (even within quoted strings). \[[ escapes the code section. \[[ prevents a code section from starting and is copied to the resulting document as "[[" \[ escapes the expression section. \[ does not start a expression section and is copied to the resulting document as "[" Everything else is html text and is copied to the resulting document verbatim. For example: ================ Source ====================== <foo value="[fillin]">bar</foo> " some quoted text [[ code section ignored in string ]] [expression-filledin-string] " [[ ...code ]] \[[ foo ]] \[ foo ] ===================== Result ======================== <foo value="out.print(fillin)">bar</foo> "some quoted text [[ -- totally ignored ]] out.print(filledin2) " ...code.... [[ foo ]] [ foo ] ======================================== ============ Trying to write a grammar/lexer for this is proving to a be a bit tricky. This is because: a) [ and [[ have a common prefix "[" b) \[ and \[[ are both valid escape sequences. However, if I have: \[[text]] I want to ignore the entire line, not treat that as: --> \[ [text] ] --> \[ out.print(text) ] So far I've come up with: text -> html* | exp* | code* html -> all chars exp -> '[' exptext ']' code -> '[[' codetext ']]' codetext -> (any char)* exptext -> (any char)* But I want to specify that \[ does not start an exp and \[[ does not start code. How to do that in the grammar ? I also want to specify that \] does not end an exp and \]] does not end code. How to do that in the grammar ? Best regards, --j [How come you want to reinvent php rather than using the one that already exists? -John]
Post Follow-up to this messageEasy fix is to differentiate between the "[", "[[", "\[", and "\[[" in the lexical analysis phase so that each is assigned a representative symbol (and ditto that for their partners). then you have ... exp -> LEFT_BRACKET exptext RIGHT_BRACKET code -> DBL_LEFT_BRACKET codetext DBL_RIGHT_BRACKET ... You could do it with the gramatic syntax, but it would be trickier. What tools are you using? Are you hand coding? Is your parser recursive decent? Is this a homework assignment? > But I want to specify that \[ does not start an exp > and \[[ does not start code. How to do that in the grammar ? > > I also want to specify that \] does not end an exp > and \]] does not end code. How to do that in the grammar ? Nathan Moore
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.