Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I lex this input?
    primarykey
    data
    text
    <p>I currently have a working, simple language implemented in Java using ANTLR. What I want to do is embed it in plain text, in a similar fashion to PHP.</p> <p>For example:</p> <pre><code>Lorem ipsum dolor sit amet &lt;% print('consectetur adipiscing elit'); %&gt; Phasellus volutpat dignissim sapien. </code></pre> <p>I anticipate that the resulting token stream would look something like:</p> <pre><code>CDATA OPEN PRINT OPAREN APOS STRING APOS CPAREN SEMI CLOSE CDATA </code></pre> <p>How can I achieve this, or is there a better way?</p> <p>There is no restriction on what might be outside the <code>&lt;%</code> block. I assumed something like <code>&lt;% print('%&gt;'); %&gt;</code>, as per Michael Mrozek's answer, would be possible, but outside of a situation like that, <code>&lt;%</code> would always indicate the start of a code block.</p> <hr> <h2>Sample Implementation</h2> <p>I developed a solution based on ideas given in Michael Mrozek's answer, simulating Flex's start conditions using ANTLR's gated semantic predicates:</p> <pre><code>lexer grammar Lexer; @members { boolean codeMode = false; } OPEN : {!codeMode}?=&gt; '&lt;%' { codeMode = true; } ; CLOSE : {codeMode}?=&gt; '%&gt;' { codeMode = false;} ; LPAREN : {codeMode}?=&gt; '('; //etc. CHAR : {!codeMode}?=&gt; ~('&lt;%'); parser grammar Parser; options { tokenVocab = Lexer; output = AST; } tokens { VERBATIM; } program : (code | verbatim)+ ; code : OPEN statement+ CLOSE -&gt; statement+ ; verbatim : CHAR -&gt; ^(VERBATIM CHAR) ; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload